All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.handler.admin.api.BalanceShardUnique Maven / Gradle / Ivy

There is a newer version: 9.7.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.handler.admin.api;

import static org.apache.solr.cloud.Overseer.QUEUE_OPERATION;
import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.ONLY_ACTIVE_NODES;
import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.SHARD_UNIQUE;
import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;
import static org.apache.solr.common.cloud.ZkStateReader.PROPERTY_PROP;
import static org.apache.solr.common.params.CollectionAdminParams.PROPERTY_PREFIX;
import static org.apache.solr.common.params.CommonAdminParams.ASYNC;
import static org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;

import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.solr.client.api.endpoint.BalanceShardUniqueApi;
import org.apache.solr.client.api.model.BalanceShardUniqueRequestBody;
import org.apache.solr.client.api.model.SubResponseAccumulatingJerseyResponse;
import org.apache.solr.cloud.overseer.SliceMutator;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.params.CollectionParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.handler.api.V2ApiUtils;
import org.apache.solr.jersey.PermissionName;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

/**
 * V2 API implementation for insuring that a particular property is distributed evenly amongst the
 * physical nodes comprising a collection.
 */
public class BalanceShardUnique extends AdminAPIBase implements BalanceShardUniqueApi {
  @Inject
  public BalanceShardUnique(
      CoreContainer coreContainer,
      SolrQueryRequest solrQueryRequest,
      SolrQueryResponse solrQueryResponse) {
    super(coreContainer, solrQueryRequest, solrQueryResponse);
  }

  @Override
  @PermissionName(COLL_EDIT_PERM)
  public SubResponseAccumulatingJerseyResponse balanceShardUnique(
      String collectionName, BalanceShardUniqueRequestBody requestBody) throws Exception {
    final var response = instantiateJerseyResponse(SubResponseAccumulatingJerseyResponse.class);
    if (requestBody == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing required request body");
    }
    ensureRequiredParameterProvided(COLLECTION_PROP, collectionName);
    ensureRequiredParameterProvided(PROPERTY_PROP, requestBody.property);
    validatePropertyToBalance(requestBody.property, Boolean.TRUE.equals(requestBody.shardUnique));
    fetchAndValidateZooKeeperAwareCoreContainer();
    recordCollectionForLogAndTracing(collectionName, solrQueryRequest);

    final ZkNodeProps remoteMessage = createRemoteMessage(collectionName, requestBody);
    submitRemoteMessageAndHandleResponse(
        response,
        CollectionParams.CollectionAction.BALANCESHARDUNIQUE,
        remoteMessage,
        requestBody.async);

    return response;
  }

  public static ZkNodeProps createRemoteMessage(
      String collectionName, BalanceShardUniqueRequestBody requestBody) {
    final Map remoteMessage = new HashMap<>();
    remoteMessage.put(
        QUEUE_OPERATION, CollectionParams.CollectionAction.BALANCESHARDUNIQUE.toLower());
    remoteMessage.put(COLLECTION_PROP, collectionName);
    remoteMessage.put(PROPERTY_PROP, requestBody.property);
    insertIfNotNull(remoteMessage, ONLY_ACTIVE_NODES, requestBody.onlyActiveNodes);
    insertIfNotNull(remoteMessage, SHARD_UNIQUE, requestBody.shardUnique);
    insertIfNotNull(remoteMessage, ASYNC, requestBody.async);

    return new ZkNodeProps(remoteMessage);
  }

  public static void invokeFromV1Params(
      CoreContainer coreContainer,
      SolrQueryRequest solrQueryRequest,
      SolrQueryResponse solrQueryResponse)
      throws Exception {
    final var api = new BalanceShardUnique(coreContainer, solrQueryRequest, solrQueryResponse);
    final SolrParams params = solrQueryRequest.getParams();
    params.required().check(COLLECTION_PROP, PROPERTY_PROP);

    final String collection = params.get(COLLECTION_PROP);
    final var requestBody = new BalanceShardUniqueRequestBody();
    requestBody.property = params.get(PROPERTY_PROP);
    requestBody.onlyActiveNodes = params.getBool(ONLY_ACTIVE_NODES);
    requestBody.shardUnique = params.getBool(SHARD_UNIQUE);
    requestBody.async = params.get(ASYNC);

    V2ApiUtils.squashIntoSolrResponseWithoutHeader(
        solrQueryResponse, api.balanceShardUnique(collection, requestBody));
  }

  private void validatePropertyToBalance(String prop, boolean shardUnique) {
    prop = prop.toLowerCase(Locale.ROOT);
    if (!prop.startsWith(PROPERTY_PREFIX)) {
      prop = PROPERTY_PREFIX + prop;
    }

    if (!shardUnique && !SliceMutator.SLICE_UNIQUE_BOOLEAN_PROPERTIES.contains(prop)) {
      throw new SolrException(
          SolrException.ErrorCode.BAD_REQUEST,
          "Balancing properties amongst replicas in a slice requires that"
              + " the property be pre-defined as a unique property (e.g. 'preferredLeader') or that 'shardUnique' be set to 'true'. "
              + " Property: "
              + prop
              + " shardUnique: "
              + shardUnique);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy