Java Code Examples for org.elasticsearch.cluster.ClusterState

The following examples show how to use org.elasticsearch.cluster.ClusterState. These examples are extracted from open source projects. 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 Project: crate   Author: crate   File: SnapshotRestoreAnalyzerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() throws IOException {
    RepositoriesMetaData repositoriesMetaData = new RepositoriesMetaData(
        Collections.singletonList(
            new RepositoryMetaData(
                "my_repo",
                "fs",
                Settings.builder().put("location", "/tmp/my_repo").build()
        )));
    ClusterState clusterState = ClusterState.builder(new ClusterName("testing"))
        .metaData(MetaData.builder()
            .putCustom(RepositoriesMetaData.TYPE, repositoriesMetaData))
        .build();
    ClusterServiceUtils.setState(clusterService, clusterState);
    e = SQLExecutor.builder(clusterService)
        .addTable(USER_TABLE_DEFINITION)
        .addTable(TEST_DOC_LOCATIONS_TABLE_DEFINITION)
        .addPartitionedTable(TEST_PARTITIONED_TABLE_DEFINITION, TEST_PARTITIONED_TABLE_PARTITIONS)
        .addBlobTable("create blob table my_blobs")
        .build();
    plannerContext = e.getPlannerContext(clusterService.state());
}
 
Example #2
Source Project: crate   Author: crate   File: Coordinator.java    License: Apache License 2.0 6 votes vote down vote up
private void cleanMasterService() {
    masterService.submitStateUpdateTask("clean-up after stepping down as master",
        new LocalClusterUpdateTask() {
            @Override
            public void onFailure(String source, Exception e) {
                // ignore
                LOGGER.trace("failed to clean-up after stepping down as master", e);
            }

            @Override
            public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) {
                if (currentState.nodes().isLocalNodeElectedMaster() == false) {
                    allocationService.cleanCaches();
                }
                return unchanged();
            }

        });
}
 
Example #3
Source Project: crate   Author: crate   File: AlterTableClusterStateExecutor.java    License: Apache License 2.0 6 votes vote down vote up
private ClusterState updateMapping(ClusterState currentState, AlterTableRequest request, Index[] concreteIndices) throws Exception {
    if (request.mappingDelta() == null) {
        return currentState;
    }
    Map<Index, MapperService> indexMapperServices = new HashMap<>();
    for (Index index : concreteIndices) {
        final IndexMetaData indexMetaData = currentState.metaData().getIndexSafe(index);
        if (indexMapperServices.containsKey(indexMetaData.getIndex()) == false) {
            MapperService mapperService = indicesService.createIndexMapperService(indexMetaData);
            indexMapperServices.put(index, mapperService);
            // add mappings for all types, we need them for cross-type validation
            mapperService.merge(indexMetaData, MapperService.MergeReason.MAPPING_RECOVERY, false);
        }
    }

    PutMappingClusterStateUpdateRequest updateRequest = new PutMappingClusterStateUpdateRequest()
        .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
        .indices(concreteIndices).type(Constants.DEFAULT_MAPPING_TYPE)
        .source(request.mappingDelta());

    return metaDataMappingService.putMappingExecutor.applyRequest(currentState, updateRequest, indexMapperServices);
}
 
Example #4
Source Project: crate   Author: crate   File: PublicationTests.java    License: Apache License 2.0 6 votes vote down vote up
public MockPublication publish(ClusterState clusterState, Discovery.AckListener ackListener, Set<DiscoveryNode> faultyNodes) {
    PublishRequest publishRequest = coordinationState.handleClientValue(clusterState);
    MockPublication currentPublication = new MockPublication(publishRequest, ackListener, () -> 0L) {
        @Override
        protected boolean isPublishQuorum(CoordinationState.VoteCollection votes) {
            return coordinationState.isPublishQuorum(votes);
        }

        @Override
        protected Optional<ApplyCommitRequest> handlePublishResponse(DiscoveryNode sourceNode, PublishResponse publishResponse) {
            return coordinationState.handlePublishResponse(sourceNode, publishResponse);
        }
    };
    currentPublication.start(faultyNodes);
    return currentPublication;
}
 
