public override void Dispose() { if (_projectionStateHandler != null) { _projectionStateHandler.Dispose(); } }
private void BeginCreateAndPrepare( ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config, Action onPrepared) { _onPrepared = onPrepared; if (handlerFactory == null) { throw new ArgumentNullException("handlerFactory"); } if (config == null) { throw new ArgumentNullException("config"); } //TODO: which states are allowed here? if (_state >= ManagedProjectionState.Preparing) { throw new InvalidOperationException("Already preparing or has been prepared"); } //TODO: load configuration from the definition var createProjectionMessage = new CoreProjectionManagementMessage.CreateAndPrepare( new PublishEnvelope(_inputQueue), _id, _name, config, delegate { // this delegate runs in the context of a projection core thread // TODO: move this code to the projection core service as we may be in different processes in the future IProjectionStateHandler stateHandler = null; try { stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine); var checkpointStrategyBuilder = new CheckpointStrategy.Builder(); stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder); checkpointStrategyBuilder.Validate(Mode); // avoid future exceptions in coreprojection return(stateHandler); } catch (Exception ex) { SetFaulted( string.Format( "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}", HandlerType, Query, ex.Message), ex); if (stateHandler != null) { stateHandler.Dispose(); } throw; } }); //note: set runnign before start as coreProjection.start() can respond with faulted _state = ManagedProjectionState.Preparing; _coreQueue.Publish(createProjectionMessage); }
public void Teardown() { if (_stateHandler != null) { _stateHandler.Dispose(); } _stateHandler = null; GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); }
private void Start(IPublisher coreOutput, ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config) { if (coreOutput == null) { throw new ArgumentNullException("coreOutput"); } if (handlerFactory == null) { throw new ArgumentNullException("handlerFactory"); } if (config == null) { throw new ArgumentNullException("config"); } if (_coreProjection != null) { throw new InvalidOperationException("CoreProjection has been already created"); } IProjectionStateHandler stateHandler = null; try { stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine); var checkpointStrategyBuilder = new CheckpointStrategy.Builder(); stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder); checkpointStrategyBuilder.Validate(this.Mode); // avoid future exceptions in coreprojection // constructor can fail if wrong source defintion //TODO: revise it _coreProjection = new CoreProjection(_name, _id, coreOutput, stateHandler, config, _logger); } catch (Exception ex) { SetFaulted( String.Format( "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}", HandlerType, Query, ex.Message), ex); if (stateHandler != null) { stateHandler.Dispose(); } return; } //TODO: load configuration from the definition _state = ManagedProjectionState.Running; //note: set runnign before start as coreProjection.start() can respond with faulted _coreProjection.Start(); }
private void Start(ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config) { if (handlerFactory == null) { throw new ArgumentNullException("handlerFactory"); } if (config == null) { throw new ArgumentNullException("config"); } if (_state == ManagedProjectionState.Running) { throw new InvalidOperationException("Already started"); } //TODO: load configuration from the definition var createProjectionMessage = new ProjectionMessage.CoreService.Management.Create(new PublishEnvelope(_inputQueue), _id, _name, config, delegate { IProjectionStateHandler stateHandler = null; try { stateHandler = handlerFactory.Create(this.HandlerType, this.Query, Console.WriteLine); var checkpointStrategyBuilder = new CheckpointStrategy.Builder(); stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder); checkpointStrategyBuilder.Validate(Mode); // avoid future exceptions in coreprojection return(stateHandler); } catch (Exception ex) { SetFaulted(String.Format("Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}", HandlerType, Query, ex.Message), ex); if (stateHandler != null) { stateHandler.Dispose(); } throw; } }); //note: set runnign before start as coreProjection.start() can respond with faulted _state = ManagedProjectionState.Running; _coreQueue.Publish(createProjectionMessage); _coreQueue.Publish(new ProjectionMessage.Projections.Management.Start(_id)); }
public void can_compile_million_times() { for (var i = 0; i < 10000000; i++) { if (_stateHandler != null) { _stateHandler.Dispose(); } _stateHandler = null; /* * _state = null; */ _projection = null; _projection = @" fromAll(); on_raw(function(state, event, streamId, eventType, sequenceNumber, metadata) { emit('output-stream' + sequenceNumber, 'emitted-event' + sequenceNumber, {a: JSON.parse(event).a}); return {}; }); "; _logged = new List <string>(); _stateHandlerFactory = new ProjectionStateHandlerFactory(); _stateHandler = _stateHandlerFactory.Create( "JS", _projection, s => { if (!s.StartsWith("P:")) { _logged.Add(s); } else { _logDelegate(s); } }); // skip prelude debug output /* * if (_state != null) * _stateHandler.Load(_state); * else * _stateHandler.Initialize(); */ Console.Write("."); } }
private void BeginCreateAndPrepare( ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config, Action onPrepared) { _onPrepared = _onStopped = () => { _onStopped = null; _onPrepared = null; if (onPrepared != null) { onPrepared(); } }; if (handlerFactory == null) { throw new ArgumentNullException("handlerFactory"); } if (config == null) { throw new ArgumentNullException("config"); } //TODO: which states are allowed here? if (_state >= ManagedProjectionState.Preparing) { DisposeCoreProjection(); _state = ManagedProjectionState.Loaded; } //TODO: load configuration from the definition Func <IProjectionStateHandler> stateHandlerFactory = delegate { // this delegate runs in the context of a projection core thread // TODO: move this code to the projection core service as we may be in different processes in the future IProjectionStateHandler stateHandler = null; try { stateHandler = handlerFactory.Create( HandlerType, Query, logger: s => _logger.Trace(s), cancelCallbackFactory: _timeoutScheduler == null ? (Action <int, Action>)null : _timeoutScheduler.Schedule); return(stateHandler); } catch (Exception ex) { SetFaulted( string.Format( "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}", HandlerType, Query, ex.Message), ex); if (stateHandler != null) { stateHandler.Dispose(); } throw; } }; var createProjectionMessage = _isSlave ? (Message) new CoreProjectionManagementMessage.CreateAndPrepareSlave( new PublishEnvelope(_inputQueue), Id, _name, new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0), config, _slaveResultsPublisher, _slaveMasterCorrelationId, stateHandlerFactory) : new CoreProjectionManagementMessage.CreateAndPrepare( new PublishEnvelope(_inputQueue), Id, _name, new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0), config, HandlerType, Query, stateHandlerFactory); //note: set runnign before start as coreProjection.start() can respond with faulted _state = ManagedProjectionState.Preparing; _coreQueue.Publish(createProjectionMessage); }