org.elasticsearch.cluster.ClusterState Java Examples

The following examples show how to use org.elasticsearch.cluster.ClusterState. 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: TransportMasterNodeAction.java    From crate with 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 #2
Source File: PublicationTests.java    From crate with 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 #3
Source File: AlterTableClusterStateExecutor.java    From crate with 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 File: Coordinator.java    From crate with 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 #5
Source File: TransportClearScrollAction.java    From Elasticsearch with 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 File: TransportRecoveryAction.java    From Elasticsearch with 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 File: ESIntegTestCase.java    From crate with 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 File: CoordinationStateTests.java    From crate with 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 File: TransportCreateUserAction.java    From crate with 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 File: SnapshotRestoreAnalyzerTest.java    From crate with 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 #11
Source File: ZenDiscovery.java    From Elasticsearch with 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 #12
Source File: ElectMasterService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String validate(String setting, String value, ClusterState clusterState) {
    int intValue;
    try {
        intValue = Integer.parseInt(value);
    } catch (NumberFormatException ex) {
        return "cannot parse value [" + value + "] as an integer";
    }
    int masterNodes = clusterState.nodes().masterNodes().size();
    if (intValue > masterNodes) {
        return "cannot set " + ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES + " to more than the current master nodes count [" + masterNodes + "]";
    }
    return null;
}
 
Example #13
Source File: TransportMasterNodeAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
AsyncSingleAction(Task task, final Request request, ActionListener<Response> listener) {
    this.task = task;
    this.request = request;
    if (task != null) {
        request.setParentTask(clusterService.localNode().getId(), task.getId());
    }
    // TODO do we really need to wrap it in a listener? the handlers should be cheap
    if ((listener instanceof ThreadedActionListener) == false) {
        listener = new ThreadedActionListener<>(logger, threadPool, ThreadPool.Names.LISTENER, listener);
    }
    final ActionListener listenerAfterFilter = listener;
    this.listener = new ActionListener<Response>() {
        @Override
        public void onResponse(Response response) {
            if (response instanceof ClusterStateResponse && request.getHeader(LoginUserContext.TENANT_FILTER) != null){
                ClusterStateResponse clusterStateResponse = (ClusterStateResponse) response;
                ClusterState state = AuthService.filterState(clusterStateResponse.getState(), clusterService.state().metaData(),
                        (Long) request.getHeader(LoginUserContext.TENANT_FILTER));
                listenerAfterFilter.onResponse(new ClusterStateResponse(clusterStateResponse.getClusterName(), state));
            } else {
                listenerAfterFilter.onResponse(response);
            }
        }

        @Override
        public void onFailure(Throwable e) {
            listenerAfterFilter.onFailure(e);
        }
    };
}
 
Example #14
Source File: SQLExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
private void addNodesToClusterState(int numNodes) {
    ClusterState prevState = clusterService.state();
    DiscoveryNodes.Builder builder = DiscoveryNodes.builder(prevState.nodes());
    for (int i = 1; i <= numNodes; i++) {
        if (builder.get("n" + i) == null) {
            builder.add(new DiscoveryNode("n" + i, newFakeAddress(), Version.CURRENT));
        }
    }
    builder.localNodeId("n1");
    builder.masterNodeId("n1");
    ClusterServiceUtils.setState(
        clusterService,
        ClusterState.builder(prevState).nodes(builder).build());
}
 
Example #15
Source File: SnapshotsService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Check if any of the indices to be closed are currently being snapshotted. Fail as closing an index that is being
 * snapshotted (with partial == false) makes the snapshot fail.
 */
public static void checkIndexClosing(ClusterState currentState, Set<IndexMetaData> indices) {
    Set<Index> indicesToFail = indicesToFailForCloseOrDeletion(currentState, indices);
    if (indicesToFail != null) {
        throw new SnapshotInProgressException("Cannot close indices that are being snapshotted: " + indicesToFail +
                                              ". Try again after snapshot finishes or cancel the currently running snapshot.");
    }
}
 
Example #16
Source File: TransportCreateSnapshotAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected ClusterBlockException checkBlock(CreateSnapshotRequest request, ClusterState state) {
    // We are reading the cluster metadata and indices - so we need to check both blocks
    ClusterBlockException clusterBlockException = state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
    if (clusterBlockException != null) {
        return clusterBlockException;
    }
    return state.blocks().indicesBlockedException(ClusterBlockLevel.READ, indexNameExpressionResolver.concreteIndices(state, request));
}
 
