public GroupedCorrelationEvents groupByComponent(String rootEventId) { return new GroupedCorrelationEvents(buildRelatedEventsMap(rootEventId), rootEventId); }
private void addNonExistingParents(Map<String, EventInformation> allEventsMap, Map<String, EventInformation> relatedEventsMap) { Map<String, EventInformation> eventsToAddMap = new HashMap<>(); relatedEventsMap.forEach((eventId, event) -> findAndAddParentsIfNecessary(allEventsMap, relatedEventsMap, eventsToAddMap, event)); // add all the events we newly found relatedEventsMap.putAll(eventsToAddMap); }
@GET @Path("/topologies/{topologyId}/testhistories/{historyId}/events/correlated/{rootEventId}") public Response getGroupedCorrelatedEventsOfTestRunTopologyHistory(@Context UriInfo urlInfo, @PathParam("topologyId") Long topologyId, @PathParam("historyId") Long historyId, @PathParam("rootEventId") String rootEventId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkRoleOrPermissions(authorizer, securityContext, Roles.ROLE_TOPOLOGY_USER, Topology.NAMESPACE, topologyId, READ); File eventLogFile = getEventLogFile(topologyId, historyId); List<EventInformation> events = eventLogFileReader.loadEventLogFile(eventLogFile); GroupedCorrelationEvents groupedEvents = new CorrelatedEventsGrouper(events).groupByComponent(rootEventId); if (!groupedEvents.getAllEvents().containsKey(rootEventId)) { throw BadRequestException.message("Can't find provided root event " + rootEventId + " from events."); } return WSUtils.respondEntity(groupedEvents, OK); }
@GET @Path("/topologies/{topologyId}/testhistories/{historyId}/events/root") public Response getRootEventsOfTestRunTopologyHistory(@Context UriInfo urlInfo, @PathParam("topologyId") Long topologyId, @PathParam("historyId") Long historyId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkRoleOrPermissions(authorizer, securityContext, Roles.ROLE_TOPOLOGY_USER, Topology.NAMESPACE, topologyId, READ); File eventLogFile = getEventLogFile(topologyId, historyId); List<EventInformation> events = eventLogFileReader.loadEventLogFile(eventLogFile); List<com.hortonworks.registries.common.QueryParam> qps = com.hortonworks.registries.common.QueryParam.params( TopologySource.TOPOLOGYID, topologyId.toString(), TopologySource.VERSIONID, catalogService.getCurrentVersionId(topologyId).toString()); Set<String> sourceNames = catalogService.listTopologySources(qps).stream() .map(TopologyComponent::getName) .collect(Collectors.toSet()); return WSUtils.respondEntities(new CorrelatedEventsGrouper(events).groupByRelatedSourceEvents(sourceNames), OK); }
private Map<String, EventInformation> buildRelatedEventsMap(String rootEventId) { Map<String, EventInformation> allEventsMap = events.stream() .collect(toMap(EventInformation::getEventId, e -> e)); Stream<EventInformation> eventsAssociatedToRootEventStream = events.stream().filter(e -> { boolean isRootEvent = e.getEventId().equals(rootEventId); boolean containsRootEvent = e.getRootIds() != null && e.getRootIds().contains(rootEventId); return isRootEvent || containsRootEvent; }); Map<String, EventInformation> relatedEventsMap = eventsAssociatedToRootEventStream.collect( toMap(EventInformation::getEventId, e -> e)); addNonExistingParents(allEventsMap, relatedEventsMap); return relatedEventsMap; }
private void findAndAddParentsIfNecessary(Map<String, EventInformation> allEventsMap, Map<String, EventInformation> relatedEventsMap, Map<String, EventInformation> eventsToAddMap, EventInformation event) { Set<String> parentIds = event.getParentIds(); for (String parentId : parentIds) { if (!relatedEventsMap.containsKey(parentId) && !eventsToAddMap.containsKey(parentId)) { // find and add parent to relatedEventsMap EventInformation parent = allEventsMap.get(parentId); if (parent == null) { throw new RuntimeException("Failed to find parent event: logged event information may be corrupted."); } eventsToAddMap.put(parent.getEventId(), parent); // newly found parent event may have parent event(s) as well... find them as well findAndAddParentsIfNecessary(allEventsMap, relatedEventsMap, eventsToAddMap, parent); } } }
/** * Returns the group of correlated events per source where the group of related source events * produces the same downstream events * * @param sources the set of source component names * @return the group of correlated events per source */ public List<Set<String>> groupByRelatedSourceEvents(Set<String> sources) { Multimap<Set<String>, String> allEventsToSourceEvents = LinkedHashMultimap.create(); Stream<String> rootEventIds = events.stream().filter(e -> e != null && e.getRootIds().isEmpty()) .map(EventInformation::getEventId); rootEventIds.forEach(rootEventId -> { Map<String, EventInformation> allRelatedEvents = buildRelatedEventsMap(rootEventId); allEventsToSourceEvents.put(allRelatedEvents.keySet(), rootEventId); }); List<Set<String>> result = new ArrayList<>(); allEventsToSourceEvents.asMap().values().forEach(v -> result.add(new HashSet<>(v)) ); return result; }