Example #5
Source Project: Elasticsearch   Author: baidu   File: TransportClearScrollAction.java    License: Apache License 2.0 6 votes vote down vote up
private Async(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener, ClusterState clusterState) {
    int expectedOps = 0;
    this.nodes = clusterState.nodes();
    if (request.getScrollIds().size() == 1 && "_all".equals(request.getScrollIds().get(0))) {
        expectedOps = nodes.size();
    } else {
        for (String parsedScrollId : request.getScrollIds()) {
            ScrollIdForNode[] context = parseScrollId(parsedScrollId).getContext();
            expectedOps += context.length;
            this.contexts.add(context);
        }
    }

    this.request = request;
    this.listener = listener;
    this.expHolder = new AtomicReference<>();
    this.expectedOps = new CountDown(expectedOps);
}
 
Example #6
Source Project: Elasticsearch   Author: baidu   File: TransportRecoveryAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected RecoveryResponse newResponse(RecoveryRequest request, int totalShards, int successfulShards, int failedShards, List<RecoveryState> responses, List<ShardOperationFailedException> shardFailures, ClusterState clusterState) {
    Map<String, List<RecoveryState>> shardResponses = Maps.newHashMap();
    for (RecoveryState recoveryState : responses) {
        if (recoveryState == null) {
            continue;
        }
        String indexName = recoveryState.getShardId().getIndex();
        if (!shardResponses.containsKey(indexName)) {
            shardResponses.put(indexName, new ArrayList<RecoveryState>());
        }
        if (request.activeOnly()) {
            if (recoveryState.getStage() != RecoveryState.Stage.DONE) {
                shardResponses.get(indexName).add(recoveryState);
            }
        } else {
            shardResponses.get(indexName).add(recoveryState);
        }
    }
    return new RecoveryResponse(totalShards, successfulShards, failedShards, request.detailed(), shardResponses, shardFailures);
}
 
Example #7
Source Project: crate   Author: crate   File: ESIntegTestCase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove any customs except for customs that we know all clients understand.
 *
 * @param clusterState the cluster state to remove possibly-unknown customs from
 * @return the cluster state with possibly-unknown customs removed
 */
private ClusterState removePluginCustoms(final ClusterState clusterState) {
    final ClusterState.Builder builder = ClusterState.builder(clusterState);
    clusterState.customs().keysIt().forEachRemaining(key -> {
        if (SAFE_CUSTOMS.contains(key) == false) {
            builder.removeCustom(key);
        }
    });
    final MetaData.Builder mdBuilder = MetaData.builder(clusterState.metaData());
    clusterState.metaData().customs().keysIt().forEachRemaining(key -> {
        if (SAFE_METADATA_CUSTOMS.contains(key) == false) {
            mdBuilder.removeCustom(key);
        }
    });
    builder.metaData(mdBuilder);
    return builder.build();
}
 
Example #8
Source Project: crate   Author: crate   File: CoordinationStateTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testJoinDoesNotWinElection() {
    VotingConfiguration initialConfig = new VotingConfiguration(Collections.singleton(node1.getId()));
    ClusterState state1 = clusterState(0L, 0L, node1, initialConfig, initialConfig, 42L);
    cs1.setInitialState(state1);

    StartJoinRequest startJoinRequest1 = new StartJoinRequest(node2, randomLongBetween(1, 5));
    cs1.handleStartJoin(startJoinRequest1);
    ClusterState state2 = clusterState(startJoinRequest1.getTerm(), randomLongBetween(2, 20), node1, initialConfig, initialConfig, 42L);
    cs1.handlePublishRequest(new PublishRequest(state2));
    StartJoinRequest startJoinRequest2 = new StartJoinRequest(node2, randomLongBetween(startJoinRequest1.getTerm() + 1, 10));
    Join v1 = cs1.handleStartJoin(startJoinRequest2);

    Join join = new Join(node2, node1, v1.getTerm(), randomLongBetween(0, state2.term()), randomLongBetween(0, state2.version()));
    assertTrue(cs1.handleJoin(join));
    assertFalse(cs1.electionWon());
    assertEquals(cs1.getLastPublishedVersion(), 0L);
    assertFalse(cs1.handleJoin(join));
}
 
