public void nextEvent(PartitionedEvent event) { this.context.counter().incr("receive_count"); dispatch(event); }
private void inplaceAdd(Map<String, PolicyDefinition> policies, Map<String, CompositePolicyHandler> handlers, PolicyDefinition policy, Map<String, StreamDefinition> sds) { if (handlers.containsKey(policy.getName())) { LOG.error("metadata calculation error, try to add existing PolicyDefinition " + policy); } else { policies.put(policy.getName(), policy); CompositePolicyHandler handler = new CompositePolicyHandler(sds); try { PolicyHandlerContext handlerContext = new PolicyHandlerContext(); handlerContext.setPolicyCounter(this.context.counter()); handlerContext.setPolicyDefinition(policy); handlerContext.setPolicyEvaluator(this); handlerContext.setPolicyEvaluatorId(policyEvaluatorId); handlerContext.setConfig(this.context.config()); handler.prepare(collector, handlerContext); handlers.put(policy.getName(), handler); } catch (Exception e) { LOG.error(e.getMessage(), e); policies.remove(policy.getName()); handlers.remove(policy.getName()); } } }
/** * fixme make selection of PolicyStreamHandler to be more efficient. * * @param partitionedEvent PartitionedEvent */ private void dispatch(PartitionedEvent partitionedEvent) { boolean handled = false; for (Map.Entry<String, CompositePolicyHandler> policyStreamHandler : policyStreamHandlerMap.entrySet()) { if (isAcceptedByPolicy(partitionedEvent, policyDefinitionMap.get(policyStreamHandler.getKey()))) { try { handled = true; this.context.counter().incr("eval_count"); policyStreamHandler.getValue().send(partitionedEvent.getEvent()); } catch (Exception e) { this.context.counter().incr("fail_count"); LOG.error("{} failed to handle {}", policyStreamHandler.getValue(), partitionedEvent.getEvent(), e); } } } if (!handled) { this.context.counter().incr("drop_count"); LOG.warn("Drop stream non-matched event {}, which should not be sent to evaluator", partitionedEvent); } else { this.context.counter().incr("accept_count"); } }
@Override public void execute(Tuple input) { try { this.streamContext.counter().incr("execute_count"); this.router.nextEvent(deserialize(input.getValueByField(AlertConstants.FIELD_0)).withAnchor(input)); } catch (Exception ex) { this.streamContext.counter().incr("fail_count"); LOG.error(ex.getMessage(), ex); this.collector.fail(input); } }
@Override public void drop(PartitionedEvent event) { synchronized (outputLock) { this.streamContext.counter().incr("drop_count"); if (event.getAnchor() != null) { this.outputCollector.ack(event); } else { throw new IllegalStateException(event.toString() + " was not acked as anchor is null"); } } }
/** * TODO: Potential improvement: if StreamSortHandler is expensive, we can use DISRUPTOR to buffer. * * @param event StreamEvent */ public void nextEvent(PartitionedEvent event) { this.context.counter().incr("receive_count"); if (!dispatchToSortHandler(event)) { this.context.counter().incr("direct_count"); // Pass through directly if no need to sort outputCollector.emit(event); } this.context.counter().incr("sort_count"); // Update stream clock time if moving forward and trigger all tick listeners streamTimeClockManager.onTimeUpdate(event.getStreamId(), event.getTimestamp()); }
/** * @param event input event. * @return whether sorted. */ private boolean dispatchToSortHandler(PartitionedEvent event) { if (event.getTimestamp() <= 0) { return false; } StreamSortHandler sortHandler = streamSortHandlers.get(event.getPartition()); if (sortHandler == null) { if (event.isSortRequired()) { LOG.warn("Stream sort handler required has not been loaded so emmit directly: {}", event); this.context.counter().incr("miss_sort_count"); } return false; } else { sortHandler.nextEvent(event); return true; } }
public void emit(PartitionedEvent event) { try { this.streamContext.counter().incr("send_count"); StreamPartition partition = event.getPartition(); List<StreamRouterSpec> routerSpecs = routeSpecMap.get(partition); LOG.error("Partitioner for " + routerSpecs.get(0) + " is null"); synchronized (outputLock) { this.streamContext.counter().incr("fail_count"); this.outputCollector.fail(event); this.streamContext.counter().incr("emit_count"); } catch (RuntimeException ex) { this.streamContext.counter().incr("fail_count"); LOG.error("Failed to emit to {} with {}", targetStreamId, newEvent, ex); throw ex; LOG.error(ex.getMessage(), ex); synchronized (outputLock) { this.streamContext.counter().incr("fail_count"); this.outputCollector.fail(event);
@Override public void execute(Tuple input) { try { streamContext.counter().incr("receive_count"); PublishPartition partition = (PublishPartition) input.getValueByField(AlertConstants.FIELD_0); AlertStreamEvent event = (AlertStreamEvent) input.getValueByField(AlertConstants.FIELD_1); if (logEventEnabled) { LOG.info("Alert publish bolt {}/{} with partition {} received event: {}", this.getBoltId(), this.context.getThisTaskId(), partition, event); } DedupKey dedupKey = new DedupKey(event.getPolicyId(), event.getStreamId()); if (deduplicatorMap != null && deduplicatorMap.containsKey(dedupKey)) { List<AlertStreamEvent> eventList = deduplicatorMap.get(dedupKey).dedup(event); if (eventList == null || eventList.isEmpty()) { collector.ack(input); return; } event.setDuplicationChecked(true); } AlertStreamEvent filteredEvent = alertFilter.filter(event); if (filteredEvent != null) { alertPublisher.nextEvent(partition, filteredEvent); } this.collector.ack(input); streamContext.counter().incr("ack_count"); } catch (Throwable ex) { streamContext.counter().incr("fail_count"); LOG.error(ex.getMessage(), ex); collector.reportError(ex); } }
@Override public void execute(Tuple input) { this.streamContext.counter().incr("execute_count"); try { PartitionedEvent pe = deserialize(input.getValueByField(AlertConstants.FIELD_0)); this.streamContext.counter().incr("meta_conflict"); this.collector.ack(input); this.streamContext.counter().incr("ack_count"); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); synchronized (outputLock) { this.streamContext.counter().incr("fail_count"); this.collector.fail(input);
@Override public void emit(AlertStreamEvent event) { if (event == null) { return; } event.ensureAlertId(); Set<PublishPartition> clonedPublishPartitions = new HashSet<>(publishPartitions); for (PublishPartition publishPartition : clonedPublishPartitions) { // skip the publish partition which is not belong to this policy and also check streamId PublishPartition cloned = publishPartition.clone(); Optional.ofNullable(event) .filter(x -> x != null && x.getSchema() != null && cloned.getPolicyId().equalsIgnoreCase(x.getPolicyId()) && (cloned.getStreamId().equalsIgnoreCase(x.getSchema().getStreamId()) || cloned.getStreamId().equalsIgnoreCase(Publishment.STREAM_NAME_DEFAULT))) .ifPresent(x -> { cloned.getColumns().stream() .filter(y -> event.getSchema().getColumnIndex(y) >= 0 && event.getSchema().getColumnIndex(y) < event.getSchema().getColumns().size()) .map(y -> event.getData()[event.getSchema().getColumnIndex(y)]) .filter(y -> y != null) .forEach(y -> cloned.getColumnValues().add(y)); synchronized (outputLock) { streamContext.counter().incr("alert_count"); delegate.emit(Arrays.asList(cloned, event)); } }); } }
@Test public void testNormal() throws Exception { doReturn(streamCounter).when(streamContext).counter(); publishPartitions.add(createPublishPartition(samplePublishId, samplePolicyId, sampleStreamId)); publishPartitions.add(createPublishPartition(samplePublishId2, samplePolicyId, sampleStreamId2)); alertBoltOutputCollectorWrapper.onAlertBoltSpecChange(publishPartitions, new HashSet<>(), new HashSet<>()); AlertStreamEvent event = new AlertStreamEvent(); event.setPolicyId(samplePolicyId); StreamDefinition sd = new StreamDefinition(); sd.setStreamId(sampleStreamId); sd.setColumns(new ArrayList<>()); event.setSchema(sd); alertBoltOutputCollectorWrapper.emit(event); verify(streamCounter, times(1)).incr(anyString()); verify(outputCollector, times(1)).emit(anyObject()); }
@Test public void testExceptional() throws Exception { doReturn(streamCounter).when(streamContext).counter(); publishPartitions.add(createPublishPartition(samplePublishId, samplePolicyId, sampleStreamId)); publishPartitions.add(createPublishPartition(samplePublishId, samplePolicyId, sampleStreamId)); alertBoltOutputCollectorWrapper.onAlertBoltSpecChange(publishPartitions, new HashSet<>(), new HashSet<>()); AlertStreamEvent event = new AlertStreamEvent(); event.setPolicyId(samplePolicyId); StreamDefinition sd = new StreamDefinition(); sd.setStreamId(sampleStreamId); sd.setColumns(new ArrayList<>()); event.setSchema(sd); alertBoltOutputCollectorWrapper.emit(event); verify(streamCounter, times(1)).incr(anyString()); verify(outputCollector, times(1)).emit(anyObject()); }