Search in sources :

Example 1 with Address

use of io.atomix.utils.net.Address in project zeppelin by apache.

the class ClusterManagerServer method initThread.

private void initThread() {
    // RaftServer Thread
    new Thread(new Runnable() {

        @Override
        public void run() {
            LOGGER.info("RaftServer run() >>>");
            Address address = Address.from(zeplServerHost, raftServerPort);
            Member member = Member.builder(MemberId.from(zeplServerHost + ":" + raftServerPort)).withAddress(address).build();
            messagingService = NettyMessagingService.builder().withAddress(address).build().start().join();
            RaftServerProtocol protocol = new RaftServerMessagingProtocol(messagingService, ClusterManager.protocolSerializer, raftAddressMap::get);
            BootstrapService bootstrapService = new BootstrapService() {

                @Override
                public MessagingService getMessagingService() {
                    return messagingService;
                }

                @Override
                public BroadcastService getBroadcastService() {
                    return new BroadcastServiceAdapter();
                }
            };
            ManagedClusterMembershipService clusterService = new DefaultClusterMembershipService(member, new DefaultNodeDiscoveryService(bootstrapService, member, new BootstrapDiscoveryProvider(clusterNodes)), bootstrapService, new MembershipConfig());
            File atomixDateDir = com.google.common.io.Files.createTempDir();
            atomixDateDir.deleteOnExit();
            RaftServer.Builder builder = RaftServer.builder(member.id()).withMembershipService(clusterService).withProtocol(protocol).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.MEMORY).withDirectory(atomixDateDir).withSerializer(storageSerializer).withMaxSegmentSize(1024 * 1024).build());
            raftServer = builder.build();
            raftServer.bootstrap(clusterMemberIds);
            messagingService.registerHandler(CLUSTER_INTP_EVENT_TOPIC, subscribeClusterIntpEvent, MoreExecutors.directExecutor());
            messagingService.registerHandler(CLUSTER_NOTE_EVENT_TOPIC, subscribeClusterNoteEvent, MoreExecutors.directExecutor());
            messagingService.registerHandler(CLUSTER_AUTH_EVENT_TOPIC, subscribeClusterAuthEvent, MoreExecutors.directExecutor());
            messagingService.registerHandler(CLUSTER_INTP_SETTING_EVENT_TOPIC, subscribeIntpSettingEvent, MoreExecutors.directExecutor());
            HashMap<String, Object> meta = new HashMap<String, Object>();
            String nodeName = getClusterNodeName();
            meta.put(ClusterMeta.NODE_NAME, nodeName);
            meta.put(ClusterMeta.SERVER_HOST, zeplServerHost);
            meta.put(ClusterMeta.SERVER_PORT, raftServerPort);
            meta.put(ClusterMeta.SERVER_START_TIME, LocalDateTime.now());
            putClusterMeta(SERVER_META, nodeName, meta);
            LOGGER.info("RaftServer run() <<<");
        }
    }).start();
}
Also used : RaftServerProtocol(io.atomix.protocols.raft.protocol.RaftServerProtocol) RaftServerMessagingProtocol(org.apache.zeppelin.cluster.protocol.RaftServerMessagingProtocol) DefaultNodeDiscoveryService(io.atomix.cluster.impl.DefaultNodeDiscoveryService) Address(io.atomix.utils.net.Address) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) RaftServer(io.atomix.protocols.raft.RaftServer) DefaultClusterMembershipService(io.atomix.cluster.impl.DefaultClusterMembershipService) File(java.io.File)

Example 2 with Address

use of io.atomix.utils.net.Address in project zeppelin by apache.

the class ClusterManager method start.

