org.apache.zookeeper.KeeperException.SessionExpiredException#org.apache.solr.common.cloud.ZkStateReader源码实例Demo

下面列出了org.apache.zookeeper.KeeperException.SessionExpiredException#org.apache.solr.common.cloud.ZkStateReader 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ambari-logsearch   文件: SolrSchemaFieldDao.java
@SuppressWarnings("unchecked")
private List<LukeResponse> getLukeResponsesForCores(CloudSolrClient solrClient) {
  ZkStateReader zkStateReader = solrClient.getZkStateReader();
  Collection<Slice> activeSlices = zkStateReader.getClusterState().getCollection(solrClient.getDefaultCollection()).getActiveSlices();
  
  List<LukeResponse> lukeResponses = new ArrayList<>();
  for (Slice slice : activeSlices) {
    for (Replica replica : slice.getReplicas()) {
      try (CloseableHttpClient httpClient = HttpClientUtil.createClient(null)) {
        HttpGet request = new HttpGet(replica.getCoreUrl() + LUKE_REQUEST_URL_SUFFIX);
        HttpResponse response = httpClient.execute(request);
        @SuppressWarnings("resource") // JavaBinCodec implements Closeable, yet it can't be closed if it is used for unmarshalling only
        NamedList<Object> lukeData = (NamedList<Object>) new JavaBinCodec().unmarshal(response.getEntity().getContent());
        LukeResponse lukeResponse = new LukeResponse();
        lukeResponse.setResponse(lukeData);
        lukeResponses.add(lukeResponse);
      } catch (IOException e) {
        logger.error("Exception during getting luke responses", e);
      }
    }
  }
  return lukeResponses;
}
 
源代码2 项目: lucene-solr   文件: LeaderElectionTest.java
private String getLeaderUrl(final String collection, final String slice)
    throws KeeperException, InterruptedException {
  int iterCount = 60;
  while (iterCount-- > 0) {
    try {
      byte[] data = zkClient.getData(
          ZkStateReader.getShardLeadersPath(collection, slice), null, null,
          true);
      ZkCoreNodeProps leaderProps = new ZkCoreNodeProps(
          ZkNodeProps.load(data));
      return leaderProps.getCoreUrl();
    } catch (NoNodeException | SessionExpiredException e) {
      Thread.sleep(500);
    }
  }
  zkClient.printLayoutToStream(System.out);
  throw new RuntimeException("Could not get leader props for " + collection + " " + slice);
}
 
源代码3 项目: ambari-logsearch   文件: OutputSolr.java
private void addRouterField() {
  ZkStateReader reader = ((CloudSolrClient) solrClient).getZkStateReader();
  DocCollection docCollection = reader.getClusterState().getCollection(collection);
  Collection<Slice> slices = docCollection.getSlices();
  List<String> shards = slices.stream().map(Slice::getName).collect(Collectors.toList());

  Calendar cal = Calendar.getInstance();
  int weekDay = cal.get(Calendar.DAY_OF_WEEK);
  int currHour = cal.get(Calendar.HOUR_OF_DAY);
  int currMin = cal.get(Calendar.MINUTE);

  int minOfWeek = (weekDay - 1) * 24 * 60 + currHour * 60 + currMin;
  int slotByMin = minOfWeek / splitInterval % shards.size();

  String shard = shards.get(slotByMin);

  if (lastSlotByMin != slotByMin) {
    logger.info("Switching to shard " + shard + ", output=" + getShortDescription());
    lastSlotByMin = slotByMin;
  }

  for (SolrInputDocument solrInputDocument : localBuffer) {
    solrInputDocument.setField(ROUTER_FIELD, shard);
  }
}
 
