Need to know or be notified that all the nodes have been initialized successfully using Elastic Search Sniffer


(志强 赵) #1

I use a proxy server to determine how to route request to the backend elastic search clusters. The client makes request directly to the proxy server, so I need to implement some kind of service discovery mechanism in the client side.
Now I'm working on a Sniffer to sniff proxy nodes from ETCD.After reading the code from elasticsearch-rest-client 6.4.0,
I found the initial sniff procedure is running in a dedicated scheduling thread, which may result in no nodes available when making request meanwhile.so How could I know or be notified that all the nodes have been initialized successfully from ETCD before I start making request ?

    Sniffer(RestClient restClient, NodesSniffer nodesSniffer, Scheduler scheduler, long sniffInterval, long sniffAfterFailureDelay) {
        this.nodesSniffer = nodesSniffer;
        this.restClient = restClient;
        this.sniffIntervalMillis = sniffInterval;
        this.sniffAfterFailureDelayMillis = sniffAfterFailureDelay;
        this.scheduler = scheduler;
        /*
         * The first sniffing round is async, so this constructor returns before nextScheduledTask is assigned to a task.
         * The initialized flag is a protection against NPE due to that.
         */
        Task task = new Task(sniffIntervalMillis) {
            @Override
            public void run() {
                super.run();
                initialized.compareAndSet(false, true);
            }
        };
        /*
         * We do not keep track of the returned future as we never intend to cancel the initial sniffing round, we rather
         * prevent any other operation from being executed till the sniffer is properly initialized
         */
        
        scheduler.schedule(task, 0L);
    }