Java Code Examples for org.apache.solr.common.cloud.ZkStateReader#registerCollectionPropsWatcher()

The following examples show how to use org.apache.solr.common.cloud.ZkStateReader#registerCollectionPropsWatcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionPropsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWatcher() throws KeeperException, InterruptedException, IOException {
  final ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  CollectionProperties collectionProps = new CollectionProperties(zkClient());

  // Add a watcher to collection props
  final Watcher watcher = new Watcher("Watcher", random().nextBoolean());
  zkStateReader.registerCollectionPropsWatcher(collectionName, watcher);
  assertEquals(0, watcher.waitForTrigger(TEST_NIGHTLY?2000:200));

  // Trigger a new znode event
  log.info("setting value1");
  collectionProps.setCollectionProperty(collectionName, "property", "value1");
  assertEquals(1, watcher.waitForTrigger());
  assertEquals("value1", watcher.getProps().get("property"));

  // Trigger a value change event
  log.info("setting value2");
  collectionProps.setCollectionProperty(collectionName, "property", "value2");
  if (log.isInfoEnabled()) {
    log.info("(value2) waitForTrigger=={}", watcher.waitForTrigger());
  }
  assertEquals("value2", watcher.getProps().get("property"));

  // Delete the properties znode
  log.info("deleting props");
  zkStateReader.getZkClient().delete("/collections/" + collectionName + "/collectionprops.json", -1, true);
  assertEquals(1, watcher.waitForTrigger());
  final Map<String, String> props = watcher.getProps();
  assertTrue(props.toString(), props.isEmpty());

  // Remove watcher and make sure that the watcher is not triggered
  log.info("removing watcher");
  zkStateReader.removeCollectionPropsWatcher(collectionName, watcher);
  log.info("setting value1 (again)");
  collectionProps.setCollectionProperty(collectionName, "property", "value1");
  assertEquals("ZK watcher was triggered after it was removed for collection " + collectionName, 0, watcher.waitForTrigger());
}
 
Example 2
Source File: CollectionPropsTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleWatchers() throws InterruptedException, IOException {
  final ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  CollectionProperties collectionProps = new CollectionProperties(zkClient());

  // Register the core with ZkStateReader
  zkStateReader.registerCore(collectionName);

  // Subsequent watchers won't be triggered when adding
  final Watcher watcher1 = new Watcher("Watcher1", random().nextBoolean());
  zkStateReader.registerCollectionPropsWatcher(collectionName, watcher1);
  watcher1.waitForTrigger(); // this might still get triggered because of registerCore
  final Watcher watcher2 = new Watcher("Watcher2", random().nextBoolean());
  zkStateReader.registerCollectionPropsWatcher(collectionName, watcher2);
  assertEquals(0, watcher2.waitForTrigger(TEST_NIGHTLY?2000:200));

  // Make sure a value change triggers both watchers
  log.info("setting value1");
  collectionProps.setCollectionProperty(collectionName, "property", "value1");
  assertEquals(1, watcher1.waitForTrigger());
  assertEquals(1, watcher2.waitForTrigger());
  assertEquals("value1", watcher1.getProps().get("property"));
  assertEquals("value1", watcher2.getProps().get("property"));

  // The watchers should be triggered when after the core is unregistered
  zkStateReader.unregisterCore(collectionName);
  log.info("setting value2");
  collectionProps.setCollectionProperty(collectionName, "property", "value2");
  assertEquals(1, watcher1.waitForTrigger());
  assertEquals(1, watcher2.waitForTrigger());
  assertEquals("value2", watcher1.getProps().get("property"));
  assertEquals("value2", watcher2.getProps().get("property"));

  // The watcher should be triggered after another watcher is removed
  log.info("removing watcher2");
  zkStateReader.removeCollectionPropsWatcher(collectionName, watcher2);
  log.info("setting value3");
  collectionProps.setCollectionProperty(collectionName, "property", "value3");
  assertEquals(1, watcher1.waitForTrigger());
  assertEquals("value3", watcher1.getProps().get("property"));

  // The last watcher shouldn't be triggered after removed, even if the core is registered
  zkStateReader.registerCore(collectionName);
  log.info("removing watcher1");
  zkStateReader.removeCollectionPropsWatcher(collectionName, watcher1);
  log.info("setting value4");
  collectionProps.setCollectionProperty(collectionName, "property", "value4");
  assertEquals(0, watcher1.waitForTrigger(TEST_NIGHTLY?2000:200));
}