源代码4 项目: lucene-solr   文件: CreateAliasCmd.java
private void validateAllCollectionsExistAndNoDuplicates(List<String> collectionList, ZkStateReader zkStateReader) {
  final String collectionStr = StrUtils.join(collectionList, ',');

  if (new HashSet<>(collectionList).size() != collectionList.size()) {
    throw new SolrException(BAD_REQUEST,
        String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', since it contains duplicates", collectionStr));
  }
  ClusterState clusterState = zkStateReader.getClusterState();
  Set<String> aliasNames = zkStateReader.getAliases().getCollectionAliasListMap().keySet();
  for (String collection : collectionList) {
    if (clusterState.getCollectionOrNull(collection) == null && !aliasNames.contains(collection)) {
      throw new SolrException(BAD_REQUEST,
          String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', '%s' is not an existing collection or alias", collectionStr, collection));
    }
  }
}
 
源代码5 项目: lucene-solr   文件: DistribPackageStore.java
boolean fetchFromAnyNode() {
  ArrayList<String> l = coreContainer.getPackageStoreAPI().shuffledNodes();
  ZkStateReader stateReader = coreContainer.getZkController().getZkStateReader();
  for (String liveNode : l) {
    try {
      String baseurl = stateReader.getBaseUrlForNodeName(liveNode);
      String url = baseurl.replace("/solr", "/api");
      String reqUrl = url + "/node/files" + path +
          "?meta=true&wt=javabin&omitHeader=true";
      boolean nodeHasBlob = false;
      Object nl = Utils.executeGET(coreContainer.getUpdateShardHandler().getDefaultHttpClient(), reqUrl, Utils.JAVABINCONSUMER);
      if (Utils.getObjectByPath(nl, false, Arrays.asList("files", path)) != null) {
        nodeHasBlob = true;
      }

      if (nodeHasBlob) {
        boolean success = fetchFileFromNodeAndPersist(liveNode);
        if (success) return true;
      }
    } catch (Exception e) {
      //it's OK for some nodes to fail
    }
  }

  return false;
}
 
源代码6 项目: lucene-solr   文件: ZkController.java
/**
 * Create the zknodes necessary for a cluster to operate
 *
 * @param zkClient a SolrZkClient
 * @throws KeeperException      if there is a Zookeeper error
 * @throws InterruptedException on interrupt
 */
public static void createClusterZkNodes(SolrZkClient zkClient)
    throws KeeperException, InterruptedException, IOException {
  ZkCmdExecutor cmdExecutor = new ZkCmdExecutor(zkClient.getZkClientTimeout());
  cmdExecutor.ensureExists(ZkStateReader.LIVE_NODES_ZKNODE, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.COLLECTIONS_ZKNODE, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.ALIASES, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_AUTOSCALING_EVENTS_PATH, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_AUTOSCALING_TRIGGER_STATE_PATH, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_AUTOSCALING_NODE_ADDED_PATH, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_AUTOSCALING_NODE_LOST_PATH, zkClient);
  byte[] emptyJson = "{}".getBytes(StandardCharsets.UTF_8);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_SECURITY_CONF_PATH, emptyJson, CreateMode.PERSISTENT, zkClient);
  cmdExecutor.ensureExists(ZkStateReader.SOLR_AUTOSCALING_CONF_PATH, emptyJson, CreateMode.PERSISTENT, zkClient);
  bootstrapDefaultConfigSet(zkClient);
}
 
源代码7 项目: lucene-solr   文件: ClusterStateMockUtilTest.java
@Test
public void testBuildClusterState_Simple() {
  try (ZkStateReader zkStateReader = ClusterStateMockUtil.buildClusterState("csr", "baseUrl1_")) {
    ClusterState clusterState = zkStateReader.getClusterState();
    assertNotNull(clusterState);
    assertEquals(1, clusterState.getCollectionStates().size());
    DocCollection collection1 = clusterState.getCollectionOrNull("collection1");
    assertNotNull(collection1);
    assertEquals(DocRouter.DEFAULT, collection1.getRouter());
    assertEquals(1, collection1.getActiveSlices().size());
    assertEquals(1, collection1.getSlices().size());
    Slice slice1 = collection1.getSlice("slice1");
    assertNotNull(slice1);
    assertEquals(1, slice1.getReplicas().size());
    Replica replica1 = slice1.getReplica("replica1");
    assertNotNull(replica1);
    assertEquals("baseUrl1_", replica1.getNodeName());
    assertEquals("slice1_replica1", replica1.getCoreName());
    assertEquals("http://baseUrl1", replica1.getBaseUrl());
    assertEquals("http://baseUrl1/slice1_replica1/", replica1.getCoreUrl());
    assertEquals(Replica.State.ACTIVE, replica1.getState());
    assertEquals(Replica.Type.NRT, replica1.getType());
  }
}
 
public OverseerCollectionConfigSetProcessor(ZkStateReader zkStateReader, String myId,
                                             final HttpShardHandler shardHandler,
                                             String adminPath, Stats stats, Overseer overseer,
                                             OverseerNodePrioritizer overseerNodePrioritizer) {
  this(
      zkStateReader,
      myId,
      (HttpShardHandlerFactory) shardHandler.getShardHandlerFactory(),
      adminPath,
      stats,
      overseer,
      overseerNodePrioritizer,
      overseer.getCollectionQueue(zkStateReader.getZkClient(), stats),
      Overseer.getRunningMap(zkStateReader.getZkClient()),
      Overseer.getCompletedMap(zkStateReader.getZkClient()),
      Overseer.getFailureMap(zkStateReader.getZkClient())
  );
}
 
源代码9 项目: lucene-solr   文件: ImplicitSnitch.java
private void cacheRoles(String solrNode, SnitchContext ctx, String key,
                        @SuppressWarnings({"rawtypes"})Map roles) {
  ctx.store(ZkStateReader.ROLES, roles);
  if (roles != null) {
    for (Object o : roles.entrySet()) {
      @SuppressWarnings({"rawtypes"})
      Map.Entry e = (Map.Entry) o;
      if (e.getValue() instanceof List) {
        if (((List) e.getValue()).contains(solrNode)) {
          ctx.getTags().put(key, e.getKey());
          break;
        }
      }
    }
  }
}
 
源代码10 项目: lucene-solr   文件: AliasIntegrationTest.java
private ZkStateReader createColectionsAndAlias(String aliasName) throws SolrServerException, IOException, KeeperException, InterruptedException {
  CollectionAdminRequest.createCollection("collection1meta", "conf", 2, 1).process(cluster.getSolrClient());
  CollectionAdminRequest.createCollection("collection2meta", "conf", 1, 1).process(cluster.getSolrClient());

  cluster.waitForActiveCollection("collection1meta", 2, 2);
  cluster.waitForActiveCollection("collection2meta", 1, 1);

  waitForState("Expected collection1 to be created with 2 shards and 1 replica", "collection1meta", clusterShape(2, 2));
  waitForState("Expected collection2 to be created with 1 shard and 1 replica", "collection2meta", clusterShape(1, 1));
  ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  zkStateReader.createClusterStateWatchersAndUpdate();
  List<String> aliases = zkStateReader.getAliases().resolveAliases(aliasName);
  assertEquals(1, aliases.size());
  assertEquals(aliasName, aliases.get(0));
  UnaryOperator<Aliases> op6 = a -> a.cloneWithCollectionAlias(aliasName, "collection1meta,collection2meta");
  final ZkStateReader.AliasesManager aliasesManager = zkStateReader.aliasesManager;

  aliasesManager.applyModificationAndExportToZk(op6);
  aliases = zkStateReader.getAliases().resolveAliases(aliasName);
  assertEquals(2, aliases.size());
  assertEquals("collection1meta", aliases.get(0));
  assertEquals("collection2meta", aliases.get(1));
  return zkStateReader;
}
 
public void testAddReplica() {
  // with shard parameter and "client side" implicit type param
  CollectionAdminRequest.AddReplica request = CollectionAdminRequest.addReplicaToShard("collection", "shard");
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD, ZkStateReader.REPLICA_TYPE);
  
  // with only shard parameter and "server side" implicit type, so no param
  request = CollectionAdminRequest.addReplicaToShard("collection", "shard", null);
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD);
  
  // with route parameter
  request = CollectionAdminRequest.addReplicaByRouteKey("collection","route");
  assertContainsParams(request.getParams(), ACTION, COLLECTION, ShardParams._ROUTE_);
  
  // with explicit type parameter
  request = CollectionAdminRequest.addReplicaToShard("collection", "shard", Replica.Type.NRT);
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD, ZkStateReader.REPLICA_TYPE);
}
 
