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

The following examples show how to use org.apache.solr.common.cloud.DocCollection. 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: AssignTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testAssignNode() throws Exception {
  assumeWorkingMockito();
  
  SolrZkClient zkClient = mock(SolrZkClient.class);
  Map<String, byte[]> zkClientData = new HashMap<>();
  when(zkClient.setData(anyString(), any(), anyInt(), anyBoolean())).then(invocation -> {
      zkClientData.put(invocation.getArgument(0), invocation.getArgument(1));
      return null;
    }
  );
  when(zkClient.getData(anyString(), any(), any(), anyBoolean())).then(invocation ->
      zkClientData.get(invocation.getArgument(0)));
  // TODO: fix this to be independent of ZK
  ZkDistribStateManager stateManager = new ZkDistribStateManager(zkClient);
  String nodeName = Assign.assignCoreNodeName(stateManager, new DocCollection("collection1", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT));
  assertEquals("core_node1", nodeName);
  nodeName = Assign.assignCoreNodeName(stateManager, new DocCollection("collection2", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT));
  assertEquals("core_node1", nodeName);
  nodeName = Assign.assignCoreNodeName(stateManager, new DocCollection("collection1", new HashMap<>(), new HashMap<>(), DocRouter.DEFAULT));
  assertEquals("core_node2", nodeName);
}
 
Example #2
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Before
public void removeAllProperties() throws KeeperException, InterruptedException {
  forceUpdateCollectionStatus();
  DocCollection docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
  for (Slice slice : docCollection.getSlices()) {
    for (Replica rep : slice.getReplicas()) {
      rep.getProperties().forEach((key, value) -> {
        if (key.startsWith("property.")) {
          try {
            delProp(slice, rep, key);
          } catch (IOException | SolrServerException e) {
            fail("Caught unexpected exception in @Before " + e.getMessage());
          }
        }
      });
    }
  }
}
 
