Search in sources :

Example 1 with NetAddress

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);
    }
}
Also used : Channel(io.dingodb.net.Channel) ByteBuffer(java.nio.ByteBuffer) NetAddress(io.dingodb.net.NetAddress)

Example 2 with NetAddress

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;
    }
}
Also used : Channel(io.dingodb.net.Channel) NetAddress(io.dingodb.net.NetAddress)

Example 3 with NetAddress

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

Channel (io.dingodb.net.Channel)3 NetAddress (io.dingodb.net.NetAddress)3 ByteBuffer (java.nio.ByteBuffer)2 PrimitiveCodec (io.dingodb.common.codec.PrimitiveCodec)1 ThreadPoolBuilder (io.dingodb.common.concurrent.ThreadPoolBuilder)1 PreParameters (io.dingodb.common.util.PreParameters)1 Message (io.dingodb.net.Message)1 NetService (io.dingodb.net.NetService)1 NetServiceProvider (io.dingodb.net.NetServiceProvider)1 Connector (io.dingodb.server.client.connector.Connector)1 Tags (io.dingodb.server.protocol.Tags)1 PING (io.dingodb.server.protocol.code.BaseCode.PING)1 RaftServiceCode (io.dingodb.server.protocol.code.RaftServiceCode)1 GET_ALL_LOCATION (io.dingodb.server.protocol.code.RaftServiceCode.GET_ALL_LOCATION)1 GET_LEADER_LOCATION (io.dingodb.server.protocol.code.RaftServiceCode.GET_LEADER_LOCATION)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ServiceLoader (java.util.ServiceLoader)1