Search in sources :

Example 6 with SolrZkClient

use of org.apache.solr.common.cloud.SolrZkClient in project lucene-solr by apache.

the class BackupCmd method copyIndexFiles.

private void copyIndexFiles(URI backupPath, ZkNodeProps request, NamedList results) throws Exception {
    String collectionName = request.getStr(COLLECTION_PROP);
    String backupName = request.getStr(NAME);
    String asyncId = request.getStr(ASYNC);
    String repoName = request.getStr(CoreAdminParams.BACKUP_REPOSITORY);
    ShardHandler shardHandler = ocmh.shardHandlerFactory.getShardHandler();
    Map<String, String> requestMap = new HashMap<>();
    String commitName = request.getStr(CoreAdminParams.COMMIT_NAME);
    Optional<CollectionSnapshotMetaData> snapshotMeta = Optional.empty();
    if (commitName != null) {
        SolrZkClient zkClient = ocmh.overseer.getZkController().getZkClient();
        snapshotMeta = SolrSnapshotManager.getCollectionLevelSnapshot(zkClient, collectionName, commitName);
        if (!snapshotMeta.isPresent()) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Snapshot with name " + commitName + " does not exist for collection " + collectionName);
        }
        if (snapshotMeta.get().getStatus() != SnapshotStatus.Successful) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Snapshot with name " + commitName + " for collection " + collectionName + " has not completed successfully. The status is " + snapshotMeta.get().getStatus());
        }
    }
    log.info("Starting backup of collection={} with backupName={} at location={}", collectionName, backupName, backupPath);
    Collection<String> shardsToConsider = Collections.emptySet();
    if (snapshotMeta.isPresent()) {
        shardsToConsider = snapshotMeta.get().getShards();
    }
    for (Slice slice : ocmh.zkStateReader.getClusterState().getCollection(collectionName).getActiveSlices()) {
        Replica replica = null;
        if (snapshotMeta.isPresent()) {
            if (!shardsToConsider.contains(slice.getName())) {
                log.warn("Skipping the backup for shard {} since it wasn't part of the collection {} when snapshot {} was created.", slice.getName(), collectionName, snapshotMeta.get().getName());
                continue;
            }
            replica = selectReplicaWithSnapshot(snapshotMeta.get(), slice);
        } else {
            // Note - Actually this can return a null value when there is no leader for this shard.
            replica = slice.getLeader();
            if (replica == null) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "No 'leader' replica available for shard " + slice.getName() + " of collection " + collectionName);
            }
        }
        String coreName = replica.getStr(CORE_NAME_PROP);
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set(CoreAdminParams.ACTION, CoreAdminParams.CoreAdminAction.BACKUPCORE.toString());
        params.set(NAME, slice.getName());
        params.set(CoreAdminParams.BACKUP_REPOSITORY, repoName);
        // note: index dir will be here then the "snapshot." + slice name
        params.set(CoreAdminParams.BACKUP_LOCATION, backupPath.toASCIIString());
        params.set(CORE_NAME_PROP, coreName);
        if (snapshotMeta.isPresent()) {
            params.set(CoreAdminParams.COMMIT_NAME, snapshotMeta.get().getName());
        }
        ocmh.sendShardRequest(replica.getNodeName(), params, shardHandler, asyncId, requestMap);
        log.debug("Sent backup request to core={} for backupName={}", coreName, backupName);
    }
    log.debug("Sent backup requests to all shard leaders for backupName={}", backupName);
    ocmh.processResponses(results, shardHandler, true, "Could not backup all replicas", asyncId, requestMap);
}
Also used : HashMap(java.util.HashMap) Slice(org.apache.solr.common.cloud.Slice) ShardHandler(org.apache.solr.handler.component.ShardHandler) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Replica(org.apache.solr.common.cloud.Replica) SolrException(org.apache.solr.common.SolrException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CollectionSnapshotMetaData(org.apache.solr.core.snapshots.CollectionSnapshotMetaData)

Example 7 with SolrZkClient

use of org.apache.solr.common.cloud.SolrZkClient in project lucene-solr by apache.

the class DeleteCollectionCmd method call.

@Override
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
    ZkStateReader zkStateReader = ocmh.zkStateReader;
    final String collection = message.getStr(NAME);
    try {
        // Remove the snapshots meta-data for this collection in ZK. Deleting actual index files
        // should be taken care of as part of collection delete operation.
        SolrZkClient zkClient = zkStateReader.getZkClient();
        SolrSnapshotManager.cleanupCollectionLevelSnapshots(zkClient, collection);
        if (zkStateReader.getClusterState().getCollectionOrNull(collection) == null) {
            if (zkStateReader.getZkClient().exists(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection, true)) {
                // is not in the clusterstate
                return;
            }
        }
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set(CoreAdminParams.ACTION, CoreAdminParams.CoreAdminAction.UNLOAD.toString());
        params.set(CoreAdminParams.DELETE_INSTANCE_DIR, true);
        params.set(CoreAdminParams.DELETE_DATA_DIR, true);
        String asyncId = message.getStr(ASYNC);
        Map<String, String> requestMap = null;
        if (asyncId != null) {
            requestMap = new HashMap<>();
        }
        Set<String> okayExceptions = new HashSet<>(1);
        okayExceptions.add(NonExistentCoreException.class.getName());
        ocmh.collectionCmd(message, params, results, null, asyncId, requestMap, okayExceptions);
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, DELETE.toLower(), NAME, collection);
        Overseer.getStateUpdateQueue(zkStateReader.getZkClient()).offer(Utils.toJSON(m));
        // wait for a while until we don't see the collection
        TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS);
        boolean removed = false;
        while (!timeout.hasTimedOut()) {
            Thread.sleep(100);
            removed = !zkStateReader.getClusterState().hasCollection(collection);
            if (removed) {
                // just a bit of time so it's more likely other
                Thread.sleep(500);
                // readers see on return
                break;
            }
        }
        if (!removed) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Could not fully remove collection: " + collection);
        }
    } finally {
        try {
            if (zkStateReader.getZkClient().exists(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection, true)) {
                zkStateReader.getZkClient().clean(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection);
            }
        } catch (InterruptedException e) {
            SolrException.log(log, "Cleaning up collection in zk was interrupted:" + collection, e);
            Thread.currentThread().interrupt();
        } catch (KeeperException e) {
            SolrException.log(log, "Problem cleaning up collection in zk:" + collection, e);
        }
    }
}
Also used : TimeOut(org.apache.solr.util.TimeOut) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) NonExistentCoreException(org.apache.solr.common.NonExistentCoreException) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 8 with SolrZkClient

