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

The following examples show how to use org.apache.solr.common.cloud.ZkNodeProps. 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: DeleteNodeCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static List<String> verifyReplicaAvailability(List<ZkNodeProps> sourceReplicas, ClusterState state) {
  List<String> res = new ArrayList<>();
  for (ZkNodeProps sourceReplica : sourceReplicas) {
    String coll = sourceReplica.getStr(COLLECTION_PROP);
    String shard = sourceReplica.getStr(SHARD_ID_PROP);
    String replicaName = sourceReplica.getStr(ZkStateReader.REPLICA_PROP);
    DocCollection collection = state.getCollection(coll);
    Slice slice = collection.getSlice(shard);
    if (slice.getReplicas().size() < 2) {
      // can't delete the only replica in existence
      res.add(coll + "/" + shard + "/" + replicaName + ", type=" + sourceReplica.getStr(ZkStateReader.REPLICA_TYPE));
    } else { // check replica types
      int otherNonPullReplicas = 0;
      for (Replica r : slice.getReplicas()) {
        if (!r.getName().equals(replicaName) && !r.getType().equals(Replica.Type.PULL)) {
          otherNonPullReplicas++;
        }
      }
      // can't delete - there are no other non-pull replicas
      if (otherNonPullReplicas == 0) {
        res.add(coll + "/" + shard + "/" + replicaName + ", type=" + sourceReplica.getStr(ZkStateReader.REPLICA_TYPE));
      }
    }
  }
  return res;
}
 
Example #2
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 #3
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 #4
Source File: TestConfigsApi.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void testCommands() throws Exception {

  try (ConfigSetsHandler handler = new ConfigSetsHandler(null) {
    @Override
    protected void sendToZk(SolrQueryResponse rsp,
                            ConfigSetOperation operation,
                            Map<String, Object> result)
        throws KeeperException, InterruptedException {
      result.put(QUEUE_OPERATION, operation.action.toLower());
      rsp.add(ZkNodeProps.class.getName(), new ZkNodeProps(result));
    }
  }) {
    ApiBag apiBag = new ApiBag(false);
    for (Api api : handler.getApis()) apiBag.register(api, EMPTY_MAP);
    compareOutput(apiBag, "/cluster/configs/sample", DELETE, null, null,
        "{name :sample, operation:delete}");

    compareOutput(apiBag, "/cluster/configs", POST, "{create:{name : newconf, baseConfigSet: sample }}", null,
        "{operation:create, name :newconf,  baseConfigSet: sample, immutable: false }");
  }
}
 
Example #5
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Grabs an exclusive lock for this particular task.
 * @return <code>null</code> if locking is not possible. When locking is not possible, it will remain
 * impossible for the passed value of <code>batchSessionId</code>. This is to guarantee tasks are executed
 * in queue order (and a later task is not run earlier than its turn just because it happens that a lock got released).
 */
@Override
public Lock lockTask(ZkNodeProps message, long batchSessionId) {
  if (sessionId != batchSessionId) {
    //this is always called in the same thread.
    //Each batch is supposed to have a new taskBatch
    //So if taskBatch changes we must create a new Session
    lockSession = lockTree.getSession();
    sessionId = batchSessionId;
  }

  return lockSession.lock(getCollectionAction(message.getStr(Overseer.QUEUE_OPERATION)),
      Arrays.asList(
          getTaskKey(message),
          message.getStr(ZkStateReader.SHARD_ID_PROP),
          message.getStr(ZkStateReader.REPLICA_PROP))
  );
}
 
Example #6
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static String getUrlFromZk(ClusterState clusterState, String collection) {
  Map<String,Slice> slices = clusterState.getCollection(collection).getSlicesMap();

  if (slices == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Could not find collection:" + collection);
  }

  for (Map.Entry<String,Slice> entry : slices.entrySet()) {
    Slice slice = entry.getValue();
    Map<String,Replica> shards = slice.getReplicasMap();
    Set<Map.Entry<String,Replica>> shardEntries = shards.entrySet();
    for (Map.Entry<String,Replica> shardEntry : shardEntries) {
      final ZkNodeProps node = shardEntry.getValue();
      if (clusterState.liveNodesContain(node.getStr(ZkStateReader.NODE_NAME_PROP))) {
        return ZkCoreNodeProps.getCoreUrl(node.getStr(ZkStateReader.BASE_URL_PROP), collection); //new ZkCoreNodeProps(node).getCoreUrl();
      }
    }
  }

  throw new RuntimeException("Could not find a live node for collection:" + collection);
}
 