Example #9
Source Project: crate   Author: crate   File: TransportCreateUserAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(CreateUserRequest request, ClusterState state, ActionListener<WriteUserResponse> listener) throws Exception {
    clusterService.submitStateUpdateTask("create_user [" + request.userName() + "]",
        new AckedClusterStateUpdateTask<WriteUserResponse>(Priority.URGENT, request, listener) {

            private boolean alreadyExists = false;

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                MetaData currentMetaData = currentState.metaData();
                MetaData.Builder mdBuilder = MetaData.builder(currentMetaData);
                alreadyExists = putUser(mdBuilder, request.userName(), request.secureHash());
                return ClusterState.builder(currentState).metaData(mdBuilder).build();
            }

            @Override
            protected WriteUserResponse newResponse(boolean acknowledged) {
                return new WriteUserResponse(acknowledged, alreadyExists);
            }
        });
}
 
Example #10
Source Project: Elasticsearch   Author: baidu   File: ZenDiscovery.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * In the case we follow an elected master the new cluster state needs to have the same elected master and
 * the new cluster state version needs to be equal or higher than our cluster state version.
 * If the first condition fails we reject the cluster state and throw an error.
 * If the second condition fails we ignore the cluster state.
 */
static boolean shouldIgnoreOrRejectNewClusterState(ESLogger logger, ClusterState currentState, ClusterState newClusterState) {
    if (currentState.nodes().masterNodeId() == null) {
        return false;
    }
    if (!currentState.nodes().masterNodeId().equals(newClusterState.nodes().masterNodeId())) {
        logger.warn("received a cluster state from a different master then the current one, rejecting (received {}, current {})", newClusterState.nodes().masterNode(), currentState.nodes().masterNode());
        throw new IllegalStateException("cluster state from a different master than the current one, rejecting (received " + newClusterState.nodes().masterNode() + ", current " + currentState.nodes().masterNode() + ")");
    } else if (newClusterState.version() < currentState.version()) {
        // if the new state has a smaller version, and it has the same master node, then no need to process it
        logger.debug("received a cluster state that has a lower version than the current one, ignoring (received {}, current {})", newClusterState.version(), currentState.version());
        return true;
    } else {
        return false;
    }
}
 
Example #11
Source Project: crate   Author: crate   File: TransportMasterNodeAction.java    License: Apache License 2.0 6 votes vote down vote up
private void retry(final Throwable failure, final Predicate<ClusterState> statePredicate) {
    observer.waitForNextChange(
        new ClusterStateObserver.Listener() {
            @Override
            public void onNewClusterState(ClusterState state) {
                doStart(state);
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new NodeClosedException(clusterService.localNode()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                logger.debug(() -> new ParameterizedMessage("timed out while retrying [{}] after failure (timeout [{}])",
                    actionName, timeout), failure);
                listener.onFailure(new MasterNotDiscoveredException(failure));
            }
        }, statePredicate
    );
}
 
Example #12
Source Project: crate   Author: crate   File: ClusterApplierService.java    License: Apache License 2.0 5 votes vote down vote up
public void runOnApplierThread(final String source, Consumer<ClusterState> clusterStateConsumer,
                               final ClusterApplyListener listener, Priority priority) {
    submitStateUpdateTask(source, ClusterStateTaskConfig.build(priority),
        (clusterState) -> {
            clusterStateConsumer.accept(clusterState);
            return clusterState;
        },
        listener);
}
 
Example #13
Source Project: crate   Author: crate   File: TransportDropTableAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected ClusterBlockException checkBlock(DropTableRequest request, ClusterState state) {
    IndicesOptions indicesOptions = INDICES_OPTIONS;
    if (request.isPartitioned()) {
        indicesOptions = IndicesOptions.lenientExpandOpen();
    }
    return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE,
        indexNameExpressionResolver.concreteIndexNames(state, indicesOptions, request.tableIdent().indexNameOrAlias()));
}
 
Example #14
Source Project: crate   Author: crate   File: DefaultTemplateServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAddDefaultTemplate() throws Exception {
    ClusterState state = ClusterState.builder(new ClusterName("foo")).build();

    ClusterState newState = DefaultTemplateService.addDefaultTemplate(state);
    assertThat(newState.getMetaData().templates().containsKey(DefaultTemplateService.TEMPLATE_NAME), is(true));

    // verify that it doesn't fail it the template already exists
    newState = DefaultTemplateService.addDefaultTemplate(newState);
    assertThat(newState.getMetaData().templates().containsKey(DefaultTemplateService.TEMPLATE_NAME), is(true));
}
 
