Java Code Examples for org.elasticsearch.cluster.routing.ShardRouting#shardId()

The following examples show how to use org.elasticsearch.cluster.routing.ShardRouting#shardId() . 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: TestGatewayAllocator.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected AsyncShardFetch.FetchResult<NodeGatewayStartedShards> fetchData(ShardRouting shard, RoutingAllocation allocation) {
    // for now always return immediately what we know
    final ShardId shardId = shard.shardId();
    final Set<String> ignoreNodes = allocation.getIgnoreNodes(shardId);
    Map<DiscoveryNode, NodeGatewayStartedShards> foundShards = knownAllocations.values().stream()
        .flatMap(shardMap -> shardMap.values().stream())
        .filter(ks -> ks.shardId().equals(shardId))
        .filter(ks -> ignoreNodes.contains(ks.currentNodeId()) == false)
        .filter(ks -> currentNodes.nodeExists(ks.currentNodeId()))
        .collect(Collectors.toMap(
            routing -> currentNodes.get(routing.currentNodeId()),
            routing ->
                new NodeGatewayStartedShards(
                    currentNodes.get(routing.currentNodeId()), routing.allocationId().getId(), routing.primary())));

    return new AsyncShardFetch.FetchResult<>(shardId, foundShards, ignoreNodes);
}
 
Example 2
Source File: TransportBroadcastByNodeAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void onShardOperation(final NodeRequest request, final Object[] shardResults, final int shardIndex, final ShardRouting shardRouting) {
    try {
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  executing operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
        ShardOperationResult result = shardOperation(request.indicesLevelRequest, shardRouting);
        shardResults[shardIndex] = result;
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  completed operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
    } catch (Throwable t) {
        BroadcastShardOperationFailedException e = new BroadcastShardOperationFailedException(shardRouting.shardId(), "operation " + actionName + " failed", t);
        e.setIndex(shardRouting.getIndex());
        e.setShard(shardRouting.shardId());
        shardResults[shardIndex] = e;
        if (TransportActions.isShardNotAvailableException(t)) {
            if (logger.isTraceEnabled()) {
                logger.trace("[{}] failed to execute operation for shard [{}]", t, actionName, shardRouting.shortSummary());
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("[{}] failed to execute operation for shard [{}]", t, actionName, shardRouting.shortSummary());
            }
        }
    }
}
 
Example 3
Source File: IndicesClusterStateService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Notifies master about shards that don't exist but are supposed to be active on this node.
 *
 * @param state new cluster state
 */
private void failMissingShards(final ClusterState state) {
    RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (localRoutingNode == null) {
        return;
    }
    for (final ShardRouting shardRouting : localRoutingNode) {
        ShardId shardId = shardRouting.shardId();
        if (shardRouting.initializing() == false &&
            failedShardsCache.containsKey(shardId) == false &&
            indicesService.getShardOrNull(shardId) == null) {
            // the master thinks we are active, but we don't have this shard at all, mark it as failed
            sendFailShard(shardRouting, "master marked shard as active, but shard has not been created, mark shard as failed", null,
                state);
        }
    }
}
 
Example 4
Source File: TransportBroadcastByNodeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
private void onShardOperation(final NodeRequest request, final Object[] shardResults, final int shardIndex, final ShardRouting shardRouting) {
    try {
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  executing operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
        ShardOperationResult result = shardOperation(request.indicesLevelRequest, shardRouting);
        shardResults[shardIndex] = result;
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  completed operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
    } catch (Exception e) {
        BroadcastShardOperationFailedException failure =
            new BroadcastShardOperationFailedException(shardRouting.shardId(), "operation " + actionName + " failed", e);
        failure.setShard(shardRouting.shardId());
        shardResults[shardIndex] = failure;
        if (TransportActions.isShardNotAvailableException(e)) {
            if (logger.isTraceEnabled()) {
                logger.trace(new ParameterizedMessage(
                    "[{}] failed to execute operation for shard [{}]", actionName, shardRouting.shortSummary()), e);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(new ParameterizedMessage(
                    "[{}] failed to execute operation for shard [{}]", actionName, shardRouting.shortSummary()), e);
            }
        }
    }
}
 