Example #7
Source File: TestCollectionAPIs.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
void invokeAction(SolrQueryRequest req, SolrQueryResponse rsp,
                  CoreContainer cores,
                  CollectionParams.CollectionAction action,
                  CollectionOperation operation) throws Exception {
  Map<String, Object> result = null;
  if (action == CollectionParams.CollectionAction.COLLECTIONPROP) {
    //Fake this action, since we don't want to write to ZooKeeper in this test
    result = new HashMap<>();
    result.put(NAME, req.getParams().required().get(NAME));
    result.put(PROPERTY_NAME, req.getParams().required().get(PROPERTY_NAME));
    result.put(PROPERTY_VALUE, req.getParams().required().get(PROPERTY_VALUE));
  } else {
    result = operation.execute(req, rsp, this);
  }
  if (result != null) {
    result.put(QUEUE_OPERATION, operation.action.toLower());
    rsp.add(ZkNodeProps.class.getName(), new ZkNodeProps(result));
  }
}
 
Example #8
Source File: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void rejoinElectionQueue(Slice slice, String electionNode, String core, boolean rejoinAtHead)
    throws KeeperException, InterruptedException {
  Replica replica = slice.getReplica(LeaderElector.getNodeName(electionNode));
  Map<String, Object> propMap = new HashMap<>();
  propMap.put(COLLECTION_PROP, collectionName);
  propMap.put(SHARD_ID_PROP, slice.getName());
  propMap.put(QUEUE_OPERATION, REBALANCELEADERS.toLower());
  propMap.put(CORE_NAME_PROP, core);
  propMap.put(CORE_NODE_NAME_PROP, replica.getName());
  propMap.put(ZkStateReader.BASE_URL_PROP, replica.getProperties().get(ZkStateReader.BASE_URL_PROP));
  propMap.put(REJOIN_AT_HEAD_PROP, Boolean.toString(rejoinAtHead)); // Get ourselves to be first in line.
  propMap.put(ELECTION_NODE_PROP, electionNode);
  String asyncId = REBALANCELEADERS.toLower() + "_" + core + "_" + Math.abs(System.nanoTime());
  propMap.put(ASYNC, asyncId);
  asyncRequests.add(asyncId);

  collectionsHandler.sendToOCPQueue(new ZkNodeProps(propMap)); // ignore response; we construct our own
}
 
Example #9
Source File: ZkNodePropsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic() throws IOException {
  
  Map<String,Object> props = new HashMap<>();
  props.put("prop1", "value1");
  props.put("prop2", "value2");
  props.put("prop3", "value3");
  props.put("prop4", "value4");
  props.put("prop5", "value5");
  props.put("prop6", "value6");
  
  ZkNodeProps zkProps = new ZkNodeProps(props);
  byte[] bytes = Utils.toJSON(zkProps);
  ZkNodeProps props2 = ZkNodeProps.load(bytes);

  props.forEach((s, o) -> assertEquals(o, props2.get(s)));
  SimplePostTool.BAOS baos = new SimplePostTool.BAOS();
  try (JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.marshal(zkProps.getProperties(), baos);
  }
  bytes = baos.toByteArray();
  System.out.println("BIN size : " + bytes.length);
  ZkNodeProps props3 = ZkNodeProps.load(bytes);
  props.forEach((s, o) -> assertEquals(o, props3.get(s)));
}
 
Example #10
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Executes a query against each live and active replica of the specified shard
 * and aserts that the results are identical.
 *
 * @see #queryAndCompare
 */
public QueryResponse queryAndCompareReplicas(SolrParams params, String shard)
  throws Exception {

  ArrayList<SolrClient> shardClients = new ArrayList<>(7);

  updateMappingsFromZk(jettys, clients);
  ZkStateReader zkStateReader = cloudClient.getZkStateReader();
  List<CloudJettyRunner> solrJetties = shardToJetty.get(shard);
  assertNotNull("no jetties found for shard: " + shard, solrJetties);


  for (CloudJettyRunner cjetty : solrJetties) {
    ZkNodeProps props = cjetty.info;
    String nodeName = props.getStr(ZkStateReader.NODE_NAME_PROP);
    boolean active = Replica.State.getState(props.getStr(ZkStateReader.STATE_PROP)) == Replica.State.ACTIVE;
    boolean live = zkStateReader.getClusterState().liveNodesContain(nodeName);
    if (active && live) {
      shardClients.add(cjetty.client.solrClient);
    }
  }
  return queryAndCompare(params, shardClients);
}
 
