org.elasticsearch.cluster.metadata.MetaData Java Examples

The following examples show how to use org.elasticsearch.cluster.metadata.MetaData. 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: GatewayMetaState.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the current meta state for each index in the new cluster state and checks if it has to be persisted.
 * Each index state that should be written to disk will be returned. This is only run for data only nodes.
 * It will return only the states for indices that actually have a shard allocated on the current node.
 *
 * @param previouslyWrittenIndices    A list of indices for which the state was already written before
 * @param potentiallyUnwrittenIndices The list of indices for which state should potentially be written
 * @param previousMetaData            The last meta data we know of. meta data for all indices in previouslyWrittenIndices list is persisted now
 * @param newMetaData                 The new metadata
 * @return iterable over all indices states that should be written to disk
 */
public static Iterable<GatewayMetaState.IndexMetaWriteInfo> resolveStatesToBeWritten(ImmutableSet<String> previouslyWrittenIndices, Set<String> potentiallyUnwrittenIndices, MetaData previousMetaData, MetaData newMetaData) {
    List<GatewayMetaState.IndexMetaWriteInfo> indicesToWrite = new ArrayList<>();
    for (String index : potentiallyUnwrittenIndices) {
        IndexMetaData newIndexMetaData = newMetaData.index(index);
        IndexMetaData previousIndexMetaData = previousMetaData == null ? null : previousMetaData.index(index);
        String writeReason = null;
        if (previouslyWrittenIndices.contains(index) == false || previousIndexMetaData == null) {
            writeReason = "freshly created";
        } else if (previousIndexMetaData.getVersion() != newIndexMetaData.getVersion()) {
            writeReason = "version changed from [" + previousIndexMetaData.getVersion() + "] to [" + newIndexMetaData.getVersion() + "]";
        }
        if (writeReason != null) {
            indicesToWrite.add(new GatewayMetaState.IndexMetaWriteInfo(newIndexMetaData, previousIndexMetaData, writeReason));
        }
    }
    return indicesToWrite;
}
 
Example #2
Source File: JoinTaskExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that all indices are compatible with the given node version. This will ensure that all indices in the given metadata
 * will not be created with a newer version of elasticsearch as well as that all indices are newer or equal to the minimum index
 * compatibility version.
 * @see Version#minimumIndexCompatibilityVersion()
 * @throws IllegalStateException if any index is incompatible with the given version
 */
public static void ensureIndexCompatibility(final Version nodeVersion, MetaData metaData) {
    Version supportedIndexVersion = nodeVersion.minimumIndexCompatibilityVersion();
    // we ensure that all indices in the cluster we join are compatible with us no matter if they are
    // closed or not we can't read mappings of these indices so we need to reject the join...
    for (IndexMetaData idxMetaData : metaData) {
        if (idxMetaData.getCreationVersion().after(nodeVersion)) {
            throw new IllegalStateException("index " + idxMetaData.getIndex() + " version not supported: "
                + idxMetaData.getCreationVersion() + " the node version is: " + nodeVersion);
        }
        if (idxMetaData.getCreationVersion().before(supportedIndexVersion)) {
            throw new IllegalStateException("index " + idxMetaData.getIndex() + " version not supported: "
                + idxMetaData.getCreationVersion() + " minimum compatible index version is: " + supportedIndexVersion);
        }
    }
}
 
Example #3
Source File: UnassignedInfo.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the next (closest) delay expiration of an delayed shard in nanoseconds based on current time.
 * Returns 0 if delay is negative.
 * Returns -1 if no delayed shard is found.
 */
