Ejemplo n.º 1
0
  @Test
  public void testEventsByIdFilter() {
    Set<Integer> idsToFilter = Sets.newHashSet(3, 10, 6);

    Map<EventObmId, Event> expectedEvents =
        new ImmutableMap.Builder<EventObmId, Event>()
            .put(newEntryWithId(1))
            .put(newEntryWithId(2))
            .put(newEntryWithId(4))
            .put(newEntryWithId(5))
            .put(newEntryWithId(7))
            .put(newEntryWithId(8))
            .put(newEntryWithId(9))
            .build();
    Map<EventObmId, Event> unfilteredEvents =
        new ImmutableMap.Builder<EventObmId, Event>()
            .put(newEntryWithId(1))
            .put(newEntryWithId(2))
            .put(newEntryWithId(3))
            .put(newEntryWithId(4))
            .put(newEntryWithId(5))
            .put(newEntryWithId(6))
            .put(newEntryWithId(7))
            .put(newEntryWithId(8))
            .put(newEntryWithId(9))
            .put(newEntryWithId(10))
            .build();

    EventFilter filter = new EventsByIdFilter(idsToFilter);
    final Map<EventObmId, Event> filteredEvents = filter.filter(unfilteredEvents);
    assertThat(filteredEvents).isEqualTo(expectedEvents);
  }
Ejemplo n.º 2
0
  /**
   * This method creates an event filter registration handler
   *
   * @param eventFilter the event filter for which to create the registration handler
   * @return the created registration handler for the filter
   */
  public synchronized EventFilterRegistrationHandler createEventFilterRegistrationHandler(
      EventFilter eventFilter, int limit) {
    EventFilterRegistrationHandler registrationHandler =
        new EventFilterRegistrationHandler(this, eventFilter, limit);

    this.activeEventFilterRegistrationHandlers.put(eventFilter.getUuid(), registrationHandler);

    return registrationHandler;
  }
Ejemplo n.º 3
0
  /**
   * Via this method, event filters are registered with the event logger service for live
   * notification about events of interest. Before this happens though, the template in the filter
   * is used to query the event registry. Matching events in this registry are sent to the client
   * first before live event notification is established via <code>doRegisterEventFilter()</code>.
   *
   * @param filter the event filter to register
   */
  public void registerEventFilter(EventFilter filter, int limit) {
    if (log.isDebugEnabled())
      log.debug("Passing event filter on to registration: " + filter.toString());

    // hibernate proxy prefetch workaround
    if (filter.getTemplate().getContext() != null) {
      if (Ranger.isEnumerationRange(filter.getTemplate().getContext())) {
        for (Deployment d :
            Ranger.castToEnumerationRange(filter.getTemplate().getContext()).getEnumeration()) {
          d.getDeployedComponent().getCode();
        }
      } else {
        filter.getTemplate().getContext().getDeployedComponent().getCode();
      }
    }
    EventFilterRegistrationHandler eventFilterRegistrationHandler =
        this.createEventFilterRegistrationHandler(filter, limit);

    this.eventFilterRegistrationThreadPool.execute(eventFilterRegistrationHandler);
  }
Ejemplo n.º 4
0
  /**
   * Via this method, event filters are registered with the event logger service for live
   * notification about events of interest.
   *
   * @param filter the filter to register
   */
  public synchronized void registerFilterForLiveEvents(EventFilter filter) {
    if (log.isDebugEnabled())
      log.debug("Registering event filter for live events: " + filter.getUuid());

    // hibernate proxy prefetch workaround
    if (filter.getTemplate().getContext() != null) {
      if (Ranger.isEnumerationRange(filter.getTemplate().getContext())) {
        for (Deployment d :
            Ranger.castToEnumerationRange(filter.getTemplate().getContext()).getEnumeration()) {
          d.getDeployedComponent().getCode();
        }
      } else {
        filter.getTemplate().getContext().getDeployedComponent().getCode();
      }
    }
    this.eventFiltersByUuid.put(filter.getUuid(), filter);

    if (log.isDebugEnabled()) log.debug("Event filter for live events registered.");
  }
