use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class MetadataTests method testSerializationClusterUUID.
public void testSerializationClusterUUID() throws IOException {
final Metadata originalMeta = Metadata.builder().clusterUUID(UUIDs.randomBase64UUID()).clusterUUIDCommitted(randomBoolean()).build();
final BytesStreamOutput out = new BytesStreamOutput();
originalMeta.writeTo(out);
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
final Metadata fromStreamMeta = Metadata.readFrom(new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry));
assertThat(fromStreamMeta.clusterUUID(), equalTo(originalMeta.clusterUUID()));
assertThat(fromStreamMeta.clusterUUIDCommitted(), equalTo(originalMeta.clusterUUIDCommitted()));
}
use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class GeometryIOTests method testRandomSerialization.
public void testRandomSerialization() throws Exception {
for (int i = 0; i < randomIntBetween(1, 20); i++) {
boolean hasAlt = randomBoolean();
Geometry geometry = randomGeometry(hasAlt);
if (shapeSupported(geometry) && randomBoolean()) {
// Shape builder conversion doesn't support altitude
ShapeBuilder<?, ?, ?> shapeBuilder = geometryToShapeBuilder(geometry);
if (randomBoolean()) {
Geometry actual = shapeBuilder.buildGeometry();
assertEquals(geometry, actual);
}
if (randomBoolean()) {
// Test ShapeBuilder -> Geometry Serialization
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeNamedWriteable(shapeBuilder);
try (StreamInput in = out.bytes().streamInput()) {
Geometry actual = GeometryIO.readGeometry(in);
assertEquals(geometry, actual);
assertEquals(0, in.available());
}
}
} else {
// Test Geometry -> ShapeBuilder Serialization
try (BytesStreamOutput out = new BytesStreamOutput()) {
GeometryIO.writeGeometry(out, geometry);
try (StreamInput in = out.bytes().streamInput()) {
try (StreamInput nin = new NamedWriteableAwareStreamInput(in, this.writableRegistry())) {
ShapeBuilder<?, ?, ?> actual = nin.readNamedWriteable(ShapeBuilder.class);
assertEquals(shapeBuilder, actual);
assertEquals(0, in.available());
}
}
}
}
// Test Geometry -> Geometry
try (BytesStreamOutput out = new BytesStreamOutput()) {
GeometryIO.writeGeometry(out, geometry);
;
try (StreamInput in = out.bytes().streamInput()) {
Geometry actual = GeometryIO.readGeometry(in);
assertEquals(geometry, actual);
assertEquals(0, in.available());
}
}
}
}
}
use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class ClusterRerouteTests method testSerializeRequest.
public void testSerializeRequest() throws IOException {
ClusterRerouteRequest req = new ClusterRerouteRequest();
req.setRetryFailed(randomBoolean());
req.dryRun(randomBoolean());
req.explain(randomBoolean());
req.add(new AllocateEmptyPrimaryAllocationCommand("foo", 1, "bar", randomBoolean()));
req.timeout(TimeValue.timeValueMillis(randomIntBetween(0, 100)));
BytesStreamOutput out = new BytesStreamOutput();
req.writeTo(out);
BytesReference bytes = out.bytes();
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(NetworkModule.getNamedWriteables());
StreamInput wrap = new NamedWriteableAwareStreamInput(bytes.streamInput(), namedWriteableRegistry);
ClusterRerouteRequest deserializedReq = new ClusterRerouteRequest(wrap);
assertEquals(req.isRetryFailed(), deserializedReq.isRetryFailed());
assertEquals(req.dryRun(), deserializedReq.dryRun());
assertEquals(req.explain(), deserializedReq.explain());
assertEquals(req.timeout(), deserializedReq.timeout());
// allocation commands have their own tests
assertEquals(1, deserializedReq.getCommands().commands().size());
assertEquals(req.getCommands().commands().size(), deserializedReq.getCommands().commands().size());
}
use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class IndexMetadataTests method testIndexMetadataSerialization.
public void testIndexMetadataSerialization() throws IOException {
Integer numShard = randomFrom(1, 2, 4, 8, 16);
int numberOfReplicas = randomIntBetween(0, 10);
final boolean system = randomBoolean();
Map<String, String> customMap = new HashMap<>();
customMap.put(randomAlphaOfLength(5), randomAlphaOfLength(10));
customMap.put(randomAlphaOfLength(10), randomAlphaOfLength(15));
IndexMetadata metadata = IndexMetadata.builder("foo").settings(Settings.builder().put("index.version.created", 1).put("index.number_of_shards", numShard).put("index.number_of_replicas", numberOfReplicas).build()).creationDate(randomLong()).primaryTerm(0, 2).setRoutingNumShards(32).system(system).putCustom("my_custom", customMap).putRolloverInfo(new RolloverInfo(randomAlphaOfLength(5), Arrays.asList(new MaxAgeCondition(TimeValue.timeValueMillis(randomNonNegativeLong())), new MaxSizeCondition(new ByteSizeValue(randomNonNegativeLong())), new MaxDocsCondition(randomNonNegativeLong())), randomNonNegativeLong())).build();
assertEquals(system, metadata.isSystem());
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
IndexMetadata.FORMAT.toXContent(builder, metadata);
builder.endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
final IndexMetadata fromXContentMeta = IndexMetadata.fromXContent(parser);
assertEquals("expected: " + Strings.toString(metadata) + "\nactual : " + Strings.toString(fromXContentMeta), metadata, fromXContentMeta);
assertEquals(metadata.hashCode(), fromXContentMeta.hashCode());
assertEquals(metadata.getNumberOfReplicas(), fromXContentMeta.getNumberOfReplicas());
assertEquals(metadata.getNumberOfShards(), fromXContentMeta.getNumberOfShards());
assertEquals(metadata.getCreationVersion(), fromXContentMeta.getCreationVersion());
assertEquals(metadata.getRoutingNumShards(), fromXContentMeta.getRoutingNumShards());
assertEquals(metadata.getCreationDate(), fromXContentMeta.getCreationDate());
assertEquals(metadata.getRoutingFactor(), fromXContentMeta.getRoutingFactor());
assertEquals(metadata.primaryTerm(0), fromXContentMeta.primaryTerm(0));
assertEquals(metadata.isSystem(), fromXContentMeta.isSystem());
ImmutableOpenMap.Builder<String, DiffableStringMap> expectedCustomBuilder = ImmutableOpenMap.builder();
expectedCustomBuilder.put("my_custom", new DiffableStringMap(customMap));
ImmutableOpenMap<String, DiffableStringMap> expectedCustom = expectedCustomBuilder.build();
assertEquals(metadata.getCustomData(), expectedCustom);
assertEquals(metadata.getCustomData(), fromXContentMeta.getCustomData());
final BytesStreamOutput out = new BytesStreamOutput();
metadata.writeTo(out);
try (StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry())) {
IndexMetadata deserialized = IndexMetadata.readFrom(in);
assertEquals(metadata, deserialized);
assertEquals(metadata.hashCode(), deserialized.hashCode());
assertEquals(metadata.getNumberOfReplicas(), deserialized.getNumberOfReplicas());
assertEquals(metadata.getNumberOfShards(), deserialized.getNumberOfShards());
assertEquals(metadata.getCreationVersion(), deserialized.getCreationVersion());
assertEquals(metadata.getRoutingNumShards(), deserialized.getRoutingNumShards());
assertEquals(metadata.getCreationDate(), deserialized.getCreationDate());
assertEquals(metadata.getRoutingFactor(), deserialized.getRoutingFactor());
assertEquals(metadata.primaryTerm(0), deserialized.primaryTerm(0));
assertEquals(metadata.getRolloverInfos(), deserialized.getRolloverInfos());
assertEquals(deserialized.getCustomData(), expectedCustom);
assertEquals(metadata.getCustomData(), deserialized.getCustomData());
assertEquals(metadata.isSystem(), deserialized.isSystem());
}
}
use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class ClusterSerializationTests method testSnapshotDeletionsInProgressSerialization.
public void testSnapshotDeletionsInProgressSerialization() throws Exception {
boolean includeRestore = randomBoolean();
ClusterState.Builder builder = ClusterState.builder(ClusterState.EMPTY_STATE).putCustom(SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress.of(Collections.singletonList(new SnapshotDeletionsInProgress.Entry(Collections.singletonList(new SnapshotId("snap1", UUIDs.randomBase64UUID())), "repo1", randomNonNegativeLong(), randomNonNegativeLong(), SnapshotDeletionsInProgress.State.STARTED))));
if (includeRestore) {
builder.putCustom(RestoreInProgress.TYPE, new RestoreInProgress.Builder().add(new RestoreInProgress.Entry(UUIDs.randomBase64UUID(), new Snapshot("repo2", new SnapshotId("snap2", UUIDs.randomBase64UUID())), RestoreInProgress.State.STARTED, Collections.singletonList("index_name"), ImmutableOpenMap.of())).build());
}
ClusterState clusterState = builder.incrementVersion().build();
Diff<ClusterState> diffs = clusterState.diff(ClusterState.EMPTY_STATE);
// serialize with current version
BytesStreamOutput outStream = new BytesStreamOutput();
Version version = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
outStream.setVersion(version);
diffs.writeTo(outStream);
StreamInput inStream = outStream.bytes().streamInput();
inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
inStream.setVersion(version);
Diff<ClusterState> serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
ClusterState stateAfterDiffs = serializedDiffs.apply(ClusterState.EMPTY_STATE);
assertThat(stateAfterDiffs.custom(RestoreInProgress.TYPE), includeRestore ? notNullValue() : nullValue());
assertThat(stateAfterDiffs.custom(SnapshotDeletionsInProgress.TYPE), notNullValue());
// remove the custom and try serializing again
clusterState = ClusterState.builder(clusterState).removeCustom(SnapshotDeletionsInProgress.TYPE).incrementVersion().build();
outStream = new BytesStreamOutput();
outStream.setVersion(version);
diffs.writeTo(outStream);
inStream = outStream.bytes().streamInput();
inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
inStream.setVersion(version);
serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
stateAfterDiffs = serializedDiffs.apply(stateAfterDiffs);
assertThat(stateAfterDiffs.custom(RestoreInProgress.TYPE), includeRestore ? notNullValue() : nullValue());
assertThat(stateAfterDiffs.custom(SnapshotDeletionsInProgress.TYPE), notNullValue());
}
Aggregations