public static long findNextDelayedAllocation(long currentNanoTime, ClusterState state) {
    MetaData metaData = state.metaData();
    RoutingTable routingTable = state.routingTable();
    long nextDelayNanos = Long.MAX_VALUE;
    for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.UNASSIGNED)) {
        UnassignedInfo unassignedInfo = shard.unassignedInfo();
        if (unassignedInfo.isDelayed()) {
            Settings indexSettings = metaData.index(shard.index()).getSettings();
            // calculate next time to schedule
            final long newComputedLeftDelayNanos = unassignedInfo.getRemainingDelay(currentNanoTime, indexSettings);
            if (newComputedLeftDelayNanos < nextDelayNanos) {
                nextDelayNanos = newComputedLeftDelayNanos;
            }
        }
    }
    return nextDelayNanos == Long.MAX_VALUE ? -1L : nextDelayNanos;
}
 
Example #4
Source File: FetchTask.java    From crate with Apache License 2.0 6 votes vote down vote up
public FetchTask(UUID jobId,
                 FetchPhase phase,
                 String localNodeId,
                 SharedShardContexts sharedShardContexts,
                 MetaData metaData,
                 Function<RelationName, DocTableInfo> getTableInfo,
                 Iterable<? extends Routing> routingIterable) {
    super(phase.phaseId());
    this.jobId = jobId;
    this.phase = phase;
    this.localNodeId = localNodeId;
    this.sharedShardContexts = sharedShardContexts;
    this.metaData = metaData;
    this.routingIterable = routingIterable;
    this.toFetch = new HashMap<>(phase.tableIndices().size());
    this.getTableInfo = getTableInfo;
}
 
Example #5
Source File: SQLPlugin.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<NamedXContentRegistry.Entry> getNamedXContent() {
    List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
    entries.add(new NamedXContentRegistry.Entry(
        MetaData.Custom.class,
        new ParseField(UserDefinedFunctionsMetaData.TYPE),
        UserDefinedFunctionsMetaData::fromXContent
    ));
    entries.add(new NamedXContentRegistry.Entry(
        MetaData.Custom.class,
        new ParseField(ViewsMetaData.TYPE),
        ViewsMetaData::fromXContent
    ));

    if (userExtension != null) {
        entries.addAll(userExtension.getNamedXContent());
    }
    if (licenseExtension != null) {
        entries.addAll(licenseExtension.getNamedXContent());
    }
    return entries;
}
 
Example #6
Source File: TransportCreateUserAction.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Puts a user into the meta data and creates an empty privileges set.
 *
 * @return boolean true if the user already exists, otherwise false
 */
@VisibleForTesting
static boolean putUser(MetaData.Builder mdBuilder, String name, @Nullable SecureHash secureHash) {
    UsersMetaData oldMetaData = (UsersMetaData) mdBuilder.getCustom(UsersMetaData.TYPE);
    if (oldMetaData != null && oldMetaData.contains(name)) {
        return true;
    }
    // create a new instance of the metadata, to guarantee the cluster changed action.
    UsersMetaData newMetaData = UsersMetaData.newInstance(oldMetaData);
    newMetaData.put(name, secureHash);
    assert !newMetaData.equals(oldMetaData) : "must not be equal to guarantee the cluster change action";
    mdBuilder.putCustom(UsersMetaData.TYPE, newMetaData);

    // create empty privileges for this user
    UsersPrivilegesMetaData privilegesMetaData = UsersPrivilegesMetaData.copyOf(
        (UsersPrivilegesMetaData) mdBuilder.getCustom(UsersPrivilegesMetaData.TYPE));
    privilegesMetaData.createPrivileges(name, Collections.emptySet());
    mdBuilder.putCustom(UsersPrivilegesMetaData.TYPE, privilegesMetaData);
    return false;
}
 
