Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [BUG] Bug fix for checksum validation for mapping metadata #15888

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -41,8 +41,10 @@
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BufferedChecksumStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.VerifiableWriteable;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.mapper.DocumentMapper;
import org.opensearch.index.mapper.MapperService;
Expand All @@ -61,7 +63,7 @@
* @opensearch.api
*/
@PublicApi(since = "1.0.0")
public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
public class MappingMetadata extends AbstractDiffable<MappingMetadata> implements VerifiableWriteable {
public static final MappingMetadata EMPTY_MAPPINGS = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, Collections.emptyMap());

private final String type;
Expand Down Expand Up @@ -168,6 +170,13 @@ public void writeTo(StreamOutput out) throws IOException {
}
}

@Override
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 @@ -38,6 +38,7 @@
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BufferedChecksumStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.compress.Compressor;
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
Loading