Example #15
Source Project: crate   Author: crate   File: GatewayService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState execute(final ClusterState currentState) {
    final ClusterState newState = Function.<ClusterState>identity()
            .andThen(ClusterStateUpdaters::updateRoutingTable)
            .andThen(ClusterStateUpdaters::removeStateNotRecoveredBlock)
            .apply(currentState);

    return allocationService.reroute(newState, "state recovered");
}
 
Example #16
Source Project: crate   Author: crate   File: DiskThresholdDeciderTests.java    License: Apache License 2.0 5 votes vote down vote up
public void logShardStates(ClusterState state) {
    RoutingNodes rn = state.getRoutingNodes();
    logger.info("--> counts: total: {}, unassigned: {}, initializing: {}, relocating: {}, started: {}",
                rn.shards(shard -> true).size(),
                rn.shardsWithState(UNASSIGNED).size(),
                rn.shardsWithState(INITIALIZING).size(),
                rn.shardsWithState(RELOCATING).size(),
                rn.shardsWithState(STARTED).size());
    logger.info("--> unassigned: {}, initializing: {}, relocating: {}, started: {}",
                rn.shardsWithState(UNASSIGNED),
                rn.shardsWithState(INITIALIZING),
                rn.shardsWithState(RELOCATING),
                rn.shardsWithState(STARTED));
}
 
Example #17
Source Project: Elasticsearch   Author: baidu   File: ClusterHealthResponse.java    License: Apache License 2.0 5 votes vote down vote up
public ClusterHealthResponse(String clusterName, String[] concreteIndices, ClusterState clusterState, int numberOfPendingTasks,
                             int numberOfInFlightFetch, int delayedUnassignedShards, TimeValue taskMaxWaitingTime) {
    this.clusterName = clusterName;
    this.numberOfPendingTasks = numberOfPendingTasks;
    this.numberOfInFlightFetch = numberOfInFlightFetch;
    this.delayedUnassignedShards = delayedUnassignedShards;
    this.clusterName = clusterName;
    this.numberOfPendingTasks = numberOfPendingTasks;
    this.numberOfInFlightFetch = numberOfInFlightFetch;
    this.taskMaxWaitingTime = taskMaxWaitingTime;
    this.clusterStateHealth = new ClusterStateHealth(clusterState, concreteIndices);
}
 
Example #18
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: ADClusterEventListenerTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testIsWarmNode() {
    HashMap<String, String> attributesForNode1 = new HashMap<>();
    attributesForNode1.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
    dataNode1 = new DiscoveryNode(dataNode1Id, buildNewFakeTransportAddress(), attributesForNode1, BUILT_IN_ROLES, Version.CURRENT);

    ClusterState warmNodeClusterState = ClusterState
        .builder(new ClusterName(clusterName))
        .nodes(new DiscoveryNodes.Builder().masterNodeId(masterNodeId).localNodeId(dataNode1Id).add(masterNode).add(dataNode1))
        .blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK))
        .build();
    listener.clusterChanged(new ClusterChangedEvent("foo", warmNodeClusterState, oldClusterState));
    assertTrue(testAppender.containsMessage(ADClusterEventListener.NODE_NOT_APPLIED_MSG));
}
 
Example #19
Source Project: crate   Author: crate   File: TransportBroadcastReplicationAction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return all shard ids the request should run on
 */
protected List<ShardId> shards(Request request, ClusterState clusterState) {
    List<ShardId> shardIds = new ArrayList<>();
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
    for (String index : concreteIndices) {
        IndexMetaData indexMetaData = clusterState.metaData().getIndices().get(index);
        if (indexMetaData != null) {
            for (IntObjectCursor<IndexShardRoutingTable> shardRouting : clusterState.getRoutingTable().indicesRouting().get(index).getShards()) {
                shardIds.add(shardRouting.value.shardId());
            }
        }
    }
    return shardIds;
}
 
Example #20
Source Project: crate   Author: crate   File: ShardStateAction.java    License: 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 #21
Source Project: crate   Author: crate   File: ShardStateObserver.java    License: Apache License 2.0 5 votes vote down vote up
private CompletableFuture<ShardRouting> waitForActiveShard(ShardId shardId, ClusterState state) {
    var stateObserver = new ClusterStateObserver(
        state, clusterService, MAX_WAIT_TIME_FOR_NEW_STATE, LOGGER);
    var listener = new RetryIsShardActive(shardId);
    stateObserver.waitForNextChange(listener, newState -> shardStartedOrIndexDeleted(newState, shardId));
    return listener.result();
}
 