Example 5
Source File: GatewayAllocator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected AsyncShardFetch.FetchResult<TransportNodesListGatewayStartedShards.NodeGatewayStartedShards> fetchData(ShardRouting shard, RoutingAllocation allocation) {
    AsyncShardFetch<TransportNodesListGatewayStartedShards.NodeGatewayStartedShards> fetch = asyncFetchStarted.get(shard.shardId());
    if (fetch == null) {
        fetch = new InternalAsyncFetch<>(logger, "shard_started", shard.shardId(), startedAction);
        asyncFetchStarted.put(shard.shardId(), fetch);
    }
    AsyncShardFetch.FetchResult<TransportNodesListGatewayStartedShards.NodeGatewayStartedShards> shardState =
            fetch.fetchData(allocation.nodes(), allocation.metaData(), allocation.getIgnoreNodes(shard.shardId()));

    if (shardState.hasData() == true) {
        shardState.processAllocation(allocation);
    }
    return shardState;
}
 
Example 6
Source File: TransportUpgradeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardUpgradeResult shardOperation(UpgradeRequest request, ShardRouting shardRouting) throws IOException {
    IndexShard indexShard = indicesService.indexServiceSafe(shardRouting.shardId().getIndex()).getShard(shardRouting.shardId().id());
    org.apache.lucene.util.Version oldestLuceneSegment = indexShard.upgrade(request);
    // We are using the current version of Elasticsearch as upgrade version since we update mapping to match the current version
    return new ShardUpgradeResult(shardRouting.shardId(), indexShard.routingEntry().primary(), Version.CURRENT, oldestLuceneSegment);
}
 
Example 7
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * creates a new initializing shard. The shard will will be put in its proper path under the
 * current node id the shard is assigned to.
 * @param routing                shard routing to use
 * @param indexMetaData          indexMetaData for the shard, including any mapping
 * @param indexSearcherWrapper   an optional wrapper to be used during searchers
 * @param globalCheckpointSyncer callback for syncing global checkpoints
 * @param listeners              an optional set of listeners to add to the shard
 */
protected IndexShard newShard(ShardRouting routing, IndexMetaData indexMetaData,
                              @Nullable IndexSearcherWrapper indexSearcherWrapper,
                              @Nullable EngineFactory engineFactory,
                              Runnable globalCheckpointSyncer,
                              IndexingOperationListener... listeners)
    throws IOException {
    // add node id as name to settings for proper logging
    final ShardId shardId = routing.shardId();
    final NodeEnvironment.NodePath nodePath = new NodeEnvironment.NodePath(createTempDir());
    ShardPath shardPath = new ShardPath(false, nodePath.resolve(shardId), nodePath.resolve(shardId), shardId);
    return newShard(routing, shardPath, indexMetaData, null, indexSearcherWrapper, engineFactory, globalCheckpointSyncer,
        EMPTY_EVENT_LISTENER, listeners);
}
 
Example 8
Source File: TransportUpgradeAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardUpgradeResult shardOperation(UpgradeRequest request, ShardRouting shardRouting) throws IOException {
    IndexShard indexShard = indicesService.indexServiceSafe(shardRouting.shardId().getIndex()).shardSafe(shardRouting.shardId().id());
    org.apache.lucene.util.Version oldestLuceneSegment = indexShard.upgrade(request);
    // We are using the current version of Elasticsearch as upgrade version since we update mapping to match the current version
    return new ShardUpgradeResult(shardRouting.shardId(), indexShard.routingEntry().primary(), Version.CURRENT, oldestLuceneSegment);
}
 
Example 9
Source File: ShardStateAction.java    From crate with Apache License 2.0 5 votes vote down vote up
public void shardStarted(final ShardRouting shardRouting,
                         final String message,
                         ActionListener<Void> listener,
                         ClusterState currentState) {
    StartedShardEntry shardEntry = new StartedShardEntry(shardRouting.shardId(), shardRouting.allocationId().getId(), message);
    sendShardAction(SHARD_STARTED_ACTION_NAME, currentState, shardEntry, listener);
}
 
Example 10
Source File: TransportIndicesShardStoresAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private IndicesShardStoresResponse.StoreStatus.Allocation getAllocation(String index, int shardID, DiscoveryNode node) {
    for (ShardRouting shardRouting : routingNodes.node(node.id())) {
        ShardId shardId = shardRouting.shardId();
        if (shardId.id() == shardID && shardId.getIndex().equals(index)) {
            if (shardRouting.primary()) {
                return IndicesShardStoresResponse.StoreStatus.Allocation.PRIMARY;
            } else if (shardRouting.assignedToNode()) {
                return IndicesShardStoresResponse.StoreStatus.Allocation.REPLICA;
            } else {
                return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED;
            }
        }
    }
    return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED;
}
 
