Search in sources :

Example 31 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 32 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 33 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 34 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)

Example 35 with NamedWriteableAwareStreamInput

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());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.opensearch.cluster.ClusterState) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) SnapshotDeletionsInProgress(org.opensearch.cluster.SnapshotDeletionsInProgress) SnapshotId(org.opensearch.snapshots.SnapshotId) Snapshot(org.opensearch.snapshots.Snapshot) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) Version(org.opensearch.Version) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)

Aggregations

NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)74 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)63 StreamInput (org.opensearch.common.io.stream.StreamInput)37 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)21 Version (org.opensearch.Version)11 Test (org.junit.Test)8 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)8 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)7 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)7 ADTask (org.opensearch.ad.model.ADTask)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 QueryBuilder (org.opensearch.index.query.QueryBuilder)5 SearchModule (org.opensearch.search.SearchModule)5 InputStream (java.io.InputStream)4 Map (java.util.Map)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