public void start() {
    if (!zConf.isClusterMode()) {
        return;
    }
    // RaftClient Thread
    new Thread(new Runnable() {

        @Override
        public void run() {
            LOGGER.info("RaftClientThread run() >>>");
            int raftClientPort = 0;
            try {
                raftClientPort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
            }
            MemberId memberId = MemberId.from(zeplServerHost + ":" + raftClientPort);
            Address address = Address.from(zeplServerHost, raftClientPort);
            raftAddressMap.put(memberId, address);
            MessagingService messagingManager = NettyMessagingService.builder().withAddress(address).build().start().join();
            RaftClientProtocol protocol = new RaftClientMessagingProtocol(messagingManager, protocolSerializer, raftAddressMap::get);
            raftClient = RaftClient.builder().withMemberId(memberId).withPartitionId(PartitionId.from("partition", 1)).withProtocol(protocol).build();
            raftClient.connect(clusterMemberIds).join();
            raftSessionClient = createProxy(raftClient);
            LOGGER.info("RaftClientThread run() <<<");
        }
    }).start();
    // Cluster Meta Consume Thread
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                while (getRunning().get()) {
                    ClusterMetaEntity metaEntity = clusterMetaQueue.peek();
                    if (null != metaEntity) {
                        // Determine whether the client is connected
                        int retry = 0;
                        while (!raftInitialized()) {
                            retry++;
                            if (0 == retry % 30) {
                                LOGGER.warn("Raft incomplete initialization! retry[{}]", retry);
                            }
                            Thread.sleep(100);
                        }
                        boolean success = false;
                        switch(metaEntity.getOperation()) {
                            case DELETE_OPERATION:
                                success = deleteClusterMeta(metaEntity);
                                break;
                            case PUT_OPERATION:
                                success = putClusterMeta(metaEntity);
                                break;
                        }
                        if (true == success) {
                            // The operation was successfully deleted
                            clusterMetaQueue.remove(metaEntity);
                            LOGGER.info("Cluster Meta Consume success! {}", metaEntity);
                        } else {
                            LOGGER.error("Cluster Meta Consume faild!");
                        }
                    } else {
                        Thread.sleep(100);
                    }
                }
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage());
            }
        }
    }).start();
}
Also used : RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Address(io.atomix.utils.net.Address) InetAddress(java.net.InetAddress) IOException(java.io.IOException) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) MemberId(io.atomix.cluster.MemberId) ClusterMetaEntity(org.apache.zeppelin.cluster.meta.ClusterMetaEntity) RaftClientMessagingProtocol(org.apache.zeppelin.cluster.protocol.RaftClientMessagingProtocol)

Example 3 with Address

use of io.atomix.utils.net.Address in project zeppelin by apache.

the class ClusterManagerServer method initTestCluster.

@VisibleForTesting
public void initTestCluster(String clusterAddrList, String host, int port) {
    isTest = true;
    this.zeplServerHost = host;
    this.raftServerPort = port;
    // clear
    clusterNodes.clear();
    raftAddressMap.clear();
    clusterMemberIds.clear();
    String[] cluster = clusterAddrList.split(",");
    for (int i = 0; i < cluster.length; i++) {
        String[] parts = cluster[i].split(":");
        String clusterHost = parts[0];
        int clusterPort = Integer.valueOf(parts[1]);
        String memberId = clusterHost + ":" + clusterPort;
        Address address = Address.from(clusterHost, clusterPort);
        Node node = Node.builder().withId(memberId).withAddress(address).build();
        clusterNodes.add(node);
        raftAddressMap.put(MemberId.from(memberId), address);
        clusterMemberIds.add(MemberId.from(memberId));
    }
}
Also used : Address(io.atomix.utils.net.Address) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Address

use of io.atomix.utils.net.Address in project zeppelin by apache.

the class ClusterManagerServer method unicastClusterEvent.

public void unicastClusterEvent(String host, int port, String topic, String msg) {
    LOGGER.info("send unicastClusterEvent host:{} port:{} topic:{} message:{}", host, port, topic, msg);
    Address address = Address.from(host, port);
    CompletableFuture<byte[]> response = messagingService.sendAndReceive(address, topic, msg.getBytes(), Duration.ofSeconds(2));
    response.whenComplete((r, e) -> {
        if (null == e) {
            LOGGER.error(e.getMessage(), e);
        }
    });
}
Also used : Address(io.atomix.utils.net.Address)

Aggregations

Address (io.atomix.utils.net.Address)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MemberId (io.atomix.cluster.MemberId)1 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)1 DefaultClusterMembershipService (io.atomix.cluster.impl.DefaultClusterMembershipService)1 DefaultNodeDiscoveryService (io.atomix.cluster.impl.DefaultNodeDiscoveryService)1 MessagingService (io.atomix.cluster.messaging.MessagingService)1 NettyMessagingService (io.atomix.cluster.messaging.impl.NettyMessagingService)1 RaftServer (io.atomix.protocols.raft.RaftServer)1 RaftClientProtocol (io.atomix.protocols.raft.protocol.RaftClientProtocol)1 RaftServerProtocol (io.atomix.protocols.raft.protocol.RaftServerProtocol)1 File (java.io.File)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ClusterMetaEntity (org.apache.zeppelin.cluster.meta.ClusterMetaEntity)1 RaftClientMessagingProtocol (org.apache.zeppelin.cluster.protocol.RaftClientMessagingProtocol)1 RaftServerMessagingProtocol (org.apache.zeppelin.cluster.protocol.RaftServerMessagingProtocol)1