Example #22
Source Project: crate   Author: crate   File: ClusterServiceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static MasterService createMasterService(ThreadPool threadPool, ClusterState initialClusterState) {
    MasterService masterService = new MasterService("test_master_node", Settings.EMPTY, threadPool);
    AtomicReference<ClusterState> clusterStateRef = new AtomicReference<>(initialClusterState);
    masterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
        clusterStateRef.set(event.state());
        publishListener.onResponse(null);
    });
    masterService.setClusterStateSupplier(clusterStateRef::get);
    masterService.start();
    return masterService;
}
 
Example #23
Source Project: crate   Author: crate   File: TransportCreateUserDefinedFunctionAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(final CreateUserDefinedFunctionRequest request,
                               ClusterState state,
                               ActionListener<AcknowledgedResponse> listener) throws Exception {
    UserDefinedFunctionMetaData metaData = request.userDefinedFunctionMetaData();
    String errorMessage = udfService.getLanguage(metaData.language()).validate(metaData);
    if (errorMessage != null) {
        throw new ScriptException(errorMessage, metaData.language());
    }
    udfService.registerFunction(metaData, request.replace(), listener, request.masterNodeTimeout());
}
 
Example #24
Source Project: crate   Author: crate   File: ActiveShardCount.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns true iff the given cluster state's routing table contains enough active
 * shards for the given indices to meet the required shard count represented by this instance.
 */
public boolean enoughShardsActive(final ClusterState clusterState, final String... indices) {
    if (this == ActiveShardCount.NONE) {
        // not waiting for any active shards
        return true;
    }

    for (final String indexName : indices) {
        final IndexMetaData indexMetaData = clusterState.metaData().index(indexName);
        if (indexMetaData == null) {
            // its possible the index was deleted while waiting for active shard copies,
            // in this case, we'll just consider it that we have enough active shard copies
            // and we can stop waiting
            continue;
        }
        final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName);
        assert indexRoutingTable != null;
        if (indexRoutingTable.allPrimaryShardsActive() == false) {
            // all primary shards aren't active yet
            return false;
        }
        ActiveShardCount waitForActiveShards = this;
        if (waitForActiveShards == ActiveShardCount.DEFAULT) {
            waitForActiveShards = SETTING_WAIT_FOR_ACTIVE_SHARDS.get(indexMetaData.getSettings());
        }
        for (final IntObjectCursor<IndexShardRoutingTable> shardRouting : indexRoutingTable.getShards()) {
            if (waitForActiveShards.enoughShardsActive(shardRouting.value) == false) {
                // not enough active shard copies yet
                return false;
            }
        }
    }

    return true;
}
 
Example #25
Source Project: crate   Author: crate   File: SQLExecutor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a view definition to the metaData.
 * Note that this by-passes the analyzer step and directly operates on the clusterState. So there is no
 * resolve logic for columns (`*` is not resolved to the column names)
 */
public Builder addView(RelationName name, String query) {
    ClusterState prevState = clusterService.state();
    ViewsMetaData newViews = ViewsMetaData.addOrReplace(
        prevState.metaData().custom(ViewsMetaData.TYPE), name, query, user == null ? null : user.name());

    MetaData newMetaData = MetaData.builder(prevState.metaData()).putCustom(ViewsMetaData.TYPE, newViews).build();
    ClusterState newState = ClusterState.builder(prevState)
        .metaData(newMetaData)
        .build();

    ClusterServiceUtils.setState(clusterService, newState);
    return this;
}
 
Example #26
Source Project: Elasticsearch   Author: baidu   File: GatewayMetaState.java    License: Apache License 2.0 5 votes vote down vote up
public static Set<String> getRelevantIndices(ClusterState state, ClusterState previousState,ImmutableSet<String> previouslyWrittenIndices) {
    Set<String> relevantIndices;
    if (isDataOnlyNode(state)) {
        relevantIndices = getRelevantIndicesOnDataOnlyNode(state, previousState, previouslyWrittenIndices);
    } else if (state.nodes().localNode().masterNode() == true) {
        relevantIndices = getRelevantIndicesForMasterEligibleNode(state);
    } else {
        relevantIndices = Collections.emptySet();
    }
    return relevantIndices;
}
 
