Repository: nifi
Updated Branches:
  refs/heads/master e977729b5 -> 8000304e6


NIFI-1442: This closes #306. Use CircularFifoQueue instead of Set to store 
nodes' bulletins
Joint effort by Toivo Adams from PR306 and and Mark Payne

Signed-off-by: joewitt <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/8000304e
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/8000304e
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/8000304e

Branch: refs/heads/master
Commit: 8000304e6a07d9cd850a0a1352095010e206d398
Parents: e977729
Author: Mark Payne <[email protected]>
Authored: Mon Mar 28 10:35:44 2016 -0400
Committer: joewitt <[email protected]>
Committed: Mon Mar 28 13:35:36 2016 -0400

----------------------------------------------------------------------
 .../events/NodeBulletinProcessingStrategy.java  | 40 ++++------------
 .../TestNodeBulletinProcessingStrategy.java     | 49 ++++++++++++++++++++
 2 files changed, 57 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/8000304e/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java
index d3cfd9e..8c00d64 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java
@@ -17,50 +17,26 @@
 package org.apache.nifi.events;
 
 import java.util.HashSet;
-import java.util.LinkedHashSet;
 import java.util.Set;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 
+import org.apache.commons.collections4.queue.CircularFifoQueue;
 import org.apache.nifi.reporting.Bulletin;
 
 /**
  *
  */
 public class NodeBulletinProcessingStrategy implements 
BulletinProcessingStrategy {
-
-    private final Lock lock;
-    private final Set<Bulletin> bulletins;
-
-    public NodeBulletinProcessingStrategy() {
-        lock = new ReentrantLock();
-        bulletins = new LinkedHashSet<>();
-    }
+    static final int MAX_ENTRIES = 5;
+    private final CircularFifoQueue<Bulletin> ringBuffer = new 
CircularFifoQueue<>(MAX_ENTRIES);
 
     @Override
-    public void update(final Bulletin bulletin) {
-        lock.lock();
-        try {
-            bulletins.add(bulletin);
-        } finally {
-            lock.unlock();
-        }
+    public synchronized void update(final Bulletin bulletin) {
+        ringBuffer.add(bulletin);
     }
 
-    public Set<Bulletin> getBulletins() {
-        final Set<Bulletin> response = new HashSet<>();
-
-        lock.lock();
-        try {
-            // get all the bulletins currently stored
-            response.addAll(bulletins);
-
-            // remove the bulletins
-            bulletins.clear();
-        } finally {
-            lock.unlock();
-        }
-
+    public synchronized Set<Bulletin> getBulletins() {
+        final Set<Bulletin> response = new HashSet<>(ringBuffer);
+        ringBuffer.clear();
         return response;
     }
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/8000304e/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java
new file mode 100644
index 0000000..394c940
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.events;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TestNodeBulletinProcessingStrategy {
+
+    @Test
+    public void testUpdate() {
+
+        NodeBulletinProcessingStrategy nBulletinProcessingStrategy = new 
NodeBulletinProcessingStrategy();
+
+        nBulletinProcessingStrategy.update(new ComponentBulletin(1));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(2));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(3));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(4));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(5));
+        assertEquals(5, nBulletinProcessingStrategy.getBulletins().size());
+
+        nBulletinProcessingStrategy.update(new ComponentBulletin(1));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(2));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(3));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(4));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(5));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(6));
+        nBulletinProcessingStrategy.update(new ComponentBulletin(7));
+        assertEquals(NodeBulletinProcessingStrategy.MAX_ENTRIES, 
nBulletinProcessingStrategy.getBulletins().size());
+
+    }
+
+}
\ No newline at end of file

Reply via email to