Search in sources :

Example 6 with ZkTestServer

use of org.apache.solr.cloud.ZkTestServer 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 7 with ZkTestServer

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

the class SolrZkClientTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    final String SCHEME = "digest";
    final String AUTH = "user:pass";
    String zkDir = createTempDir().toString();
    log.info("ZooKeeper dataDir:" + zkDir);
    zkServer = new ZkTestServer(zkDir);
    zkServer.run();
    try (SolrZkClient client = new SolrZkClient(zkServer.getZkHost(), AbstractZkTestCase.TIMEOUT)) {
        // Set up chroot
        client.makePath("/solr", false, true);
    }
    defaultClient = new SolrZkClient(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT);
    defaultClient.makePath(PATH, true);
    aclClient = new SolrZkClient(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT) {

        @Override
        protected ZkACLProvider createZkACLProvider() {
            return new DefaultZkACLProvider() {

                @Override
                protected List<ACL> createGlobalACLsToAdd() {
                    try {
                        Id id = new Id(SCHEME, DigestAuthenticationProvider.generateDigest(AUTH));
                        return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, id));
                    } catch (NoSuchAlgorithmException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
        }
    };
    credentialsClient = new SolrZkClient(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT) {

        @Override
        protected ZkCredentialsProvider createZkCredentialsToAddAutomatically() {
            return new DefaultZkCredentialsProvider() {

                @Override
                protected Collection<ZkCredentials> createCredentials() {
                    return Collections.singleton(new ZkCredentials(SCHEME, AUTH.getBytes(StandardCharsets.UTF_8)));
                }
            };
        }
    };
}
Also used : ZkTestServer(org.apache.solr.cloud.ZkTestServer) ACL(org.apache.zookeeper.data.ACL) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Collection(java.util.Collection) List(java.util.List) Id(org.apache.zookeeper.data.Id)

Example 8 with ZkTestServer

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

the class TestZkConfigManager method startZkServer.

@BeforeClass
public static void startZkServer() throws InterruptedException {
    zkServer = new ZkTestServer(createTempDir("zkData").toString());
    zkServer.run();
}
Also used : ZkTestServer(org.apache.solr.cloud.ZkTestServer) BeforeClass(org.junit.BeforeClass)

Example 9 with ZkTestServer

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

the class TestManagedSchemaThreadSafety method startZkServer.

@BeforeClass
public static void startZkServer() throws Exception {
    zkServer = new ZkTestServer(createTempDir().toString());
    zkServer.run();
    loaderPath = createTempDir();
}
Also used : ZkTestServer(org.apache.solr.cloud.ZkTestServer) BeforeClass(org.junit.BeforeClass)

Example 10 with ZkTestServer

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

the class ZkStateReaderTest method testStateFormatUpdate.

public void testStateFormatUpdate(boolean explicitRefresh, boolean isInteresting) throws Exception {
    String zkDir = createTempDir("testStateFormatUpdate").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient zkClient = null;
    ZkStateReader reader = null;
    try {
        server.run();
        AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
        AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
        zkClient = new SolrZkClient(server.getZkAddress(), OverseerTest.DEFAULT_CONNECTION_TIMEOUT);
        ZkController.createClusterZkNodes(zkClient);
        reader = new ZkStateReader(zkClient);
        reader.createClusterStateWatchersAndUpdate();
        if (isInteresting) {
            reader.registerCore("c1");
        }
        ZkStateWriter writer = new ZkStateWriter(reader, new Overseer.Stats());
        zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/c1", true);
        {
            // create new collection with stateFormat = 1
            DocCollection stateV1 = new DocCollection("c1", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT, 0, ZkStateReader.CLUSTER_STATE);
            ZkWriteCommand c1 = new ZkWriteCommand("c1", stateV1);
            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);
            if (explicitRefresh) {
                reader.forceUpdateCollection("c1");
            } else {
                reader.waitForState("c1", TIMEOUT, TimeUnit.SECONDS, (n, c) -> c != null);
            }
            DocCollection collection = reader.getClusterState().getCollection("c1");
            assertEquals(1, collection.getStateFormat());
        }
        {
            // Now update the collection to stateFormat = 2
            DocCollection stateV2 = new DocCollection("c1", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT, 0, ZkStateReader.COLLECTIONS_ZKNODE + "/c1/state.json");
            ZkWriteCommand c2 = new ZkWriteCommand("c1", stateV2);
            writer.enqueueUpdate(reader.getClusterState(), c2, null);
            writer.writePendingUpdates();
            Map map = (Map) Utils.fromJSON(zkClient.getData("/clusterstate.json", null, null, true));
            assertNull(map.get("c1"));
            boolean exists = zkClient.exists(ZkStateReader.COLLECTIONS_ZKNODE + "/c1/state.json", true);
            assertTrue(exists);
            if (explicitRefresh) {
                reader.forceUpdateCollection("c1");
            } else {
                reader.waitForState("c1", TIMEOUT, TimeUnit.SECONDS, (n, c) -> c != null && c.getStateFormat() == 2);
            }
            DocCollection collection = reader.getClusterState().getCollection("c1");
            assertEquals(2, collection.getStateFormat());
        }
    } finally {
        IOUtils.close(reader, zkClient);
        server.shutdown();
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) DocCollection(org.apache.solr.common.cloud.DocCollection) ClusterState(org.apache.solr.common.cloud.ClusterState) Utils(org.apache.solr.common.util.Utils) IOUtils(org.apache.lucene.util.IOUtils) HashMap(java.util.HashMap) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) OverseerTest(org.apache.solr.cloud.OverseerTest) TimeUnit(java.util.concurrent.TimeUnit) Map(java.util.Map) Overseer(org.apache.solr.cloud.Overseer) DocRouter(org.apache.solr.common.cloud.DocRouter) AbstractZkTestCase(org.apache.solr.cloud.AbstractZkTestCase) ZkController(org.apache.solr.cloud.ZkController) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ZkTestServer(org.apache.solr.cloud.ZkTestServer) 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)

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