From 71ed5bafac92f3dd0e8ca4388f49f2c039a8db5b Mon Sep 17 00:00:00 2001 From: Cao Manh Dat Date: Thu, 24 May 2018 09:42:17 +0700 Subject: [PATCH] SOLR-12247: Ensure an event will contains newly added node --- .../autoscaling/NodeAddedTriggerTest.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/cloud/autoscaling/NodeAddedTriggerTest.java b/solr/core/src/test/org/apache/solr/cloud/autoscaling/NodeAddedTriggerTest.java index 183d154e5e9..a186a6ccc10 100644 --- a/solr/core/src/test/org/apache/solr/cloud/autoscaling/NodeAddedTriggerTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/autoscaling/NodeAddedTriggerTest.java @@ -272,19 +272,20 @@ public class NodeAddedTriggerTest extends SolrCloudTestCase { try (NodeAddedTrigger newTrigger = new NodeAddedTrigger("node_added_trigger")) { newTrigger.configure(container.getResourceLoader(), container.getZkController().getSolrCloudManager(), props); newTrigger.init(); - AtomicBoolean fired = new AtomicBoolean(false); + AtomicBoolean stop = new AtomicBoolean(false); AtomicReference eventRef = new AtomicReference<>(); newTrigger.setProcessor(event -> { - if (fired.compareAndSet(false, true)) { + //the processor may get called 2 times, for newly added node and initial nodes + long currentTimeNanos = timeSource.getTimeNs(); + long eventTimeNanos = event.getEventTime(); + long waitForNanos = TimeUnit.NANOSECONDS.convert(waitForSeconds, TimeUnit.SECONDS) - WAIT_FOR_DELTA_NANOS; + if (currentTimeNanos - eventTimeNanos <= waitForNanos) { + fail("NodeAddedListener was fired before the configured waitFor period: currentTimeNanos=" + currentTimeNanos + ", eventTimeNanos=" + eventTimeNanos + ",waitForNanos=" + waitForNanos); + } + List nodeNames = (List) event.getProperty(NodeAddedTrigger.NodeAddedEvent.NODE_NAMES); + if (nodeNames.contains(newNode.getNodeName())) { + stop.set(true); eventRef.set(event); - long currentTimeNanos = timeSource.getTimeNs(); - long eventTimeNanos = event.getEventTime(); - long waitForNanos = TimeUnit.NANOSECONDS.convert(waitForSeconds, TimeUnit.SECONDS) - WAIT_FOR_DELTA_NANOS; - if (currentTimeNanos - eventTimeNanos <= waitForNanos) { - fail("NodeAddedListener was fired before the configured waitFor period: currentTimeNanos=" + currentTimeNanos + ", eventTimeNanos=" + eventTimeNanos + ",waitForNanos=" + waitForNanos); - } - } else { - fail("NodeAddedTrigger was fired more than once!"); } return true; }); @@ -296,15 +297,12 @@ public class NodeAddedTriggerTest extends SolrCloudTestCase { if (counter++ > 10) { fail("Newly added node was not discovered by trigger even after 10 seconds"); } - } while (!fired.get()); + } while (!stop.get()); // ensure the event was fired - assertTrue(fired.get()); + assertTrue(stop.get()); TriggerEvent nodeAddedEvent = eventRef.get(); assertNotNull(nodeAddedEvent); - List nodeNames = (List) nodeAddedEvent.getProperty(NodeAddedTrigger.NodeAddedEvent.NODE_NAMES); - assertTrue("Newly added node was not present in event message", - nodeNames.contains(newNode.getNodeName())); } }