源代码12 项目: lucene-solr   文件: TrackingShardHandlerFactory.java
/**
 * Retrieve all requests recorded by this queue which were sent to given collection and shard
 *
 * @param zkStateReader  the {@link org.apache.solr.common.cloud.ZkStateReader} from which cluster state is read
 * @param collectionName the given collection name for which requests have to be extracted
 * @param shardId        the given shard name for which requests have to be extracted
 * @return a list of {@link org.apache.solr.handler.component.TrackingShardHandlerFactory.ShardRequestAndParams}
 * or empty list if none are found
 */
public List<ShardRequestAndParams> getShardRequests(ZkStateReader zkStateReader, String collectionName, String shardId) {
  DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
  assert collection != null;
  Slice slice = collection.getSlice(shardId);
  assert slice != null;

  for (Map.Entry<String, List<ShardRequestAndParams>> entry : requests.entrySet()) {
    // multiple shard addresses may be present separated by '|'
    List<String> list = StrUtils.splitSmart(entry.getKey(), '|');
    for (Map.Entry<String, Replica> replica : slice.getReplicasMap().entrySet()) {
      String coreUrl = new ZkCoreNodeProps(replica.getValue()).getCoreUrl();
      if (list.contains(coreUrl)) {
        return new ArrayList<>(entry.getValue());
      }
    }
  }
  return Collections.emptyList();
}
 
