예제 #1
0
  private static void setBatchIndexConf(byte[] defaultConf, byte[] customConf, boolean buildNow)
      throws Exception {
    String lock = model.lockIndex(INDEX_NAME);

    try {
      IndexDefinition index = model.getMutableIndex(INDEX_NAME);
      if (defaultConf != null) index.setDefaultBatchIndexConfiguration(defaultConf);
      if (customConf != null) index.setBatchIndexConfiguration(customConf);
      if (buildNow) index.setBatchBuildState(IndexBatchBuildState.BUILD_REQUESTED);

      model.updateIndex(index, lock);
    } finally {
      model.unlockIndex(lock);
    }
  }
예제 #2
0
  @Test
  public void testSomeBasics() throws Exception {
    ZooKeeperItf zk1 = ZkUtil.connect("localhost:" + ZK_CLIENT_PORT, 3000);
    ZooKeeperItf zk2 = ZkUtil.connect("localhost:" + ZK_CLIENT_PORT, 3000);
    WriteableIndexerModel model1 = null;
    WriteableIndexerModel model2 = null;
    try {

      TestListener listener = new TestListener();

      model1 = new IndexerModelImpl(zk1);
      model1.registerListener(listener);

      // Create an index
      IndexDefinition index1 = model1.newIndex("index1");
      index1.setConfiguration("<indexer/>".getBytes("UTF-8"));
      index1.setSolrShards(Collections.singletonMap("shard1", "http://localhost:8983/solr"));
      model1.addIndex(index1);

      listener.waitForEvents(1);
      listener.verifyEvents(new IndexerModelEvent(IndexerModelEventType.INDEX_ADDED, "index1"));

      // Verify that a fresh indexer model has the index
      model2 = new IndexerModelImpl(zk2);
      assertEquals(1, model2.getIndexes().size());
      assertTrue(model2.hasIndex("index1"));

      // Update the index
      index1.setGeneralState(IndexGeneralState.DISABLED);
      String lock = model1.lockIndex("index1");
      model1.updateIndex(index1, lock);

      listener.waitForEvents(1);
      listener.verifyEvents(new IndexerModelEvent(IndexerModelEventType.INDEX_UPDATED, "index1"));

      // Do not release the lock, updating through model2 should fail
      index1.setConfiguration("<indexer></indexer>".getBytes("UTF-8"));
      try {
        model2.updateIndex(index1, lock + "foo");
        fail("expected exception");
      } catch (IndexUpdateException e) {
        // verify the exception says something about locks
        assertTrue(e.getMessage().indexOf("lock") != -1);
      }

      model1.unlockIndex(lock);

      model1.deleteIndex("index1");

      listener.waitForEvents(1);
      listener.verifyEvents(new IndexerModelEvent(IndexerModelEventType.INDEX_REMOVED, "index1"));

      // Create some more indexes
      IndexerModelEvent[] expectedEvents = new IndexerModelEvent[9];
      for (int i = 2; i <= 10; i++) {
        String name = "index" + i;
        IndexDefinition index = model1.newIndex(name);
        index.setConfiguration("<indexer/>".getBytes("UTF-8"));
        index.setSolrShards(Collections.singletonMap("shard1", "http://localhost:8983/solr"));
        model1.addIndex(index);
        expectedEvents[i - 2] = new IndexerModelEvent(IndexerModelEventType.INDEX_ADDED, name);
      }

      listener.waitForEvents(9);
      listener.verifyEvents(expectedEvents);
    } finally {
      Closer.close(model1);
      Closer.close(model2);
      Closer.close(zk1);
      Closer.close(zk2);
    }
  }