Skip to content

Commit

Permalink
Fix node bootup, add context to response
Browse files Browse the repository at this point in the history
Signed-off-by: Mohit Godwani <[email protected]>
  • Loading branch information
mgodwan committed Aug 21, 2024
1 parent 4ab9c9b commit 0a75973
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.apache.lucene.util.CollectionUtil;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.Context;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -167,6 +168,7 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
Settings indexSettings = null;
Settings indexDefaultSettings = null;
String dataStream = null;
Context context = null;
// We start at START_OBJECT since fromXContent ensures that
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser);
Expand All @@ -185,6 +187,8 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
case "defaults":
indexDefaultSettings = Settings.fromXContent(parser);
break;
case "context":
context = Context.fromXContent(parser);
default:
parser.skipChildren();
}
Expand All @@ -197,7 +201,7 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
parser.skipChildren();
}
}
return new IndexEntry(indexAliases, indexMappings, indexSettings, indexDefaultSettings, dataStream);
return new IndexEntry(indexAliases, indexMappings, indexSettings, indexDefaultSettings, dataStream, context);
}

// This is just an internal container to make stuff easier for returning
Expand All @@ -207,19 +211,22 @@ private static class IndexEntry {
Settings indexSettings = Settings.EMPTY;
Settings indexDefaultSettings = Settings.EMPTY;
String dataStream;
Context context;

IndexEntry(
List<AliasMetadata> indexAliases,
MappingMetadata indexMappings,
Settings indexSettings,
Settings indexDefaultSettings,
String dataStream
String dataStream,
Context context
) {
if (indexAliases != null) this.indexAliases = indexAliases;
if (indexMappings != null) this.indexMappings = indexMappings;
if (indexSettings != null) this.indexSettings = indexSettings;
if (indexDefaultSettings != null) this.indexDefaultSettings = indexDefaultSettings;
if (dataStream != null) this.dataStream = dataStream;
if (context != null) this.context = context;
}
}

Expand All @@ -229,6 +236,7 @@ public static GetIndexResponse fromXContent(XContentParser parser) throws IOExce
Map<String, Settings> settings = new HashMap<>();
Map<String, Settings> defaultSettings = new HashMap<>();
Map<String, String> dataStreams = new HashMap<>();
Map<String, Context> contexts = new HashMap<>();
List<String> indices = new ArrayList<>();

if (parser.currentToken() == null) {
Expand All @@ -254,6 +262,9 @@ public static GetIndexResponse fromXContent(XContentParser parser) throws IOExce
if (indexEntry.dataStream != null) {
dataStreams.put(indexName, indexEntry.dataStream);
}
if (indexEntry.context != null) {
contexts.put(indexName, indexEntry.context);
}
} else if (parser.currentToken() == Token.START_ARRAY) {
parser.skipChildren();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected org.opensearch.action.admin.indices.get.GetIndexResponse createServerT
aliases,
settings,
defaultSettings,
dataStreams
dataStreams,
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class GetIndexRequest extends ClusterInfoRequest<GetIndexRequest> {
public enum Feature {
ALIASES((byte) 0),
MAPPINGS((byte) 1),
SETTINGS((byte) 2);
SETTINGS((byte) 2),
CONTEXT((byte) 3);

private static final Feature[] FEATURES = new Feature[Feature.values().length];

Expand Down Expand Up @@ -86,7 +87,7 @@ public static Feature fromId(byte id) {
}
}

private static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS };
private static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS, Feature.CONTEXT };
private Feature[] features = DEFAULT_FEATURES;
private boolean humanReadable = false;
private transient boolean includeDefaults = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.opensearch.Version;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.Context;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
private Map<String, Settings> settings = Map.of();
private Map<String, Settings> defaultSettings = Map.of();
private Map<String, String> dataStreams = Map.of();
private Map<String, Context> contexts = Map.of();
private final String[] indices;