Example #27
Source Project: crate   Author: crate   File: DelayedAllocationService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Figure out if an existing scheduled reroute is good enough or whether we need to cancel and reschedule.
 */
private synchronized void scheduleIfNeeded(long currentNanoTime, ClusterState state) {
    assertClusterOrMasterStateThread();
    long nextDelayNanos = UnassignedInfo.findNextDelayedAllocation(currentNanoTime, state);
    if (nextDelayNanos < 0) {
        LOGGER.trace("no need to schedule reroute - no delayed unassigned shards");
        removeTaskAndCancel();
    } else {
        TimeValue nextDelay = TimeValue.timeValueNanos(nextDelayNanos);
        final boolean earlierRerouteNeeded;
        DelayedRerouteTask existingTask = delayedRerouteTask.get();
        DelayedRerouteTask newTask = new DelayedRerouteTask(nextDelay, currentNanoTime);
        if (existingTask == null) {
            earlierRerouteNeeded = true;
        } else if (newTask.scheduledTimeToRunInNanos() < existingTask.scheduledTimeToRunInNanos()) {
            // we need an earlier delayed reroute
            LOGGER.trace("cancelling existing delayed reroute task as delayed reroute has to happen [{}] earlier",
                TimeValue.timeValueNanos(existingTask.scheduledTimeToRunInNanos() - newTask.scheduledTimeToRunInNanos()));
            existingTask.cancelScheduling();
            earlierRerouteNeeded = true;
        } else {
            earlierRerouteNeeded = false;
        }

        if (earlierRerouteNeeded) {
            LOGGER.info("scheduling reroute for delayed shards in [{}] ({} delayed shards)", nextDelay,
                UnassignedInfo.getNumberOfDelayedUnassigned(state));
            DelayedRerouteTask currentTask = delayedRerouteTask.getAndSet(newTask);
            assert existingTask == currentTask || currentTask == null;
            newTask.schedule();
        } else {
            LOGGER.trace("no need to reschedule delayed reroute - currently scheduled delayed reroute in [{}] is enough", nextDelay);
        }
    }
}
 
Example #28
Source Project: Elasticsearch   Author: baidu   File: TransportBroadcastReplicationAction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return all shard ids the request should run on
 */
protected List<ShardId> shards(Request request, ClusterState clusterState) {
    List<ShardId> shardIds = new ArrayList<>();
    String[] concreteIndices = indexNameExpressionResolver.concreteIndices(clusterState, request);
    for (String index : concreteIndices) {
        IndexMetaData indexMetaData = clusterState.metaData().getIndices().get(index);
        if (indexMetaData != null) {
            for (IntObjectCursor<IndexShardRoutingTable> shardRouting : clusterState.getRoutingTable().indicesRouting().get(index).getShards()) {
                shardIds.add(shardRouting.value.shardId());
            }
        }
    }
    return shardIds;
}
 
Example #29
Source Project: crate   Author: crate   File: InMemoryPersistedState.java    License: Apache License 2.0 5 votes vote down vote up
public InMemoryPersistedState(long term, ClusterState acceptedState) {
    this.currentTerm = term;
    this.acceptedState = acceptedState;

    assert currentTerm >= 0;
    assert getLastAcceptedState().term() <= currentTerm :
        "last accepted term " + getLastAcceptedState().term() + " cannot be above current term " + currentTerm;
}
 
Example #30
Source Project: Elasticsearch   Author: baidu   File: AutoCreateIndex.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Should the index be auto created?
 */
public boolean shouldAutoCreate(String index, ClusterState state) {
    if (!needToCheck) {
        return false;
    }
    boolean exists = resolver.hasIndexOrAlias(index, state);
    if (exists) {
        return false;
    }
    if (globallyDisabled || dynamicMappingDisabled) {
        return false;
    }
    // matches not set, default value of "true"
    if (matches == null) {
        return true;
    }
    for (int i = 0; i < matches.length; i++) {
        char c = matches[i].charAt(0);
        if (c == '-') {
            if (Regex.simpleMatch(matches2[i], index)) {
                return false;
            }
        } else if (c == '+') {
            if (Regex.simpleMatch(matches2[i], index)) {
                return true;
            }
        } else {
            if (Regex.simpleMatch(matches[i], index)) {
                return true;
            }
        }
    }
    return false;
}