Example #17
Source File: CoordinationStateTests.java    From crate with Apache License 2.0 5 votes vote down vote up
void invariant() {
    // one master per term
    messages.stream().filter(m -> m.payload instanceof PublishRequest)
        .collect(Collectors.groupingBy(m -> ((PublishRequest) m.payload).getAcceptedState().term()))
        .forEach((term, publishMessages) -> {
            Set<DiscoveryNode> mastersForTerm = publishMessages.stream().collect(Collectors.groupingBy(m -> m.sourceNode)).keySet();
            assertThat("Multiple masters " + mastersForTerm + " for term " + term, mastersForTerm, hasSize(1));
        });

    // unique cluster state per (term, version) pair
    messages.stream().filter(m -> m.payload instanceof PublishRequest)
        .map(m -> ((PublishRequest) m.payload).getAcceptedState())
        .collect(Collectors.groupingBy(ClusterState::term))
        .forEach((term, clusterStates) -> {
            clusterStates.stream().collect(Collectors.groupingBy(ClusterState::version))
            .forEach((version, clusterStates1) -> {
                Set<String> clusterStateUUIDsForTermAndVersion = clusterStates1.stream().collect(Collectors.groupingBy(
                    ClusterState::stateUUID
                )).keySet();
                assertThat("Multiple cluster states " + clusterStates1 + " for term " + term + " and version " + version,
                    clusterStateUUIDsForTermAndVersion, hasSize(1));

                Set<Long> clusterStateValuesForTermAndVersion = clusterStates1.stream().collect(Collectors.groupingBy(
                    CoordinationStateTests::value
                )).keySet();

                assertThat("Multiple cluster states " + clusterStates1 + " for term " + term + " and version " + version,
                    clusterStateValuesForTermAndVersion, hasSize(1));
            });
        });
}
 
Example #18
Source File: SQLExecutor.java    From crate with 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 #19
Source File: TransportCoordinateMultiSearchAction.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
private void doExecuteRequest(final MultiSearchRequest request, final ActionListener<MultiSearchResponse> listener,
                              final List<CoordinateSearchMetadata> metadatas) {
  ClusterState clusterState = clusterService.state();
  clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);

  final AtomicArray<CoordinateMultiSearchResponse.Item> responses = new AtomicArray<>(request.requests().size());
  final AtomicInteger counter = new AtomicInteger(responses.length());
  for (int i = 0; i < responses.length(); i++) {
    final int index = i;
    SearchRequest searchRequest = new SearchRequest(request.requests().get(i), request);
    searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {

      @Override
      public void onResponse(SearchResponse searchResponse) {
        responses.set(index, new CoordinateMultiSearchResponse.Item(new CoordinateSearchResponse(searchResponse, metadatas.get(index)), null));
        if (counter.decrementAndGet() == 0) {
          finishHim();
        }
      }

      @Override
      public void onFailure(Throwable e) {
        responses.set(index, new CoordinateMultiSearchResponse.Item(null, ExceptionsHelper.detailedMessage(e)));
        if (counter.decrementAndGet() == 0) {
          finishHim();
        }
      }

      private void finishHim() {
        listener.onResponse(new CoordinateMultiSearchResponse(responses.toArray(new CoordinateMultiSearchResponse.Item[responses.length()])));
      }

    });
  }
}
 
Example #20
Source File: PrimaryTermsTests.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean startInitializingShards(String index) {
    final List<ShardRouting> startedShards = this.clusterState.getRoutingNodes().shardsWithState(index, INITIALIZING);
    logger.info("start primary shards for index [{}]: {} ", index, startedShards);
    ClusterState rerouteResult = allocationService.applyStartedShards(this.clusterState, startedShards);
    boolean changed = rerouteResult.equals(this.clusterState) == false;
    applyRerouteResult(rerouteResult);
    return changed;
}
 
Example #21
Source File: DocTableInfoBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
DocTableInfoBuilder(Functions functions,
                    RelationName ident,
                    ClusterState state,
                    IndexNameExpressionResolver indexNameExpressionResolver) {
    this.functions = functions;
    this.indexNameExpressionResolver = indexNameExpressionResolver;
    this.ident = ident;
    this.state = state;
    this.metaData = state.metaData();
}
 
