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

The following examples show how to use org.apache.solr.common.cloud.Slice. 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: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
int waitForNodeChange(Slice slice, String electionNode) throws InterruptedException, KeeperException {
  String nodeName = LeaderElector.getNodeName(electionNode);
  int oldSeq = LeaderElector.getSeq(electionNode);
  for (int idx = 0; idx < 600; ++idx) {

    ZkStateReader zkStateReader = coreContainer.getZkController().getZkStateReader();
    List<String> electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zkStateReader.getZkClient(),
        ZkStateReader.getShardLeadersElectPath(collectionName, slice.getName()));
    for (String testNode : electionNodes) {
      if (LeaderElector.getNodeName(testNode).equals(nodeName) && oldSeq != LeaderElector.getSeq(testNode)) {
        return LeaderElector.getSeq(testNode);
      }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    zkStateReader.forciblyRefreshAllClusterStateSlow();
  }
  return -1;
}
 
Example #2
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 #3
Source File: ManagedIndexSchema.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static List<String> getActiveReplicaCoreUrls(ZkController zkController, String collection, String localCoreNodeName) {
  List<String> activeReplicaCoreUrls = new ArrayList<>();
  ZkStateReader zkStateReader = zkController.getZkStateReader();
  ClusterState clusterState = zkStateReader.getClusterState();
  Set<String> liveNodes = clusterState.getLiveNodes();
  final DocCollection docCollection = clusterState.getCollectionOrNull(collection);
  if (docCollection != null && docCollection.getActiveSlicesArr().length > 0) {
    final Slice[] activeSlices = docCollection.getActiveSlicesArr();
    for (Slice next : activeSlices) {
      Map<String, Replica> replicasMap = next.getReplicasMap();
      if (replicasMap != null) {
        for (Map.Entry<String, Replica> entry : replicasMap.entrySet()) {
          Replica replica = entry.getValue();
          if (!localCoreNodeName.equals(replica.getName()) &&
              replica.getState() == Replica.State.ACTIVE &&
              liveNodes.contains(replica.getNodeName())) {
            ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(replica);
            activeReplicaCoreUrls.add(replicaCoreProps.getCoreUrl());
          }
        }
      }
    }
  }
  return activeReplicaCoreUrls;
}
 
Example #4
Source File: SyncSliceTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void waitTillAllNodesActive() throws Exception {
  for (int i = 0; i < 60; i++) { 
    Thread.sleep(3000);
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    ClusterState clusterState = zkStateReader.getClusterState();
    DocCollection collection1 = clusterState.getCollection("collection1");
    Slice slice = collection1.getSlice("shard1");
    Collection<Replica> replicas = slice.getReplicas();
    boolean allActive = true;
    for (Replica replica : replicas) {
      if (!clusterState.liveNodesContain(replica.getNodeName()) || replica.getState() != Replica.State.ACTIVE) {
        allActive = false;
        break;
      }
    }
    if (allActive) {
      return;
    }
  }
  printLayout();
  fail("timeout waiting to see all nodes active");
}
 
Example #5
Source File: DeleteReplicaCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Validate if there is less replicas than requested to remove. Also error out if there is
 * only one replica available
 */
private void validateReplicaAvailability(Slice slice, String shard, String collectionName, int count) {
  //If there is a specific shard passed, validate if there any or just 1 replica left
  if (slice != null) {
    Collection<Replica> allReplicasForShard = slice.getReplicas();
    if (allReplicasForShard == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No replicas found  in shard/collection: " +
              shard + "/"  + collectionName);
    }


    if (allReplicasForShard.size() == 1) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "There is only one replica available in shard/collection: " +
              shard + "/" + collectionName + ". Cannot delete that.");
    }

    if (allReplicasForShard.size() <= count) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "There are lesser num replicas requested to be deleted than are available in shard/collection : " +
              shard + "/"  + collectionName  + " Requested: "  + count + " Available: " + allReplicasForShard.size() + ".");
    }
  }
}
 