Example 11
Source File: ShardStateAction.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Send a shard failed request to the master node to update the cluster state when a shard on the local node failed.
 */
public void localShardFailed(final ShardRouting shardRouting,
                             final String message,
                             @Nullable final Exception failure,
                             ActionListener<Void> listener,
                             final ClusterState currentState) {
    FailedShardEntry shardEntry = new FailedShardEntry(shardRouting.shardId(), shardRouting.allocationId().getId(), 0L, message, failure, true);
    sendShardAction(SHARD_FAILED_ACTION_NAME, currentState, shardEntry, listener);
}
 
Example 12
Source File: ShardSearchLocalRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
ShardSearchLocalRequest(SearchRequest searchRequest, ShardRouting shardRouting, int numberOfShards,
                        String[] filteringAliases, long nowInMillis) {
    this(shardRouting.shardId(), numberOfShards, searchRequest.searchType(),
            searchRequest.source(), searchRequest.types(), searchRequest.requestCache());
    this.extraSource = searchRequest.extraSource();
    this.templateSource = searchRequest.templateSource();
    this.template = searchRequest.template();
    this.scroll = searchRequest.scroll();
    this.filteringAliases = filteringAliases;
    this.nowInMillis = nowInMillis;
    copyContextAndHeadersFrom(searchRequest);
}
 