Example #7
Source File: DropTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, DropTableRequest request) throws Exception {
    RelationName relationName = request.tableIdent();
    final Set<Index> concreteIndices = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndices(
        currentState, IndicesOptions.lenientExpandOpen(), relationName.indexNameOrAlias())));
    currentState = deleteIndexService.deleteIndices(currentState, concreteIndices);

    if (request.isPartitioned()) {
        // delete template
        String templateName = PartitionName.templateName(relationName.schema(), relationName.name());
        MetaData.Builder metaData = MetaData.builder(currentState.metaData());
        metaData.removeTemplate(templateName);
        currentState = ClusterState.builder(currentState).metaData(metaData).build();
    }

    // call possible modifiers
    currentState = ddlClusterStateService.onDropTable(currentState, request.tableIdent());

    return currentState;
}
 
Example #8
Source File: TransportAlterUserAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(AlterUserRequest request, ClusterState state, ActionListener<WriteUserResponse> listener) {
    clusterService.submitStateUpdateTask("alter_user [" + request.userName() + "]",
        new AckedClusterStateUpdateTask<WriteUserResponse>(Priority.URGENT, request, listener) {

            private boolean userExists = true;

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

            @Override
            protected WriteUserResponse newResponse(boolean acknowledged) {
                return new WriteUserResponse(acknowledged, userExists);
            }
        });
}
 
Example #9
Source File: InsertFromValues.java    From crate with Apache License 2.0 6 votes vote down vote up
private static CompletableFuture<AcknowledgedResponse> createIndices(TransportCreatePartitionsAction
                                                                         createPartitionsAction,
                                                                     Set<String> indices,
                                                                     ClusterService clusterService,
                                                                     UUID jobId) {
    MetaData metaData = clusterService.state().getMetaData();
    List<String> indicesToCreate = new ArrayList<>();
    for (var index : indices) {
        if (IndexParts.isPartitioned(index) && metaData.hasIndex(index) == false) {
            indicesToCreate.add(index);
        }
    }

    FutureActionListener<AcknowledgedResponse, AcknowledgedResponse> listener = new FutureActionListener<>(r -> r);
    createPartitionsAction.execute(new CreatePartitionsRequest(indicesToCreate, jobId), listener);
    return listener;
}
 
Example #10
Source File: CreateDropRepositoryAnalyzerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() {
    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).build();
    plannerContext = e.getPlannerContext(clusterService.state());
    repositoryParamValidator = new RepositoryParamValidator(Map.of(
        "fs", new TypeSettings(FsRepository.mandatorySettings(), FsRepository.optionalSettings())
    ));
}
 
Example #11
Source File: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected Tuple<IndexResponse, IndexRequest> shardOperationOnPrimary(MetaData metaData, IndexRequest request) throws Throwable {

    // validate, if routing is required, that we got routing
    IndexMetaData indexMetaData = metaData.index(request.shardId().getIndex());
    MappingMetaData mappingMd = indexMetaData.mappingOrDefault(request.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (request.routing() == null) {
            throw new RoutingMissingException(request.shardId().getIndex(), request.type(), request.id());
        }
    }

    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    indexShard.checkDiskSpace(fsService);
    final WriteResult<IndexResponse> result = executeIndexRequestOnPrimary(null, request, indexShard, mappingUpdatedAction);
    final IndexResponse response = result.response;
    final Translog.Location location = result.location;
    processAfterWrite(request.refresh(), indexShard, location);
    return new Tuple<>(response, request);
}
 
Example #12
Source File: PartitionInfosTest.java    From crate with Apache License 2.0 6 votes vote down vote up
private void addIndexMetaDataToClusterState(IndexMetaData.Builder imdBuilder) throws Exception {
    CompletableFuture<Boolean> processed = new CompletableFuture<>();
    ClusterState currentState = clusterService.state();
    ClusterState newState = ClusterState.builder(currentState)
        .metaData(MetaData.builder(currentState.metaData())
            .put(imdBuilder))
        .build();
    clusterService.addListener(event -> {
        if (event.state() == newState) {
            processed.complete(true);
        }
    });
    clusterService.getClusterApplierService()
        .onNewClusterState("test", () -> newState, (source, e) -> processed.completeExceptionally(e));
    processed.get(5, TimeUnit.SECONDS);
}
 