Example #11
Source File: ZkController.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void checkOverseerDesignate() {
  try {
    byte[] data = zkClient.getData(ZkStateReader.ROLES, null, new Stat(), true);
    if (data == null) return;
    @SuppressWarnings({"rawtypes"})
    Map roles = (Map) Utils.fromJSON(data);
    if (roles == null) return;
    @SuppressWarnings({"rawtypes"})
    List nodeList = (List) roles.get("overseer");
    if (nodeList == null) return;
    if (nodeList.contains(getNodeName())) {
      ZkNodeProps props = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.ADDROLE.toString().toLowerCase(Locale.ROOT),
          "node", getNodeName(),
          "role", "overseer");
      log.info("Going to add role {} ", props);
      getOverseerCollectionQueue().offer(Utils.toJSON(props));
    }
  } catch (NoNodeException nne) {
    return;
  } catch (Exception e) {
    log.warn("could not read the overseer designate ", e);
  }
}
 
Example #12
Source File: CreateCollectionCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static List<String> populateShardNames(ZkNodeProps message, String router) {
  List<String> shardNames = new ArrayList<>();
  Integer numSlices = message.getInt(OverseerCollectionMessageHandler.NUM_SLICES, null);
  if (ImplicitDocRouter.NAME.equals(router)) {
    ClusterStateMutator.getShardNames(shardNames, message.getStr("shards", null));
    numSlices = shardNames.size();
  } else {
    if (numSlices == null) {
      throw new SolrException(ErrorCode.BAD_REQUEST, OverseerCollectionMessageHandler.NUM_SLICES + " is a required param (when using CompositeId router).");
    }
    if (numSlices <= 0) {
      throw new SolrException(ErrorCode.BAD_REQUEST, OverseerCollectionMessageHandler.NUM_SLICES + " must be > 0");
    }
    ClusterStateMutator.getShardNames(numSlices, shardNames);
  }
  return shardNames;
}
 
Example #13
Source File: Assign.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static List<String> getLiveOrLiveAndCreateNodeSetList(final Set<String> liveNodes, final ZkNodeProps message, final Random random) {
  List<String> nodeList;
  final String createNodeSetStr = message.getStr(CREATE_NODE_SET);
  final List<String> createNodeList = (createNodeSetStr == null) ? null :
      StrUtils.splitSmart((OverseerCollectionMessageHandler.CREATE_NODE_SET_EMPTY.equals(createNodeSetStr) ?
          "" : createNodeSetStr), ",", true);

  if (createNodeList != null) {
    nodeList = new ArrayList<>(createNodeList);
    nodeList.retainAll(liveNodes);
    if (message.getBool(OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE,
        OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE_DEFAULT)) {
      Collections.shuffle(nodeList, random);
    }
  } else {
    nodeList = new ArrayList<>(liveNodes);
    Collections.shuffle(nodeList, random);
  }

  return nodeList;
}
 
Example #14
Source File: AutoScalingHandlerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static void testAutoAddReplicas() throws Exception {
  TimeOut timeOut = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
  while (!timeOut.hasTimedOut()) {
    byte[] data = zkClient().getData(SOLR_AUTOSCALING_CONF_PATH, null, null, true);
    ZkNodeProps loaded = ZkNodeProps.load(data);
    @SuppressWarnings({"rawtypes"})
    Map triggers = (Map) loaded.get("triggers");
    if (triggers != null && triggers.containsKey(".auto_add_replicas")) {
      @SuppressWarnings({"unchecked"})
      Map<String, Object> autoAddReplicasTrigger = (Map<String, Object>) triggers.get(".auto_add_replicas");
      assertNotNull(autoAddReplicasTrigger);
      @SuppressWarnings({"unchecked"})
      List<Map<String, Object>> actions = (List<Map<String, Object>>) autoAddReplicasTrigger.get("actions");
      assertNotNull(actions);
      assertEquals(2, actions.size());
      assertEquals("auto_add_replicas_plan", actions.get(0).get("name").toString());
      assertEquals("solr.AutoAddReplicasPlanAction", actions.get(0).get("class").toString());
      break;
    } else {
      Thread.sleep(300);
    }
  }
  if (timeOut.hasTimedOut()) {
    fail("Timeout waiting for .auto_add_replicas being created");
  }
}
 
