Search in sources :

Example 6 with NamedWriteableAwareStreamInput

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()));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 7 with NamedWriteableAwareStreamInput

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()));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 8 with NamedWriteableAwareStreamInput

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());
                }
            }
        }
    }
}
Also used : GeometryTestUtils.randomGeometry(org.opensearch.geo.GeometryTestUtils.randomGeometry) Geometry(org.opensearch.geometry.Geometry) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 9 with NamedWriteableAwareStreamInput

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());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AllocateEmptyPrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 10 with NamedWriteableAwareStreamInput

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());
    }
}
Also used : MaxDocsCondition(org.opensearch.action.admin.indices.rollover.MaxDocsCondition) HashMap(java.util.HashMap) MaxAgeCondition(org.opensearch.action.admin.indices.rollover.MaxAgeCondition) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) RolloverInfo(org.opensearch.action.admin.indices.rollover.RolloverInfo) MaxSizeCondition(org.opensearch.action.admin.indices.rollover.MaxSizeCondition) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)43 StreamInput (org.opensearch.common.io.stream.StreamInput)37 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)32 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)21 Version (org.opensearch.Version)11 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 QueryBuilder (org.opensearch.index.query.QueryBuilder)5 SearchModule (org.opensearch.search.SearchModule)5 InputStream (java.io.InputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ClusterState (org.opensearch.cluster.ClusterState)4 BytesReference (org.opensearch.common.bytes.BytesReference)4 AliasFilter (org.opensearch.search.internal.AliasFilter)4 ArrayList (java.util.ArrayList)3 LegacyESVersion (org.opensearch.LegacyESVersion)3 ShardId (org.opensearch.index.shard.ShardId)3 HashMap (java.util.HashMap)2