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();
}
}
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);
}
});
}
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);
}
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);
}
}
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;
}
}
Aggregations