use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class ScaleCubeDirectMarshallerTransport method fromMessage.
/**
* Wrap ScaleCube {@link Message} with {@link NetworkMessage}.
*
* @param message ScaleCube message.
* @return Netowork message that wraps ScaleCube message.
* @throws IgniteInternalException If failed to write message to ObjectOutputStream.
*/
private NetworkMessage fromMessage(Message message) throws IgniteInternalException {
Object dataObj = message.data();
ScaleCubeMessageBuilder scaleCubeMessageBuilder = messageFactory.scaleCubeMessage();
if (dataObj instanceof NetworkMessage) {
// If data object is a network message, we can use direct marshaller to serialize it
scaleCubeMessageBuilder.message((NetworkMessage) dataObj);
} else {
// If data object is not a network message user object marshaller will be used
scaleCubeMessageBuilder.data(dataObj);
}
return scaleCubeMessageBuilder.headers(message.headers()).build();
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class InboundDecoderTest method sendAndReceive.
/**
* Serializes and then deserializes the given message.
*/
private <T extends NetworkMessage> T sendAndReceive(T msg) {
var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
var perSessionSerializationService = new PerSessionSerializationService(serializationService);
var channel = new EmbeddedChannel(new InboundDecoder(perSessionSerializationService));
var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
ByteBuffer buf = ByteBuffer.allocate(10_000);
T received;
do {
buf.clear();
writer.setBuffer(buf);
serializer.writeMessage(msg, writer);
buf.flip();
ByteBuf buffer = allocator.buffer(buf.limit());
buffer.writeBytes(buf);
channel.writeInbound(buffer);
} while ((received = channel.readInbound()) == null);
return received;
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class RaftGroupServiceImpl method transferLeadership.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> transferLeadership(Peer newLeader) {
Peer leader = this.leader;
if (leader == null)
return refreshLeader().thenCompose(res -> transferLeadership(newLeader));
TransferLeaderRequest req = factory.transferLeaderRequest().groupId(groupId).leaderId(PeerId.fromPeer(newLeader).toString()).build();
CompletableFuture<NetworkMessage> fut = cluster.messagingService().invoke(newLeader.address(), req, rpcTimeout);
return fut.thenCompose(resp -> {
if (resp != null) {
RpcRequests.ErrorResponse resp0 = (RpcRequests.ErrorResponse) resp;
if (resp0.errorCode() != RaftError.SUCCESS.getNumber())
CompletableFuture.failedFuture(new RaftException(RaftError.forNumber(resp0.errorCode()), resp0.errorMsg()));
else
this.leader = newLeader;
}
return CompletableFuture.completedFuture(null);
});
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class RaftGroupServiceImpl method snapshot.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> snapshot(Peer peer) {
SnapshotRequest req = factory.snapshotRequest().groupId(groupId).build();
// Disable the timeout for a snapshot request.
CompletableFuture<NetworkMessage> fut = cluster.messagingService().invoke(peer.address(), req, Integer.MAX_VALUE);
return fut.thenCompose(resp -> {
if (resp != null) {
RpcRequests.ErrorResponse resp0 = (RpcRequests.ErrorResponse) resp;
if (resp0.errorCode() != RaftError.SUCCESS.getNumber())
return CompletableFuture.failedFuture(new RaftException(RaftError.forNumber(resp0.errorCode()), resp0.errorMsg()));
}
return CompletableFuture.completedFuture(null);
});
}
Aggregations