Example #22
Source File: CoordinatorTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testClusterRecoversAfterExceptionDuringSerialization() {
    final Cluster cluster = new Cluster(randomIntBetween(2, 5)); // 1-node cluster doesn't do any serialization
    cluster.runRandomly();
    cluster.stabilise();

    final ClusterNode leader1 = cluster.getAnyLeader();

    logger.info("--> submitting broken task to [{}]", leader1);

    final AtomicBoolean failed = new AtomicBoolean();
    leader1.submitUpdateTask("broken-task",
        cs -> ClusterState.builder(cs).putCustom("broken", new BrokenCustom()).build(),
        (source, e) -> {
            assertThat(e.getCause(), instanceOf(ElasticsearchException.class));
            assertThat(e.getCause().getMessage(), equalTo(BrokenCustom.EXCEPTION_MESSAGE));
            failed.set(true);
        });
    cluster.runFor(DEFAULT_DELAY_VARIABILITY + 1, "processing broken task");
    assertTrue(failed.get());

    cluster.stabilise();

    final ClusterNode leader2 = cluster.getAnyLeader();
    long finalValue = randomLong();

    logger.info("--> submitting value [{}] to [{}]", finalValue, leader2);
    leader2.submitValue(finalValue);
    cluster.stabilise(DEFAULT_CLUSTER_STATE_UPDATE_DELAY);

    for (final ClusterNode clusterNode : cluster.clusterNodes) {
        final String nodeId = clusterNode.getId();
        final ClusterState appliedState = clusterNode.getLastAppliedClusterState();
        assertThat(nodeId + " has the applied value", value(appliedState), is(finalValue));
    }
}
 
Example #23
Source File: TransportCreatePartitionsAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(final CreatePartitionsRequest request,
                               final ClusterState state,
                               final ActionListener<AcknowledgedResponse> listener) throws ElasticsearchException {

    if (request.indices().isEmpty()) {
        listener.onResponse(new AcknowledgedResponse(true));
        return;
    }

    createIndices(request, ActionListener.wrap(response -> {
        if (response.isAcknowledged()) {
            activeShardsObserver.waitForActiveShards(request.indices().toArray(new String[0]), ActiveShardCount.DEFAULT, request.ackTimeout(),
                shardsAcked -> {
                    if (!shardsAcked && logger.isInfoEnabled()) {
                        String partitionTemplateName = PartitionName.templateName(request.indices().iterator().next());
                        IndexTemplateMetaData templateMetaData = state.metaData().getTemplates().get(partitionTemplateName);

                        logger.info("[{}] Table partitions created, but the operation timed out while waiting for " +
                                     "enough shards to be started. Timeout={}, wait_for_active_shards={}. " +
                                     "Consider decreasing the 'number_of_shards' table setting (currently: {}) or adding nodes to the cluster.",
                            request.indices(), request.timeout(),
                            SETTING_WAIT_FOR_ACTIVE_SHARDS.get(templateMetaData.getSettings()),
                            INDEX_NUMBER_OF_SHARDS_SETTING.get(templateMetaData.getSettings()));
                    }
                    listener.onResponse(new AcknowledgedResponse(response.isAcknowledged()));
                }, listener::onFailure);
        } else {
            logger.warn("[{}] Table partitions created, but publishing new cluster state timed out. Timeout={}",
                request.indices(), request.timeout());
            listener.onResponse(new AcknowledgedResponse(false));
        }
    }, listener::onFailure));
}
 
Example #24
Source File: TransportShowTenantsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(final ShowTenantsRequest request, ClusterState state, final ActionListener<ShowTenantsResponse> listener) {

    ShowTenantsResponse response = new ShowTenantsResponse();
    List<TenantProperty> tenantProperties = new ArrayList<TenantProperty>();
    ImmutableMap<Long, TenantProperty> allTenants = state.metaData().getTenantMetadata().tenantProperties();
    for (Long tenantId : allTenants.keySet()) {
        tenantProperties.add(allTenants.get(tenantId));
    }
    response.setTenantProperty(tenantProperties);
    listener.onResponse(response);
}
 
Example #25
Source File: AutoCreateIndex.java    From Elasticsearch with 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;
}
 
Example #26
Source File: InMemoryPersistedState.java    From crate with 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 #27
Source File: TransportBroadcastReplicationAction.java    From Elasticsearch with 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 #28
Source File: DelayedAllocationService.java    From crate with 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 #29
Source File: GatewayMetaState.java    From Elasticsearch with 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 #30
Source File: TransportDropTableAction.java    From crate with 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()));
}