Example #13
Source File: InternalCountOperationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testCount() throws Exception {
    execute("create table t (name string) clustered into 1 shards with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name) values ('Marvin'), ('Arthur'), ('Trillian')");
    execute("refresh table t");

    CountOperation countOperation = internalCluster().getDataNodeInstance(CountOperation.class);
    ClusterService clusterService = internalCluster().getDataNodeInstance(ClusterService.class);
    CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
    MetaData metaData = clusterService.state().getMetaData();
    Index index = metaData.index(getFqn("t")).getIndex();
    assertThat(countOperation.count(txnCtx, index, 0, Literal.BOOLEAN_TRUE), is(3L));

    Schemas schemas = internalCluster().getInstance(Schemas.class);
    TableInfo tableInfo = schemas.getTableInfo(new RelationName(sqlExecutor.getCurrentSchema(), "t"));
    TableRelation tableRelation = new TableRelation(tableInfo);
    Map<RelationName, AnalyzedRelation> tableSources = Map.of(tableInfo.ident(), tableRelation);
    SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);

    Symbol filter = sqlExpressions.normalize(sqlExpressions.asSymbol("name = 'Marvin'"));
    assertThat(countOperation.count(txnCtx, index, 0, filter), is(1L));
}
 
Example #14
Source File: TransportPrivilegesActionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyPrivilegesCreatesNewPrivilegesInstance() {
    // given
    MetaData.Builder mdBuilder = MetaData.builder();
    Map<String, Set<Privilege>> usersPrivileges = new HashMap<>();
    usersPrivileges.put("Ford", new HashSet<>(PRIVILEGES));
    UsersPrivilegesMetaData initialPrivilegesMetadata = new UsersPrivilegesMetaData(usersPrivileges);
    mdBuilder.putCustom(UsersPrivilegesMetaData.TYPE, initialPrivilegesMetadata);
    PrivilegesRequest denyPrivilegeRequest =
        new PrivilegesRequest(Collections.singletonList("Ford"), Collections.singletonList(DENY_DQL));

    //when
    TransportPrivilegesAction.applyPrivileges(mdBuilder, denyPrivilegeRequest);

    // then
    UsersPrivilegesMetaData newPrivilegesMetadata =
        (UsersPrivilegesMetaData) mdBuilder.getCustom(UsersPrivilegesMetaData.TYPE);
    assertNotSame(newPrivilegesMetadata, initialPrivilegesMetadata);
}
 
Example #15
Source File: TransportShardBulkAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private WriteResult<IndexResponse> shardIndexOperation(BulkShardRequest request, IndexRequest indexRequest, MetaData metaData,
                                        IndexShard indexShard, boolean processed) throws Throwable {
    indexShard.checkDiskSpace(fsService);
    // validate, if routing is required, that we got routing
    MappingMetaData mappingMd = metaData.index(request.index()).mappingOrDefault(indexRequest.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (indexRequest.routing() == null) {
            throw new RoutingMissingException(request.index(), indexRequest.type(), indexRequest.id());
        }
    }

    if (!processed) {
        indexRequest.process(metaData, mappingMd, allowIdGeneration, request.index());
    }

    return TransportIndexAction.executeIndexRequestOnPrimary(request, indexRequest, indexShard, mappingUpdatedAction);
}
 
