public void Run() { long currentThreadId = Thread.CurrentThread.ManagedThreadId; // Choose one of filter specifications, randomly, then reserve to make sure no one else has the same FilterSpecCompiled filterSpec = null; EventBean unmatchedEvent = null; EventBean matchedEvent = null; var index = 0; do { index = Random.Next(_testFilterSpecs.Count); filterSpec = _testFilterSpecs[index]; unmatchedEvent = _unmatchedEvents[index]; matchedEvent = _matchedEvents[index]; }while(!ObjectReservationSingleton.Instance.Reserve(filterSpec)); // Add expression var filterValues = filterSpec.GetValueSet(null, null, null); FilterHandle filterCallback = new SupportFilterHandle(); var pathAddedTo = IndexTreeBuilder.Add(filterValues, filterCallback, _topNode, _lockFactory); // Fire a no-match IList <FilterHandle> matches = new List <FilterHandle>(); _topNode.MatchEvent(unmatchedEvent, matches); if (matches.Count != 0) { Log.Fatal(".run (" + currentThreadId + ") Got a match but expected no-match, matchCount=" + matches.Count + " bean=" + unmatchedEvent + " match=" + matches[0].GetHashCode()); Assert.IsFalse(true); } // Fire a match _topNode.MatchEvent(matchedEvent, matches); if (matches.Count != 1) { Log.Fatal(".run (" + currentThreadId + ") Got zero or two or more match but expected a match, count=" + matches.Count + " bean=" + matchedEvent); Assert.IsFalse(true); } // Remove the same expression again IndexTreeBuilder.Remove(_eventType, filterCallback, pathAddedTo[0].ToArray(), _topNode); Log.Debug(".run (" + Thread.CurrentThread.ManagedThreadId + ")" + " Completed"); ObjectReservationSingleton.Instance.Unreserve(filterSpec); }
public ViewableActivationResult Activate(AgentInstanceContext agentInstanceContext, bool isSubselect, bool isRecoveringResilient) { // New event stream EventType resultEventType = _filterSpec.ResultEventType; EventStream zeroDepthStream = _isCanIterate ? (EventStream) new ZeroDepthStreamIterable(resultEventType) : (EventStream) new ZeroDepthStreamNoIterate(resultEventType); // audit proxy var inputStream = EventStreamProxy.GetAuditProxy(agentInstanceContext.StatementContext.EngineURI, agentInstanceContext.EpStatementAgentInstanceHandle.StatementHandle.StatementName, _annotations, _filterSpec, zeroDepthStream); var eventStream = inputStream; var statementId = agentInstanceContext.StatementContext.StatementId; var filterCallback = new ProxyFilterHandleCallback { ProcStatementId = () => statementId, ProcIsSubselect = () => _isSubSelect }; if (_filterSpec.OptionalPropertyEvaluator != null) { filterCallback.ProcMatchFound = (theEvent, allStmtMatches) => { var result = _filterSpec.OptionalPropertyEvaluator.GetProperty(theEvent, agentInstanceContext); if (result != null) { eventStream.Insert(result); } }; } else { filterCallback.ProcMatchFound = (theEvent, allStmtMatches) => { if (InstrumentationHelper.ENABLED) { _instrumentationAgent.IndicateQ(); } eventStream.Insert(theEvent); if (InstrumentationHelper.ENABLED) { _instrumentationAgent.IndicateA(); } }; } var filterHandle = new EPStatementHandleCallback(agentInstanceContext.EpStatementAgentInstanceHandle, filterCallback); FilterValueSetParam[][] addendum = null; if (agentInstanceContext.AgentInstanceFilterProxy != null) { addendum = agentInstanceContext.AgentInstanceFilterProxy.GetAddendumFilters(_filterSpec); } var filterValueSet = _filterSpec.GetValueSet(null, agentInstanceContext, addendum); var filterServiceEntry = _services.FilterService.Add(filterValueSet, filterHandle); var stopCallback = new ViewableActivatorFilterProxyStopCallback(this, filterHandle, filterServiceEntry); return(new ViewableActivationResult(inputStream, stopCallback, null, null, null, false, false, null)); }
/// <summary> /// See the method of the same name in <seealso cref="com.espertech.esper.view.stream.StreamFactoryService" />. /// Always attempts to reuse an existing event stream. May thus return a new event stream or an existing event /// stream depending on whether filter criteria match. /// </summary> /// <param name="statementId">the statement id</param> /// <param name="filterSpec">is the filter definition</param> /// <param name="filterService">filter service to activate filter if not already active</param> /// <param name="epStatementAgentInstanceHandle">is the statement resource lock</param> /// <param name="isJoin">is indicatng whether the stream will participate in a join statement, information necessary for stream reuse and multithreading concerns</param> /// <param name="agentInstanceContext"></param> /// <param name="hasOrderBy">if the consumer has order-by</param> /// <param name="filterWithSameTypeSubselect">if set to <c>true</c> [filter with same type subselect].</param> /// <param name="annotations">The annotations.</param> /// <param name="stateless">if set to <c>true</c> [stateless].</param> /// <param name="streamNum">The stream num.</param> /// <param name="isCanIterateUnbound">if set to <c>true</c> [is can iterate unbound].</param> /// <returns> /// newly createdStatement event stream, not reusing existing instances /// </returns> /// <exception cref="IllegalStateException">Filter spec object already found in collection</exception> public Pair <EventStream, IReaderWriterLock> CreateStream( int statementId, FilterSpecCompiled filterSpec, FilterService filterService, EPStatementAgentInstanceHandle epStatementAgentInstanceHandle, bool isJoin, AgentInstanceContext agentInstanceContext, bool hasOrderBy, bool filterWithSameTypeSubselect, Attribute[] annotations, bool stateless, int streamNum, bool isCanIterateUnbound) { EventStream eventStream; if (Log.IsDebugEnabled) { Log.Debug(".createStream hashCode=" + filterSpec.GetHashCode() + " filter=" + filterSpec); } // Check if a stream for this filter already exists StreamEntry entry; var forceNewStream = isJoin || (!_isReuseViews) || hasOrderBy || filterWithSameTypeSubselect || stateless; if (forceNewStream) { entry = _eventStreamsIdentity.Get(filterSpec); } else { entry = _eventStreamsRefCounted[filterSpec]; } // If pair exists, either reference count or illegal state if (entry != null) { if (forceNewStream) { throw new IllegalStateException("Filter spec object already found in collection"); } else { Log.Debug(".createStream filter already found"); _eventStreamsRefCounted.Reference(filterSpec); // audit proxy eventStream = EventStreamProxy.GetAuditProxy( _engineURI, epStatementAgentInstanceHandle.StatementHandle.StatementName, annotations, filterSpec, entry.EventStream); // We return the lock of the statement first establishing the stream to use that as the new statement's lock return(new Pair <EventStream, IReaderWriterLock>( eventStream, entry.Callback.AgentInstanceHandle.StatementAgentInstanceLock)); } } // New event stream var resultEventType = filterSpec.ResultEventType; var zeroDepthStream = isCanIterateUnbound ? (EventStream) new ZeroDepthStreamIterable(resultEventType) : (EventStream) new ZeroDepthStreamNoIterate(resultEventType); // audit proxy var inputStream = EventStreamProxy.GetAuditProxy( _engineURI, epStatementAgentInstanceHandle.StatementHandle.StatementName, annotations, filterSpec, zeroDepthStream); eventStream = inputStream; FilterHandleCallback filterCallback; if (filterSpec.OptionalPropertyEvaluator != null) { filterCallback = new ProxyFilterHandleCallback() { ProcStatementId = () => statementId, ProcMatchFound = (theEvent, allStmtMatches) => { var result = filterSpec.OptionalPropertyEvaluator.GetProperty(theEvent, agentInstanceContext); if (result != null) { eventStream.Insert(result); } }, ProcIsSubselect = () => false }; } else { filterCallback = new ProxyFilterHandleCallback() { ProcStatementId = () => statementId, ProcMatchFound = (theEvent, allStmtMatches) => { if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QFilterActivationStream(theEvent.EventType.Name, streamNum); } eventStream.Insert(theEvent); if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AFilterActivationStream(); } }, ProcIsSubselect = () => false }; } var handle = new EPStatementHandleCallback(epStatementAgentInstanceHandle, filterCallback); // Store stream for reuse entry = new StreamEntry(eventStream, handle); if (forceNewStream) { _eventStreamsIdentity.Put(filterSpec, entry); } else { _eventStreamsRefCounted[filterSpec] = entry; } // Activate filter var filterValues = filterSpec.GetValueSet(null, agentInstanceContext, null); var filterServiceEntry = filterService.Add(filterValues, handle); entry.FilterServiceEntry = filterServiceEntry; return(new Pair <EventStream, IReaderWriterLock>(inputStream, null)); }
public void Run() { long currentThreadId = Thread.CurrentThread.ManagedThreadId; // Choose one of filter specifications, randomly, then reserve to make sure no one else has the same FilterSpecCompiled filterSpec = null; EventBean unmatchedEvent = null; EventBean matchedEvent = null; var index = 0; do { //index = (int) (atomic.IncrementAndGet() % _testFilterSpecs.Count); index = Random.Next(_testFilterSpecs.Count); filterSpec = _testFilterSpecs[index]; unmatchedEvent = _unmatchedEvents[index]; matchedEvent = _matchedEvents[index]; }while(!ObjectReservationSingleton.Instance.Reserve(filterSpec)); #if DEBUG && DIAGNOSTIC Log.Info("Reserved: {0} = {1}", index, filterSpec); #endif // Add expression var filterValues = filterSpec.GetValueSet(null, null, null); var filterCallback = new SupportFilterHandle(); #if DEBUG && DIAGNOSTIC Log.Info("TestMultithreaded: {0}", filterValues); #endif var pathAddedTo = IndexTreeBuilder.Add(filterValues, filterCallback, _topNode, _lockFactory); // Fire a no-match IList <FilterHandle> matches = new List <FilterHandle>(); _topNode.MatchEvent(unmatchedEvent, matches); if (matches.Count != 0) { Log.Fatal(".Run (" + currentThreadId + ") Got a match but expected no-match, matchCount=" + matches.Count + " bean=" + unmatchedEvent + " match=" + matches[0].GetHashCode()); Assert.IsFalse(true); } // Fire a match _topNode.MatchEvent(matchedEvent, matches); if (matches.Count != 1) { Log.Fatal(".Run (" + currentThreadId + ") Got zero or two or more match but expected a match, count=" + matches.Count + " bean=" + matchedEvent); foreach (var entry in LoggerNLog.MemoryTarget.Logs) { System.Diagnostics.Debug.WriteLine(entry); } Assert.IsFalse(true); } // Remove the same expression again IndexTreeBuilder.Remove(_eventType, filterCallback, pathAddedTo[0].ToArray(), _topNode); ObjectReservationSingleton.Instance.Unreserve(filterSpec); }
public void SetCompiledFilter(FilterSpecCompiled filterSpec, AgentInstanceContext agentInstanceContext) { CompiledFilterParam = filterSpec.GetValueSet(null, agentInstanceContext, null).Parameters; }