Java Code Examples for org.apache.solr.common.cloud.ZkNodeProps#get()

The following examples show how to use org.apache.solr.common.cloud.ZkNodeProps#get() . 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: 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 2
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void checkRequired(ZkNodeProps message, String... props) {
  for (String prop : props) {
    if(message.get(prop) == null){
      throw new SolrException(ErrorCode.BAD_REQUEST, StrUtils.join(Arrays.asList(props),',') +" are required params" );
    }
  }

}
 
Example 3
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 4
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 5
Source File: CollectionsHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SolrResponse sendToOCPQueue(ZkNodeProps m, long timeout) throws KeeperException, InterruptedException {
  String operation = m.getStr(QUEUE_OPERATION);
  if (operation == null) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "missing key " + QUEUE_OPERATION);
  }
  if (m.get(ASYNC) != null) {

    String asyncId = m.getStr(ASYNC);

    if (asyncId.equals("-1")) {
      throw new SolrException(ErrorCode.BAD_REQUEST, "requestid can not be -1. It is reserved for cleanup purposes.");
    }

    NamedList<String> r = new NamedList<>();


    if (coreContainer.getZkController().claimAsyncId(asyncId)) {
      boolean success = false;
      try {
        coreContainer.getZkController().getOverseerCollectionQueue()
            .offer(Utils.toJSON(m));
        success = true;
      } finally {
        if (!success) {
          try {
            coreContainer.getZkController().clearAsyncId(asyncId);
          } catch (Exception e) {
            // let the original exception bubble up
            log.error("Unable to release async ID={}", asyncId, e);
            SolrZkClient.checkInterrupted(e);
          }
        }
      }
    } else {
      r.add("error", "Task with the same requestid already exists.");
    }
    r.add(CoreAdminParams.REQUESTID, (String) m.get(ASYNC));

    return new OverseerSolrResponse(r);
  }

  long time = System.nanoTime();
  QueueEvent event = coreContainer.getZkController()
      .getOverseerCollectionQueue()
      .offer(Utils.toJSON(m), timeout);
  if (event.getBytes() != null) {
    return OverseerSolrResponseSerializer.deserialize(event.getBytes());
  } else {
    if (System.nanoTime() - time >= TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS)) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection time out:" + timeout / 1000 + "s");
    } else if (event.getWatchedEvent() != null) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection error [Watcher fired on path: "
          + event.getWatchedEvent().getPath() + " state: "
          + event.getWatchedEvent().getState() + " type "
          + event.getWatchedEvent().getType() + "]");
    } else {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection unknown case");
    }
  }
}
 
Example 6
Source File: MigrateCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void call(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  String extSourceCollectionName = message.getStr("collection");
  String splitKey = message.getStr("split.key");
  String extTargetCollectionName = message.getStr("target.collection");
  int timeout = message.getInt("forward.timeout", 10 * 60) * 1000;

  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String sourceCollectionName;
  String targetCollectionName;
  if (followAliases) {
    sourceCollectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extSourceCollectionName);
    targetCollectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extTargetCollectionName);
  } else {
    sourceCollectionName = extSourceCollectionName;
    targetCollectionName = extTargetCollectionName;
  }

  DocCollection sourceCollection = clusterState.getCollection(sourceCollectionName);
  if (sourceCollection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown source collection: " + sourceCollectionName);
  }
  DocCollection targetCollection = clusterState.getCollection(targetCollectionName);
  if (targetCollection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown target collection: " + sourceCollectionName);
  }
  if (!(sourceCollection.getRouter() instanceof CompositeIdRouter)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Source collection must use a compositeId router");
  }
  if (!(targetCollection.getRouter() instanceof CompositeIdRouter)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Target collection must use a compositeId router");
  }

  if (splitKey == null || splitKey.trim().length() == 0)  {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The split.key cannot be null or empty");
  }

  CompositeIdRouter sourceRouter = (CompositeIdRouter) sourceCollection.getRouter();
  CompositeIdRouter targetRouter = (CompositeIdRouter) targetCollection.getRouter();
  Collection<Slice> sourceSlices = sourceRouter.getSearchSlicesSingle(splitKey, null, sourceCollection);
  if (sourceSlices.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "No active slices available in source collection: " + sourceCollection + "for given split.key: " + splitKey);
  }
  Collection<Slice> targetSlices = targetRouter.getSearchSlicesSingle(splitKey, null, targetCollection);
  if (targetSlices.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "No active slices available in target collection: " + targetCollection + "for given split.key: " + splitKey);
  }

  String asyncId = null;
  if (message.containsKey(ASYNC) && message.get(ASYNC) != null)
    asyncId = message.getStr(ASYNC);

  for (Slice sourceSlice : sourceSlices) {
    for (Slice targetSlice : targetSlices) {
      log.info("Migrating source shard: {} to target shard: {} for split.key = {}", sourceSlice, targetSlice, splitKey);
      migrateKey(clusterState, sourceCollection, sourceSlice, targetCollection, targetSlice, splitKey,
          timeout, results, asyncId, message);
    }
  }
}
 
