use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class TransportReplicationActionTests method testRetryOnReplicaWithRealTransport.
public void testRetryOnReplicaWithRealTransport() throws Exception {
final ShardId shardId = new ShardId("test", "_na_", 0);
final ClusterState initialState = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED);
final ShardRouting replica = initialState.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0);
final long primaryTerm = initialState.metadata().index(shardId.getIndexName()).primaryTerm(shardId.id());
// simulate execution of the node holding the replica
final ClusterState stateWithNodes = ClusterState.builder(initialState).nodes(DiscoveryNodes.builder(initialState.nodes()).localNodeId(replica.currentNodeId())).build();
setState(clusterService, stateWithNodes);
AtomicBoolean throwException = new AtomicBoolean(true);
final ReplicationTask task = maybeTask();
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
final Transport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, namedWriteableRegistry, new NoneCircuitBreakerService());
transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
transportService.start();
transportService.acceptIncomingRequests();
AtomicBoolean calledSuccessfully = new AtomicBoolean(false);
TestAction action = new TestAction(Settings.EMPTY, "internal:testActionWithExceptions", transportService, clusterService, shardStateAction, threadPool) {
@Override
protected void shardOperationOnReplica(Request shardRequest, IndexShard replica, ActionListener<ReplicaResult> listener) {
ActionListener.completeWith(listener, () -> {
assertPhase(task, "replica");
if (throwException.get()) {
throw new RetryOnReplicaException(shardId, "simulation");
}
calledSuccessfully.set(true);
return new ReplicaResult();
});
}
};
final PlainActionFuture<TransportResponse> listener = new PlainActionFuture<>();
final Request request = new Request(shardId);
final long checkpoint = randomNonNegativeLong();
final long maxSeqNoOfUpdates = randomNonNegativeLong();
action.handleReplicaRequest(new TransportReplicationAction.ConcreteReplicaRequest<>(request, replica.allocationId().getId(), primaryTerm, checkpoint, maxSeqNoOfUpdates), createTransportChannel(listener), task);
if (listener.isDone()) {
// fail with the exception if there
listener.get();
fail("listener shouldn't be done");
}
// release the waiting
throwException.set(false);
// publish a new state (same as the old state with the version incremented)
setState(clusterService, stateWithNodes);
// Assert that the request was retried, this time successful
assertTrue("action should have been successfully called on retry but was not", calledSuccessfully.get());
transportService.stop();
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class NodeClientHeadersTests method buildClient.
@Override
protected Client buildClient(Settings headersSettings, ActionType[] testedActions) {
Settings settings = HEADER_SETTINGS;
Actions actions = new Actions(settings, threadPool, testedActions);
NodeClient client = new NodeClient(settings, threadPool);
client.initialize(actions, () -> "test", null, new NamedWriteableRegistry(Collections.emptyList()));
return client;
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class MetadataTests method testSerialization.
public void testSerialization() throws IOException {
final Metadata orig = randomMetadata();
final BytesStreamOutput out = new BytesStreamOutput();
orig.writeTo(out);
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
final Metadata fromStreamMeta = Metadata.readFrom(new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry));
assertTrue(Metadata.isGlobalStateEquals(orig, fromStreamMeta));
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class ClusterSerializationTests method testObjectReuseWhenApplyingClusterStateDiff.
public void testObjectReuseWhenApplyingClusterStateDiff() throws Exception {
IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1).build();
IndexTemplateMetadata indexTemplateMetadata = IndexTemplateMetadata.builder("test-template").patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))).build();
Metadata metadata = Metadata.builder().put(indexMetadata, true).put(indexTemplateMetadata).build();
RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index("test")).build();
ClusterState clusterState1 = ClusterState.builder(new ClusterName("clusterName1")).metadata(metadata).routingTable(routingTable).build();
BytesStreamOutput outStream = new BytesStreamOutput();
outStream.setVersion(Version.CURRENT);
clusterState1.writeTo(outStream);
StreamInput inStream = new NamedWriteableAwareStreamInput(outStream.bytes().streamInput(), new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
ClusterState serializedClusterState1 = ClusterState.readFrom(inStream, newNode("node4"));
// Create a new, albeit equal, IndexMetadata object
ClusterState clusterState2 = ClusterState.builder(clusterState1).incrementVersion().metadata(Metadata.builder().put(IndexMetadata.builder(indexMetadata).numberOfReplicas(1).build(), true)).build();
assertNotSame("Should have created a new, equivalent, IndexMetadata object in clusterState2", clusterState1.metadata().index("test"), clusterState2.metadata().index("test"));
ClusterState serializedClusterState2 = updateUsingSerialisedDiff(serializedClusterState1, clusterState2.diff(clusterState1));
assertSame("Unchanged metadata should not create new IndexMetadata objects", serializedClusterState1.metadata().index("test"), serializedClusterState2.metadata().index("test"));
assertSame("Unchanged routing table should not create new IndexRoutingTable objects", serializedClusterState1.routingTable().index("test"), serializedClusterState2.routingTable().index("test"));
// Create a new and different IndexMetadata object
ClusterState clusterState3 = ClusterState.builder(clusterState1).incrementVersion().metadata(Metadata.builder().put(IndexMetadata.builder(indexMetadata).numberOfReplicas(2).build(), true)).build();
ClusterState serializedClusterState3 = updateUsingSerialisedDiff(serializedClusterState2, clusterState3.diff(clusterState2));
assertNotEquals("Should have a new IndexMetadata object", serializedClusterState2.metadata().index("test"), serializedClusterState3.metadata().index("test"));
assertSame("Unchanged routing table should not create new IndexRoutingTable objects", serializedClusterState2.routingTable().index("test"), serializedClusterState3.routingTable().index("test"));
assertSame("nodes", serializedClusterState2.nodes(), serializedClusterState3.nodes());
assertSame("blocks", serializedClusterState2.blocks(), serializedClusterState3.blocks());
assertSame("template", serializedClusterState2.metadata().templates().get("test-template"), serializedClusterState3.metadata().templates().get("test-template"));
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class ClusterSerializationTests method testClusterStateSerialization.
public void testClusterStateSerialization() throws Exception {
Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1)).build();
RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index("test")).build();
DiscoveryNodes nodes = DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).localNodeId("node1").masterNodeId("node2").build();
ClusterState clusterState = ClusterState.builder(new ClusterName("clusterName1")).nodes(nodes).metadata(metadata).routingTable(routingTable).build();
AllocationService strategy = createAllocationService();
clusterState = ClusterState.builder(clusterState).routingTable(strategy.reroute(clusterState, "reroute").routingTable()).build();
ClusterState serializedClusterState = ClusterState.Builder.fromBytes(ClusterState.Builder.toBytes(clusterState), newNode("node1"), new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
assertThat(serializedClusterState.getClusterName().value(), equalTo(clusterState.getClusterName().value()));
assertThat(serializedClusterState.routingTable().toString(), equalTo(clusterState.routingTable().toString()));
}
Aggregations