Create your own projections

Hi all, im triying to create my own projections but i’m stuck.

i’ve tryied the example of counting existing events "fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state; }) " but the sistem complains with

"Cannot create a projection state handler.

Handler type: native:EventStore.Projections.Core.Standard.IndexStreams
Query:
fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state; }) Message:

Exception has been thrown by the target of an invocation."

I’ve also tried to post a new projection but the system crashed giving an exception. you can find a screenshoot of the exception in the atachments.

Can anyone help me understanding the projections and creating my own projections.

Thanks in advance.

eventstore Exception.GIF

Hi,

It looks like you are not creating a JavaScript projection, but a pre-defined one. The error message says handler type is native:EventStore.Projections.Core.Standard.IndexStreams. Do you use “Create a standard projection” menu item from the UI?

I’ll fix the error message.

-yuriy

I tried to create a projection from the “create a standard projection” menu ui item and it gives the same error:

"Cannot create a projection state handler.

Handler type: native:EventStore.Projections.
Core.Standard.IndexStreams
Query:
fromAll().whenAny(function(
state,e) { linkTo(‘all’, e); return state; }) Message:

Exception has been thrown by the target of an invocation."

The server does not crash this time but keeps saying “retrying to write $Streams failed to write events to stream $Streams. Error WrongExpectedVersion”

You should use “Post New” to create JavaScript projections.

So, if you try creating a projection with “Post New” the server crashes? Do you have js1.dll in your bin folder? If you are building the system yourself, is it x64?

if you try creating a projection with “Post New” the server crashes?

Yes

I’m not building the system myself i got the exe from downloads. I’m running under x64. windows xp sp2

Do you see any messages in log files or console window after the crash?
Is it sp2? I see Microsoft states .net 4 requires XP SP3.

No i don´t see any message in the logs files refering to an error. The console shuts down when the process crashes, i’ve also checked the eventviewer but nothing.

And for the os versión, there is not sp3 for windows xp x64 but i’ve the system updated with all the windows updates and the framework works fine for the rest of the projects.

At the end i ran the source code to see the problem. It seems the problem is in the v8 engine, the exception details are(in the atachment you’ll find a screenshot):

System.AccessViolationException was unhandled

Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Source=EventStore.Projections.Core

StackTrace:

at EventStore.Projections.Core.v8.Js1.CompileQuery(IntPtr prelude, String script, String fileName, CommandHandlerRegisteredDelegate commandHandlerRegisteredCallback, ReverseCommandHandlerDelegate reverseCommandHandler)

at EventStore.Projections.Core.v8.QueryScript.CompileScript(PreludeScript prelude, String script, String fileName)

at EventStore.Projections.Core.v8.QueryScript..ctor(PreludeScript prelude, String script, String fileName)

at EventStore.Projections.Core.Services.v8.V8ProjectionStateHandler..ctor(String preludeName, String querySource, Func`2 getModuleSource, Action`1 logger)

at EventStore.Projections.Core.Services.v8.DefaultV8ProjectionStateHandler..ctor(String query, Action`1 logger)

at EventStore.Projections.Core.Services.Management.ProjectionStateHandlerFactory.Create(String factoryType, String source, Action`1 logger)

at EventStore.Projections.Core.Services.Management.ManagedProjection.Start(IPublisher coreOutput, ProjectionStateHandlerFactory handlerFactory, ProjectionConfconfig)

at EventStore.Projections.Core.Services.Management.ManagedProjection.StartIfEnabled()

at EventStore.Projections.Core.Services.Management.ManagedProjection.StartNew(Action completed)

at EventStore.Projections.Core.Services.Management.ManagedProjection.<>c__DisplayClassc.<InitializeNew>b__b()

at EventStore.Projections.Core.Services.Management.ManagedProjection.BeginWrite(Action completed)

at EventStore.Projections.Core.Services.Management.ManagedProjection.InitializeNew(Post message, Action completed)

