Skip to content

Commit

Permalink
HWKALERTS-243 Ensure DefinitionsEvents are notified to listeners in o…
Browse files Browse the repository at this point in the history
…rder (#308)
  • Loading branch information
lucasponce authored and jshaughn committed Mar 24, 2017
1 parent c0973ad commit 1d0eda1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.hawkular.alerts.api.services;

import java.util.Set;
import java.util.List;

/**
* A listener for reacting to definitions changes.
Expand All @@ -37,5 +37,5 @@ public interface DefinitionsListener {
*
* @param events change events triggering the notification.
*/
void onChange(Set<DefinitionsEvent> events);
void onChange(List<DefinitionsEvent> events);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -98,6 +98,7 @@ public void init() {
initialCacheUpdate();

definitions.registerListener(events -> {
log.debugf("Receiving %s", events);
events.stream().forEach(e -> {
String tenantId = e.getTargetTenantId();
String triggerId = e.getTargetId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public List<ActionListener> getActionsListeners() {
return actionsListeners;
}

public void notifyListeners(Set<DefinitionsEvent> notifications) {
public void notifyListeners(List<DefinitionsEvent> notifications) {
Set<DefinitionsEvent.Type> notificationTypes = notifications.stream()
.map(n -> n.getType())
.collect(Collectors.toSet());
Expand All @@ -142,7 +142,7 @@ public void notifyListeners(Set<DefinitionsEvent> notifications) {
log.debugf("Notified Listener %s of %s", e.getKey(), notificationTypes);
e.getKey().onChange(notifications.stream()
.filter(de -> e.getValue().contains(de.getType()))
.collect(Collectors.toSet()));
.collect(Collectors.toList()));
});
if (!distributed) {
distributedListener.stream().forEach(listener -> listener.onChange(mapDistributedEvents(notifications)));
Expand All @@ -155,7 +155,7 @@ private boolean shouldNotify(Set<DefinitionsEvent.Type> listenerTypes, Set<Defin
return !intersection.isEmpty();
}

private Set<DistributedEvent> mapDistributedEvents(Set<DefinitionsEvent> notification) {
private Set<DistributedEvent> mapDistributedEvents(List<DefinitionsEvent> notification) {
if (notification == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import static org.hawkular.alerts.api.services.DefinitionsEvent.Type.ACTION_DEFINITION_UPDATE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -124,7 +124,7 @@ public class CassDefinitionsServiceImpl implements DefinitionsService {
// desirable to only send a listener update one time, at the end, because we want listeners to be
// efficient (i.e. don't update a cache 100 times in a row for one import of 100 triggers). Methods should not
// manipulate these variables directly, instead call deferNotifications() and releaseNotifications().
private Set<DefinitionsEvent> deferredNotifications = new LinkedHashSet<>();
private List<DefinitionsEvent> deferredNotifications = new ArrayList<>();
private int deferNotificationsCount = 0;
private int batchSize;
private final BatchStatement.Type batchType = BatchStatement.Type.LOGGED;
Expand Down Expand Up @@ -3110,16 +3110,16 @@ private void notifyListeners(final DefinitionsEvent de) {
deferredNotifications.add(de);
return;
}
alertsContext.notifyListeners(Collections.singleton(de));
alertsContext.notifyListeners(Arrays.asList(de));
}

private void notifyListenersDeferred() {
if (deferredNotifications.isEmpty()) {
return;
}

Set<DefinitionsEvent> notifications = deferredNotifications;
deferredNotifications = new LinkedHashSet<>();
List<DefinitionsEvent> notifications = deferredNotifications;
deferredNotifications = new ArrayList<>();
alertsContext.notifyListeners(notifications);
}

Expand Down

0 comments on commit 1d0eda1

Please sign in to comment.