use of io.dingodb.net.NetAddress 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.NetAddress 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;
}
}
use of io.dingodb.net.NetAddress 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();
}
Aggregations