Search in sources :

Example 31 with ZkNodeProps

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

the class OverseerTest method testStateChange.

@Test
public void testStateChange() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient zkClient = null;
    ZkStateReader reader = null;
    SolrZkClient overseerClient = null;
    try {
        server.run();
        zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
        AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
        AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
        ZkController.createClusterZkNodes(zkClient);
        reader = new ZkStateReader(zkClient);
        reader.createClusterStateWatchersAndUpdate();
        overseerClient = electNewOverseer(server.getZkAddress());
        DistributedQueue q = Overseer.getStateUpdateQueue(zkClient);
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(), ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr", ZkStateReader.NODE_NAME_PROP, "node1", ZkStateReader.COLLECTION_PROP, COLLECTION, ZkStateReader.CORE_NAME_PROP, "core1", ZkStateReader.ROLES_PROP, "", ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());
        q.offer(Utils.toJSON(m));
        waitForCollections(reader, COLLECTION);
        assertSame(reader.getClusterState().toString(), Replica.State.RECOVERING, reader.getClusterState().getSlice(COLLECTION, "shard1").getReplica("core_node1").getState());
        //publish node state (active)
        m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(), ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr", ZkStateReader.NODE_NAME_PROP, "node1", ZkStateReader.COLLECTION_PROP, COLLECTION, ZkStateReader.CORE_NAME_PROP, "core1", ZkStateReader.ROLES_PROP, "", ZkStateReader.STATE_PROP, Replica.State.ACTIVE.toString());
        q.offer(Utils.toJSON(m));
        verifyReplicaStatus(reader, "collection1", "shard1", "core_node1", Replica.State.ACTIVE);
    } finally {
        close(zkClient);
        close(overseerClient);
        close(reader);
        server.shutdown();
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Test(org.junit.Test)

Example 32 with ZkNodeProps

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

the class OverseerTest method verifyShardLeader.

private void verifyShardLeader(ZkStateReader reader, String collection, String shard, String expectedCore) throws InterruptedException, KeeperException {
    int maxIterations = 200;
    while (maxIterations-- > 0) {
        ZkNodeProps props = reader.getClusterState().getLeader(collection, shard);
        if (props != null) {
            if (expectedCore.equals(props.getStr(ZkStateReader.CORE_NAME_PROP))) {
                return;
            }
        }
        Thread.sleep(200);
    }
    assertEquals("Unexpected shard leader coll:" + collection + " shard:" + shard, expectedCore, (reader.getClusterState().getLeader(collection, shard) != null) ? reader.getClusterState().getLeader(collection, shard).getStr(ZkStateReader.CORE_NAME_PROP) : null);
}
Also used : ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps)

Example 33 with ZkNodeProps

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

the class NodeMutatorTest method downNodeReportsAllImpactedCollectionsAndNothingElse.