Example 7
Source File: ClusterStateMutator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public ZkWriteCommand createCollection(ClusterState clusterState, ZkNodeProps message) {
  String cName = message.getStr(NAME);
  log.debug("building a new cName: {}", cName);
  if (clusterState.hasCollection(cName)) {
    log.warn("Collection {} already exists. exit", cName);
    return ZkStateWriter.NO_OP;
  }

  Map<String, Object> routerSpec = DocRouter.getRouterSpec(message);
  String routerName = routerSpec.get(NAME) == null ? DocRouter.DEFAULT_NAME : (String) routerSpec.get(NAME);
  DocRouter router = DocRouter.getDocRouter(routerName);

  Object messageShardsObj = message.get("shards");

  Map<String, Slice> slices;
  if (messageShardsObj instanceof Map) { // we are being explicitly told the slice data (e.g. coll restore)
    slices = Slice.loadAllFromMap(cName, (Map<String, Object>)messageShardsObj);
  } else {
    List<String> shardNames = new ArrayList<>();

    if (router instanceof ImplicitDocRouter) {
      getShardNames(shardNames, message.getStr("shards", DocRouter.DEFAULT_NAME));
    } else {
      int numShards = message.getInt(ZkStateReader.NUM_SHARDS_PROP, -1);
      if (numShards < 1)
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "numShards is a required parameter for 'compositeId' router");
      getShardNames(numShards, shardNames);
    }
    List<DocRouter.Range> ranges = router.partitionRange(shardNames.size(), router.fullRange());//maybe null

    slices = new LinkedHashMap<>();
    for (int i = 0; i < shardNames.size(); i++) {
      String sliceName = shardNames.get(i);

      Map<String, Object> sliceProps = new LinkedHashMap<>(1);
      sliceProps.put(Slice.RANGE, ranges == null ? null : ranges.get(i));

      slices.put(sliceName, new Slice(sliceName, null, sliceProps,cName));
    }
  }

  Map<String, Object> collectionProps = new HashMap<>();

  for (Map.Entry<String, Object> e : OverseerCollectionMessageHandler.COLLECTION_PROPS_AND_DEFAULTS.entrySet()) {
    Object val = message.get(e.getKey());
    if (val == null) {
      val = OverseerCollectionMessageHandler.COLLECTION_PROPS_AND_DEFAULTS.get(e.getKey());
    }
    if (val != null) collectionProps.put(e.getKey(), val);
  }
  collectionProps.put(DocCollection.DOC_ROUTER, routerSpec);

  if (message.getStr("fromApi") == null) {
    collectionProps.put("autoCreated", "true");
  }

  DocCollection newCollection = new DocCollection(cName, slices, collectionProps, router, -1);

  return new ZkWriteCommand(cName, newCollection);
}