use of org.apache.solr.common.cloud.SolrZkClient in project lucene-solr by apache.

the class DeleteSnapshotCmd method call.

@Override
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
    String collectionName = message.getStr(COLLECTION_PROP);
    String commitName = message.getStr(CoreAdminParams.COMMIT_NAME);
    String asyncId = message.getStr(ASYNC);
    Map<String, String> requestMap = new HashMap<>();
    NamedList shardRequestResults = new NamedList();
    ShardHandler shardHandler = ocmh.shardHandlerFactory.getShardHandler();
    SolrZkClient zkClient = ocmh.overseer.getZkController().getZkClient();
    Optional<CollectionSnapshotMetaData> meta = SolrSnapshotManager.getCollectionLevelSnapshot(zkClient, collectionName, commitName);
    if (!meta.isPresent()) {
        // Snapshot not found. Nothing to do.
        return;
    }
    log.info("Deleting a snapshot for collection={} with commitName={}", collectionName, commitName);
    Set<String> existingCores = new HashSet<>();
    for (Slice s : ocmh.zkStateReader.getClusterState().getCollection(collectionName).getSlices()) {
        for (Replica r : s.getReplicas()) {
            existingCores.add(r.getCoreName());
        }
    }
    Set<String> coresWithSnapshot = new HashSet<>();
    for (CoreSnapshotMetaData m : meta.get().getReplicaSnapshots()) {
        if (existingCores.contains(m.getCoreName())) {
            coresWithSnapshot.add(m.getCoreName());
        }
    }
    log.info("Existing cores with snapshot for collection={} are {}", collectionName, existingCores);
    for (Slice slice : ocmh.zkStateReader.getClusterState().getCollection(collectionName).getSlices()) {
        for (Replica replica : slice.getReplicas()) {
            if (replica.getState() == State.DOWN) {
                // Since replica is down - no point sending a request.
                continue;
            }
            // replicas to contact at this point, we try on all replicas.
            if (meta.get().getStatus() == SnapshotStatus.InProgress || coresWithSnapshot.contains(replica.getCoreName())) {
                String coreName = replica.getStr(CORE_NAME_PROP);
                ModifiableSolrParams params = new ModifiableSolrParams();
                params.set(CoreAdminParams.ACTION, CoreAdminAction.DELETESNAPSHOT.toString());
                params.set(NAME, slice.getName());
                params.set(CORE_NAME_PROP, coreName);
                params.set(CoreAdminParams.COMMIT_NAME, commitName);
                log.info("Sending deletesnapshot request to core={} with commitName={}", coreName, commitName);
                ocmh.sendShardRequest(replica.getNodeName(), params, shardHandler, asyncId, requestMap);
            }
        }
    }
    ocmh.processResponses(shardRequestResults, shardHandler, false, null, asyncId, requestMap);
    NamedList success = (NamedList) shardRequestResults.get("success");
    List<CoreSnapshotMetaData> replicas = new ArrayList<>();
    if (success != null) {
        for (int i = 0; i < success.size(); i++) {
            NamedList resp = (NamedList) success.getVal(i);
            // Unfortunately async processing logic doesn't provide the "core" name automatically.
            String coreName = (String) resp.get("core");
            coresWithSnapshot.remove(coreName);
        }
    }
    if (!coresWithSnapshot.isEmpty()) {
        // One or more failures.
        log.warn("Failed to delete a snapshot for collection {} with commitName = {}. Snapshot could not be deleted for following cores {}", collectionName, commitName, coresWithSnapshot);
        List<CoreSnapshotMetaData> replicasWithSnapshot = new ArrayList<>();
        for (CoreSnapshotMetaData m : meta.get().getReplicaSnapshots()) {
            if (coresWithSnapshot.contains(m.getCoreName())) {
                replicasWithSnapshot.add(m);
            }
        }
        // Update the ZK meta-data to include only cores with the snapshot. This will enable users to figure out
        // which cores still contain the named snapshot.
        CollectionSnapshotMetaData newResult = new CollectionSnapshotMetaData(meta.get().getName(), SnapshotStatus.Failed, meta.get().getCreationDate(), replicasWithSnapshot);
        SolrSnapshotManager.updateCollectionLevelSnapshot(zkClient, collectionName, newResult);
        log.info("Saved snapshot information for collection={} with commitName={} in Zookeeper as follows", collectionName, commitName, Utils.toJSON(newResult));
        throw new SolrException(ErrorCode.SERVER_ERROR, "Failed to delete snapshot on cores " + coresWithSnapshot);
    } else {
        // Delete the ZK path so that we eliminate the references of this snapshot from collection level meta-data.
        SolrSnapshotManager.deleteCollectionLevelSnapshot(zkClient, collectionName, commitName);
        log.info("Deleted Zookeeper snapshot metdata for collection={} with commitName={}", collectionName, commitName);
        log.info("Successfully deleted snapshot for collection={} with commitName={}", collectionName, commitName);
    }
}
Also used : HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) ShardHandler(org.apache.solr.handler.component.ShardHandler) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Replica(org.apache.solr.common.cloud.Replica) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CollectionSnapshotMetaData(org.apache.solr.core.snapshots.CollectionSnapshotMetaData) Slice(org.apache.solr.common.cloud.Slice) CoreSnapshotMetaData(org.apache.solr.core.snapshots.CollectionSnapshotMetaData.CoreSnapshotMetaData) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 9 with SolrZkClient