@Test
public void downNodeReportsAllImpactedCollectionsAndNothingElse() throws IOException {
    NodeMutator nm = new NodeMutator();
    ZkNodeProps props = new ZkNodeProps(ZkStateReader.NODE_NAME_PROP, NODE1);
    //We use 2 nodes with maxShardsPerNode as 1
    //Collection1: 2 shards X 1 replica = replica1 on node1 and replica2 on node2
    //Collection2: 1 shard X 1 replica = replica1 on node2
    ClusterStateMockUtil.Result result = ClusterStateMockUtil.buildClusterState(null, "csrr2rD*csr2", 1, 1, NODE1, NODE2);
    ClusterState clusterState = result.reader.getClusterState();
    assertEquals(clusterState.getCollection("collection1").getReplica("replica1").getBaseUrl(), NODE1_URL);
    assertEquals(clusterState.getCollection("collection1").getReplica("replica2").getBaseUrl(), NODE2_URL);
    assertEquals(clusterState.getCollection("collection2").getReplica("replica4").getBaseUrl(), NODE2_URL);
    props = new ZkNodeProps(ZkStateReader.NODE_NAME_PROP, NODE1);
    List<ZkWriteCommand> writes = nm.downNode(clusterState, props);
    assertEquals(writes.size(), 1);
    assertEquals(writes.get(0).name, "collection1");
    assertEquals(writes.get(0).collection.getReplica("replica1").getState(), Replica.State.DOWN);
    assertEquals(writes.get(0).collection.getReplica("replica2").getState(), Replica.State.ACTIVE);
    result.close();
    //We use 3 nodes with maxShardsPerNode as 1
    //Collection1: 2 shards X 1 replica = replica1 on node1 and replica2 on node2
    //Collection2: 1 shard X 1 replica = replica1 on node2
    //Collection3: 1 shard X 3 replica = replica1 on node1 , replica2 on node2, replica3 on node3
    result = ClusterStateMockUtil.buildClusterState(null, "csrr2rD*csr2csr1r2r3", 1, 1, NODE1, NODE2, NODE3);
    clusterState = result.reader.getClusterState();
    assertEquals(clusterState.getCollection("collection1").getReplica("replica1").getBaseUrl(), NODE1_URL);
    assertEquals(clusterState.getCollection("collection1").getReplica("replica2").getBaseUrl(), NODE2_URL);
    assertEquals(clusterState.getCollection("collection2").getReplica("replica4").getBaseUrl(), NODE2_URL);
    assertEquals(clusterState.getCollection("collection3").getReplica("replica5").getBaseUrl(), NODE1_URL);
    assertEquals(clusterState.getCollection("collection3").getReplica("replica6").getBaseUrl(), NODE2_URL);
    assertEquals(clusterState.getCollection("collection3").getReplica("replica7").getBaseUrl(), NODE3_URL);
    writes = nm.downNode(clusterState, props);
    assertEquals(writes.size(), 2);
    for (ZkWriteCommand write : writes) {
        if (write.name.equals("collection1")) {
            assertEquals(write.collection.getReplica("replica1").getState(), Replica.State.DOWN);
            assertEquals(write.collection.getReplica("replica2").getState(), Replica.State.ACTIVE);
        } else if (write.name.equals("collection3")) {
            assertEquals(write.collection.getReplica("replica5").getState(), Replica.State.DOWN);
            assertEquals(write.collection.getReplica("replica6").getState(), Replica.State.ACTIVE);
            assertEquals(write.collection.getReplica("replica7").getState(), Replica.State.ACTIVE);
        } else {
            fail("No other collection needs to be changed");
        }
    }
    result.close();
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) ZkWriteCommand(org.apache.solr.cloud.overseer.ZkWriteCommand) NodeMutator(org.apache.solr.cloud.overseer.NodeMutator) SolrTestCaseJ4Test(org.apache.solr.SolrTestCaseJ4Test) Test(org.junit.Test)

Example 34 with ZkNodeProps

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

the class TestClusterStateMutator method testCreateCollection.

public void testCreateCollection() throws Exception {
    ClusterState state = new ClusterState(-1, Collections.<String>emptySet(), Collections.<String, DocCollection>emptyMap());
    MockZkStateReader zkStateReader = new MockZkStateReader(state, Collections.<String>emptySet());
    ClusterState clusterState = zkStateReader.getClusterState();
    ClusterStateMutator mutator = new ClusterStateMutator(zkStateReader);
    ZkNodeProps message = new ZkNodeProps(Utils.makeMap("name", "xyz", "numShards", "1"));
    ZkWriteCommand cmd = mutator.createCollection(clusterState, message);
    DocCollection collection = cmd.collection;
    assertEquals("xyz", collection.getName());
    assertEquals(1, collection.getSlicesMap().size());
    assertEquals(1, collection.getMaxShardsPerNode());
    state = new ClusterState(-1, Collections.<String>emptySet(), Collections.singletonMap("xyz", collection));
    message = new ZkNodeProps(Utils.makeMap("name", "abc", "numShards", "2", "router.name", "implicit", "shards", "x,y", "replicationFactor", "3", "maxShardsPerNode", "4"));
    cmd = mutator.createCollection(state, message);
    collection = cmd.collection;
    assertEquals("abc", collection.getName());
    assertEquals(2, collection.getSlicesMap().size());
    assertNotNull(collection.getSlicesMap().get("x"));
    assertNotNull(collection.getSlicesMap().get("y"));
    assertNull(collection.getSlicesMap().get("x").getRange());
    assertNull(collection.getSlicesMap().get("y").getRange());
    assertSame(Slice.State.ACTIVE, collection.getSlicesMap().get("x").getState());
    assertSame(Slice.State.ACTIVE, collection.getSlicesMap().get("y").getState());
    assertEquals(4, collection.getMaxShardsPerNode());
    assertEquals(ImplicitDocRouter.class, collection.getRouter().getClass());
    // we still have the old collection
    assertNotNull(state.getCollectionOrNull("xyz"));
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) DocCollection(org.apache.solr.common.cloud.DocCollection) MockZkStateReader(org.apache.solr.cloud.MockZkStateReader)

Example 35 with ZkNodeProps

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

the class ZkCLITest method testUpConfigLinkConfigClearZk.

