Search in sources :

Example 1 with Message

use of io.dingodb.net.Message in project dingo by dingodb.

the class SendOperator method push.

@Override
public boolean push(@Nonnull Object[] tuple) {
    try {
        if (log.isDebugEnabled()) {
            log.debug("(tag = {}) Send tuple {}...", tag, schema.formatTuple(tuple));
        }
        Message msg = SimpleMessage.builder().tag(TagUtil.getTag(tag)).content(codec.encode(tuple)).build();
        channel.send(msg);
        return true;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Message(io.dingodb.net.Message) SimpleMessage(io.dingodb.net.SimpleMessage) IOException(java.io.IOException)

Example 2 with Message

use of io.dingodb.net.Message in project dingo by dingodb.

the class SendOperator method fin.

@Override
public void fin(@Nonnull Fin fin) {
    try {
        if (log.isDebugEnabled()) {
            log.debug("(tag = {}) Send FIN", tag);
        }
        Message msg = SimpleMessage.builder().tag(TagUtil.getTag(tag)).content(codec.encodeFin(fin)).build();
        channel.send(msg);
        channel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(io.dingodb.net.Message) SimpleMessage(io.dingodb.net.SimpleMessage) IOException(java.io.IOException)

Example 3 with Message

use of io.dingodb.net.Message in project dingo by dingodb.

the class GenericMessageHandler method handle.

@Override
public void handle(Connection<Message> connection, Packet<Message> packet) {
    switch(packet.header().type()) {
        case PING:
            connection.genericSubChannel().send(MessagePacket.pong(0));
            return;
        case PONG:
            break;
        case CONNECT_CHANNEL:
            ConnectionSubChannel<Message> channel = connection.openSubChannel(packet.header().targetChannelId());
            channel.send(MessagePacket.ack(channel.channelId(), channel.targetChannelId(), channel.nextSeq()));
            break;
        case DIS_CONNECT_CHANNEL:
            connection.closeSubChannel(packet.header().channelId());
            break;
        case ACK:
            Optional.ofNullable(ackFuture.get(connection)).map(futureMap -> futureMap.remove(packet.header().channelId())).ifPresent(future -> future.complete(packet));
            break;
        case HANDSHAKE_ERROR:
            ErrorMessage errorMessage = ErrorMessage.builder().build().load(packet.content().toBytes());
            log.error("Handshake failed, msg is: {}", errorMessage.getDetailMessage());
            try {
                connection.close();
            } catch (Exception e) {
                log.error("Connection close error.", e);
            }
            break;
        default:
            throw new IllegalStateException("Unexpected value: " + packet.header().type());
    }
}
Also used : PacketMode(io.dingodb.net.netty.packet.PacketMode) Packet(io.dingodb.net.netty.packet.Packet) PacketType(io.dingodb.net.netty.packet.PacketType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message(io.dingodb.net.Message) TimeoutException(java.util.concurrent.TimeoutException) ErrorMessage(io.dingodb.net.netty.packet.message.ErrorMessage) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Slf4j(lombok.extern.slf4j.Slf4j) Future(java.util.concurrent.Future) MessagePacket(io.dingodb.net.netty.packet.impl.MessagePacket) MessageDispatcher(io.dingodb.net.netty.handler.MessageDispatcher) AutoService(com.google.auto.service.AutoService) Map(java.util.Map) Optional(io.dingodb.common.util.Optional) ConnectionSubChannel(io.dingodb.net.netty.channel.ConnectionSubChannel) MessageHandler(io.dingodb.net.netty.handler.MessageHandler) Logs(io.dingodb.net.netty.utils.Logs) ChannelId(io.dingodb.net.netty.channel.ChannelId) Connection(io.dingodb.net.netty.connection.Connection) Message(io.dingodb.net.Message) ErrorMessage(io.dingodb.net.netty.packet.message.ErrorMessage) ErrorMessage(io.dingodb.net.netty.packet.message.ErrorMessage) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with Message

use of io.dingodb.net.Message in project dingo by dingodb.

the class NetServiceTest method hello.

// @Test
public void hello() throws Exception {
    String hello = "hello";
    Tag tag = SimpleTag.builder().tag("TEST".getBytes(StandardCharsets.UTF_8)).build();
    ServiceLoader<NetServiceProvider> loader = ServiceLoader.load(NetServiceProvider.class);
    NettyNetService netService = (NettyNetService) loader.iterator().next().get();
    netService.listenPort(19199);
    netService.registerMessageListenerProvider(tag, () -> (msg, ch) -> assertThat(new String(msg.toBytes())).isEqualTo(hello));
    netService.registerMessageListenerProvider(tag, () -> (msg, ch) -> System.out.println(String.format("%s %s %s", new String(msg.toBytes()), ((ConnectionSubChannel) ch).channelId(), StackTraces.stack(2))));
    Channel channel = netService.newChannel(NetAddress.builder().host("localhost").port(19199).build());
    Message helloMsg = SimpleMessage.builder().tag(tag).content(hello.getBytes()).build();
    channel.send(helloMsg);
    Thread.sleep(100000);
}
Also used : SimpleMessage(io.dingodb.net.SimpleMessage) Message(io.dingodb.net.Message) ConnectionSubChannel(io.dingodb.net.netty.channel.ConnectionSubChannel) Channel(io.dingodb.net.Channel) NetServiceProvider(io.dingodb.net.NetServiceProvider) SimpleTag(io.dingodb.net.SimpleTag) Tag(io.dingodb.net.Tag)

Example 5 with Message

use of io.dingodb.net.Message in project dingo by dingodb.

the class CoordinatorConnector method connectAll.

private void connectAll(Message message, Channel channel) {
    connected(null, channel);
    byte[] buf = message.toBytes();
    if (buf.length == 0) {
        channel.send(GET_ALL_LOCATION.message());
        return;
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);
    int size = PreParameters.cleanNull(PrimitiveCodec.readVarInt(bais), 0);
    IntStream.range(0, size).forEach(i -> {
        String host = PrimitiveCodec.readString(bais);
        Integer port = PrimitiveCodec.readVarInt(bais);
        NetAddress address = new NetAddress(host, port);
        coordinatorAddresses.add(address);
    });
    coordinatorAddresses.stream().filter(address -> !address.equals(channel.remoteAddress())).forEach(address -> executorService.submit(() -> listenLeaderChannels.computeIfAbsent(address, a -> {
        try {
            Channel ch = netService.newChannel(address);
            ch.registerMessageListener(this::connected);
            ch.send(PING.message(Tags.RAFT_SERVICE));
            log.info("Open coordinator channel, address: [{}]", address);
            return ch;
        } catch (Exception e) {
            log.error("Open coordinator channel error, address: {}", address, e);
        }
        return null;
    })));
    lastUpdateNotLeaderChannelsTime = System.currentTimeMillis();
}
Also used : IntStream(java.util.stream.IntStream) NetService(io.dingodb.net.NetService) NetServiceProvider(io.dingodb.net.NetServiceProvider) Message(io.dingodb.net.Message) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GET_LEADER_LOCATION(io.dingodb.server.protocol.code.RaftServiceCode.GET_LEADER_LOCATION) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) PING(io.dingodb.server.protocol.code.BaseCode.PING) HashSet(java.util.HashSet) Tags(io.dingodb.server.protocol.Tags) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) PrimitiveCodec(io.dingodb.common.codec.PrimitiveCodec) Connector(io.dingodb.server.client.connector.Connector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) NetAddress(io.dingodb.net.NetAddress) Channel(io.dingodb.net.Channel) ThreadPoolBuilder(io.dingodb.common.concurrent.ThreadPoolBuilder) GET_ALL_LOCATION(io.dingodb.server.protocol.code.RaftServiceCode.GET_ALL_LOCATION) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RaftServiceCode(io.dingodb.server.protocol.code.RaftServiceCode) PreParameters(io.dingodb.common.util.PreParameters) ByteArrayInputStream(java.io.ByteArrayInputStream) Channel(io.dingodb.net.Channel) NetAddress(io.dingodb.net.NetAddress)

Aggregations

Message (io.dingodb.net.Message)9 SimpleMessage (io.dingodb.net.SimpleMessage)7 Channel (io.dingodb.net.Channel)5 NetServiceProvider (io.dingodb.net.NetServiceProvider)4 ConnectionSubChannel (io.dingodb.net.netty.channel.ConnectionSubChannel)4 SimpleTag (io.dingodb.net.SimpleTag)3 Tag (io.dingodb.net.Tag)3 MessagePacket (io.dingodb.net.netty.packet.impl.MessagePacket)2 IOException (java.io.IOException)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Slf4j (lombok.extern.slf4j.Slf4j)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AutoService (com.google.auto.service.AutoService)1 PrimitiveCodec (io.dingodb.common.codec.PrimitiveCodec)1 ThreadPoolBuilder (io.dingodb.common.concurrent.ThreadPoolBuilder)1 Optional (io.dingodb.common.util.Optional)1 PreParameters (io.dingodb.common.util.PreParameters)1 Task (io.dingodb.exec.base.Task)1 Location (io.dingodb.meta.Location)1