use of org.apache.solr.common.cloud.SolrZkClient in project lucene-solr by apache.

the class OverseerCollectionMessageHandler method processReplicaDeletePropertyCommand.

private void processReplicaDeletePropertyCommand(ClusterState clusterState, ZkNodeProps message, NamedList results) throws KeeperException, InterruptedException {
    checkRequired(message, COLLECTION_PROP, SHARD_ID_PROP, REPLICA_PROP, PROPERTY_PROP);
    SolrZkClient zkClient = zkStateReader.getZkClient();
    DistributedQueue inQueue = Overseer.getStateUpdateQueue(zkClient);
    Map<String, Object> propMap = new HashMap<>();
    propMap.put(Overseer.QUEUE_OPERATION, DELETEREPLICAPROP.toLower());
    propMap.putAll(message.getProperties());
    ZkNodeProps m = new ZkNodeProps(propMap);
    inQueue.offer(Utils.toJSON(m));
}
Also used : HashMap(java.util.HashMap) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 10 with SolrZkClient

use of org.apache.solr.common.cloud.SolrZkClient in project lucene-solr by apache.

the class CdcrBufferStateManager method createStateNode.

private void createStateNode() {
    SolrZkClient zkClient = core.getCoreContainer().getZkController().getZkClient();
    try {
        if (!zkClient.exists(this.getZnodePath(), true)) {
            if (!zkClient.exists(this.getZnodeBase(), true)) {
                // Should be a no-op if node exists
                zkClient.makePath(this.getZnodeBase(), null, CreateMode.PERSISTENT, null, false, true);
            }
            zkClient.create(this.getZnodePath(), DEFAULT_STATE.getBytes(), CreateMode.PERSISTENT, true);
            log.info("Created znode {}", this.getZnodePath());
        }
    } catch (KeeperException.NodeExistsException ne) {
    // Someone got in first and created the node.
    } catch (KeeperException | InterruptedException e) {
        log.warn("Failed to create CDCR buffer state node", e);
    }
}
Also used : SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)130 Test (org.junit.Test)46 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)34 HashMap (java.util.HashMap)21 KeeperException (org.apache.zookeeper.KeeperException)18 SolrException (org.apache.solr.common.SolrException)15 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)14 IOException (java.io.IOException)13 ClusterState (org.apache.solr.common.cloud.ClusterState)13 DocCollection (org.apache.solr.common.cloud.DocCollection)12 Map (java.util.Map)11 Slice (org.apache.solr.common.cloud.Slice)11 Replica (org.apache.solr.common.cloud.Replica)10 ArrayList (java.util.ArrayList)9 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)8 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)8 Overseer (org.apache.solr.cloud.Overseer)8 ZkTestServer (org.apache.solr.cloud.ZkTestServer)8 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 NamedList (org.apache.solr.common.util.NamedList)7