Example #16
Source File: JoinTaskExecutorTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testSuccess() {
    Settings.builder().build();
    MetaData.Builder metaBuilder = MetaData.builder();
    IndexMetaData indexMetaData = IndexMetaData.builder("test")
        .settings(settings(VersionUtils.randomVersionBetween(random(),
            Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
        .numberOfShards(1)
        .numberOfReplicas(1).build();
    metaBuilder.put(indexMetaData, false);
    indexMetaData = IndexMetaData.builder("test1")
        .settings(settings(VersionUtils.randomVersionBetween(random(),
            Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
        .numberOfShards(1)
        .numberOfReplicas(1).build();
    metaBuilder.put(indexMetaData, false);
    MetaData metaData = metaBuilder.build();
        JoinTaskExecutor.ensureIndexCompatibility(Version.CURRENT,
            metaData);
}
 
Example #17
Source File: ListSfsIndexes.java    From sfs with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<String> call(Void aVoid) {
    Elasticsearch elasticsearch = vertxContext.verticle().elasticsearch();

    ClusterStateRequestBuilder request =
            elasticsearch.get()
                    .admin()
                    .cluster()
                    .prepareState();
    return elasticsearch.execute(vertxContext, request, elasticsearch.getDefaultAdminTimeout())
            .map(Optional::get)
            .flatMap(clusterStateResponse -> {
                ClusterState state = clusterStateResponse.getState();
                MetaData metadata = state.getMetaData();
                return from(toArray(metadata.getIndices().keysIt(), String.class));
            })
            .filter(s -> s.startsWith(elasticsearch.indexPrefix()));
}
 
Example #18
Source File: GetSettingsRequestBuilder.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
@Override
protected XContentBuilder toXContent(ClusterStateRequest request, ClusterStateResponse response, XContentBuilder builder) throws IOException {
    MetaData metaData = response.getState().metaData();

    if (metaData.indices().isEmpty()) {
        return builder.startObject().endObject();
    }

    builder.startObject();
    for (IndexMetaData indexMetaData : metaData) {
        builder.startObject(indexMetaData.index(), XContentBuilder.FieldCaseConversion.NONE);
        builder.startObject("settings");
        for (Map.Entry<String, String> entry : indexMetaData.settings().getAsMap().entrySet()) {
            builder.field(entry.getKey(), entry.getValue());
        }
        builder.endObject();
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
 
Example #19
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 #20
Source File: TransportBulkAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private boolean addFailureIfIndexIsUnavailable(DocumentRequest request, BulkRequest bulkRequest, AtomicArray<BulkItemResponse> responses, int idx,
                                          final ConcreteIndices concreteIndices,
                                          final MetaData metaData) {
    String concreteIndex = concreteIndices.getConcreteIndex(request.index());
    Exception unavailableException = null;
    if (concreteIndex == null) {
        try {
            concreteIndex = concreteIndices.resolveIfAbsent(request);
        } catch (IndexClosedException | IndexNotFoundException ex) {
            // Fix for issue where bulk request references an index that
            // cannot be auto-created see issue #8125
            unavailableException = ex;
        }
    }
    if (unavailableException == null) {
        IndexMetaData indexMetaData = metaData.index(concreteIndex);
        if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
            unavailableException = new IndexClosedException(new Index(metaData.index(request.index()).getIndex()));
        }
    }
    if (unavailableException != null) {
        BulkItemResponse.Failure failure = new BulkItemResponse.Failure(request.index(), request.type(), request.id(),
                unavailableException);
        String operationType = "unknown";
        if (request instanceof IndexRequest) {
            operationType = "index";
        } else if (request instanceof DeleteRequest) {
            operationType = "delete";
        } else if (request instanceof UpdateRequest) {
            operationType = "update";
        }
        BulkItemResponse bulkItemResponse = new BulkItemResponse(idx, operationType, failure);
        responses.set(idx, bulkItemResponse);
        // make sure the request gets never processed again
        bulkRequest.requests.set(idx, null);
        return true;
    }
    return false;
}
 
Example #21
Source File: EnterpriseLicenseExtension.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
    List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
    entries.add(new NamedWriteableRegistry.Entry(
        MetaData.Custom.class,
        LicenseKey.WRITEABLE_TYPE,
        LicenseKey::new
    ));
    entries.add(new NamedWriteableRegistry.Entry(
        NamedDiff.class,
        LicenseKey.WRITEABLE_TYPE,
        in -> LicenseKey.readDiffFrom(MetaData.Custom.class, LicenseKey.WRITEABLE_TYPE, in)
    ));
    return entries;
}
 
Example #22
Source File: DiskThresholdDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the expected shard size for the given shard or the default value provided if not enough information are available
 * to estimate the shards size.
 */
public static long getExpectedShardSize(ShardRouting shard,
                                        long defaultValue,
                                        ClusterInfo clusterInfo,
                                        MetaData metaData,
                                        RoutingTable routingTable) {
    final IndexMetaData indexMetaData = metaData.getIndexSafe(shard.index());
    if (indexMetaData.getResizeSourceIndex() != null && shard.active() == false &&
        shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in
        // the worst case
        long targetShardSize = 0;
        final Index mergeSourceIndex = indexMetaData.getResizeSourceIndex();
        final IndexMetaData sourceIndexMeta = metaData.index(mergeSourceIndex);
        if (sourceIndexMeta != null) {
            final Set<ShardId> shardIds = IndexMetaData.selectRecoverFromShards(shard.id(),
                sourceIndexMeta, indexMetaData.getNumberOfShards());
            for (IndexShardRoutingTable shardRoutingTable : routingTable.index(mergeSourceIndex.getName())) {
                if (shardIds.contains(shardRoutingTable.shardId())) {
                    targetShardSize += clusterInfo.getShardSize(shardRoutingTable.primaryShard(), 0);
                }
            }
        }
        return targetShardSize == 0 ? defaultValue : targetShardSize;
    } else {
        return clusterInfo.getShardSize(shard, defaultValue);
    }
}
 
Example #23
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
private UpdatedState applyDropRelations(ClusterState state, UpdatedState updatedState, List<RelationName> dropRelations) {
    ClusterState stateAfterRename = updatedState.newState;
    MetaData.Builder updatedMetaData = MetaData.builder(stateAfterRename.metaData());
    RoutingTable.Builder routingBuilder = RoutingTable.builder(stateAfterRename.routingTable());

    for (RelationName dropRelation : dropRelations) {
        for (Index index : indexNameResolver.concreteIndices(
            state, IndicesOptions.LENIENT_EXPAND_OPEN, dropRelation.indexNameOrAlias())) {

            String indexName = index.getName();
            updatedMetaData.remove(indexName);
            routingBuilder.remove(indexName);
            updatedState.newIndices.remove(indexName);
        }
    }
    ClusterState stateAfterDropRelations = ClusterState
        .builder(stateAfterRename)
        .metaData(updatedMetaData)
        .routingTable(routingBuilder.build())
        .build();
    return new UpdatedState(
        allocationService.reroute(
            applyDropTableClusterStateModifiers(stateAfterDropRelations, dropRelations),
            "indices drop after name switch"
        ),
        updatedState.newIndices
    );
}
 
Example #24
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState onSwapRelations(ClusterState currentState, RelationName source, RelationName target) {
    MetaData currentMetaData = currentState.metaData();
    UsersPrivilegesMetaData userPrivileges = currentMetaData.custom(UsersPrivilegesMetaData.TYPE);
    if (userPrivileges == null) {
        return currentState;
    }
    UsersPrivilegesMetaData updatedPrivileges = UsersPrivilegesMetaData.swapPrivileges(userPrivileges, source, target);
    return ClusterState.builder(currentState)
        .metaData(MetaData.builder(currentMetaData)
            .putCustom(UsersPrivilegesMetaData.TYPE, updatedPrivileges)
            .build())
        .build();
}
 
Example #25
Source File: CreateAlterPartitionedTableAnalyzerTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Before
public void prepare() throws IOException {
    String analyzerSettings = FulltextAnalyzerResolver.encodeSettings(
        Settings.builder().put("search", "foobar").build()).utf8ToString();
    MetaData metaData = MetaData.builder()
        .persistentSettings(
            Settings.builder()
                .put(ANALYZER.buildSettingName("ft_search"), analyzerSettings)
                .build())
        .build();
    ClusterState state =
        ClusterState.builder(ClusterName.DEFAULT)
            .nodes(DiscoveryNodes.builder()
                       .add(new DiscoveryNode("n1", buildNewFakeTransportAddress(),
                                              Version.CURRENT))
                       .add(new DiscoveryNode("n2", buildNewFakeTransportAddress(),
                                              Version.CURRENT))
                       .add(new DiscoveryNode("n3", buildNewFakeTransportAddress(),
                                              Version.CURRENT))
                       .localNodeId("n1")
            )
            .metaData(metaData)
            .build();
    ClusterServiceUtils.setState(clusterService, state);
    e = SQLExecutor.builder(clusterService).enableDefaultTables().build();
    plannerContext = e.getPlannerContext(clusterService.state());
}
 
Example #26
Source File: RoutingTable.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public RoutingTableValidation validate(MetaData metaData) {
    RoutingTableValidation validation = new RoutingTableValidation();
    for (IndexRoutingTable indexRoutingTable : this) {
        indexRoutingTable.validate(validation, metaData);
    }
    return validation;
}
 
Example #27
Source File: ClusterFormationFailureHelperTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testDescriptionBeforeBootstrapping() {
    final DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
    final ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT)
        .version(7L)
        .metaData(MetaData.builder().coordinationMetaData(CoordinationMetaData.builder().term(4L).build()))
        .nodes(DiscoveryNodes.builder().add(localNode).localNodeId(localNode.getId())).build();

    assertThat(new ClusterFormationState(Settings.EMPTY, clusterState, emptyList(), emptyList(), 1L).getDescription(),
        is("master not discovered yet, this node has not previously joined a bootstrapped (v4+) cluster, and " +
            "[cluster.initial_master_nodes] is empty on this node: have discovered []; " +
            "discovery will continue using [] from hosts providers and [" + localNode +
            "] from last-known cluster state; node term 1, last-accepted version 7 in term 4"));

    final TransportAddress otherAddress = buildNewFakeTransportAddress();
    assertThat(new ClusterFormationState(Settings.EMPTY, clusterState, singletonList(otherAddress), emptyList(), 2L).getDescription(),
        is("master not discovered yet, this node has not previously joined a bootstrapped (v4+) cluster, and " +
            "[cluster.initial_master_nodes] is empty on this node: have discovered []; " +
            "discovery will continue using [" + otherAddress + "] from hosts providers and [" + localNode +
            "] from last-known cluster state; node term 2, last-accepted version 7 in term 4"));

    final DiscoveryNode otherNode = new DiscoveryNode("other", buildNewFakeTransportAddress(), Version.CURRENT);
    assertThat(new ClusterFormationState(Settings.EMPTY, clusterState, emptyList(), singletonList(otherNode), 3L).getDescription(),
        is("master not discovered yet, this node has not previously joined a bootstrapped (v4+) cluster, and " +
            "[cluster.initial_master_nodes] is empty on this node: have discovered [" + otherNode + "]; " +
            "discovery will continue using [] from hosts providers and [" + localNode +
            "] from last-known cluster state; node term 3, last-accepted version 7 in term 4"));

    assertThat(new ClusterFormationState(Settings.builder().putList(INITIAL_MASTER_NODES_SETTING.getKey(), "other").build(),
            clusterState, emptyList(), emptyList(), 4L).getDescription(),
        is("master not discovered yet, this node has not previously joined a bootstrapped (v4+) cluster, and " +
            "this node must discover master-eligible nodes [other] to bootstrap a cluster: have discovered []; " +
            "discovery will continue using [] from hosts providers and [" + localNode +
            "] from last-known cluster state; node term 4, last-accepted version 7 in term 4"));
}
 
Example #28
Source File: UserManagerService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void clusterChanged(ClusterChangedEvent event) {
    MetaData prevMetaData = event.previousState().metaData();
    MetaData newMetaData = event.state().metaData();

    UsersMetaData prevUsers = prevMetaData.custom(UsersMetaData.TYPE);
    UsersMetaData newUsers = newMetaData.custom(UsersMetaData.TYPE);

    UsersPrivilegesMetaData prevUsersPrivileges = prevMetaData.custom(UsersPrivilegesMetaData.TYPE);
    UsersPrivilegesMetaData newUsersPrivileges = newMetaData.custom(UsersPrivilegesMetaData.TYPE);

    if (prevUsers != newUsers || prevUsersPrivileges != newUsersPrivileges) {
        users = getUsers(newUsers, newUsersPrivileges);
    }
}
 
Example #29
Source File: FetchTaskTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearcherIsAcquiredForShard() throws Exception {
    IntArrayList shards = IntArrayList.from(1, 2);
    Routing routing = new Routing(Map.of("dummy", Map.of("i1", shards)));
    IndexBaseBuilder ibb = new IndexBaseBuilder();
    ibb.allocate("i1", shards);

    HashMultimap<RelationName, String> tableIndices = HashMultimap.create();
    tableIndices.put(new RelationName(Schemas.DOC_SCHEMA_NAME, "i1"), "i1");

    MetaData metaData = MetaData.builder()
        .put(IndexMetaData.builder("i1")
            .settings(Settings.builder()
                .put(SETTING_NUMBER_OF_SHARDS, 1)
                .put(SETTING_NUMBER_OF_REPLICAS, 0)
                .put(SETTING_VERSION_CREATED, Version.CURRENT))
            .build(), true)
        .build();
    final FetchTask context = new FetchTask(
        UUID.randomUUID(),
        new FetchPhase(
            1,
            null,
            ibb.build(),
            tableIndices,
            ImmutableList.of(createReference("i1", new ColumnIdent("x"), DataTypes.STRING))),
        "dummy",
        new SharedShardContexts(mock(IndicesService.class, RETURNS_MOCKS), UnaryOperator.identity()),
        metaData,
        relationName -> null,
        ImmutableList.of(routing));

    context.prepare();

    assertThat(context.searcher(1), Matchers.notNullValue());
    assertThat(context.searcher(2), Matchers.notNullValue());
}
 
Example #30
Source File: SysShardsTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathAndBlobPath() throws Exception {
    Path blobs = createTempDir("blobs");
    execute("create table t1 (x int) clustered into 1 shards with (number_of_replicas = 0)");
    execute("create blob table b1 clustered into 1 shards with (number_of_replicas = 0)");
    execute("create blob table b2 " +
            "clustered into 1 shards with (number_of_replicas = 0, blobs_path = '" + blobs.toString() + "')");
    ensureYellow();

    execute("select path, blob_path from sys.shards where table_name in ('t1', 'b1', 'b2') " +
            "order by table_name asc");
    // b1
    // path + /blobs == blob_path without custom blob path
    assertThat(response.rows()[0][0] + resolveCanonicalString("/blobs"), is(response.rows()[0][1]));

    ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    MetaData metaData = clusterService.state().getMetaData();
    IndexMetaData index = metaData.index(".blob_b2");
    String indexUUID = index.getIndexUUID();

    // b2
    String b2Path = (String) response.rows()[1][0];
    assertThat(b2Path, containsString(resolveCanonicalString("/nodes/")));
    assertThat(b2Path, endsWith(resolveCanonicalString("/indices/" + indexUUID + "/0")));

    String b2BlobPath = (String) response.rows()[1][1];
    assertThat(b2BlobPath, containsString(resolveCanonicalString("/nodes/")));
    assertThat(b2BlobPath, endsWith(resolveCanonicalString("/indices/" + indexUUID + "/0/blobs")));
    // t1
    assertThat(response.rows()[2][1], nullValue());
}