源代码13 项目: lucene-solr   文件: AliasIntegrationTest.java
@Test
public void testModifyPropertiesCAR() throws Exception {
  // note we don't use TZ in this test, thus it's UTC
  final String aliasName = getSaferTestName();
  ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
  CollectionAdminRequest.SetAliasProperty setAliasProperty = CollectionAdminRequest.setAliasProperty(aliasName);
  setAliasProperty.addProperty("foo","baz");
  setAliasProperty.addProperty("bar","bam");
  setAliasProperty.process(cluster.getSolrClient());
  checkFooAndBarMeta(aliasName, zkStateReader);

  // now verify we can delete
  setAliasProperty = CollectionAdminRequest.setAliasProperty(aliasName);
  setAliasProperty.addProperty("foo","");
  setAliasProperty.process(cluster.getSolrClient());
  setAliasProperty = CollectionAdminRequest.setAliasProperty(aliasName);
  setAliasProperty.addProperty("bar",null);
  setAliasProperty.process(cluster.getSolrClient());
  setAliasProperty = CollectionAdminRequest.setAliasProperty(aliasName);

  // whitespace value
  setAliasProperty.addProperty("foo"," ");
  setAliasProperty.process(cluster.getSolrClient());


}
 
protected SocketProxy getProxyForReplica(Replica replica) throws Exception {
  String replicaBaseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
  assertNotNull(replicaBaseUrl);

  List<JettySolrRunner> runners = new ArrayList<>(jettys);
  runners.add(controlJetty);
  
  for (JettySolrRunner j : runners) {
    if (replicaBaseUrl.replaceAll("/$", "").equals(j.getProxyBaseUrl().toExternalForm().replaceAll("/$", ""))) {
      return j.getProxy();
    }
  }
  
  printLayout();

  fail("No proxy found for " + replicaBaseUrl + "!");
  return null;
}
 
源代码15 项目: lucene-solr   文件: OverseerTest.java
private void waitForCollections(ZkStateReader stateReader, String... collections) throws InterruptedException, KeeperException, TimeoutException {
  int maxIterations = 100;
  while (0 < maxIterations--) {

    final ClusterState state = stateReader.getClusterState();
    Set<String> availableCollections = state.getCollectionsMap().keySet();
    int availableCount = 0;
    for(String requiredCollection: collections) {
      stateReader.waitForState(requiredCollection, 30000, TimeUnit.MILLISECONDS, (liveNodes, collectionState) ->  collectionState != null);
      if(availableCollections.contains(requiredCollection)) {
        availableCount++;
      }
      if(availableCount == collections.length) return;

    }
  }
  log.warn("Timeout waiting for collections: {} state: {}"
      , Arrays.asList(collections), stateReader.getClusterState());
}
 
源代码16 项目: lucene-solr   文件: ClusterStateMutator.java
public static String getAssignedCoreNodeName(DocCollection collection, String forNodeName, String forCoreName) {
  Collection<Slice> slices = collection != null ? collection.getSlices() : null;
  if (slices != null) {
    for (Slice slice : slices) {
      for (Replica replica : slice.getReplicas()) {
        String nodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP);
        String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);

        if (nodeName.equals(forNodeName) && core.equals(forCoreName)) {
          return replica.getName();
        }
      }
    }
  }
  return null;
}
 