Ejemplo n.º 5
0
 /**
  * This predicate determines whether a given event filter has already been
  * registered with the service.
  *
  * @param eventFilter
  *            the filter to look for
  * @return <code>true</true> iff the filter has already been registered.
  */
 public synchronized boolean isRegistered(EventFilter eventFilter) {
   return this.eventFiltersByUuid.containsKey(eventFilter.getUuid());
 }
 @Override
 public boolean matches(EventFilter eventFilter) {
   return (getFilter().equals(eventFilter.getFilter()));
 }
Ejemplo n.º 7
0
  public static final DBObject createFilter(EventFilter filter) {

    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();

    if (filter != null) {
      if (filter.getUser() != null) {
        builder.add("user_id", filter.getUser());
      } else if (filter.getProject() != null) {
        builder.add("project_id", filter.getProject());
      }
      if (filter.getMeter() != null) {
        builder.add("name", filter.getMeter());
      }
      if (filter.getStart() != null) {
        builder.add("start", new BasicDBObject("$gte", filter.getStart()));
      }
      if (filter.getEnd() != null) {
        builder.add("end", new BasicDBObject("$lt", filter.getEnd()));
      }
      if (filter.getResource() != null) {
        builder.add("resource_id", filter.getResource());
      }
      if (filter.getSource() != null) {
        builder.add("source", filter.getSource());
      }
    }

    return builder.get();
  }
Ejemplo n.º 8
0
 public void publishEvent(
     Address caller,
     String mapName,
     EntryEventType eventType,
     Data dataKey,
     Data dataOldValue,
     Data dataValue) {
   Collection<EventRegistration> candidates =
       nodeEngine.getEventService().getRegistrations(SERVICE_NAME, mapName);
   Set<EventRegistration> registrationsWithValue = new HashSet<EventRegistration>();
   Set<EventRegistration> registrationsWithoutValue = new HashSet<EventRegistration>();
   if (candidates.isEmpty()) return;
   Object key = null;
   Object value = null;
   Object oldValue = null;
   for (EventRegistration candidate : candidates) {
     EventFilter filter = candidate.getFilter();
     if (filter instanceof EventServiceImpl.EmptyFilter) {
       registrationsWithValue.add(candidate);
     } else if (filter instanceof QueryEventFilter) {
       Object testValue;
       if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) {
         oldValue = oldValue != null ? oldValue : toObject(dataOldValue);
         testValue = oldValue;
       } else {
         value = value != null ? value : toObject(dataValue);
         testValue = value;
       }
       key = key != null ? key : toObject(dataKey);
       QueryEventFilter queryEventFilter = (QueryEventFilter) filter;
       QueryEntry entry = new QueryEntry(getSerializationService(), dataKey, key, testValue);
       if (queryEventFilter.eval(entry)) {
         if (queryEventFilter.isIncludeValue()) {
           registrationsWithValue.add(candidate);
         } else {
           registrationsWithoutValue.add(candidate);
         }
       }
     } else if (filter.eval(dataKey)) {
       EntryEventFilter eventFilter = (EntryEventFilter) filter;
       if (eventFilter.isIncludeValue()) {
         registrationsWithValue.add(candidate);
       } else {
         registrationsWithoutValue.add(candidate);
       }
     }
   }
   if (registrationsWithValue.isEmpty() && registrationsWithoutValue.isEmpty()) return;
   String source = nodeEngine.getThisAddress().toString();
   if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) {
     dataValue = dataValue != null ? dataValue : dataOldValue;
   }
   EventData event =
       new EventData(
           source, mapName, caller, dataKey, dataValue, dataOldValue, eventType.getType());
   int orderKey = dataKey.hashCode();
   nodeEngine
       .getEventService()
       .publishEvent(SERVICE_NAME, registrationsWithValue, event, orderKey);
   nodeEngine
       .getEventService()
       .publishEvent(
           SERVICE_NAME, registrationsWithoutValue, event.cloneWithoutValues(), orderKey);
 }