@Test
public void testUpConfigLinkConfigClearZk() throws Exception {
    File tmpDir = createTempDir().toFile();
    // test upconfig
    String confsetname = "confsetone";
    final String[] upconfigArgs;
    if (random().nextBoolean()) {
        upconfigArgs = new String[] { "-zkhost", zkServer.getZkAddress(), "-cmd", ZkCLI.UPCONFIG, "-confdir", ExternalPaths.TECHPRODUCTS_CONFIGSET, "-confname", confsetname };
    } else {
        final String excluderegexOption = (random().nextBoolean() ? "--" + ZkCLI.EXCLUDE_REGEX : "-" + ZkCLI.EXCLUDE_REGEX_SHORT);
        upconfigArgs = new String[] { "-zkhost", zkServer.getZkAddress(), "-cmd", ZkCLI.UPCONFIG, excluderegexOption, ZkCLI.EXCLUDE_REGEX_DEFAULT, "-confdir", ExternalPaths.TECHPRODUCTS_CONFIGSET, "-confname", confsetname };
    }
    ZkCLI.main(upconfigArgs);
    assertTrue(zkClient.exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + confsetname, true));
    // print help
    // ZkCLI.main(new String[0]);
    // test linkconfig
    String[] args = new String[] { "-zkhost", zkServer.getZkAddress(), "-cmd", "linkconfig", "-collection", "collection1", "-confname", confsetname };
    ZkCLI.main(args);
    ZkNodeProps collectionProps = ZkNodeProps.load(zkClient.getData(ZkStateReader.COLLECTIONS_ZKNODE + "/collection1", null, null, true));
    assertTrue(collectionProps.containsKey("configName"));
    assertEquals(confsetname, collectionProps.getStr("configName"));
    // test down config
    File confDir = new File(tmpDir, "solrtest-confdropspot-" + this.getClass().getName() + "-" + System.nanoTime());
    assertFalse(confDir.exists());
    args = new String[] { "-zkhost", zkServer.getZkAddress(), "-cmd", "downconfig", "-confdir", confDir.getAbsolutePath(), "-confname", confsetname };
    ZkCLI.main(args);
    File[] files = confDir.listFiles();
    List<String> zkFiles = zkClient.getChildren(ZkConfigManager.CONFIGS_ZKNODE + "/" + confsetname, null, true);
    assertEquals(files.length, zkFiles.size());
    File sourceConfDir = new File(ExternalPaths.TECHPRODUCTS_CONFIGSET);
    // filter out all directories starting with . (e.g. .svn)
    Collection<File> sourceFiles = FileUtils.listFiles(sourceConfDir, TrueFileFilter.INSTANCE, new RegexFileFilter("[^\\.].*"));
    for (File sourceFile : sourceFiles) {
        int indexOfRelativePath = sourceFile.getAbsolutePath().lastIndexOf("sample_techproducts_configs" + File.separator + "conf");
        String relativePathofFile = sourceFile.getAbsolutePath().substring(indexOfRelativePath + 33, sourceFile.getAbsolutePath().length());
        File downloadedFile = new File(confDir, relativePathofFile);
        if (ZkConfigManager.UPLOAD_FILENAME_EXCLUDE_PATTERN.matcher(relativePathofFile).matches()) {
            assertFalse(sourceFile.getAbsolutePath() + " exists in ZK, downloaded:" + downloadedFile.getAbsolutePath(), downloadedFile.exists());
        } else {
            assertTrue(downloadedFile.getAbsolutePath() + " does not exist source:" + sourceFile.getAbsolutePath(), downloadedFile.exists());
            assertTrue(relativePathofFile + " content changed", FileUtils.contentEquals(sourceFile, downloadedFile));
        }
    }
    // test reset zk
    args = new String[] { "-zkhost", zkServer.getZkAddress(), "-cmd", "clear", "/" };
    ZkCLI.main(args);
    assertEquals(0, zkClient.getChildren("/", null, true).size());
}
Also used : ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) File(java.io.File) Test(org.junit.Test)

Aggregations

ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)91 SolrException (org.apache.solr.common.SolrException)35 HashMap (java.util.HashMap)28 Replica (org.apache.solr.common.cloud.Replica)22 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)20 ArrayList (java.util.ArrayList)19 Slice (org.apache.solr.common.cloud.Slice)19 KeeperException (org.apache.zookeeper.KeeperException)19 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)16 Test (org.junit.Test)16 DocCollection (org.apache.solr.common.cloud.DocCollection)15 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)14 Map (java.util.Map)13 ClusterState (org.apache.solr.common.cloud.ClusterState)13 IOException (java.io.IOException)10 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)10 ZooKeeperException (org.apache.solr.common.cloud.ZooKeeperException)10 NamedList (org.apache.solr.common.util.NamedList)10 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)9 SolrCore (org.apache.solr.core.SolrCore)8