@Before
public void beforeTest() throws Exception {
  cluster.deleteAllCollections();
  // clear any persisted auto scaling configuration
  Stat stat = zkClient().setData(SOLR_AUTOSCALING_CONF_PATH, Utils.toJSON(new ZkNodeProps()), true);
  if (log.isInfoEnabled()) {
    log.info("{} reset, new znode version {}", SOLR_AUTOSCALING_CONF_PATH, stat.getVersion());
  }
  deleteChildrenRecursively(ZkStateReader.SOLR_AUTOSCALING_EVENTS_PATH);
  deleteChildrenRecursively(ZkStateReader.SOLR_AUTOSCALING_TRIGGER_STATE_PATH);
  deleteChildrenRecursively(ZkStateReader.SOLR_AUTOSCALING_NODE_LOST_PATH);
  deleteChildrenRecursively(ZkStateReader.SOLR_AUTOSCALING_NODE_ADDED_PATH);
  
  finished = new CountDownLatch(1);
  started = new CountDownLatch(1);
  listenerCreated = new CountDownLatch(1);
  listenerEvents = new HashMap<>();
  listenerEventLatch = new CountDownLatch(0);
  
  waitForSeconds = 3 + random().nextInt(5);
}
 
源代码18 项目: lucene-solr   文件: TestStressInPlaceUpdates.java
/**
 * Method gets the SolrClient for the leader replica. This is needed for a workaround for SOLR-8733.
 */
public SolrClient getClientForLeader() throws KeeperException, InterruptedException {
  ZkStateReader zkStateReader = cloudClient.getZkStateReader();
  cloudClient.getZkStateReader().forceUpdateCollection(DEFAULT_COLLECTION);
  ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
  Replica leader = null;
  Slice shard1 = clusterState.getCollection(DEFAULT_COLLECTION).getSlice(SHARD1);
  leader = shard1.getLeader();

  for (int i = 0; i < clients.size(); i++) {
    String leaderBaseUrl = zkStateReader.getBaseUrlForNodeName(leader.getNodeName());
    if (((HttpSolrClient) clients.get(i)).getBaseURL().startsWith(leaderBaseUrl))
      return clients.get(i);
  }

  return null;
}
 
/**
 * Send request to all replicas of a slice
 * @return List of replicas which is not live for receiving the request
 */
public List<Replica> sliceCmd(ClusterState clusterState, ModifiableSolrParams params, Replica.State stateMatcher,
              Slice slice, ShardHandler shardHandler) {
  List<Replica> notLiveReplicas = new ArrayList<>();
  for (Replica replica : slice.getReplicas()) {
    if ((stateMatcher == null || Replica.State.getState(replica.getStr(ZkStateReader.STATE_PROP)) == stateMatcher)) {
      if (clusterState.liveNodesContain(replica.getStr(ZkStateReader.NODE_NAME_PROP))) {
        // For thread safety, only simple clone the ModifiableSolrParams
        ModifiableSolrParams cloneParams = new ModifiableSolrParams();
        cloneParams.add(params);
        cloneParams.set(CoreAdminParams.CORE, replica.getStr(ZkStateReader.CORE_NAME_PROP));

        sendShardRequest(replica.getStr(ZkStateReader.NODE_NAME_PROP), cloneParams, shardHandler);
      } else {
        notLiveReplicas.add(replica);
      }
    }
  }
  return notLiveReplicas;
}
 
@Test
public void deleteCollectionOnlyInZk() throws Exception {
  final String collectionName = "onlyinzk";

  // create the collections node, but nothing else
  cluster.getZkClient().makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collectionName, true);

  // delete via API - should remove collections node
  CollectionAdminRequest.deleteCollection(collectionName).process(cluster.getSolrClient());
  assertFalse(CollectionAdminRequest.listCollections(cluster.getSolrClient()).contains(collectionName));
  
  // now creating that collection should work
  CollectionAdminRequest.createCollection(collectionName, "conf", 2, 1)
      .process(cluster.getSolrClient());
  assertTrue(CollectionAdminRequest.listCollections(cluster.getSolrClient()).contains(collectionName));
}
 