Example #3
Source File: SliceStateTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultSliceState() {
  Map<String, DocCollection> collectionStates = new HashMap<>();
  Set<String> liveNodes = new HashSet<>();
  liveNodes.add("node1");

  Map<String, Slice> slices = new HashMap<>();
  Map<String, Replica> sliceToProps = new HashMap<>();
  Map<String, Object> props = new HashMap<>();
  props.put("node_name", "127.0.0.1:10000_solr");
  props.put("core", "core1");

  Replica replica = new Replica("node1", props, "collection1", "shard1");
  sliceToProps.put("node1", replica);
  Slice slice = new Slice("shard1", sliceToProps, null, "collection1");
  assertSame("Default state not set to active", Slice.State.ACTIVE, slice.getState());
  slices.put("shard1", slice);
  collectionStates.put("collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT));

  ClusterState clusterState = new ClusterState(liveNodes, collectionStates);
  byte[] bytes = Utils.toJSON(clusterState);
  ClusterState loadedClusterState = ClusterState.createFromJson(-1, bytes, liveNodes);

  assertSame("Default state not set to active", Slice.State.ACTIVE, loadedClusterState.getCollection("collection1").getSlice("shard1").getState());
}
 
Example #4
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 #5
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 #6
Source File: DeleteLastCustomShardedReplicaTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {

  final String collectionName = "customcollreplicadeletion";

  CollectionAdminRequest.createCollectionWithImplicitRouter(collectionName, "conf", "a,b", 1)
      .setMaxShardsPerNode(5)
      .process(cluster.getSolrClient());

  DocCollection collectionState = getCollectionState(collectionName);
  Replica replica = getRandomReplica(collectionState.getSlice("a"));

  CollectionAdminRequest.deleteReplica(collectionName, "a", replica.getName())
      .process(cluster.getSolrClient());

  waitForState("Expected shard 'a' to have no replicas", collectionName, (n, c) -> {
    return c.getSlice("a") == null || c.getSlice("a").getReplicas().size() == 0;
  });

}
 
Example #7
Source File: SolrClientNodeStateProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void readReplicaDetails() throws IOException {
  ClusterStateProvider clusterStateProvider = getClusterStateProvider();
  ClusterState clusterState = clusterStateProvider.getClusterState();
  if (clusterState == null) { // zkStateReader still initializing
    return;
  }
  Map<String, ClusterState.CollectionRef> all = clusterStateProvider.getClusterState().getCollectionStates();
  all.forEach((collName, ref) -> {
    DocCollection coll = ref.get();
    if (coll == null) return;
    if (coll.getProperties().get(CollectionAdminParams.WITH_COLLECTION) != null) {
      withCollectionsMap.put(coll.getName(), (String) coll.getProperties().get(CollectionAdminParams.WITH_COLLECTION));
    }
    coll.forEachReplica((shard, replica) -> {
      Map<String, Map<String, List<ReplicaInfo>>> nodeData = nodeVsCollectionVsShardVsReplicaInfo.computeIfAbsent(replica.getNodeName(), k -> new HashMap<>());
      Map<String, List<ReplicaInfo>> collData = nodeData.computeIfAbsent(collName, k -> new HashMap<>());
      List<ReplicaInfo> replicas = collData.computeIfAbsent(shard, k -> new ArrayList<>());
      replicas.add(new ReplicaInfo(collName, shard, replica, new HashMap<>(replica.getProperties())));
    });
  });
}
 
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: 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 #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: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean checkPreferredsAreLeaders(boolean doAsserts) throws KeeperException, InterruptedException {
  forceUpdateCollectionStatus();
  DocCollection docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
  for (Slice slice : docCollection.getSlices()) {
    for (Replica rep : slice.getReplicas()) {
      if (rep.getBool("property.preferredleader", false)) {
        boolean isLeader = rep.getBool("leader", false);
        if (doAsserts) {
          assertTrue("PreferredLeader should be the leader: ", isLeader);
        } else if (isLeader == false) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example #12
Source File: ShardLeaderElectionContext.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Do other replicas with higher term participated in the election
 *
 * @return true if other replicas with higher term participated in the election, false if otherwise
 */
private boolean replicasWithHigherTermParticipated(ZkShardTerms zkShardTerms, String coreNodeName) {
  ClusterState clusterState = zkController.getClusterState();
  DocCollection docCollection = clusterState.getCollectionOrNull(collection);
  Slice slices = (docCollection == null) ? null : docCollection.getSlice(shardId);
  if (slices == null) return false;

  long replicaTerm = zkShardTerms.getTerm(coreNodeName);
  boolean isRecovering = zkShardTerms.isRecovering(coreNodeName);

  for (Replica replica : slices.getReplicas()) {
    if (replica.getName().equals(coreNodeName)) continue;

    if (clusterState.getLiveNodes().contains(replica.getNodeName())) {
      long otherTerm = zkShardTerms.getTerm(replica.getName());
      boolean isOtherReplicaRecovering = zkShardTerms.isRecovering(replica.getName());

      if (isRecovering && !isOtherReplicaRecovering) return true;
      if (otherTerm > replicaTerm) return true;
    }
  }
  return false;
}
 
Example #13
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 List<SolrCmdDistributor.Node> getSubShardLeaders(DocCollection coll, String shardId, String docId, SolrInputDocument doc) {
  Collection<Slice> allSlices = coll.getSlices();
  List<SolrCmdDistributor.Node> nodes = null;
  for (Slice aslice : allSlices) {
    final Slice.State state = aslice.getState();
    if (state == Slice.State.CONSTRUCTION || state == Slice.State.RECOVERY)  {
      DocRouter.Range myRange = coll.getSlice(shardId).getRange();
      if (myRange == null) myRange = new DocRouter.Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
      boolean isSubset = aslice.getRange() != null && aslice.getRange().isSubsetOf(myRange);
      if (isSubset &&
          (docId == null // in case of deletes
              || coll.getRouter().isTargetSlice(docId, doc, req.getParams(), aslice.getName(), coll))) {
        Replica sliceLeader = aslice.getLeader();
        // slice leader can be null because node/shard is created zk before leader election
        if (sliceLeader != null && clusterState.liveNodesContain(sliceLeader.getNodeName()))  {
          if (nodes == null) nodes = new ArrayList<>();
          ZkCoreNodeProps nodeProps = new ZkCoreNodeProps(sliceLeader);
          nodes.add(new SolrCmdDistributor.StdNode(nodeProps, coll.getName(), aslice.getName()));
        }
      }
    }
  }
  return nodes;
}
 
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: ZooKeeperInspector.java    From examples with Apache License 2.0 6 votes vote down vote up
public List<List<String>> extractShardUrls(String zkHost, String collection) {

    DocCollection docCollection = extractDocCollection(zkHost, collection);
    List<Slice> slices = getSortedSlices(docCollection.getSlices());
    List<List<String>> solrUrls = new ArrayList<List<String>>(slices.size());
    for (Slice slice : slices) {
      if (slice.getLeader() == null) {
        throw new IllegalArgumentException("Cannot find SolrCloud slice leader. "
            + "It looks like not all of your shards are registered in ZooKeeper yet");
      }
      Collection<Replica> replicas = slice.getReplicas();
      List<String> urls = new ArrayList<String>(replicas.size());
      for (Replica replica : replicas) {
        ZkCoreNodeProps props = new ZkCoreNodeProps(replica);
        urls.add(props.getCoreUrl());
      }
      solrUrls.add(urls);
    }
    return solrUrls;
  }
 
Example #16
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * should be called by implementing class after setting up replicas
 * @param cmd delete command
 * @param replicas list of Nodes replicas to pass to {@link DistributedUpdateProcessor#doDistribDeleteByQuery(DeleteUpdateCommand, List, DocCollection)}
 * @param coll the collection in zookeeper {@link org.apache.solr.common.cloud.DocCollection},
 *             passed to {@link DistributedUpdateProcessor#doDistribDeleteByQuery(DeleteUpdateCommand, List, DocCollection)}
 */
protected void doDeleteByQuery(DeleteUpdateCommand cmd, List<SolrCmdDistributor.Node> replicas, DocCollection coll) throws IOException {
  if (vinfo == null) {
    super.processDelete(cmd);
    return;
  }

  // at this point, there is an update we need to try and apply.
  // we may or may not be the leader.

  versionDeleteByQuery(cmd);

  doDistribDeleteByQuery(cmd, replicas, coll);


  if (returnVersions && rsp != null) {
    if (deleteByQueryResponse == null) {
      deleteByQueryResponse = new NamedList<>(1);
      rsp.add("deleteByQuery", deleteByQueryResponse);
    }
    deleteByQueryResponse.add(cmd.getQuery(), cmd.getVersion());
  }
}
 
Example #17
Source File: SimClusterStateProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public ClusterState getClusterState() throws IOException {
  ensureNotClosed();
  try {
    lock.lockInterruptibly();
    try {
      Map<String, DocCollection> states = getCollectionStates();
      ClusterState state = new ClusterState(liveNodes.get(), states);
      return state;
    } finally {
      lock.unlock();
    }
  } catch (InterruptedException e) {
    throw new IOException(e);
  }
}
 
Example #18
Source File: RoutedAliasUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("WeakerAccess")
Set<String> getLeaderCoreNames(ClusterState clusterState) {
  Set<String> leaders = new TreeSet<>(); // sorted just to make it easier to read when debugging...
  List<JettySolrRunner> jettySolrRunners = cluster.getJettySolrRunners();
  for (JettySolrRunner jettySolrRunner : jettySolrRunners) {
    List<CoreDescriptor> coreDescriptors = jettySolrRunner.getCoreContainer().getCoreDescriptors();
    for (CoreDescriptor core : coreDescriptors) {
      String nodeName = jettySolrRunner.getNodeName();
      String collectionName = core.getCollectionName();
      DocCollection collectionOrNull = clusterState.getCollectionOrNull(collectionName);
      List<Replica> leaderReplicas = collectionOrNull.getLeaderReplicas(nodeName);
      if (leaderReplicas != null) {
        for (Replica leaderReplica : leaderReplicas) {
          leaders.add(leaderReplica.getCoreName());
        }
      }
    }
  }
  return leaders;
}
 
Example #19
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean checkdUniquePropPerShard(Map<String, String> uniques, String prop) throws KeeperException, InterruptedException {
  forceUpdateCollectionStatus();
  DocCollection docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);

  for (Slice slice : docCollection.getSlices()) {
    int propfCount = 0;
    for (Replica rep : slice.getReplicas()) {
      if (rep.getBool("property." + prop.toLowerCase(Locale.ROOT), false)) {
        propfCount++;
        uniques.put(slice.getName(), rep.getName());
      }
    }
    if (1 != propfCount) {
      return false;
    }
  }
  return true;
}
 
Example #20
Source File: MiniSolrCloudCluster.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void waitForActiveCollection(String collection, long wait, TimeUnit unit, int shards, int totalReplicas) {
  log.info("waitForActiveCollection: {}", collection);
  CollectionStatePredicate predicate = expectedShardsAndActiveReplicas(shards, totalReplicas);

  AtomicReference<DocCollection> state = new AtomicReference<>();
  AtomicReference<Set<String>> liveNodesLastSeen = new AtomicReference<>();
  try {
    getSolrClient().waitForState(collection, wait, unit, (n, c) -> {
      state.set(c);
      liveNodesLastSeen.set(n);

      return predicate.matches(n, c);
    });
  } catch (TimeoutException | InterruptedException e) {
    throw new RuntimeException("Failed while waiting for active collection" + "\n" + e.getMessage() + "\nLive Nodes: " + Arrays.toString(liveNodesLastSeen.get().toArray())
        + "\nLast available state: " + state.get());
  }

}
 
Example #21
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 #22
Source File: SolrIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void process(@Element Read spec, OutputReceiver<Read> out) throws IOException {
  ConnectionConfiguration connectionConfig = spec.getConnectionConfiguration();
  try (AuthorizedSolrClient<CloudSolrClient> client = connectionConfig.createClient()) {
    String collection = spec.getCollection();
    final ClusterState clusterState = AuthorizedSolrClient.getClusterState(client);
    DocCollection docCollection = clusterState.getCollection(collection);
    for (Slice slice : docCollection.getSlices()) {
      ArrayList<Replica> replicas = new ArrayList<>(slice.getReplicas());
      Collections.shuffle(replicas);
      // Load balancing by randomly picking an active replica
      Replica randomActiveReplica = null;
      for (Replica replica : replicas) {
        // We need to check both state of the replica and live nodes
        // to make sure that the replica is alive
        if (replica.getState() == Replica.State.ACTIVE
            && clusterState.getLiveNodes().contains(replica.getNodeName())) {
          randomActiveReplica = replica;
          break;
        }
      }
      // TODO in case of this replica goes inactive while the pipeline runs.
      // We should pick another active replica of this shard.
      checkState(
          randomActiveReplica != null,
          "Can not found an active replica for slice %s",
          slice.getName());
      out.output(spec.withReplicaInfo(ReplicaInfo.create(checkNotNull(randomActiveReplica))));
    }
  }
}
 
Example #23
Source File: TestHashPartitioner.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void doNormalIdHashing(DocCollection coll) throws Exception {
  assertEquals(4, coll.getSlices().size());

  doId(coll, "b", "shard1");
  doId(coll, "c", "shard2");
  doId(coll, "d", "shard3");
  doId(coll, "e", "shard4");
}
 
Example #24
Source File: TestSimPolicyCloud.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCreateCollectionAddShardUsingPolicy() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();
  String nodeId = cluster.getSimClusterStateProvider().simGetRandomNode();
  int port = (Integer)cluster.getSimNodeStateProvider().simGetNodeValue(nodeId, ImplicitSnitch.PORT);

  String commands =  "{set-policy :{c1 : [{replica:1 , shard:'#EACH', port: '" + port + "'}]}}";
  solrClient.request(AutoScalingRequest.create(SolrRequest.METHOD.POST, commands));
  Map<String, Object> json = Utils.getJson(cluster.getDistribStateManager(), ZkStateReader.SOLR_AUTOSCALING_CONF_PATH);
  assertEquals("full json:"+ Utils.toJSONString(json) , "#EACH",
      Utils.getObjectByPath(json, true, "/policies/c1[0]/shard"));
  CollectionAdminRequest.createCollectionWithImplicitRouter("policiesTest", "conf", "s1,s2", 1)
      .setPolicy("c1")
      .process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "policiesTest",
      CloudUtil.clusterShape(2, 1));

  DocCollection coll = getCollectionState("policiesTest");
  assertEquals("c1", coll.getPolicyName());
  assertEquals(2,coll.getReplicas().size());
  coll.forEachReplica((s, replica) -> assertEquals(nodeId, replica.getNodeName()));
  CollectionAdminRequest.createShard("policiesTest", "s3").process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "policiesTest",
      CloudUtil.clusterShape(3, 1));

  coll = getCollectionState("policiesTest");
  assertEquals(1, coll.getSlice("s3").getReplicas().size());
  coll.getSlice("s3").forEach(replica -> assertEquals(nodeId, replica.getNodeName()));
}
 
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: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
ClusterState waitForNewShard(String collectionName, String sliceName) throws KeeperException, InterruptedException {
  log.debug("Waiting for slice {} of collection {} to be available", sliceName, collectionName);
  RTimer timer = new RTimer();
  int retryCount = 320;
  while (retryCount-- > 0) {
    ClusterState clusterState = zkStateReader.getClusterState();
    DocCollection collection = clusterState.getCollection(collectionName);

    if (collection == null) {
      throw new SolrException(ErrorCode.SERVER_ERROR,
          "Unable to find collection: " + collectionName + " in clusterstate");
    }
    Slice slice = collection.getSlice(sliceName);
    if (slice != null) {
      if (log.isDebugEnabled()) {
        log.debug("Waited for {}ms for slice {} of collection {} to be available",
            timer.getTime(), sliceName, collectionName);
      }
      return clusterState;
    }
    Thread.sleep(1000);
  }
  throw new SolrException(ErrorCode.SERVER_ERROR,
      "Could not find new slice " + sliceName + " in collection " + collectionName
          + " even after waiting for " + timer.getTime() + "ms"
  );
}
 
Example #27
Source File: DistribDocExpirationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * returns a map whose key is the coreNodeName and whose value is data about that core needed for the test
 */
private Map<String,ReplicaData> getTestDataForAllReplicas() throws IOException, SolrServerException {
  Map<String,ReplicaData> results = new HashMap<>();

  DocCollection collectionState = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION);

  for (Replica replica : collectionState.getReplicas()) {

    String coreName = replica.getCoreName();
    try (HttpSolrClient client = getHttpSolrClient(replica.getCoreUrl())) {

      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set("command", "indexversion");
      params.set("_trace", "getIndexVersion");
      params.set("qt", ReplicationHandler.PATH);
      QueryRequest req = setAuthIfNeeded(new QueryRequest(params));

      NamedList<Object> res = client.request(req);
      assertNotNull("null response from server: " + coreName, res);

      Object version = res.get("indexversion");
      assertNotNull("null version from server: " + coreName, version);
      assertTrue("version isn't a long: " + coreName, version instanceof Long);

      long numDocs = 
        setAuthIfNeeded(new QueryRequest
                        (params("q", "*:*",
                                "distrib", "false",
                                "rows", "0",
                                "_trace", "counting_docs"))).process(client).getResults().getNumFound();

      final ReplicaData data = new ReplicaData(replica.getSlice(),coreName,(Long)version,numDocs);
      log.info("{}", data);
      results.put(coreName, data);

    }
  }

  return results;
}
 
Example #28
Source File: Assign.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static String buildSolrCoreName(DistribStateManager stateManager, DocCollection collection, String shard, Replica.Type type, boolean newCollection) {
  Slice slice = collection.getSlice(shard);
  int defaultValue = defaultCounterValue(collection, newCollection, shard);
  int replicaNum = incAndGetId(stateManager, collection.getName(), defaultValue);
  String coreName = buildSolrCoreName(collection.getName(), shard, type, replicaNum);
  while (existCoreName(coreName, slice)) {
    replicaNum = incAndGetId(stateManager, collection.getName(), defaultValue);
    coreName = buildSolrCoreName(collection.getName(), shard, type, replicaNum);
  }
  return coreName;
}
 
Example #29
Source File: TestSolrConfigHandlerCloud.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static String getRandomServer(CloudSolrClient cloudClient, String collName) {
  DocCollection coll = cloudClient.getZkStateReader().getClusterState().getCollection(collName);
  List<String> urls = new ArrayList<>();
  for (Slice slice : coll.getSlices()) {
    for (Replica replica : slice.getReplicas())
      urls.add(""+replica.get(ZkStateReader.BASE_URL_PROP) + "/"+replica.get(ZkStateReader.CORE_NAME_PROP));
  }
  return urls.get(random().nextInt(urls.size()));
}
 
Example #30
Source File: Assign.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if auto scaling policy framework should be used for replica placement
 * for this collection, otherwise false
 */
public static boolean usePolicyFramework(DocCollection collection, SolrCloudManager cloudManager)
    throws IOException, InterruptedException {
  Objects.requireNonNull(collection, "The DocCollection instance cannot be null");
  Objects.requireNonNull(cloudManager, "The SolrCloudManager instance cannot be null");
  return usePolicyFramework(Optional.of(collection), cloudManager);
}