org.apache.solr.common.cloud.ZkStateReader Java Examples

The following examples show how to use org.apache.solr.common.cloud.ZkStateReader. 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: ClusterStateMockUtilTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@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());
  }
}
 
Example #2
Source File: AbstractDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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());
        }
      }
    }
}
 
Example #3
Source File: DistribPackageStore.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: CollectionPropsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: CreateAliasCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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));
    }
  }
}
 
Example #6
Source File: ZkController.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: OutputSolr.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #8
Source File: SliceMutator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #9
Source File: CollectionsAPIDistributedZkTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #10
Source File: LeaderElectionTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #11
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #12
Source File: TestStressInPlaceUpdates.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: SearchRateTriggerIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #14
Source File: ClusterStateMutator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #15
Source File: OverseerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #16
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #17
Source File: OverseerCollectionConfigSetProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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())
  );
}
 
Example #18
Source File: AliasIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@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());


}
 
Example #19
Source File: ImplicitSnitch.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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;
        }
      }
    }
  }
}
 
Example #20
Source File: TrackingShardHandlerFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #21
Source File: AliasIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #22
Source File: CollectionAdminRequestRequiredParamsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #23
Source File: SolrSchemaFieldDao.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #24
Source File: CreateAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void call(ClusterState state, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results)
    throws Exception {
  final String aliasName = message.getStr(CommonParams.NAME);
  ZkStateReader zkStateReader = ocmh.zkStateReader;
  // make sure we have the latest version of existing aliases
  if (zkStateReader.aliasesManager != null) { // not a mock ZkStateReader
    zkStateReader.aliasesManager.update();
  }

  if (!anyRoutingParams(message)) {
    callCreatePlainAlias(message, aliasName, zkStateReader);
  } else {
    callCreateRoutedAlias(message, aliasName, zkStateReader, state);
  }

  // Sleep a bit to allow ZooKeeper state propagation.
  //
  // THIS IS A KLUDGE.
  //
  // Solr's view of the cluster is eventually consistent. *Eventually* all nodes and CloudSolrClients will be aware of
  // alias changes, but not immediately. If a newly created alias is queried, things should work right away since Solr
  // will attempt to see if it needs to get the latest aliases when it can't otherwise resolve the name.  However
  // modifications to an alias will take some time.
  //
  // We could levy this requirement on the client but they would probably always add an obligatory sleep, which is
  // just kicking the can down the road.  Perhaps ideally at this juncture here we could somehow wait until all
  // Solr nodes in the cluster have the latest aliases?
  Thread.sleep(100);
}
 
Example #25
Source File: RoutedAliasUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean haveCollection(String alias, String collection) {
  // separated into separate lines to make it easier to track down an NPE that occurred once
  // 3000 runs if it shows up again...
  CloudSolrClient solrClient = cluster.getSolrClient();
  ZkStateReader zkStateReader = solrClient.getZkStateReader();
  Aliases aliases = zkStateReader.getAliases();
  Map<String, List<String>> collectionAliasListMap = aliases.getCollectionAliasListMap();
  List<String> strings = collectionAliasListMap.get(alias);
  return strings.contains(collection);
}
 
Example #26
Source File: TestStressLiveNodes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** returns the number of nodes actually added w/o error */
public Integer call() {
  running = true;
  // NOTE: test includes 'running'
  for (int i = 0; running && i < numNodesToAdd; i++) {
    final String nodePath = ZkStateReader.LIVE_NODES_ZKNODE + "/thrasher-" + id + "-" + i;
    try {
      client.makePath(nodePath, CreateMode.EPHEMERAL, true);
      numAdded++;
    } catch (Exception e) {
      log.error("failed to create: {}", nodePath, e);
    }
  }
  return numAdded;
}
 
Example #27
Source File: SimClusterStateProvider.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void createEphemeralLiveNode(String nodeId) throws Exception {
  DistribStateManager mgr = stateManager.withEphemeralId(nodeId);
  mgr.makePath(ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeId, null, CreateMode.EPHEMERAL, true);
  AutoScalingConfig cfg = stateManager.getAutoScalingConfig(null);
  if (cfg.hasTriggerForEvents(TriggerEventType.NODEADDED)) {
    byte[] json = Utils.toJSON(Collections.singletonMap("timestamp", cloudManager.getTimeSource().getEpochTimeNs()));
    String path = ZkStateReader.SOLR_AUTOSCALING_NODE_ADDED_PATH + "/" + nodeId;
    log.debug("-- creating marker: {}", path);
    mgr.makePath(path, json, CreateMode.EPHEMERAL, true);
  }
}
 
Example #28
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void distribSetUp() throws Exception {
  super.distribSetUp();
  // ignoreException(".*");
  
  cloudInit = false;
  
  if (sliceCount > 0) {
    System.setProperty("numShards", Integer.toString(sliceCount));
  } else {
    System.clearProperty("numShards");
  }

  if (isSSLMode()) {
    System.clearProperty("urlScheme");
    try (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));
        zkStateReader.getZkClient().setData(ZkStateReader.CLUSTER_PROPS, Utils.toJSON(props.plus("urlScheme", "https")), true);
      }
    }
  }
  if (useTlogReplicas()) {
    log.info("Will use {} replicas unless explicitly asked otherwise", Replica.Type.TLOG);
  } else {
    log.info("Will use {} replicas unless explicitly asked otherwise", Replica.Type.NRT);
  }
}
 
Example #29
Source File: TracerConfigurator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void registerListener(ZkStateReader stateReader) {
  stateReader.registerClusterPropertiesListener(properties -> {
    if (properties.containsKey(ZkStateReader.SAMPLE_PERCENTAGE)) {
      try {
        double sampleRate = Double.parseDouble(properties.get(ZkStateReader.SAMPLE_PERCENTAGE).toString());
        GlobalTracer.get().setSamplePercentage(sampleRate);
      } catch (NumberFormatException e) {
        log.error("Unable to set sample rate", e);
      }
    } else {
      GlobalTracer.get().setSamplePercentage(0.1);
    }
    return false;
  });
}
 
Example #30
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void initCloud() throws Exception {
  assert(cloudInit == false);
  cloudInit = true;
  cloudClient = createCloudClient(DEFAULT_COLLECTION);
  cloudClient.connect();

  ZkStateReader zkStateReader = cloudClient.getZkStateReader();

  chaosMonkey = new ChaosMonkey(zkServer, zkStateReader, DEFAULT_COLLECTION,
      shardToJetty, shardToLeaderJetty);
}