源代码21 项目: lucene-solr   文件: SliceMutator.java
public ZkWriteCommand addReplica(ClusterState clusterState, ZkNodeProps message) {
  log.info("createReplica() {} ", message);
  String coll = message.getStr(ZkStateReader.COLLECTION_PROP);
  if (!checkCollectionKeyExistence(message)) return ZkStateWriter.NO_OP;
  String slice = message.getStr(ZkStateReader.SHARD_ID_PROP);
  DocCollection collection = clusterState.getCollection(coll);
  Slice sl = collection.getSlice(slice);
  if (sl == null) {
    log.error("Invalid Collection/Slice {}/{} ", coll, slice);
    return ZkStateWriter.NO_OP;
  }
  String coreNodeName;
  if (message.getStr(ZkStateReader.CORE_NODE_NAME_PROP) != null) {
    coreNodeName = message.getStr(ZkStateReader.CORE_NODE_NAME_PROP);
  } else {
    coreNodeName = Assign.assignCoreNodeName(stateManager, collection);
  }
  Replica replica = new Replica(coreNodeName,
      makeMap(
          ZkStateReader.CORE_NAME_PROP, message.getStr(ZkStateReader.CORE_NAME_PROP),
          ZkStateReader.BASE_URL_PROP, message.getStr(ZkStateReader.BASE_URL_PROP),
          ZkStateReader.STATE_PROP, message.getStr(ZkStateReader.STATE_PROP),
          ZkStateReader.NODE_NAME_PROP, message.getStr(ZkStateReader.NODE_NAME_PROP), 
          ZkStateReader.REPLICA_TYPE, message.get(ZkStateReader.REPLICA_TYPE)), coll, slice);
  return new ZkWriteCommand(coll, updateReplica(collection, sl, replica.getName(), replica));
}
 
源代码22 项目: lucene-solr   文件: CollectionPropsTest.java
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public boolean onStateChanged(Map<String, String> collectionProperties) {
  log.info("{}: state changed...", name);
  if (forceReadPropsFromZk) {
    final ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
    props = Collections.unmodifiableMap(new HashMap(zkStateReader.getCollectionProperties(collectionName)));
    log.info("{}: Setting props from zk={}", name, props);
  } else {
    props = Collections.unmodifiableMap(new HashMap(collectionProperties));
    log.info("{}: Setting props from caller={}", name, props);
  }
  
  synchronized (this) {
    triggered.incrementAndGet();
    log.info("{}: notifying", name);
    notifyAll();
  }

  log.info("{}: done", name);
  return false;
}
 
源代码23 项目: lucene-solr   文件: AbstractDistribZkTestBase.java
protected static void assertAllActive(String collection, ZkStateReader zkStateReader)
    throws KeeperException, InterruptedException {

    zkStateReader.forceUpdateCollection(collection);
    ClusterState clusterState = zkStateReader.getClusterState();
    final DocCollection docCollection = clusterState.getCollectionOrNull(collection);
    if (docCollection == null || docCollection.getSlices() == null) {
      throw new IllegalArgumentException("Cannot find collection:" + collection);
    }

    Map<String,Slice> slices = docCollection.getSlicesMap();
    for (Map.Entry<String,Slice> entry : slices.entrySet()) {
      Slice slice = entry.getValue();
      if (slice.getState() != Slice.State.ACTIVE) {
        fail("Not all shards are ACTIVE - found a shard " + slice.getName() + " that is: " + slice.getState());
      }
      Map<String,Replica> shards = slice.getReplicasMap();
      for (Map.Entry<String,Replica> shard : shards.entrySet()) {
        Replica replica = shard.getValue();
        if (replica.getState() != Replica.State.ACTIVE) {
          fail("Not all replicas are ACTIVE - found a replica " + replica.getName() + " that is: " + replica.getState());
        }
      }
    }
}
 
源代码24 项目: incubator-atlas   文件: Solr5Index.java
/**
 * Wait for all the collection shards to be ready.
 */