Example 13
Source File: Get.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public ExecutionPlan build(PlannerContext plannerContext,
                           ProjectionBuilder projectionBuilder,
                           int limitHint,
                           int offsetHint,
                           @Nullable OrderBy order,
                           @Nullable Integer pageSizeHint,
                           Row params,
                           SubQueryResults subQueryResults) {
    HashMap<String, Map<ShardId, List<PKAndVersion>>> idsByShardByNode = new HashMap<>();
    DocTableInfo docTableInfo = tableRelation.tableInfo();
    List<Symbol> boundOutputs = Lists2.map(
        outputs, s -> SubQueryAndParamBinder.convert(s, params, subQueryResults));
    for (DocKeys.DocKey docKey : docKeys) {
        String id = docKey.getId(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        if (id == null) {
            continue;
        }
        List<String> partitionValues = docKey.getPartitionValues(
            plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        String indexName = indexName(docTableInfo, partitionValues);

        String routing = docKey.getRouting(
            plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        ShardRouting shardRouting;
        try {
            shardRouting = plannerContext.resolveShard(indexName, id, routing);
        } catch (IndexNotFoundException e) {
            if (docTableInfo.isPartitioned()) {
                continue;
            }
            throw e;
        }
        String currentNodeId = shardRouting.currentNodeId();
        if (currentNodeId == null) {
            // If relocating is fast enough this will work, otherwise it will result in a shard failure which
            // will cause a statement retry
            currentNodeId = shardRouting.relocatingNodeId();
            if (currentNodeId == null) {
                throw new ShardNotFoundException(shardRouting.shardId());
            }
        }
        Map<ShardId, List<PKAndVersion>> idsByShard = idsByShardByNode.get(currentNodeId);
        if (idsByShard == null) {
            idsByShard = new HashMap<>();
            idsByShardByNode.put(currentNodeId, idsByShard);
        }
        List<PKAndVersion> pkAndVersions = idsByShard.get(shardRouting.shardId());
        if (pkAndVersions == null) {
            pkAndVersions = new ArrayList<>();
            idsByShard.put(shardRouting.shardId(), pkAndVersions);
        }
        long version = docKey
            .version(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(Versions.MATCH_ANY);
        long sequenceNumber = docKey.sequenceNo(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(SequenceNumbers.UNASSIGNED_SEQ_NO);
        long primaryTerm = docKey.primaryTerm(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
        pkAndVersions.add(new PKAndVersion(id, version, sequenceNumber, primaryTerm));
    }
    return new Collect(
        new PKLookupPhase(
            plannerContext.jobId(),
            plannerContext.nextExecutionPhaseId(),
            docTableInfo.partitionedBy(),
            boundOutputs,
            idsByShardByNode
        ),
        TopN.NO_LIMIT,
        0,
        boundOutputs.size(),
        docKeys.size(),
        null
    );
}
 
Example 14
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new {@link TermsByQueryShardRequest}
 */
@Override
protected TermsByQueryShardRequest newShardRequest(int numShards, ShardRouting shard, TermsByQueryRequest request) {
  String[] filteringAliases = indexNameExpressionResolver.filteringAliases(clusterService.state(), shard.index(), request.indices());
  return new TermsByQueryShardRequest(shard.shardId(), filteringAliases, request);
}
 
Example 15
Source File: ShardDfsOnlyRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
ShardDfsOnlyRequest(ShardRouting shardRouting, int numberOfShards, @Nullable String[] filteringAliases, long nowInMillis, DfsOnlyRequest request) {
    super(shardRouting.shardId(), request);
    this.shardSearchRequest = new ShardSearchTransportRequest(request.getSearchRequest(), shardRouting, numberOfShards,
            filteringAliases, nowInMillis);
}
 
Example 16
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardSuggestRequest newShardRequest(int numShards, ShardRouting shard, SuggestRequest request) {
    return new ShardSuggestRequest(shard.shardId(), request);
}
 
Example 17
Source File: TransportFieldStatsTransportAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected FieldStatsShardRequest newShardRequest(int numShards, ShardRouting shard, FieldStatsRequest request) {
    return new FieldStatsShardRequest(shard.shardId(), request);
}
 
Example 18
Source File: TransportValidateQueryAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardValidateQueryRequest newShardRequest(int numShards, ShardRouting shard, ValidateQueryRequest request) {
    String[] filteringAliases = indexNameExpressionResolver.filteringAliases(clusterService.state(), shard.index(), request.indices());
    return new ShardValidateQueryRequest(shard.shardId(), filteringAliases, request);
}
 
Example 19
Source File: TransportExistsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardExistsRequest newShardRequest(int numShards, ShardRouting shard, ExistsRequest request) {
    String[] filteringAliases = indexNameExpressionResolver.filteringAliases(clusterService.state(), shard.index(), request.indices());
    return new ShardExistsRequest(shard.shardId(), filteringAliases, request);
}
 
Example 20
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the shards routing entry. This mutate the shards internal state depending
 * on the changes that get introduced by the new routing value. This method will persist shard level metadata
 * unless explicitly disabled.
 */
public void updateRoutingEntry(final ShardRouting newRouting, final boolean persistState) {
    final ShardRouting currentRouting = this.shardRouting;
    if (!newRouting.shardId().equals(shardId())) {
        throw new IllegalArgumentException("Trying to set a routing entry with shardId [" + newRouting.shardId() + "] on a shard with" +
                " shardId [" + shardId() + "]");
    }
    if ((currentRouting == null || newRouting.isSameAllocation(currentRouting)) == false) {
        throw new IllegalArgumentException("Trying to set a routing entry with a different allocation. Current " + currentRouting +
                ", new " + newRouting);
    }
    try {
        if (engineUnsafe() != null) {
            if (engineUnsafe() instanceof DLBasedEngine) {
                ((DLBasedEngine)engineUnsafe()).setIsPrimaryInRouting(newRouting.primary());
            }
        }
        if (currentRouting != null) {
            if (!newRouting.primary() && currentRouting.primary()) {
                logger.warn("suspect illegal state: trying to move shard from primary mode to replica mode");
            }
            // if its the same routing except for some metadata info, return
            if (currentRouting.equalsIgnoringMetaData(newRouting)) {
                this.shardRouting = newRouting; // might have a new version
                return;
            }
        }

        if (state == IndexShardState.POST_RECOVERY) {
            // if the state is started or relocating (cause it might move right away from started to relocating)
            // then move to STARTED
            if (newRouting.state() == ShardRoutingState.STARTED || newRouting.state() == ShardRoutingState.RELOCATING) {
                // we want to refresh *before* we move to internal STARTED state
                try {
                    engine().refresh("cluster_state_started");
                } catch (Throwable t) {
                    logger.debug("failed to refresh due to move to cluster wide started", t);
                }

                boolean movedToStarted = false;
                synchronized (mutex) {
                    // do the check under a mutex, so we make sure to only change to STARTED if in POST_RECOVERY
                    if (state == IndexShardState.POST_RECOVERY) {
                        changeState(IndexShardState.STARTED, "global state is [" + newRouting.state() + "]");
                        movedToStarted = true;
                    } else {
                        logger.debug("state [{}] not changed, not in POST_RECOVERY, global state is [{}]", state, newRouting.state());
                    }
                }
                if (movedToStarted) {
                    indicesLifecycle.afterIndexShardStarted(this);
                }
            }
        }
        this.shardRouting = newRouting;
        indicesLifecycle.shardRoutingChanged(this, currentRouting, newRouting);
    } finally {
        if (persistState) {
            persistMetadata(newRouting, currentRouting);
        }
    }
}