Search in sources :

Example 1 with Channel

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

the class ReceiveOperator method init.

@Override
public void init() {
    super.init();
    codec = new AvroTxRxCodec(schema);
    tupleQueue = new LinkedBlockingDeque<>();
    messageListenerProvider = new ReceiveMessageListenerProvider();
    tag = TagUtil.tag(getTask().getJobId(), getId());
    Services.NET.registerMessageListenerProvider(TagUtil.getTag(tag), messageListenerProvider);
    try (Channel channel = Services.openNewSysChannel(host, port)) {
        channel.send(SimpleMessage.builder().tag(SimpleTag.RCV_READY_TAG).content(TagUtil.toBytes(tag)).build());
        if (log.isDebugEnabled()) {
            log.debug("(tag = {}) Sent ready signal.", tag);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Channel(io.dingodb.net.Channel) AvroTxRxCodec(io.dingodb.exec.codec.AvroTxRxCodec) IOException(java.io.IOException)

Example 2 with Channel

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

the class Services method initNetService.

public static void initNetService() {
    NET.registerMessageListenerProvider(SimpleTag.RCV_READY_TAG, () -> ((message, channel) -> {
        String tag = TagUtil.fromBytes(message.toBytes());
        if (log.isDebugEnabled()) {
            log.debug("Received RCV_READY of tag {}.", tag);
        }
        SendOperator so = Services.rcvReadyFlag.put(tag, SendOperator.DUMMY);
        if (so != null) {
            so.wakeUp();
        }
    }));
    NET.registerMessageListenerProvider(SimpleTag.TASK_TAG, () -> (message, channel) -> {
        String taskStr = new String(message.toBytes(), StandardCharsets.UTF_8);
        if (log.isInfoEnabled()) {
            log.info("Received task: {}", taskStr);
        }
        try {
            Task task = TaskImpl.deserialize(taskStr);
            executorService.execute(() -> {
                task.init();
                task.run();
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Cannot deserialize received task.", e);
        }
    });
}
Also used : NetService(io.dingodb.net.NetService) NetError(io.dingodb.net.NetError) StoreServiceProvider(io.dingodb.store.api.StoreServiceProvider) MetaService(io.dingodb.meta.MetaService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpleTag(io.dingodb.net.SimpleTag) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NetAddress(io.dingodb.net.NetAddress) Channel(io.dingodb.net.Channel) DingoException(io.dingodb.common.error.DingoException) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) SendOperator(io.dingodb.exec.operator.SendOperator) Slf4j(lombok.extern.slf4j.Slf4j) Optional(io.dingodb.common.util.Optional) StoreService(io.dingodb.store.api.StoreService) Task(io.dingodb.exec.base.Task) ExecutorService(java.util.concurrent.ExecutorService) TaskImpl(io.dingodb.exec.impl.TaskImpl) TagUtil(io.dingodb.exec.util.TagUtil) Task(io.dingodb.exec.base.Task) SendOperator(io.dingodb.exec.operator.SendOperator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Channel

use of io.dingodb.net.Channel 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 4 with Channel

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

the class CoordinatorConnector method connectLeader.

private void connectLeader(Message message, Channel channel) {
    byte[] bytes = message.toBytes();
    if (bytes == null || bytes.length == 0) {
        closeChannel(channel);
        initChannels();
        return;
    }
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    String host = PrimitiveCodec.readString(buffer);
    Integer port = PrimitiveCodec.readVarInt(buffer);
    NetAddress leaderAddress = new NetAddress(host, port);
    try {
        Channel newLeaderChannel = netService.newChannel(leaderAddress);
        listenLeader(null, newLeaderChannel);
        newLeaderChannel.registerMessageListener((m, c) -> {
            newLeaderChannel.registerMessageListener(this::connectAll);
            c.send(GET_ALL_LOCATION.message());
        });
        newLeaderChannel.send(PING.message(Tags.RAFT_SERVICE));
        lastUpdateLeaderTime = System.currentTimeMillis();
        log.info("Connect coordinator leader success, remote: [{}]", newLeaderChannel.remoteAddress());
    } catch (Exception e) {
        log.error("Open coordinator leader channel error, address: {}", leaderAddress, e);
        refresh.set(false);
        if (!verify()) {
            refresh();
        }
    } finally {
        closeChannel(channel);
    }
}
Also used : Channel(io.dingodb.net.Channel) ByteBuffer(java.nio.ByteBuffer) NetAddress(io.dingodb.net.NetAddress)

Example 5 with Channel

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

the class CoordinatorConnector method initChannels.

private void initChannels() {
    Channel channel;
    for (NetAddress address : coordinatorAddresses) {
        try {
            channel = netService.newChannel(address);
        } catch (Exception e) {
            log.error("Open coordinator channel error, address: {}", address, e);
            continue;
        }
        channel.registerMessageListener((m, c) -> {
            c.registerMessageListener(this::connectLeader);
            c.send(GET_LEADER_LOCATION.message());
        });
        channel.send(PING.message(Tags.RAFT_SERVICE));
        return;
    }
}
Also used : Channel(io.dingodb.net.Channel) NetAddress(io.dingodb.net.NetAddress)

Aggregations

Channel (io.dingodb.net.Channel)11 Message (io.dingodb.net.Message)5 NetAddress (io.dingodb.net.NetAddress)4 NetServiceProvider (io.dingodb.net.NetServiceProvider)4 SimpleMessage (io.dingodb.net.SimpleMessage)4 SimpleTag (io.dingodb.net.SimpleTag)4 Tag (io.dingodb.net.Tag)3 ConnectionSubChannel (io.dingodb.net.netty.channel.ConnectionSubChannel)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DingoException (io.dingodb.common.error.DingoException)2 Task (io.dingodb.exec.base.Task)2 NetService (io.dingodb.net.NetService)2 ByteBuffer (java.nio.ByteBuffer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 Slf4j (lombok.extern.slf4j.Slf4j)2 PrimitiveCodec (io.dingodb.common.codec.PrimitiveCodec)1 ThreadPoolBuilder (io.dingodb.common.concurrent.ThreadPoolBuilder)1 EXEC_INTERRUPT (io.dingodb.common.error.CommonError.EXEC_INTERRUPT)1 EXEC_TIMEOUT (io.dingodb.common.error.CommonError.EXEC_TIMEOUT)1