private static void waitForRecoveriesToFinish(CloudSolrClient server, String collection) throws KeeperException, InterruptedException {
    ZkStateReader zkStateReader = server.getZkStateReader();
    try {
        boolean cont = true;

        while (cont) {
            boolean sawLiveRecovering = false;
            zkStateReader.updateClusterState();
            ClusterState clusterState = zkStateReader.getClusterState();
            Map<String, Slice> slices = clusterState.getSlicesMap(collection);
            Preconditions.checkNotNull("Could not find collection:" + collection, slices);

            for (Map.Entry<String, Slice> entry : slices.entrySet()) {
                Map<String, Replica> shards = entry.getValue().getReplicasMap();
                for (Map.Entry<String, Replica> shard : shards.entrySet()) {
                    String state = shard.getValue().getStr(ZkStateReader.STATE_PROP);
                    if ((state.equals(Replica.State.RECOVERING.toString())
                            || state.equals(Replica.State.DOWN.toString()))
                            && clusterState.liveNodesContain(shard.getValue().getStr(
                            ZkStateReader.NODE_NAME_PROP))) {
                        sawLiveRecovering = true;
                    }
                }
            }
            if (!sawLiveRecovering) {
                cont = false;
            } else {
                Thread.sleep(1000);
            }
        }
    } finally {
        logger.info("Exiting solr wait");
    }
}
 
private void disableAutoAddReplicasInCluster() throws SolrServerException, IOException {
  @SuppressWarnings({"rawtypes"})
  Map m = makeMap(
      "action", CollectionParams.CollectionAction.CLUSTERPROP.toLower(),
      "name", ZkStateReader.AUTO_ADD_REPLICAS,
      "val", "false");
  @SuppressWarnings({"unchecked"})
  QueryRequest request = new QueryRequest(new MapSolrParams(m));
  request.setPath("/admin/collections");
  cluster.getSolrClient().request(request);
}
 
源代码26 项目: lucene-solr   文件: BaseCdcrDistributedZkTest.java
@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();
    }
  }
}
 
源代码27 项目: lucene-solr   文件: ReplicaInfo.java
public Replica.State getState() {
  if (variables.get(ZkStateReader.STATE_PROP) != null) {
    return Replica.State.getState((String) variables.get(ZkStateReader.STATE_PROP));
  } else {
    // default to ACTIVE
    variables.put(ZkStateReader.STATE_PROP, Replica.State.ACTIVE.toString());
    return Replica.State.ACTIVE;
  }
}
 
源代码28 项目: lucene-solr   文件: AliasIntegrationTest.java
private void checkFooAndBarMeta(String aliasName, ZkStateReader zkStateReader) throws Exception {
  zkStateReader.aliasesManager.update(); // ensure our view is up to date
  Map<String, String> meta = zkStateReader.getAliases().getCollectionAliasProperties(aliasName);
  assertNotNull(meta);
  assertTrue(meta.containsKey("foo"));
  assertEquals("baz", meta.get("foo"));
  assertTrue(meta.containsKey("bar"));
  assertEquals("bam", meta.get("bar"));
}
 
源代码29 项目: lucene-solr   文件: AnalyticsShardRequestManager.java
/**
 * Pick one replica from each shard to send the shard requests to.
 *
 * @param collection that is being queried
 * @throws IOException if an exception occurs while finding replicas
 */
protected void pickShards(String collection) throws IOException {
  try {

    ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();

    Slice[] slices = clusterState.getCollection(collection).getActiveSlicesArr();

    for(Slice slice : slices) {
      Collection<Replica> replicas = slice.getReplicas();
      List<Replica> shuffler = new ArrayList<>();
      for(Replica replica : replicas) {
        if(replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName()))
        shuffler.add(replica);
      }

      Collections.shuffle(shuffler, new Random());
      Replica rep = shuffler.get(0);
      ZkCoreNodeProps zkProps = new ZkCoreNodeProps(rep);
      String url = zkProps.getCoreUrl();
      replicaUrls.add(url);
    }
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
源代码30 项目: lucene-solr   文件: TextLogitStream.java
protected List<String> getShardUrls() throws IOException {
  try {
    ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();

    Slice[] slices = CloudSolrStream.getSlices(this.collection, zkStateReader, false);

    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();

    List<String> baseUrls = new ArrayList<>();
    for(Slice slice : slices) {
      Collection<Replica> replicas = slice.getReplicas();
      List<Replica> shuffler = new ArrayList<>();
      for(Replica replica : replicas) {
        if(replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName())) {
          shuffler.add(replica);
        }
      }

      Collections.shuffle(shuffler, new Random());
      Replica rep = shuffler.get(0);
      ZkCoreNodeProps zkProps = new ZkCoreNodeProps(rep);
      String url = zkProps.getCoreUrl();
      baseUrls.add(url);
    }

    return baseUrls;
  } catch (Exception e) {
    throw new IOException(e);
  }
}