Search in sources :

Example 6 with NamedWriteableRegistry

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

the class RatedRequestsTests method copy.

private static RatedRequest copy(RatedRequest original) throws IOException {
    List<NamedWriteableRegistry.Entry> namedWriteables = new ArrayList<>();
    namedWriteables.add(new NamedWriteableRegistry.Entry(QueryBuilder.class, MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new));
    return OpenSearchTestCase.copyWriteable(original, new NamedWriteableRegistry(namedWriteables), RatedRequest::new);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 7 with NamedWriteableRegistry

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

the class ClusterStateDiffIT method testClusterStateDiffSerialization.

public void testClusterStateDiffSerialization() throws Exception {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
    DiscoveryNode masterNode = randomNode("master");
    DiscoveryNode otherNode = randomNode("other");
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(masterNode).add(otherNode).localNodeId(masterNode.getId()).build();
    ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(discoveryNodes).build();
    ClusterState clusterStateFromDiffs = ClusterState.Builder.fromBytes(ClusterState.Builder.toBytes(clusterState), otherNode, namedWriteableRegistry);
    int iterationCount = randomIntBetween(10, 300);
    for (int iteration = 0; iteration < iterationCount; iteration++) {
        ClusterState previousClusterState = clusterState;
        ClusterState previousClusterStateFromDiffs = clusterStateFromDiffs;
        int changesCount = randomIntBetween(1, 4);
        ClusterState.Builder builder = null;
        for (int i = 0; i < changesCount; i++) {
            if (i > 0) {
                clusterState = builder.build();
            }
            switch(randomInt(5)) {
                case 0:
                    builder = randomNodes(clusterState);
                    break;
                case 1:
                    builder = randomRoutingTable(clusterState);
                    break;
                case 2:
                    builder = randomBlocks(clusterState);
                    break;
                case 3:
                    builder = randomClusterStateCustoms(clusterState);
                    break;
                case 4:
                    builder = randomMetadataChanges(clusterState);
                    break;
                case 5:
                    builder = randomCoordinationMetadata(clusterState);
                    break;
                default:
                    throw new IllegalArgumentException("Shouldn't be here");
            }
        }
        clusterState = builder.incrementVersion().build();
        if (randomIntBetween(0, 10) < 1) {
            // Update cluster state via full serialization from time to time
            clusterStateFromDiffs = ClusterState.Builder.fromBytes(ClusterState.Builder.toBytes(clusterState), previousClusterStateFromDiffs.nodes().getLocalNode(), namedWriteableRegistry);
        } else {
            // Update cluster states using diffs
            Diff<ClusterState> diffBeforeSerialization = clusterState.diff(previousClusterState);
            BytesStreamOutput os = new BytesStreamOutput();
            diffBeforeSerialization.writeTo(os);
            byte[] diffBytes = BytesReference.toBytes(os.bytes());
            Diff<ClusterState> diff;
            try (StreamInput input = StreamInput.wrap(diffBytes)) {
                StreamInput namedInput = new NamedWriteableAwareStreamInput(input, namedWriteableRegistry);
                diff = ClusterState.readDiffFrom(namedInput, previousClusterStateFromDiffs.nodes().getLocalNode());
                clusterStateFromDiffs = diff.apply(previousClusterStateFromDiffs);
            }
        }
        try {
            // Check non-diffable elements
            assertThat(clusterStateFromDiffs.version(), equalTo(clusterState.version()));
            assertThat(clusterStateFromDiffs.coordinationMetadata(), equalTo(clusterState.coordinationMetadata()));
            // Check nodes
            assertThat(clusterStateFromDiffs.nodes().getNodes(), equalTo(clusterState.nodes().getNodes()));
            assertThat(clusterStateFromDiffs.nodes().getLocalNodeId(), equalTo(previousClusterStateFromDiffs.nodes().getLocalNodeId()));
            assertThat(clusterStateFromDiffs.nodes().getNodes(), equalTo(clusterState.nodes().getNodes()));
            for (ObjectCursor<String> node : clusterStateFromDiffs.nodes().getNodes().keys()) {
                DiscoveryNode node1 = clusterState.nodes().get(node.value);
                DiscoveryNode node2 = clusterStateFromDiffs.nodes().get(node.value);
                assertThat(node1.getVersion(), equalTo(node2.getVersion()));
                assertThat(node1.getAddress(), equalTo(node2.getAddress()));
                assertThat(node1.getAttributes(), equalTo(node2.getAttributes()));
            }
            // Check routing table
            assertThat(clusterStateFromDiffs.routingTable().version(), equalTo(clusterState.routingTable().version()));
            assertThat(clusterStateFromDiffs.routingTable().indicesRouting(), equalTo(clusterState.routingTable().indicesRouting()));
            // Check cluster blocks
            assertThat(clusterStateFromDiffs.blocks().global(), equalTo(clusterStateFromDiffs.blocks().global()));
            assertThat(clusterStateFromDiffs.blocks().indices(), equalTo(clusterStateFromDiffs.blocks().indices()));
            assertThat(clusterStateFromDiffs.blocks().disableStatePersistence(), equalTo(clusterStateFromDiffs.blocks().disableStatePersistence()));
            // Check metadata
            assertThat(clusterStateFromDiffs.metadata().version(), equalTo(clusterState.metadata().version()));
            assertThat(clusterStateFromDiffs.metadata().clusterUUID(), equalTo(clusterState.metadata().clusterUUID()));
            assertThat(clusterStateFromDiffs.metadata().transientSettings(), equalTo(clusterState.metadata().transientSettings()));
            assertThat(clusterStateFromDiffs.metadata().persistentSettings(), equalTo(clusterState.metadata().persistentSettings()));
            assertThat(clusterStateFromDiffs.metadata().indices(), equalTo(clusterState.metadata().indices()));
            assertThat(clusterStateFromDiffs.metadata().templates(), equalTo(clusterState.metadata().templates()));
            assertThat(clusterStateFromDiffs.metadata().customs(), equalTo(clusterState.metadata().customs()));
            assertThat(clusterStateFromDiffs.metadata().equalsAliases(clusterState.metadata()), is(true));
            // JSON Serialization test - make sure that both states produce similar JSON
            assertNull(differenceBetweenMapsIgnoringArrayOrder(convertToMap(clusterStateFromDiffs), convertToMap(clusterState)));
            // Smoke test - we cannot compare bytes to bytes because some elements might get serialized in different order
            // however, serialized size should remain the same
            assertThat(ClusterState.Builder.toBytes(clusterStateFromDiffs).length, equalTo(ClusterState.Builder.toBytes(clusterState).length));
        } catch (AssertionError error) {
            logger.error("Cluster state:\n{}\nCluster state from diffs:\n{}", clusterState.toString(), clusterStateFromDiffs.toString());
            throw error;
        }
    }
    logger.info("Final cluster state:[{}]", clusterState.toString());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 8 with NamedWriteableRegistry

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

the class MockTransportService method newMockTransport.

public static MockNioTransport newMockTransport(Settings settings, Version version, ThreadPool threadPool) {
    settings = Settings.builder().put(TransportSettings.PORT.getKey(), OpenSearchTestCase.getPortRange()).put(settings).build();
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
    return new MockNioTransport(settings, version, threadPool, new NetworkService(Collections.emptyList()), new MockPageCacheRecycler(settings), namedWriteableRegistry, new NoneCircuitBreakerService());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 9 with NamedWriteableRegistry

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

the class SimpleMockNioTransportTests method build.

@Override
protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    NetworkService networkService = new NetworkService(Collections.emptyList());
    return new MockNioTransport(settings, version, threadPool, networkService, new MockPageCacheRecycler(settings), namedWriteableRegistry, new NoneCircuitBreakerService()) {

        @Override
        public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, ActionListener<Version> listener) {
            if (doHandshake) {
                super.executeHandshake(node, channel, profile, listener);
            } else {
                listener.onResponse(version.minimumCompatibilityVersion());
            }
        }
    };
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ActionListener(org.opensearch.action.ActionListener) TcpChannel(org.opensearch.transport.TcpChannel) NetworkService(org.opensearch.common.network.NetworkService) ConnectionProfile(org.opensearch.transport.ConnectionProfile) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 10 with NamedWriteableRegistry

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

the class RestValidateQueryActionTests method stubValidateQueryAction.

/**
 * Configures {@link NodeClient} to stub {@link ValidateQueryAction} transport action.
 * <p>
 * This lower level of validation is out of the scope of this test.
 */
@BeforeClass
public static void stubValidateQueryAction() {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    final TransportAction transportAction = new TransportAction(ValidateQueryAction.NAME, new ActionFilters(Collections.emptySet()), taskManager) {

        @Override
        protected void doExecute(Task task, ActionRequest request, ActionListener listener) {
        }
    };
    final Map<ActionType, TransportAction> actions = new HashMap<>();
    actions.put(ValidateQueryAction.INSTANCE, transportAction);
    client.initialize(actions, () -> "local", null, new NamedWriteableRegistry(Collections.emptyList()));
    controller.registerHandler(action);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Task(org.opensearch.tasks.Task) TaskManager(org.opensearch.tasks.TaskManager) ActionType(org.opensearch.action.ActionType) ActionListener(org.opensearch.action.ActionListener) ActionRequest(org.opensearch.action.ActionRequest) HashMap(java.util.HashMap) TransportAction(org.opensearch.action.support.TransportAction) ActionFilters(org.opensearch.action.support.ActionFilters) BeforeClass(org.junit.BeforeClass)

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