Skip to content

Commit

Permalink
[Backport 2.x] [BUG] Bug fix for checksum validation for mapping met…
Browse files Browse the repository at this point in the history
…adata opensearch-project#15888  (opensearch-project#15890) (opensearch-project#15908)

* [BUG] Bug fix for checksum validation for mapping metadata (opensearch-project#15885)

Signed-off-by: Himshikha Gupta <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] committed Sep 12, 2024
1 parent 07d1e16 commit 1dba8c3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ public void writeVerifiableTo(BufferedChecksumStreamOutput out) throws IOExcepti
out.writeByte(state.id());
writeSettingsToStream(settings, out);
out.writeVLongArray(primaryTerms);
out.writeMapValues(mappings, (stream, val) -> val.writeTo(stream));
out.writeMapValues(mappings, (stream, val) -> val.writeVerifiableTo((BufferedChecksumStreamOutput) stream));
out.writeMapValues(aliases, (stream, val) -> val.writeTo(stream));
out.writeMap(customData, StreamOutput::writeString, (stream, val) -> val.writeTo(stream));
out.writeMap(
Expand All @@ -1314,6 +1314,44 @@ public void writeVerifiableTo(BufferedChecksumStreamOutput out) throws IOExcepti
}
}

@Override
public String toString() {
return new StringBuilder().append("IndexMetadata{routingNumShards=")
.append(routingNumShards)
.append(", index=")
.append(index)
.append(", version=")
.append(version)
.append(", state=")
.append(state)
.append(", settingsVersion=")
.append(settingsVersion)
.append(", mappingVersion=")
.append(mappingVersion)
.append(", aliasesVersion=")
.append(aliasesVersion)
.append(", primaryTerms=")
.append(Arrays.toString(primaryTerms))
.append(", aliases=")
.append(aliases)
.append(", settings=")
.append(settings)
.append(", mappings=")
.append(mappings)
.append(", customData=")
.append(customData)
.append(", inSyncAllocationIds=")
.append(inSyncAllocationIds)
.append(", rolloverInfos=")
.append(rolloverInfos)
.append(", isSystem=")
.append(isSystem)
.append(", context=")
.append(context)
.append("}")
.toString();
}

public boolean isSystem() {
return isSystem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.mapper.DocumentMapper;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.translog.BufferedChecksumStreamOutput;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -168,6 +169,12 @@ public void writeTo(StreamOutput out) throws IOException {
}
}

public void writeVerifiableTo(BufferedChecksumStreamOutput out) throws IOException {
out.writeString(type());
source().writeVerifiableTo(out);
out.writeBoolean(routingRequired);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.opensearch.core.compress.CompressorRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.translog.BufferedChecksumStreamOutput;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -169,6 +170,10 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeByteArray(bytes);
}

public void writeVerifiableTo(BufferedChecksumStreamOutput out) throws IOException {
out.writeInt(crc32);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,30 @@ public void testWriteVerifiableTo() throws IOException {
),
randomNonNegativeLong()
);

String mappings = " {\n"
+ " \"_doc\": {\n"
+ " \"properties\": {\n"
+ " \"actiongroups\": {\n"
+ " \"type\": \"text\",\n"
+ " \"fields\": {\n"
+ " \"keyword\": {\n"
+ " \"type\": \"keyword\",\n"
+ " \"ignore_above\": 256\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"allowlist\": {\n"
+ " \"type\": \"text\",\n"
+ " \"fields\": {\n"
+ " \"keyword\": {\n"
+ " \"type\": \"keyword\",\n"
+ " \"ignore_above\": 256\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }";
IndexMetadata metadata1 = IndexMetadata.builder("foo")
.settings(
Settings.builder()
Expand All @@ -220,11 +243,13 @@ public void testWriteVerifiableTo() throws IOException {
.putRolloverInfo(info1)
.putRolloverInfo(info2)
.putInSyncAllocationIds(0, Set.of("1", "2", "3"))
.putMapping(mappings)
.build();

BytesStreamOutput out = new BytesStreamOutput();
BufferedChecksumStreamOutput checksumOut = new BufferedChecksumStreamOutput(out);
metadata1.writeVerifiableTo(checksumOut);
assertNotNull(metadata1.toString());

IndexMetadata metadata2 = IndexMetadata.builder(metadata1.getIndex().getName())
.settings(
Expand All @@ -246,6 +271,7 @@ public void testWriteVerifiableTo() throws IOException {
.putRolloverInfo(info2)
.putRolloverInfo(info1)
.putInSyncAllocationIds(0, Set.of("3", "1", "2"))
.putMapping(mappings)
.build();

BytesStreamOutput out2 = new BytesStreamOutput();
Expand Down

0 comments on commit 1dba8c3

Please sign in to comment.