Search in sources :

Example 16 with NetworkMessage

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();
}
Also used : ScaleCubeMessageBuilder(org.apache.ignite.internal.network.message.ScaleCubeMessageBuilder) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 17 with NetworkMessage

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;
}
Also used : DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) NetworkMessage(org.apache.ignite.network.NetworkMessage) ByteBuf(io.netty.buffer.ByteBuf) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) ByteBuffer(java.nio.ByteBuffer)

Example 18 with NetworkMessage

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);
    });
}
Also used : System.currentTimeMillis(java.lang.System.currentTimeMillis) IgniteException(org.apache.ignite.lang.IgniteException) AddPeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerRequest) S(org.apache.ignite.internal.tostring.S) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) AddPeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerResponse) TransferLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.TransferLeaderRequest) ArrayList(java.util.ArrayList) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) RemoveLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemoveLearnersRequest) ResetLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ResetLearnersRequest) RemovePeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerRequest) ActionResponse(org.apache.ignite.raft.jraft.rpc.ActionResponse) ChangePeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) Collection(java.util.Collection) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) NetworkMessage(org.apache.ignite.network.NetworkMessage) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) ActionRequest(org.apache.ignite.raft.jraft.rpc.ActionRequest) GetPeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest) NetworkAddress(org.apache.ignite.network.NetworkAddress) ReadCommand(org.apache.ignite.raft.client.ReadCommand) GetPeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) List(java.util.List) Peer(org.apache.ignite.raft.client.Peer) RemovePeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerResponse) SnapshotRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.SnapshotRequest) ClusterService(org.apache.ignite.network.ClusterService) LearnersOpResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.LearnersOpResponse) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ChangePeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersRequest) AddLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.AddLearnersRequest) RaftError(org.apache.ignite.raft.jraft.error.RaftError) Peer(org.apache.ignite.raft.client.Peer) TransferLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.TransferLeaderRequest) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 19 with NetworkMessage

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);
    });
}
Also used : SnapshotRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.SnapshotRequest) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Aggregations

NetworkMessage (org.apache.ignite.network.NetworkMessage)19 CompletableFuture (java.util.concurrent.CompletableFuture)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 ClusterService (org.apache.ignite.network.ClusterService)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NetworkAddress (org.apache.ignite.network.NetworkAddress)4 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)4 TestMessage (org.apache.ignite.network.TestMessage)4 ByteBuf (io.netty.buffer.ByteBuf)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 TimeUnit (java.util.concurrent.TimeUnit)3 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)3 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)3 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)3 Nullable (org.jetbrains.annotations.Nullable)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2