at EventStore.Projections.Core.Services.Management.ProjectionManager.PostNewProjection(Post message, Action`1 completed)

at EventStore.Projections.Core.Services.Management.ProjectionManager.Handle(Post message)

at EventStore.Core.Bus.MessageHandler`1.TryHandle(Message message)

at EventStore.Core.Bus.InMemoryBus.PublishByType(Message message, Type type)

at EventStore.Core.Bus.InMemoryBus.DispatchByType(Message message)

at EventStore.Core.Bus.InMemoryBus.Publish(Message message)

at EventStore.Core.Bus.InMemoryBus.Handle(Message message)

at EventStore.Core.Bus.QueuedHandler.ReadFromQueue(Object o)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback, Object state, Boolean ignoreSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart(Object obj)

[Excepcion bus.TIF|attachment](upload://lzWZF6GVf4wdB4hvAe5Pfjvz1WF.TIF) (237 KB)

It may be either v8 or our native code around v8. Do you see any stack trace if you enable unmanaged debugging? If you build the src/EventStore/Projections.Dev.WindowsOnly.sln solution you should get unmanaged debug information as well.

Yes i can see unmanaged debug information:

js1.dll!js1::QueryScriptScope::QueryScriptScope(js1::QueryScript *
query_script=0x000000002e28c6a0) Line 17 + 0x16
bytes C++

js1.dll!js1::QueryScript::run() Line 50 + 0xf
bytes C++

js1.dll!compile_query(void * prelude=0x000000002c843900, const unsigned short *
script=0x00000000024a398c, const unsigned short * file_name=0x00000000024fa514,
void (const unsigned short *, void )
register_command_handler_callback=0x0000000001bb466c, void (const unsigned short
*, const unsigned short ) reverse_command_callback=0x0000000001bb478c)
Line 66 C++

[External
Code]

EventStore.Projections.Core.dll!EventStore.Projections.Core.v8.QueryScript.CompileScript(EventStore.Projections.Core.v8.PreludeScript
prelude = {EventStore.Projections.Core.v8.PreludeScript}, string script =
“fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state;
})\n”, string fileName = “POST-BODY”) Line 78 + 0x54 bytes
C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.v8.QueryScript.QueryScript(EventStore.Projections.Core.v8.PreludeScript
prelude = {EventStore.Projections.Core.v8.PreludeScript}, string script =
“fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state;
})\n”, string fileName = “POST-BODY”) Line 63 + 0x2c
bytes
C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.v8.V8ProjectionStateHandler.V8ProjectionStateHandler(string
preludeName = “1Prelude”, string querySource =
“fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state;
})\n”, System.Func<string,System.Tuple<string,string>>
getModuleSource = {Method = {System.Reflection.RuntimeMethodInfo}},
System.Action logger = {Method =
{System.Reflection.RuntimeMethodInfo}}) Line 55 + 0x3c
bytes
C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.v8.DefaultV8ProjectionStateHandler.DefaultV8ProjectionStateHandler(string
query = “fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return
state; })\n”, System.Action logger = {Method =
{System.Reflection.RuntimeMethodInfo}}) Line 39 + 0x90
bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ProjectionStateHandlerFactory.Create(string
factoryType = “JS”, string source =
“fromAll().whenAny(function(state,e) { linkTo(‘all’, e); return state;
})\n”, System.Action logger = {Method = {System.Reflection.RuntimeMethodInfo}})
Line 53 + 0x41 bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.Start(EventStore.Core.Bus.IPublisher
coreOutput = {EventStore.Core.Bus.InMemoryBus}, EventStore.Projections.Core.Services.Management.ProjectionStateHandlerFactory
handlerFactory =
{EventStore.Projections.Core.Services.Management.ProjectionStateHandlerFactory},
EventStore.Projections.Core.Services.ProjectionConfig config =
{EventStore.Projections.Core.Services.ProjectionConfig}) Line 376 + 0xa6
bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.StartIfEnabled()
Line 318 + 0x21 bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.StartNew(System.Action
completed = {Method = {System.Reflection.RuntimeMethodInfo}}) Line 450 + 0xa
bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.InitializeNew.AnonymousMethod__b()
Line 218 + 0x17 bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.BeginWrite(System.Action
completed = {Method = {System.Reflection.RuntimeMethodInfo}}) Line 279 + 0x28
bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ManagedProjection.InitializeNew(EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post
message =
{EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post},
System.Action completed = {Method = {System.Reflection.RuntimeMethodInfo}})
Line 218 + 0x65 bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ProjectionManager.PostNewProjection(EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post
message =
{EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post}, System.Action<EventStore.Projections.Core.Services.Management.ManagedProjection>
completed = {Method = {System.Reflection.RuntimeMethodInfo}}) Line 344 + 0x8b
bytes C#

EventStore.Projections.Core.dll!EventStore.Projections.Core.Services.Management.ProjectionManager.Handle(EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post
message =
{EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post}) Line
125 + 0x92 bytes C#

EventStore.Core.dll!EventStore.Core.Bus.MessageHandler<EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post>.TryHandle(EventStore.Core.Messaging.Message
message =
{EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post}) Line
59 + 0x78 bytes C#

EventStore.Core.dll!EventStore.Core.Bus.InMemoryBus.PublishByType(EventStore.Core.Messaging.Message
message = {EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post},
System.Type type = {Name = “Post” FullName = “EventStore.Projections.Core.Messages.ProjectionManagementMessage+Post”})
Line 123 + 0x30
bytes
C#

EventStore.Core.dll!EventStore.Core.Bus.InMemoryBus.DispatchByType(EventStore.Core.Messaging.Message
message = {EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post})
Line 104 + 0x14 bytes C#

EventStore.Core.dll!EventStore.Core.Bus.InMemoryBus.Publish(EventStore.Core.Messaging.Message
message = {EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post})
Line 96 + 0xf bytes C#

EventStore.Core.dll!EventStore.Core.Bus.InMemoryBus.Handle(EventStore.Core.Messaging.Message
message = {EventStore.Projections.Core.Messages.ProjectionManagementMessage.Post})
Line 90 + 0xf bytes C#

EventStore.Core.dll!EventStore.Core.Bus.QueuedHandler.ReadFromQueue(object o =
null) Line 167 + 0x45
bytes
C#

[External
Code]

kernel32.dll!0000000077d6b71a()

[Frames below may be incorrect and/or missing, no symbols loaded for
kernel32.dll]

thanks, it helps.

Assignment to __declspec(thread) field fails. It seems to be a problem in Windows XP and older with static TLS variables. http://msdn.microsoft.com/en-us/library/2s9wt68x.aspx

I’ll check if I can make it compatible.

-yuriy

I’ve removed the failing code locally. This change will appear in master branch when merged.

ok, thanks Yuri.