Java Code Examples for org.apache.solr.common.cloud.ZkStateReader#close()

The following examples show how to use org.apache.solr.common.cloud.ZkStateReader#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZkClientClusterStateProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
  synchronized (this) {
    if (false == isClosed && zkStateReader != null) {
      isClosed = true;
      
      // force zkStateReader to null first so that any parallel calls drop into the synch block 
      // getZkStateReader() as soon as possible.
      final ZkStateReader zkToClose = zkStateReader;
      zkStateReader = null;
      if (closeZkStateReader) {
        zkToClose.close();
      }
    }
  }
}
 
Example 2
Source File: ChaosMonkeyShardSplitTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Elects a new overseer
 *
 * @return SolrZkClient
 */
private SolrZkClient electNewOverseer(String address) throws KeeperException,
    InterruptedException, IOException {
  SolrZkClient zkClient = new SolrZkClient(address, TIMEOUT);
  ZkStateReader reader = new ZkStateReader(zkClient);
  LeaderElector overseerElector = new LeaderElector(zkClient);
  UpdateShardHandler updateShardHandler = new UpdateShardHandler(UpdateShardHandlerConfig.DEFAULT);
  try (HttpShardHandlerFactory hshf = new HttpShardHandlerFactory()) {
    Overseer overseer = new Overseer((HttpShardHandler) hshf.getShardHandler(), updateShardHandler, "/admin/cores",
        reader, null, new CloudConfig.CloudConfigBuilder("127.0.0.1", 8983, "solr").build());
    overseer.close();
    ElectionContext ec = new OverseerElectionContext(zkClient, overseer,
        address.replaceAll("/", "_"));
    overseerElector.setup(ec);
    overseerElector.joinElection(ec, false);
  }
  reader.close();
  return zkClient;
}
 
Example 3
Source File: TestCloudSolrClientConnections.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlreadyClosedClusterStateProvider() throws Exception {
  
  final MiniSolrCloudCluster cluster = new MiniSolrCloudCluster(1, createTempDir(),
                                                                buildJettyConfig("/solr"));
  // from a client perspective the behavior of ZkClientClusterStateProvider should be
  // consistent regardless of wether it's constructed with a zkhost or an existing ZkStateReader
  try {
    final ZkClientClusterStateProvider zkHost_provider
      = new ZkClientClusterStateProvider(cluster.getZkServer().getZkAddress());
    
    checkAndCloseProvider(zkHost_provider);
    
    final ZkStateReader reusedZkReader = new ZkStateReader(cluster.getZkClient());
    try {
      reusedZkReader.createClusterStateWatchersAndUpdate();
      final ZkClientClusterStateProvider reader_provider = new ZkClientClusterStateProvider(reusedZkReader);
      checkAndCloseProvider(reader_provider);
      
      // but in the case of a reused StateZkReader,
      // closing the provider must not have closed the ZkStateReader...
      assertEquals(false, reusedZkReader.isClosed());
      
    } finally {
      reusedZkReader.close();
    }
  } finally {
    cluster.shutdown();
  }
}
 
Example 4
Source File: BaseCdcrDistributedZkTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void distribSetUp() throws Exception {
  super.distribSetUp();

  if (shardCount > 0) {
    System.setProperty("numShards", Integer.toString(shardCount));
  } else {
    System.clearProperty("numShards");
  }

  if (isSSLMode()) {
    System.clearProperty("urlScheme");
    ZkStateReader zkStateReader = new ZkStateReader(zkServer.getZkAddress(),
        AbstractZkTestCase.TIMEOUT, AbstractZkTestCase.TIMEOUT);
    try {
      zkStateReader.getZkClient().create(ZkStateReader.CLUSTER_PROPS,
          Utils.toJSON(Collections.singletonMap("urlScheme", "https")),
          CreateMode.PERSISTENT, true);
    } catch (KeeperException.NodeExistsException e) {
      ZkNodeProps props = ZkNodeProps.load(zkStateReader.getZkClient().getData(ZkStateReader.CLUSTER_PROPS,
          null, null, true));
      props = props.plus("urlScheme", "https");
      zkStateReader.getZkClient().setData(CLUSTER_PROPS, Utils.toJSON(props), true);
    } finally {
      zkStateReader.close();
    }
  }
}
 
Example 5
Source File: NodeMutatorTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void downNodeReportsAllImpactedCollectionsAndNothingElse() throws IOException {
  NodeMutator nm = new NodeMutator();

  //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
  ZkStateReader reader = ClusterStateMockUtil.buildClusterState("csrr2rDcsr2", 1, 1, NODE1, NODE2);
  ClusterState clusterState = 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);

  ZkNodeProps 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);
  reader.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
  reader = ClusterStateMockUtil.buildClusterState("csrr2rDcsr2csr1r2r3", 1, 1, NODE1, NODE2, NODE3);
  clusterState = 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");
    }
  }
  reader.close();
}
 
Example 6
Source File: OverseerTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void close(ZkStateReader reader) {
  if (reader != null) {
    reader.close();
  }
}