use of org.opensearch.common.io.stream.NamedWriteableAwareStreamInput in project OpenSearch by opensearch-project.
the class MetadataTests method testSerializationWithIndexGraveyard.
public void testSerializationWithIndexGraveyard() throws IOException {
final IndexGraveyard graveyard = IndexGraveyardTests.createRandom();
final Metadata originalMeta = Metadata.builder().indexGraveyard(graveyard).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.indexGraveyard(), equalTo(fromStreamMeta.indexGraveyard()));
}
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());
}
}
Aggregations