Search in sources :

Example 11 with ZkTestServer

use of org.apache.solr.cloud.ZkTestServer in project lucene-solr by apache.

the class ZkStateWriterTest method testZkStateWriterBatching.

public void testZkStateWriterBatching() throws Exception {
    String zkDir = createTempDir("testZkStateWriterBatching").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient zkClient = null;
    try {
        server.run();
        AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
        AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
        zkClient = new SolrZkClient(server.getZkAddress(), OverseerTest.DEFAULT_CONNECTION_TIMEOUT);
        ZkController.createClusterZkNodes(zkClient);
        try (ZkStateReader reader = new ZkStateReader(zkClient)) {
            reader.createClusterStateWatchersAndUpdate();
            ZkStateWriter writer = new ZkStateWriter(reader, new Overseer.Stats());
            assertFalse("Deletes can always be batched", writer.maybeFlushBefore(new ZkWriteCommand("xyz", null)));
            assertFalse("Deletes can always be batched", writer.maybeFlushAfter(new ZkWriteCommand("xyz", null)));
            zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/c1", true);
            zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/c2", true);
            // create new collection with stateFormat = 2
            ZkWriteCommand c1 = new ZkWriteCommand("c1", new DocCollection("c1", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT, 0, ZkStateReader.COLLECTIONS_ZKNODE + "/c1"));
            assertFalse("First requests can always be batched", writer.maybeFlushBefore(c1));
            ClusterState clusterState = writer.enqueueUpdate(reader.getClusterState(), c1, null);
            ZkWriteCommand c2 = new ZkWriteCommand("c2", new DocCollection("c2", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT, 0, ZkStateReader.COLLECTIONS_ZKNODE + "/c2"));
            assertFalse("Different (new) collection create can be batched together with another create", writer.maybeFlushBefore(c2));
            // simulate three state changes on same collection, all should be batched together before
            assertFalse(writer.maybeFlushBefore(c1));
            assertFalse(writer.maybeFlushBefore(c1));
            assertFalse(writer.maybeFlushBefore(c1));
            // and after too
            assertFalse(writer.maybeFlushAfter(c1));
            assertFalse(writer.maybeFlushAfter(c1));
            assertFalse(writer.maybeFlushAfter(c1));
            // simulate three state changes on two different collections with stateFormat=2, all should be batched
            assertFalse(writer.maybeFlushBefore(c1));
            // flushAfter has to be called as it updates the internal batching related info
            assertFalse(writer.maybeFlushAfter(c1));
            assertFalse(writer.maybeFlushBefore(c2));
            assertFalse(writer.maybeFlushAfter(c2));
            assertFalse(writer.maybeFlushBefore(c1));
            assertFalse(writer.maybeFlushAfter(c1));
            // create a collection in stateFormat = 1 i.e. inside the main cluster state
            ZkWriteCommand c3 = new ZkWriteCommand("c3", new DocCollection("c3", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT, 0, ZkStateReader.CLUSTER_STATE));
            clusterState = writer.enqueueUpdate(clusterState, c3, null);
            // simulate three state changes in c3, all should be batched
            for (int i = 0; i < 3; i++) {
                assertFalse(writer.maybeFlushBefore(c3));
                assertFalse(writer.maybeFlushAfter(c3));
            }
            // simulate state change in c3 (stateFormat=1) interleaved with state changes from c1,c2 (stateFormat=2)
            // none should be batched together
            assertFalse(writer.maybeFlushBefore(c3));
            assertFalse(writer.maybeFlushAfter(c3));
            assertTrue("different stateFormat, should be flushed", writer.maybeFlushBefore(c1));
            assertFalse(writer.maybeFlushAfter(c1));
            assertTrue("different stateFormat, should be flushed", writer.maybeFlushBefore(c3));
            assertFalse(writer.maybeFlushAfter(c3));
            assertTrue("different stateFormat, should be flushed", writer.maybeFlushBefore(c2));
            assertFalse(writer.maybeFlushAfter(c2));
        }
    } finally {
        IOUtils.close(zkClient);
        server.shutdown();
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) ZkTestServer(org.apache.solr.cloud.ZkTestServer) Overseer(org.apache.solr.cloud.Overseer) HashMap(java.util.HashMap) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 12 with ZkTestServer

use of org.apache.solr.cloud.ZkTestServer in project lucene-solr by apache.

the class ZkStateWriterTest method testSingleLegacyCollection.

public void testSingleLegacyCollection() throws Exception {
    String zkDir = createTempDir("testSingleLegacyCollection").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient zkClient = null;
    try {
        server.run();
        AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
        AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
        zkClient = new SolrZkClient(server.getZkAddress(), OverseerTest.DEFAULT_CONNECTION_TIMEOUT);
        ZkController.createClusterZkNodes(zkClient);
        try (ZkStateReader reader = new ZkStateReader(zkClient)) {
            reader.createClusterStateWatchersAndUpdate();
            ZkStateWriter writer = new ZkStateWriter(reader, new Overseer.Stats());
            zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/c1", true);
            // create new collection with stateFormat = 1
            ZkWriteCommand c1 = new ZkWriteCommand("c1", new DocCollection("c1", new HashMap<String, Slice>(), new HashMap<String, Object>(), DocRouter.DEFAULT, 0, ZkStateReader.CLUSTER_STATE));
            ClusterState clusterState = writer.enqueueUpdate(reader.getClusterState(), c1, null);
            writer.writePendingUpdates();
            Map map = (Map) Utils.fromJSON(zkClient.getData("/clusterstate.json", null, null, true));
            assertNotNull(map.get("c1"));
            boolean exists = zkClient.exists(ZkStateReader.COLLECTIONS_ZKNODE + "/c1/state.json", true);
            assertFalse(exists);
        }
    } finally {
        IOUtils.close(zkClient);
        server.shutdown();
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) ZkTestServer(org.apache.solr.cloud.ZkTestServer) Overseer(org.apache.solr.cloud.Overseer) HashMap(java.util.HashMap) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) DocCollection(org.apache.solr.common.cloud.DocCollection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ZkTestServer (org.apache.solr.cloud.ZkTestServer)12 Overseer (org.apache.solr.cloud.Overseer)8 DocCollection (org.apache.solr.common.cloud.DocCollection)8 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)8 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)8 HashMap (java.util.HashMap)7 ClusterState (org.apache.solr.common.cloud.ClusterState)7 Map (java.util.Map)3 BeforeClass (org.junit.BeforeClass)3 KeeperException (org.apache.zookeeper.KeeperException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Collection (java.util.Collection)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 IOUtils (org.apache.lucene.util.IOUtils)1 SolrTestCaseJ4 (org.apache.solr.SolrTestCaseJ4)1 AbstractZkTestCase (org.apache.solr.cloud.AbstractZkTestCase)1 OverseerTest (org.apache.solr.cloud.OverseerTest)1 ZkController (org.apache.solr.cloud.ZkController)1 DocRouter (org.apache.solr.common.cloud.DocRouter)1