Example #15
Source File: SimClusterStateProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public void createSystemCollection() throws IOException {
  try {

    synchronized (this) {
      if (colShardReplicaMap.containsKey(CollectionAdminParams.SYSTEM_COLL)) {
        return;
      }
    }
    String repFactor = String.valueOf(Math.min(3, liveNodes.size()));
    ZkNodeProps props = new ZkNodeProps(
        NAME, CollectionAdminParams.SYSTEM_COLL,
        REPLICATION_FACTOR, repFactor,
        OverseerCollectionMessageHandler.NUM_SLICES, "1",
        CommonAdminParams.WAIT_FOR_FINAL_STATE, "true");
    simCreateCollection(props, new NamedList());
    CloudUtil.waitForState(cloudManager, CollectionAdminParams.SYSTEM_COLL, 120, TimeUnit.SECONDS,
        CloudUtil.clusterShape(1, Integer.parseInt(repFactor), false, true));
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #16
Source File: ReplaceNodeCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static List<ZkNodeProps> getReplicasOfNode(String source, ClusterState state) {
  List<ZkNodeProps> sourceReplicas = new ArrayList<>();
  for (Map.Entry<String, DocCollection> e : state.getCollectionsMap().entrySet()) {
    for (Slice slice : e.getValue().getSlices()) {
      for (Replica replica : slice.getReplicas()) {
        if (source.equals(replica.getNodeName())) {
          ZkNodeProps props = new ZkNodeProps(
              COLLECTION_PROP, e.getKey(),
              SHARD_ID_PROP, slice.getName(),
              ZkStateReader.CORE_NAME_PROP, replica.getCoreName(),
              ZkStateReader.REPLICA_PROP, replica.getName(),
              ZkStateReader.REPLICA_TYPE, replica.getType().name(),
              ZkStateReader.LEADER_PROP, String.valueOf(replica.equals(slice.getLeader())),
              CoreAdminParams.NODE, source);
          sourceReplicas.add(props);
        }
      }
    }
  }
  return sourceReplicas;
}
 
Example #17
Source File: OverseerCollectionConfigSetProcessorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void issueCreateJob(Integer numberOfSlices,
    Integer replicationFactor, Integer maxShardsPerNode, List<String> createNodeList, boolean sendCreateNodeList, boolean createNodeSetShuffle) {
  Map<String,Object> propMap = Utils.makeMap(
      Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.CREATE.toLower(),
      ZkStateReader.REPLICATION_FACTOR, replicationFactor.toString(),
      "name", COLLECTION_NAME,
      "collection.configName", CONFIG_NAME,
      OverseerCollectionMessageHandler.NUM_SLICES, numberOfSlices.toString(),
      ZkStateReader.MAX_SHARDS_PER_NODE, maxShardsPerNode.toString()
  );
  if (sendCreateNodeList) {
    propMap.put(OverseerCollectionMessageHandler.CREATE_NODE_SET,
        (createNodeList != null)?StrUtils.join(createNodeList, ','):null);
    if (OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE_DEFAULT != createNodeSetShuffle || random().nextBoolean()) {
      propMap.put(OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE, createNodeSetShuffle);
    }
  }

  ZkNodeProps props = new ZkNodeProps(propMap);
  QueueEvent qe = new QueueEvent("id", Utils.toJSON(props), null){
    @Override
    public void setBytes(byte[] bytes) {
      lastProcessMessageResult = OverseerSolrResponseSerializer.deserialize(bytes);
    }
  };
  queue.add(qe);
}
 
Example #18
Source File: ZooKeeperDownloader.java    From kite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 */
public String readConfigName(SolrZkClient zkClient, String collection)
throws KeeperException, InterruptedException {
  if (collection == null) {
    throw new IllegalArgumentException("collection must not be null");
  }
  String configName = null;

  // first check for alias
  byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
  Aliases aliases = ClusterState.load(aliasData);
  String alias = aliases.getCollectionAlias(collection);
  if (alias != null) {
    List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
    if (aliasList.size() > 1) {
      throw new IllegalArgumentException("collection cannot be an alias that maps to multiple collections");
    }
    collection = aliasList.get(0);
  }
  
  String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
  if (LOG.isInfoEnabled()) {
    LOG.info("Load collection config from:" + path);
  }
  byte[] data = zkClient.getData(path, null, null, true);
  
  if(data != null) {
    ZkNodeProps props = ZkNodeProps.load(data);
    configName = props.getStr(ZkController.CONFIGNAME_PROP);
  }
  
  if (configName != null && !zkClient.exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName, true)) {
    LOG.error("Specified config does not exist in ZooKeeper:" + configName);
    throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:"
      + configName);
  }

  return configName;
}
 
