Search in sources :

Example 36 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry 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 37 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry 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 38 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class SearchContextIdTests method testEncode.

public void testEncode() {
    final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Arrays.asList(new NamedWriteableRegistry.Entry(QueryBuilder.class, TermQueryBuilder.NAME, TermQueryBuilder::new), new NamedWriteableRegistry.Entry(QueryBuilder.class, MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new), new NamedWriteableRegistry.Entry(QueryBuilder.class, IdsQueryBuilder.NAME, IdsQueryBuilder::new)));
    final AtomicArray<SearchPhaseResult> queryResults = TransportSearchHelperTests.generateQueryResults();
    final Version version = Version.CURRENT;
    final Map<String, AliasFilter> aliasFilters = new HashMap<>();
    for (SearchPhaseResult result : queryResults.asList()) {
        final AliasFilter aliasFilter;
        if (randomBoolean()) {
            aliasFilter = new AliasFilter(randomQueryBuilder());
        } else if (randomBoolean()) {
            aliasFilter = new AliasFilter(randomQueryBuilder(), "alias-" + between(1, 10));
        } else {
            aliasFilter = AliasFilter.EMPTY;
        }
        if (randomBoolean()) {
            aliasFilters.put(result.getSearchShardTarget().getShardId().getIndex().getUUID(), aliasFilter);
        }
    }
    final String id = SearchContextId.encode(queryResults.asList(), aliasFilters, version);
    final SearchContextId context = SearchContextId.decode(namedWriteableRegistry, id);
    assertThat(context.shards().keySet(), hasSize(3));
    assertThat(context.aliasFilter(), equalTo(aliasFilters));
    SearchContextIdForNode node1 = context.shards().get(new ShardId("idx", "uuid1", 2));
    assertThat(node1.getClusterAlias(), equalTo("cluster_x"));
    assertThat(node1.getNode(), equalTo("node_1"));
    assertThat(node1.getSearchContextId().getId(), equalTo(1L));
    assertThat(node1.getSearchContextId().getSessionId(), equalTo("a"));
    SearchContextIdForNode node2 = context.shards().get(new ShardId("idy", "uuid2", 42));
    assertThat(node2.getClusterAlias(), equalTo("cluster_y"));
    assertThat(node2.getNode(), equalTo("node_2"));
    assertThat(node2.getSearchContextId().getId(), equalTo(12L));
    assertThat(node2.getSearchContextId().getSessionId(), equalTo("b"));
    SearchContextIdForNode node3 = context.shards().get(new ShardId("idy", "uuid2", 43));
    assertThat(node3.getClusterAlias(), nullValue());
    assertThat(node3.getNode(), equalTo("node_3"));
    assertThat(node3.getSearchContextId().getId(), equalTo(42L));
    assertThat(node3.getSearchContextId().getSessionId(), equalTo("c"));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AliasFilter(org.opensearch.search.internal.AliasFilter) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) HashMap(java.util.HashMap) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) ShardId(org.opensearch.index.shard.ShardId) Version(org.opensearch.Version) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 39 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry 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)

Example 40 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class ClusterSerializationTests method testCustomSerialization.

public void testCustomSerialization() throws Exception {
    ClusterState.Builder builder = ClusterState.builder(ClusterState.EMPTY_STATE).putCustom(TestCustomOne.TYPE, new TestCustomOne("test_custom_one")).putCustom(TestCustomTwo.TYPE, new TestCustomTwo(10));
    ClusterState clusterState = builder.incrementVersion().build();
    Diff<ClusterState> diffs = clusterState.diff(ClusterState.EMPTY_STATE);
    // Add the new customs to named writeables
    final List<NamedWriteableRegistry.Entry> entries = ClusterModule.getNamedWriteables();
    entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TestCustomOne.TYPE, TestCustomOne::new));
    entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, TestCustomOne.TYPE, TestCustomOne::readDiffFrom));
    entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TestCustomTwo.TYPE, TestCustomTwo::new));
    entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, TestCustomTwo.TYPE, TestCustomTwo::readDiffFrom));
    // serialize with current version
    BytesStreamOutput outStream = new BytesStreamOutput();
    Version version = Version.CURRENT;
    outStream.setVersion(version);
    diffs.writeTo(outStream);
    StreamInput inStream = outStream.bytes().streamInput();
    inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(entries));
    inStream.setVersion(version);
    Diff<ClusterState> serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
    ClusterState stateAfterDiffs = serializedDiffs.apply(ClusterState.EMPTY_STATE);
    // Current version - Both the customs are non null
    assertThat(stateAfterDiffs.custom(TestCustomOne.TYPE), notNullValue());
    assertThat(stateAfterDiffs.custom(TestCustomTwo.TYPE), notNullValue());
    // serialize with minimum compatibile version
    outStream = new BytesStreamOutput();
    version = Version.CURRENT.minimumCompatibilityVersion();
    outStream.setVersion(version);
    diffs.writeTo(outStream);
    inStream = outStream.bytes().streamInput();
    inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(entries));
    inStream.setVersion(version);
    serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
    stateAfterDiffs = serializedDiffs.apply(ClusterState.EMPTY_STATE);
    // Old version - TestCustomOne is null and TestCustomTwo is not null
    assertThat(stateAfterDiffs.custom(TestCustomOne.TYPE), nullValue());
    assertThat(stateAfterDiffs.custom(TestCustomTwo.TYPE), notNullValue());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.opensearch.cluster.ClusterState) Custom(org.opensearch.cluster.ClusterState.Custom) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) NamedDiff(org.opensearch.cluster.NamedDiff) 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

NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)117 ThreadPool (org.opensearch.threadpool.ThreadPool)41 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)37 ClusterService (org.opensearch.cluster.service.ClusterService)37 TestThreadPool (org.opensearch.threadpool.TestThreadPool)37 AsynchronousSearchActiveStore (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore)33 InternalAsynchronousSearchStats (org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats)32 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)30 TimeValue (org.opensearch.common.unit.TimeValue)29 SearchRequest (org.opensearch.action.search.SearchRequest)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 User (org.opensearch.commons.authuser.User)24 AsynchronousSearchActiveContext (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext)23 AsynchronousSearchTask (org.opensearch.search.asynchronous.task.AsynchronousSearchTask)22 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)21 SearchModule (org.opensearch.search.SearchModule)20 StreamInput (org.opensearch.common.io.stream.StreamInput)19 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)18 ArrayList (java.util.ArrayList)16 Version (org.opensearch.Version)16