Search in sources :

Example 36 with SolrZkClient

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

the class ZkStateWriterTest method testSingleExternalCollection.

public void testSingleExternalCollection() throws Exception {
    String zkDir = createTempDir("testSingleExternalCollection").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 = 2
            ZkWriteCommand c1 = new ZkWriteCommand("c1", new DocCollection("c1", new HashMap<String, Slice>(), new HashMap<String, Object>(), DocRouter.DEFAULT, 0, ZkStateReader.COLLECTIONS_ZKNODE + "/c1/state.json"));
            ClusterState clusterState = writer.enqueueUpdate(reader.getClusterState(), c1, null);
            writer.writePendingUpdates();
            Map map = (Map) Utils.fromJSON(zkClient.getData("/clusterstate.json", null, null, true));
            assertNull(map.get("c1"));
            map = (Map) Utils.fromJSON(zkClient.getData(ZkStateReader.COLLECTIONS_ZKNODE + "/c1/state.json", null, null, true));
            assertNotNull(map.get("c1"));
        }
    } 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with SolrZkClient

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

the class ZkStateWriterTest method testExternalModificationToSharedClusterState.

public void testExternalModificationToSharedClusterState() throws Exception {
    String zkDir = createTempDir("testExternalModification").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);
            zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/c2", true);
            // create collection 1 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));
            writer.enqueueUpdate(reader.getClusterState(), c1, null);
            writer.writePendingUpdates();
            reader.forceUpdateCollection("c1");
            reader.forceUpdateCollection("c2");
            // keep a reference to the current cluster state object
            ClusterState clusterState = reader.getClusterState();
            assertTrue(clusterState.hasCollection("c1"));
            assertFalse(clusterState.hasCollection("c2"));
            // Simulate an external modification to /clusterstate.json
            byte[] data = zkClient.getData("/clusterstate.json", null, null, true);
            zkClient.setData("/clusterstate.json", data, true);
            // enqueue another c1 so that ZkStateWriter has pending updates
            writer.enqueueUpdate(clusterState, c1, null);
            assertTrue(writer.hasPendingUpdates());
            // create collection 2 with stateFormat = 1
            ZkWriteCommand c2 = new ZkWriteCommand("c2", new DocCollection("c2", new HashMap<String, Slice>(), new HashMap<String, Object>(), DocRouter.DEFAULT, 0, ZkStateReader.getCollectionPath("c2")));
            try {
                // we are sending in the old cluster state object
                writer.enqueueUpdate(clusterState, c2, null);
                fail("Enqueue should not have succeeded");
            } catch (KeeperException.BadVersionException bve) {
            // expected
            }
            try {
                writer.enqueueUpdate(reader.getClusterState(), c2, null);
                fail("enqueueUpdate after BadVersionException should not have succeeded");
            } catch (IllegalStateException e) {
            // expected
            }
            try {
                writer.writePendingUpdates();
                fail("writePendingUpdates after BadVersionException should not have succeeded");
            } catch (IllegalStateException e) {
            // expected
            }
        }
    } 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) KeeperException(org.apache.zookeeper.KeeperException)

Example 38 with SolrZkClient

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

the class ZkSolrClientTest method testWatchChildren.

public void testWatchChildren() throws Exception {
    try (ZkConnection conn = new ZkConnection()) {
        final SolrZkClient zkClient = conn.getClient();
        final AtomicInteger cnt = new AtomicInteger();
        final CountDownLatch latch = new CountDownLatch(1);
        zkClient.makePath("/collections", true);
        zkClient.getChildren("/collections", new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                cnt.incrementAndGet();
                // remake watch
                try {
                    zkClient.getChildren("/collections", this, true);
                    latch.countDown();
                } catch (KeeperException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, true);
        zkClient.makePath("/collections/collection99/shards", true);
        //wait until watch has been re-created
        latch.await();
        zkClient.makePath("collections/collection99/config=collection1", true);
        zkClient.makePath("collections/collection99/config=collection3", true);
        zkClient.makePath("/collections/collection97/shards", true);
        // pause for the watches to fire
        Thread.sleep(700);
        if (cnt.intValue() < 2) {
            // wait a bit more
            Thread.sleep(4000);
        }
        if (cnt.intValue() < 2) {
            // wait a bit more
            Thread.sleep(4000);
        }
        assertEquals(2, cnt.intValue());
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 39 with SolrZkClient

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

the class ZkSolrClientTest method testClean.

public void testClean() throws Exception {
    try (ZkConnection conn = new ZkConnection()) {
        final SolrZkClient zkClient = conn.getClient();
        zkClient.makePath("/test/path/here", true);
        zkClient.makePath("/zz/path/here", true);
        zkClient.clean("/");
        assertFalse(zkClient.exists("/test", true));
        assertFalse(zkClient.exists("/zz", true));
    }
}
Also used : SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 40 with SolrZkClient

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

the class VMParamsZkACLAndCredentialsProvidersTest method testNoCredentials.

@Test
public void testNoCredentials() throws Exception {
    useNoCredentials();
    SolrZkClient zkClient = new SolrZkClient(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT);
    try {
        doTest(zkClient, false, false, false, false, false, false, false, false, false, false);
    } finally {
        zkClient.close();
    }
}
Also used : SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Test(org.junit.Test)

Aggregations

SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)130 Test (org.junit.Test)46 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)34 HashMap (java.util.HashMap)21 KeeperException (org.apache.zookeeper.KeeperException)18 SolrException (org.apache.solr.common.SolrException)15 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)14 IOException (java.io.IOException)13 ClusterState (org.apache.solr.common.cloud.ClusterState)13 DocCollection (org.apache.solr.common.cloud.DocCollection)12 Map (java.util.Map)11 Slice (org.apache.solr.common.cloud.Slice)11 Replica (org.apache.solr.common.cloud.Replica)10 ArrayList (java.util.ArrayList)9 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)8 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)8 Overseer (org.apache.solr.cloud.Overseer)8 ZkTestServer (org.apache.solr.cloud.ZkTestServer)8 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 NamedList (org.apache.solr.common.util.NamedList)7