public GetIndexResponse(
Expand All @@ -76,7 +78,8 @@ public GetIndexResponse(
final Map<String, List<AliasMetadata>> aliases,
final Map<String, Settings> settings,
final Map<String, Settings> defaultSettings,
final Map<String, String> dataStreams
final Map<String, String> dataStreams,
final Map<String, Context> contexts
) {
this.indices = indices;
// to have deterministic order
Expand All @@ -96,6 +99,9 @@ public GetIndexResponse(
if (dataStreams != null) {
this.dataStreams = Collections.unmodifiableMap(dataStreams);
}
if (contexts != null) {
this.contexts = Collections.unmodifiableMap(contexts);
}
}

GetIndexResponse(StreamInput in) throws IOException {
Expand Down Expand Up @@ -160,6 +166,15 @@ public GetIndexResponse(
dataStreamsMapBuilder.put(in.readString(), in.readOptionalString());
}
dataStreams = Collections.unmodifiableMap(dataStreamsMapBuilder);

if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
final Map<String, Context> contextMapBuilder = new HashMap<>();
int contextSize = in.readVInt();
for (int i = 0; i < contextSize; i++) {
contextMapBuilder.put(in.readString(), in.readOptionalWriteable(Context::new));
}
contexts = Collections.unmodifiableMap(contextMapBuilder);
}
}

public String[] indices() {
Expand Down Expand Up @@ -214,6 +229,11 @@ public Map<String, Settings> getSettings() {
return settings();
}


public Map<String, Context> contexts() {
return contexts;
}

/**
* Returns the string value for the specified index and setting. If the includeDefaults flag was not set or set to
* false on the {@link GetIndexRequest}, this method will only return a value where the setting was explicitly set
Expand Down Expand Up @@ -277,6 +297,14 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(indexEntry.getKey());
out.writeOptionalString(indexEntry.getValue());
}

if (out.getVersion().before(Version.V_3_0_0)) {
out.writeVInt(contexts.size());
for (final Map.Entry<String, Context> indexEntry : contexts.entrySet()) {
out.writeString(indexEntry.getKey());
out.writeOptionalWriteable(indexEntry.getValue());
}
}
}

@Override
Expand Down Expand Up @@ -320,6 +348,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (dataStream != null) {
builder.field("data_stream", dataStream);
}

Context context = contexts.get(index);
if (context != null) {
builder.field("context", context);
}
}
builder.endObject();
}
Expand All @@ -343,11 +376,12 @@ public boolean equals(Object o) {
&& Objects.equals(mappings, that.mappings)
&& Objects.equals(settings, that.settings)
&& Objects.equals(defaultSettings, that.defaultSettings)
&& Objects.equals(dataStreams, that.dataStreams);
&& Objects.equals(dataStreams, that.dataStreams)
&& Objects.equals(contexts, that.contexts);
}

@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(indices), aliases, mappings, settings, defaultSettings, dataStreams);
return Objects.hash(Arrays.hashCode(indices), aliases, mappings, settings, defaultSettings, dataStreams, contexts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
package org.opensearch.action.admin.indices.get;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.TransportAction;
import org.opensearch.action.support.clustermanager.info.TransportClusterInfoAction;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.Context;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.metadata.MappingMetadata;
Expand Down Expand Up @@ -110,6 +112,7 @@ protected void doClusterManagerOperation(
Map<String, List<AliasMetadata>> aliasesResult = Map.of();
Map<String, Settings> settings = Map.of();
Map<String, Settings> defaultSettings = Map.of();
Map<String, Context> contexts = Map.of();
final Map<String, String> dataStreams = new HashMap<>(
StreamSupport.stream(Spliterators.spliterator(state.metadata().findDataStreams(concreteIndices).entrySet(), 0), false)
.collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue().getName()))
Expand All @@ -118,6 +121,7 @@ protected void doClusterManagerOperation(
boolean doneAliases = false;
boolean doneMappings = false;
boolean doneSettings = false;
boolean doneContext = false;
for (GetIndexRequest.Feature feature : features) {
switch (feature) {
case MAPPINGS:
Expand Down Expand Up @@ -159,11 +163,23 @@ protected void doClusterManagerOperation(
doneSettings = true;
}
break;

case CONTEXT:
if (!doneContext) {
final Map<String, Context> contextBuilder = new HashMap<>();
for (String index : concreteIndices) {
Context indexContext = state.metadata().index(index).context();
if (indexContext != null) {
contextBuilder.put(index, indexContext);
}
}
contexts = contextBuilder;
doneContext = true;
}
break;
default:
throw new IllegalStateException("feature [" + feature + "] is not valid");
}
}
listener.onResponse(new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams));
listener.onResponse(new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams, contexts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.startObject();
builder.field(NAME.getPreferredName(), this.name);
builder.field(VERSION.getPreferredName(), this.version);
if (params != null) {
if (this.params != null) {
builder.field(PARAMS.getPreferredName(), this.params);
}
builder.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
isSystem = in.readBoolean();
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
context = in.readOptionalWriteable(Context::new);
} else {
context = null;
}
}

Expand Down Expand Up @@ -1242,6 +1244,10 @@ public boolean isSystem() {
return isSystem;
}

public Context context() {
return context;
}

public boolean isRemoteSnapshot() {
return isRemoteSnapshot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ private ClusterState applyCreateIndexWithTemporaryService(
temporaryIndexMeta.getRoutingNumShards(),
sourceMetadata,
temporaryIndexMeta.isSystem(),
temporaryIndexMeta.getCustomData()
temporaryIndexMeta.getCustomData(),
temporaryIndexMeta.context()
);
} catch (Exception e) {
logger.info("failed to build index metadata [{}]", request.index());
Expand Down Expand Up @@ -608,6 +609,10 @@ IndexMetadata buildAndValidateTemporaryIndexMetadata(
tmpImdBuilder.system(isSystem);
addRemoteStoreCustomMetadata(tmpImdBuilder, true);

if (request.context() != null) {
tmpImdBuilder.context(request.context());
}

// Set up everything, now locally create the index to see that things are ok, and apply
IndexMetadata tempMetadata = tmpImdBuilder.build();
validateActiveShardCount(request.waitForActiveShards(), tempMetadata);
Expand Down Expand Up @@ -1280,7 +1285,8 @@ static IndexMetadata buildIndexMetadata(
int routingNumShards,
@Nullable IndexMetadata sourceMetadata,
boolean isSystem,
Map<String, DiffableStringMap> customData
Map<String, DiffableStringMap> customData,
Context context
) {
IndexMetadata.Builder indexMetadataBuilder = createIndexMetadataBuilder(indexName, sourceMetadata, indexSettings, routingNumShards);
indexMetadataBuilder.system(isSystem);
Expand All @@ -1305,6 +1311,8 @@ static IndexMetadata buildIndexMetadata(
indexMetadataBuilder.putCustom(entry.getKey(), entry.getValue());
}

indexMetadataBuilder.context(context);

indexMetadataBuilder.state(IndexMetadata.State.OPEN);
return indexMetadataBuilder.build();
}
Expand Down

0 comments on commit 0a75973

Please sign in to comment.