Example #6
Source File: ClusterStateMockUtilTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildClusterState_ReplicaStateAndType() {
  try (ZkStateReader zkStateReader = ClusterStateMockUtil.buildClusterState("csrStRpDnF", "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(4, slice1.getReplicas().size());
    assertEquals(1, slice1.getReplicas(replica -> replica.getType() == Replica.Type.NRT && replica.getState() == Replica.State.ACTIVE).size());
    assertEquals(1, slice1.getReplicas(replica -> replica.getType() == Replica.Type.NRT && replica.getState() == Replica.State.RECOVERY_FAILED).size());
    assertEquals(1, slice1.getReplicas(replica -> replica.getType() == Replica.Type.TLOG && replica.getState() == Replica.State.RECOVERING).size());
    assertEquals(1, slice1.getReplicas(replica -> replica.getType() == Replica.Type.PULL && replica.getState() == Replica.State.DOWN).size());
  }
}
 
Example #7
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
boolean waitForCoreNodeGone(String collectionName, String shard, String replicaName, int timeoutms) throws InterruptedException {
  try {
    zkStateReader.waitForState(collectionName, timeoutms, TimeUnit.MILLISECONDS, (c) -> {
        if (c == null)
          return true;
        Slice slice = c.getSlice(shard);
        if(slice == null || slice.getReplica(replicaName) == null) {
          return true;
        }
        return false;
      });
  } catch (TimeoutException e) {
    return false;
  }

  return true;
}
 
Example #8
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 collection
 * @return List of replicas which is not live for receiving the request
 */
List<Replica> collectionCmd(ZkNodeProps message, ModifiableSolrParams params,
                   NamedList<Object> results, Replica.State stateMatcher, String asyncId, Set<String> okayExceptions) {
  log.info("Executing Collection Cmd={}, asyncId={}", params, asyncId);
  String collectionName = message.getStr(NAME);
  @SuppressWarnings("deprecation")
  ShardHandler shardHandler = shardHandlerFactory.getShardHandler(overseer.getCoreContainer().getUpdateShardHandler().getDefaultHttpClient());

  ClusterState clusterState = zkStateReader.getClusterState();
  DocCollection coll = clusterState.getCollection(collectionName);
  List<Replica> notLivesReplicas = new ArrayList<>();
  final ShardRequestTracker shardRequestTracker = new ShardRequestTracker(asyncId);
  for (Slice slice : coll.getSlices()) {
    notLivesReplicas.addAll(shardRequestTracker.sliceCmd(clusterState, params, stateMatcher, slice, shardHandler));
  }

  shardRequestTracker.processResponses(results, shardHandler, false, null, okayExceptions);
  return notLivesReplicas;
}
 
Example #9
Source File: ShardSplitTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void checkSubShardConsistency(String shard) throws SolrServerException, IOException {
  SolrQuery query = new SolrQuery("*:*").setRows(1000).setFields("id", "_version_");
  query.set("distrib", false);

  ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
  Slice slice = clusterState.getCollection(AbstractDistribZkTestBase.DEFAULT_COLLECTION).getSlice(shard);
  long[] numFound = new long[slice.getReplicasMap().size()];
  int c = 0;
  for (Replica replica : slice.getReplicas()) {
    String coreUrl = new ZkCoreNodeProps(replica).getCoreUrl();
    QueryResponse response;
    try (HttpSolrClient client = getHttpSolrClient(coreUrl)) {
      response = client.query(query);
    }
    numFound[c++] = response.getResults().getNumFound();
    if (log.isInfoEnabled()) {
      log.info("Shard: {} Replica: {} has {} docs", shard, coreUrl, String.valueOf(response.getResults().getNumFound()));
    }
    assertTrue("Shard: " + shard + " Replica: " + coreUrl + " has 0 docs", response.getResults().getNumFound() > 0);
  }
  for (int i = 0; i < slice.getReplicasMap().size(); i++) {
    assertEquals(shard + " is not consistent", numFound[0], numFound[i]);
  }
}
 
Example #10
Source File: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void checkLeaderStatus() throws InterruptedException, KeeperException {
  for (int idx = 0; pendingOps.size() > 0 && idx < 600; ++idx) {
    ClusterState clusterState = coreContainer.getZkController().getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();
    DocCollection dc = clusterState.getCollection(collectionName);
    for (Slice slice : dc.getSlices()) {
      for (Replica replica : slice.getReplicas()) {
        if (replica.isActive(liveNodes) && replica.getBool(SliceMutator.PREFERRED_LEADER_PROP, false)) {
          if (replica.getBool(LEADER_PROP, false)) {
            if (pendingOps.containsKey(slice.getName())) {
              // Record for return that the leader changed successfully
              pendingOps.remove(slice.getName());
              addToSuccesses(slice, replica);
              break;
            }
          }
        }
      }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    coreContainer.getZkController().getZkStateReader().forciblyRefreshAllClusterStateSlow();
  }
  addAnyFailures();
}
 
Example #11
Source File: TestSimClusterStateProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static void assertClusterStateEquals(ClusterState one, ClusterState two) {
  assertEquals(one.getLiveNodes(), two.getLiveNodes());
  assertEquals(one.getCollectionsMap().keySet(), two.getCollectionsMap().keySet());
  one.forEachCollection(oneColl -> {
    DocCollection twoColl = two.getCollection(oneColl.getName());
    Map<String, Slice> oneSlices = oneColl.getSlicesMap();
    Map<String, Slice> twoSlices = twoColl.getSlicesMap();
    assertEquals(oneSlices.keySet(), twoSlices.keySet());
    oneSlices.forEach((s, slice) -> {
      Slice sTwo = twoSlices.get(s);
      for (Replica oneReplica : slice.getReplicas()) {
        Replica twoReplica = sTwo.getReplica(oneReplica.getName());
        assertNotNull(twoReplica);
        SimSolrCloudTestCase.assertReplicaEquals(oneReplica, twoReplica);
      }
    });
  });
}
 
Example #12
Source File: TestPolicyCloud.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static CollectionStatePredicate expectAllReplicasOnSpecificNode
  (final String expectedNodeName,
   final int expectedSliceCount,
   final int expectedReplicaCount) {

  return (liveNodes, collection) -> {
    if (null == collection || expectedSliceCount != collection.getSlices().size()) {
      return false;
    }
    int actualReplicaCount = 0;
    for (Slice slice : collection) {
      for (Replica replica : slice) {
        if ( ! (replica.isActive(liveNodes)
                && expectedNodeName.equals(replica.getNodeName())) ) {
          return false;
        }
        actualReplicaCount++;
      }
    }
    return expectedReplicaCount == actualReplicaCount;
  };
}
 
Example #13
Source File: CrossCollectionJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private String createHashRangeFq() {
  if (routedByJoinKey) {
    ClusterState clusterState = searcher.getCore().getCoreContainer().getZkController().getClusterState();
    CloudDescriptor desc = searcher.getCore().getCoreDescriptor().getCloudDescriptor();
    Slice slice = clusterState.getCollection(desc.getCollectionName()).getSlicesMap().get(desc.getShardId());
    DocRouter.Range range = slice.getRange();

    // In CompositeIdRouter, the routing prefix only affects the top 16 bits
    int min = range.min & 0xffff0000;
    int max = range.max | 0x0000ffff;

    return String.format(Locale.ROOT, "{!hash_range f=%s l=%d u=%d}", fromField, min, max);
  } else {
    return null;
  }
}
 
Example #14
Source File: SolrCloudTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> mapReplicasToReplicaType(DocCollection collection) {
  Map<String, String> replicaTypeMap = new HashMap<>();
  for (Slice slice : collection.getSlices()) {
    for (Replica replica : slice.getReplicas()) {
      String coreUrl = replica.getCoreUrl();
      // It seems replica reports its core URL with a trailing slash while shard
      // info returned from the query doesn't. Oh well. We will include both, just in case
      replicaTypeMap.put(coreUrl, replica.getType().toString());
      if (coreUrl.endsWith("/")) {
        replicaTypeMap.put(coreUrl.substring(0, coreUrl.length() - 1), replica.getType().toString());
      }else {
        replicaTypeMap.put(coreUrl + "/", replica.getType().toString());
      }
    }
  }
  return replicaTypeMap;
}
 
Example #15
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void verifyPropCorrectlyDistributed(String prop) throws KeeperException, InterruptedException {

    TimeOut timeout = new TimeOut(timeoutMs, TimeUnit.MILLISECONDS, TimeSource.NANO_TIME);

    String propLC = prop.toLowerCase(Locale.ROOT);
    DocCollection docCollection = null;
    while (timeout.hasTimedOut() == false) {
      forceUpdateCollectionStatus();
      docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
      int maxPropCount = Integer.MAX_VALUE;
      int minPropCount = Integer.MIN_VALUE;
      for (Slice slice : docCollection.getSlices()) {
        int repCount = 0;
        for (Replica rep : slice.getReplicas()) {
          if (rep.getBool("property." + propLC, false)) {
            repCount++;
          }
        }
        maxPropCount = Math.max(maxPropCount, repCount);
        minPropCount = Math.min(minPropCount, repCount);
      }
      if (Math.abs(maxPropCount - minPropCount) < 2) return;
    }
    log.error("Property {} is not distributed evenly. {}", prop, docCollection);
    fail("Property is not distributed evenly " + prop);
  }
 
Example #16
Source File: DistributedZkUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** For {@link org.apache.solr.common.params.CollectionParams.CollectionAction#SPLITSHARD} */
protected boolean amISubShardLeader(DocCollection coll, Slice parentSlice, String id, SolrInputDocument doc) throws InterruptedException {
  // Am I the leader of a shard in "construction/recovery" state?
  String myShardId = cloudDesc.getShardId();
  Slice mySlice = coll.getSlice(myShardId);
  final Slice.State state = mySlice.getState();
  if (state == Slice.State.CONSTRUCTION || state == Slice.State.RECOVERY) {
    Replica myLeader = zkController.getZkStateReader().getLeaderRetry(collection, myShardId);
    boolean amILeader = myLeader.getName().equals(cloudDesc.getCoreNodeName());
    if (amILeader) {
      // Does the document belong to my hash range as well?
      DocRouter.Range myRange = mySlice.getRange();
      if (myRange == null) myRange = new DocRouter.Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
      if (parentSlice != null)  {
        boolean isSubset = parentSlice.getRange() != null && myRange.isSubsetOf(parentSlice.getRange());
        return isSubset && coll.getRouter().isTargetSlice(id, doc, req.getParams(), myShardId, coll);
      } else  {
        // delete by query case -- as long as I am a sub shard leader we're fine
        return true;
      }
    }
  }
  return false;
}
 
Example #17
Source File: AssignTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildCoreName() throws Exception {
  Path zkDir = createTempDir("zkData");
  ZkTestServer server = new ZkTestServer(zkDir);
  server.run();
  try (SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), 10000)) {
    // TODO: fix this to be independent of ZK
    ZkDistribStateManager stateManager = new ZkDistribStateManager(zkClient);
    Map<String, Slice> slices = new HashMap<>();
    slices.put("shard1", new Slice("shard1", new HashMap<>(), null,"collection1"));
    slices.put("shard2", new Slice("shard2", new HashMap<>(), null,"collection1"));

    DocCollection docCollection = new DocCollection("collection1", slices, null, DocRouter.DEFAULT);
    assertEquals("Core name pattern changed", "collection1_shard1_replica_n1", Assign.buildSolrCoreName(stateManager, docCollection, "shard1", Replica.Type.NRT));
    assertEquals("Core name pattern changed", "collection1_shard2_replica_p2", Assign.buildSolrCoreName(stateManager, docCollection, "shard2", Replica.Type.PULL));
  } finally {
    server.shutdown();
  }
}
 
Example #18
Source File: TestPullReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that Update logs don't exist for replicas of type {@link org.apache.solr.common.cloud.Replica.Type#PULL}
 */
private void assertUlogPresence(DocCollection collection) {
  for (Slice s:collection.getSlices()) {
    for (Replica r:s.getReplicas()) {
      if (r.getType() == Replica.Type.NRT) {
        continue;
      }
      SolrCore core = null;
      try {
        core = cluster.getReplicaJetty(r).getCoreContainer().getCore(r.getCoreName());
        assertNotNull(core);
        assertFalse("Update log should not exist for replicas of type Passive but file is present: " + core.getUlogDir(),
            new java.io.File(core.getUlogDir()).exists());
      } finally {
        core.close();
      }
    }
  }
}
 
Example #19
Source File: MiniSolrCloudCluster.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static CollectionStatePredicate expectedShardsAndActiveReplicas(int expectedShards, int expectedReplicas) {
  return (liveNodes, collectionState) -> {
    if (collectionState == null)
      return false;
    if (collectionState.getSlices().size() != expectedShards) {
      return false;
    }
    
    int activeReplicas = 0;
    for (Slice slice : collectionState) {
      for (Replica replica : slice) {
        if (replica.isActive(liveNodes)) {
          activeReplicas++;
        }
      }
    }
    if (activeReplicas == expectedReplicas) {
      return true;
    }

    return false;
  };
}
 
Example #20
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 #21
Source File: LeaderTragicEventTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOtherReplicasAreNotActive() throws Exception {
  final String collection = "collection2";
  cluster.getSolrClient().setDefaultCollection(collection);
  int numReplicas = random().nextInt(2) + 1;
  // won't do anything if leader is the only one active replica in the shard
  CollectionAdminRequest
      .createCollection(collection, "config", 1, numReplicas)
      .process(cluster.getSolrClient());
  cluster.waitForActiveCollection(collection, 1, numReplicas);

  try {
    JettySolrRunner otherReplicaJetty = null;
    if (numReplicas == 2) {
      Slice shard = getCollectionState(collection).getSlice("shard1");
      otherReplicaJetty = cluster.getReplicaJetty(getNonLeader(shard));
      if (log.isInfoEnabled()) {
        log.info("Stop jetty node : {} state:{}", otherReplicaJetty.getBaseUrl(), getCollectionState(collection));
      }
      otherReplicaJetty.stop();
      cluster.waitForJettyToStop(otherReplicaJetty);
      waitForState("Timeout waiting for replica get down", collection, (liveNodes, collectionState) -> getNonLeader(collectionState.getSlice("shard1")).getState() != Replica.State.ACTIVE);
    }

    Replica oldLeader = corruptLeader(collection, new ArrayList<>());

    if (otherReplicaJetty != null) {
      otherReplicaJetty.start();
      cluster.waitForNode(otherReplicaJetty, 30);
    }

    Replica leader = getCollectionState(collection).getSlice("shard1").getLeader();
    assertEquals(leader.getName(), oldLeader.getName());
  } finally {
    CollectionAdminRequest.deleteCollection(collection).process(cluster.getSolrClient());
  }
}
 
Example #22
Source File: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean electionQueueInBadState(List<String> electionNodes, Slice slice, Replica replica) {
  if (electionNodes.size() < 2) { // if there's only one node in the queue, should already be leader and we shouldn't be here anyway.
    log.warn("Rebalancing leaders and slice {} has less than two elements in the leader election queue, but replica {} doesn't think it's the leader."
        , slice.getName(), replica.getName());
    return true;
  }

  return false;
}
 
Example #23
Source File: ReplicaAssigner.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * @param shardVsReplicaCount shard names vs no:of replicas required for each of those shards
 * @param snitches            snitches details
 * @param shardVsNodes        The current state of the system. can be an empty map if no nodes
 *                            are created in this collection till now
 */
@SuppressWarnings({"unchecked"})
public ReplicaAssigner(List<Rule> rules,
                       Map<String, Integer> shardVsReplicaCount,
                       @SuppressWarnings({"rawtypes"})List snitches,
                       Map<String, Map<String, Integer>> shardVsNodes,
                       List<String> participatingLiveNodes,
                       SolrCloudManager cloudManager, ClusterState clusterState) {
  this.rules = rules;
  for (Rule rule : rules) tagNames.add(rule.tag.name);
  this.shardVsReplicaCount = shardVsReplicaCount;
  this.participatingLiveNodes = new ArrayList<>(participatingLiveNodes);
  this.nodeVsTags = getTagsForNodes(cloudManager, snitches);
  this.shardVsNodes = getDeepCopy(shardVsNodes, 2);

  if (clusterState != null) {
    Map<String, DocCollection> collections = clusterState.getCollectionsMap();
    for (Map.Entry<String, DocCollection> entry : collections.entrySet()) {
      DocCollection coll = entry.getValue();
      for (Slice slice : coll.getSlices()) {
        for (Replica replica : slice.getReplicas()) {
          AtomicInteger count = nodeVsCores.get(replica.getNodeName());
          if (count == null) nodeVsCores.put(replica.getNodeName(), count = new AtomicInteger());
          count.incrementAndGet();
        }
      }
    }
  }
}
 
Example #24
Source File: TestHashPartitioner.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void doQuery(DocCollection coll, String id, String expectedShards) {
  DocRouter router = coll.getRouter();
  Collection<Slice> slices = router.getSearchSlices(id, null, coll);

  List<String> expectedShardStr = StrUtils.splitSmart(expectedShards, ",", true);

  HashSet<String> expectedSet = new HashSet<>(expectedShardStr);
  HashSet<String> obtainedSet = new HashSet<>();
  for (Slice slice : slices) {
    obtainedSet.add(slice.getName());
  }

  assertEquals(slices.size(), obtainedSet.size());  // make sure no repeated slices
  assertEquals(expectedSet, obtainedSet);
}
 
Example #25
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SolrCmdDistributor.Node routeDocToSlice(String collection, SolrInputDocument doc) {
  SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
  // schema might not have key field...
  String idFieldName = uniqueKeyField == null ? null : uniqueKeyField.getName();
  String idValue = uniqueKeyField == null ? null : doc.getFieldValue(idFieldName).toString();
  DocCollection coll = zkController.getClusterState().getCollection(collection);
  Slice slice = coll.getRouter().getTargetSlice(idValue, doc, null, req.getParams(), coll);
  return getLeaderNode(collection, slice);
}
 
Example #26
Source File: LeaderRecoveryWatcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onStateChanged(Set<String> liveNodes, DocCollection collectionState) {
  if (collectionState == null) { // collection has been deleted - don't wait
    latch.countDown();
    return true;
  }
  Slice slice = collectionState.getSlice(shardId);
  if (slice == null) { // shard has been removed - don't wait
    latch.countDown();
    return true;
  }
  for (Replica replica : slice.getReplicas()) {
    // check if another replica exists - doesn't have to be the one we're moving
    // as long as it's active and can become a leader, in which case we don't have to wait
    // for recovery of specifically the one that we've just added
    if (!replica.getName().equals(replicaId)) {
      if (replica.getType().equals(Replica.Type.PULL)) { // not eligible for leader election
        continue;
      }
      // check its state
      String coreName = replica.getStr(ZkStateReader.CORE_NAME_PROP);
      if (targetCore != null && !targetCore.equals(coreName)) {
        continue;
      }
      if (replica.isActive(liveNodes)) { // recovered - stop waiting
        latch.countDown();
        return true;
      }
    }
  }
  // set the watch again to wait for the new replica to recover
  return false;
}
 
Example #27
Source File: SolrCloudScraperTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void queryCollections() throws Exception {
  List<Collector.MetricFamilySamples> collection1Metrics = solrCloudScraper.collections(
      configuration.getCollectionsConfiguration().get(0)).asList();

  assertEquals(2, collection1Metrics.size());
  Collector.MetricFamilySamples liveNodeSamples = collection1Metrics.get(0);
  assertEquals("solr_collections_live_nodes", liveNodeSamples.name);
  assertEquals("See following URL: https://lucene.apache.org/solr/guide/collections-api.html#clusterstatus", liveNodeSamples.help);
  assertEquals(1, liveNodeSamples.samples.size());

  assertEquals(
      getClusterState().getLiveNodes().size(),
      liveNodeSamples.samples.get(0).value, 0.001);

  Collector.MetricFamilySamples shardLeaderSamples = collection1Metrics.get(1);

  DocCollection collection = getCollectionState();
  List<Replica> allReplicas = collection.getReplicas();
  assertEquals(allReplicas.size(), shardLeaderSamples.samples.size());

  Collection<Slice> slices = getCollectionState().getSlices();

  Set<String> leaderCoreNames = slices.stream()
      .map(slice -> collection.getLeader(slice.getName()).getCoreName())
      .collect(Collectors.toSet());

  for (Collector.MetricFamilySamples.Sample sample : shardLeaderSamples.samples) {
    assertEquals("solr_collections_shard_leader", sample.name);
    assertEquals(Arrays.asList("collection", "shard", "replica", "core", "type", "zk_host"), sample.labelNames);
    assertEquals(leaderCoreNames.contains(sample.labelValues.get(3)) ? 1.0 : 0.0, sample.value, 0.001);
  }
}
 
Example #28
Source File: Assign.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Assign a new unique id up to slices count - then add replicas evenly.
 *
 * @return the assigned shard id
 */
public static String assignShard(DocCollection collection, Integer numShards) {
  if (numShards == null) {
    numShards = 1;
  }
  String returnShardId = null;
  Map<String, Slice> sliceMap = collection != null ? collection.getActiveSlicesMap() : null;


  // TODO: now that we create shards ahead of time, is this code needed?  Esp since hash ranges aren't assigned when creating via this method?

  if (sliceMap == null) {
    return "shard1";
  }

  List<String> shardIdNames = new ArrayList<>(sliceMap.keySet());

  if (shardIdNames.size() < numShards) {
    return "shard" + (shardIdNames.size() + 1);
  }

  // TODO: don't need to sort to find shard with fewest replicas!

  // else figure out which shard needs more replicas
  final Map<String, Integer> map = new HashMap<>();
  for (String shardId : shardIdNames) {
    int cnt = sliceMap.get(shardId).getReplicasMap().size();
    map.put(shardId, cnt);
  }

  Collections.sort(shardIdNames, (String o1, String o2) -> {
    Integer one = map.get(o1);
    Integer two = map.get(o2);
    return one.compareTo(two);
  });

  returnShardId = shardIdNames.get(0);
  return returnShardId;
}
 
Example #29
Source File: SliceMutator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ZkWriteCommand removeRoutingRule(final ClusterState clusterState, ZkNodeProps message) {
  String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
  if (!checkCollectionKeyExistence(message)) return ZkStateWriter.NO_OP;
  String shard = message.getStr(ZkStateReader.SHARD_ID_PROP);
  String routeKeyStr = message.getStr("routeKey");

  log.info("Overseer.removeRoutingRule invoked for collection: {} shard: {} routeKey: {}"
      , collectionName, shard, routeKeyStr);

  DocCollection collection = clusterState.getCollection(collectionName);
  Slice slice = collection.getSlice(shard);
  if (slice == null) {
    log.warn("Unknown collection: {} shard: {}", collectionName, shard);
    return ZkStateWriter.NO_OP;
  }
  Map<String, RoutingRule> routingRules = slice.getRoutingRules();
  if (routingRules != null) {
    routingRules.remove(routeKeyStr); // no rules left
    Map<String, Object> props = slice.shallowCopy();
    props.put("routingRules", routingRules);
    Slice newSlice = new Slice(slice.getName(), slice.getReplicasCopy(), props,collectionName);
    return new ZkWriteCommand(collectionName,
        CollectionMutator.updateSlice(collectionName, collection, newSlice));
  }

  return ZkStateWriter.NO_OP;
}
 
Example #30
Source File: AbstractCloudBackupRestoreTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Map<String, Integer> getShardToDocCountMap(CloudSolrClient client, DocCollection docCollection) throws SolrServerException, IOException {
  Map<String,Integer> shardToDocCount = new TreeMap<>();
  for (Slice slice : docCollection.getActiveSlices()) {
    String shardName = slice.getName();
    try (HttpSolrClient leaderClient = new HttpSolrClient.Builder(slice.getLeader().getCoreUrl()).withHttpClient(client.getHttpClient()).build()) {
      long docsInShard = leaderClient.query(new SolrQuery("*:*").setParam("distrib", "false"))
          .getResults().getNumFound();
      shardToDocCount.put(shardName, (int) docsInShard);
    }
  }
  return shardToDocCount;
}