Example #19
Source File: DeleteReplicaCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
void deleteReplica(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results, Runnable onComplete)
        throws KeeperException, InterruptedException {
  if (log.isDebugEnabled()) {
    log.debug("deleteReplica() : {}", Utils.toJSONString(message));
  }
  boolean parallel = message.getBool("parallel", false);

  //If a count is specified the strategy needs be different
  if (message.getStr(COUNT_PROP) != null) {
    deleteReplicaBasedOnCount(clusterState, message, results, onComplete, parallel);
    return;
  }


  ocmh.checkRequired(message, COLLECTION_PROP, SHARD_ID_PROP, REPLICA_PROP);
  String extCollectionName = message.getStr(COLLECTION_PROP);
  String shard = message.getStr(SHARD_ID_PROP);
  String replicaName = message.getStr(REPLICA_PROP);

  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String collectionName;
  if (followAliases) {
    collectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extCollectionName);
  } else {
    collectionName = extCollectionName;
  }

  DocCollection coll = clusterState.getCollection(collectionName);
  Slice slice = coll.getSlice(shard);
  if (slice == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Invalid shard name : " +  shard + " in collection : " +  collectionName);
  }

  deleteCore(slice, collectionName, replicaName, message, shard, results, onComplete,  parallel);

}
 
Example #20
Source File: SetAliasPropCmd.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 {
  String aliasName = message.getStr(NAME);

  final ZkStateReader.AliasesManager aliasesManager = messageHandler.zkStateReader.aliasesManager;

  // Ensure we see the alias.  This may be redundant but SetAliasPropCmd isn't expected to be called very frequently
  aliasesManager.update();

  if (aliasesManager.getAliases().getCollectionAliasMap().get(aliasName) == null) {
    // nicer than letting aliases object throw later on...
    throw new SolrException(BAD_REQUEST,
        String.format(Locale.ROOT,  "Can't modify non-existent alias %s", aliasName));
  }

  @SuppressWarnings("unchecked")
  Map<String, String> properties = new LinkedHashMap<>((Map<String, String>) message.get(PROPERTIES));

  // check & cleanup properties.  It's a mutable copy.
  for (Map.Entry<String, String> entry : properties.entrySet()) {
    String key = entry.getKey();
    if ("".equals(key.trim())) {
      throw new SolrException(BAD_REQUEST, "property keys must not be pure whitespace");
    }
    if (!key.equals(key.trim())) {
      throw new SolrException(BAD_REQUEST, "property keys should not begin or end with whitespace");
    }
    String value = entry.getValue();
    if ("".equals(value)) {
      entry.setValue(null);
    }
  }

  aliasesManager.applyModificationAndExportToZk(aliases1 -> aliases1.cloneWithCollectionAliasProperties(aliasName, properties));
}
 
Example #21
Source File: TestCollectionAPIs.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void assertMapEqual(@SuppressWarnings({"rawtypes"})Map expected, ZkNodeProps actual) {
  assertEquals(errorMessage(expected, actual), expected.size(), actual.getProperties().size());
  for (Object o : expected.entrySet()) {
    @SuppressWarnings({"rawtypes"})
    Map.Entry e = (Map.Entry) o;
    Object actualVal = actual.get((String) e.getKey());
    if (actualVal instanceof String[]) {
      actualVal = Arrays.asList((String[]) actualVal);
    }
    assertEquals(errorMessage(expected, actual), String.valueOf(e.getValue()), String.valueOf(actualVal));
  }
}
 
Example #22
Source File: CreateCollectionCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void checkReplicaTypes(ZkNodeProps message) {
  int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
  int numNrtReplicas = message.getInt(NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas > 0 ? 0 : 1));

  if (numNrtReplicas + numTlogReplicas <= 0) {
    throw new SolrException(ErrorCode.BAD_REQUEST, NRT_REPLICAS + " + " + TLOG_REPLICAS + " must be greater than 0");
  }
}
 
Example #23
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes this command from the client.  If there's a problem it will throw an exception.
 * Please note that is important to never add async to this invocation. This method must
 * block (up to the standard OCP timeout) to prevent large batches of add's from sending a message
 * to the overseer for every document added in RoutedAliasUpdateProcessor.
 */
static void remoteInvoke(CollectionsHandler collHandler, String aliasName, String targetCol)
    throws Exception {
  final String operation = CollectionParams.CollectionAction.MAINTAINROUTEDALIAS.toLower();
  Map<String, Object> msg = new HashMap<>();
  msg.put(Overseer.QUEUE_OPERATION, operation);
  msg.put(CollectionParams.NAME, aliasName);
  msg.put(MaintainRoutedAliasCmd.ROUTED_ALIAS_TARGET_COL, targetCol);
  final SolrResponse rsp = collHandler.sendToOCPQueue(new ZkNodeProps(msg));
  if (rsp.getException() != null) {
    throw rsp.getException();
  }
}
 
Example #24
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void deleteTargetCollection(ClusterState clusterState, @SuppressWarnings({"rawtypes"})NamedList results, String aliasName, ZkStateReader.AliasesManager aliasesManager, RoutedAlias.Action action) throws Exception {
  Map<String, Object> delProps = new HashMap<>();
  delProps.put(INVOKED_BY_ROUTED_ALIAS,
      (Runnable) () -> removeCollectionFromAlias(aliasName, aliasesManager, action.targetCollection));
  delProps.put(NAME, action.targetCollection);
  ZkNodeProps messageDelete = new ZkNodeProps(delProps);
  new DeleteCollectionCmd(ocmh).call(clusterState, messageDelete, results);
}
 
Example #25
Source File: LeaderElectionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic() throws Exception {
  LeaderElector elector = new LeaderElector(zkClient);
  ZkNodeProps props = new ZkNodeProps(ZkStateReader.BASE_URL_PROP,
      "http://127.0.0.1/solr/", ZkStateReader.CORE_NAME_PROP, "");
  ZkController zkController = MockSolrSource.makeSimpleMock(null, null, zkClient);
  ElectionContext context = new ShardLeaderElectionContextBase(elector,
      "shard2", "collection1", "dummynode1", props, zkController);
  elector.setup(context);
  elector.joinElection(context, false);
  assertEquals("http://127.0.0.1/solr/",
      getLeaderUrl("collection1", "shard2"));
}
 
Example #26
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void deleteCoreNode(String collectionName, String replicaName, Replica replica, String core) throws Exception {
  ZkNodeProps m = new ZkNodeProps(
      Overseer.QUEUE_OPERATION, OverseerAction.DELETECORE.toLower(),
      ZkStateReader.CORE_NAME_PROP, core,
      ZkStateReader.NODE_NAME_PROP, replica.getStr(ZkStateReader.NODE_NAME_PROP),
      ZkStateReader.COLLECTION_PROP, collectionName,
      ZkStateReader.CORE_NODE_NAME_PROP, replicaName,
      ZkStateReader.BASE_URL_PROP, replica.getStr(ZkStateReader.BASE_URL_PROP));
  overseer.offerStateUpdate(Utils.toJSON(m));
}
 
Example #27
Source File: OverseerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String showQpeek(ZkDistributedQueue q) throws KeeperException, InterruptedException {
  if (q == null) {
    return "";
  }
  byte[] bytes = q.peek();
  if (bytes == null) {
    return "";
  }

  ZkNodeProps json = ZkNodeProps.load(bytes);
  return json.toString();
}
 
Example #28
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void addPropertyParams(ZkNodeProps message, ModifiableSolrParams params) {
  // Now add the property.key=value pairs
  for (String key : message.keySet()) {
    if (key.startsWith(COLL_PROP_PREFIX)) {
      params.set(key, message.getStr(key));
    }
  }
}
 
Example #29
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void balanceProperty(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  if (StringUtils.isBlank(message.getStr(COLLECTION_PROP)) || StringUtils.isBlank(message.getStr(PROPERTY_PROP))) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
        "The '" + COLLECTION_PROP + "' and '" + PROPERTY_PROP +
            "' parameters are required for the BALANCESHARDUNIQUE operation, no action taken");
  }
  SolrZkClient zkClient = zkStateReader.getZkClient();
  Map<String, Object> m = new HashMap<>();
  m.put(Overseer.QUEUE_OPERATION, BALANCESHARDUNIQUE.toLower());
  m.putAll(message.getProperties());
  overseer.offerStateUpdate(Utils.toJSON(m));
}
 
Example #30
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void addPropertyParams(ZkNodeProps message, Map<String, Object> map) {
  // Now add the property.key=value pairs
  for (String key : message.keySet()) {
    if (key.startsWith(COLL_PROP_PREFIX)) {
      map.put(key, message.getStr(key));
    }
  }
}