<commit_before>package io.gatekeeper.node.service.replication.consul;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.mashape.unirest.http.*;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
import io.gatekeeper.Version;
import io.gatekeeper.logging.Loggers;
import io.gatekeeper.node.service.replication.common.Node;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

@SuppressWarnings("HardcodedFileSeparator")
public class Client implements Closeable {

    protected final Logger logger;

    protected final String consulHost;

    protected final Integer consulPort;

    protected final String serviceName;

    protected final String serviceHost;

    protected final Integer servicePort;

    protected final String token;

    protected final Executor executor;

    public Client(
        String consulHost,
        Integer consulPort,
        String serviceName,
        String serviceHost,
        Integer servicePort,
        String token
    ) {
        assert null != consulHost;
        assert null != consulPort;
        assert null != serviceName;
        assert null != serviceHost;
        assert null != servicePort;

        this.logger = Loggers.getReplicationLogger();
        this.consulHost = consulHost;
        this.consulPort = consulPort;
        this.serviceName = serviceName;
        this.serviceHost = serviceHost;
        this.servicePort = servicePort;
        this.token = token;

        this.executor = Executors.newSingleThreadExecutor(
            (new ThreadFactoryBuilder())
                .setNameFormat("Consul Interface")
                .build()
        );
    }

    public CompletableFuture<Lock> lock(String name) {
        assert null != name;
        assert name.length() > 0;

        CompletableFuture<Lock> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            try {
                future.complete(doLock(name));
            } catch (Exception exception) {
                future.completeExceptionally(exception);
            }
        });

        return future;
    }

    public CompletableFuture<Void> registerService() {
        CompletableFuture<Void> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            while (true) {
                try {
                    doRegisterService();
                    doRegisterCheck();

                    future.complete(null);

                    break;
                } catch (Exception exception) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException interrupted) {
                        future.completeExceptionally(interrupted);
                    }
                }
            }
        });

        return future;
    }

    public CompletableFuture<List<Node>> getNodes() {
        CompletableFuture<List<Node>> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            try {
                future.complete(doGetNodes());
<commit_msg>Re-register with consul in the event that a connection was lost<commit_after>package io.gatekeeper.node.service.replication.consul;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.mashape.unirest.http.*;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
import io.gatekeeper.Version;
import io.gatekeeper.logging.Loggers;
import io.gatekeeper.node.service.replication.common.Node;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

@SuppressWarnings("HardcodedFileSeparator")
public class Client implements Closeable {

    protected final Logger logger;

    protected final String consulHost;

    protected final Integer consulPort;

    protected final String serviceName;

    protected final String serviceHost;

    protected final Integer servicePort;

    protected final String token;

    protected final Executor executor;

    public Client(
        String consulHost,
        Integer consulPort,
        String serviceName,
        String serviceHost,
        Integer servicePort,
        String token
    ) {
        assert null != consulHost;
        assert null != consulPort;
        assert null != serviceName;
        assert null != serviceHost;
        assert null != servicePort;

        this.logger = Loggers.getReplicationLogger();
        this.consulHost = consulHost;
        this.consulPort = consulPort;
        this.serviceName = serviceName;
        this.serviceHost = serviceHost;
        this.servicePort = servicePort;
        this.token = token;

        this.executor = Executors.newSingleThreadExecutor(
            (new ThreadFactoryBuilder())
                .setNameFormat("Consul Interface")
                .build()
        );
    }

    public CompletableFuture<Lock> lock(String name) {
        assert null != name;
        assert name.length() > 0;

        CompletableFuture<Lock> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            try {
                future.complete(doLock(name));
            } catch (Exception exception) {
                future.completeExceptionally(exception);
            }
        });

        return future;
    }

    public CompletableFuture<Void> registerService() {
        CompletableFuture<Void> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            while (true) {
                try {
                    doRegisterService();
                    doRegisterCheck();

                    future.complete(null);

                    break;
                } catch (Exception exception) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException interrupted) {
                        future.completeExceptionally(interrupted);
                    }
                }
            }
        });

        return future;
    }

    public CompletableFuture<List<Node>> getNodes() {
        CompletableFuture<List<Node>> future = new CompletableFuture<>();

        this.executor.execute(() -> {
            try {
                List<Node> nodes = doGetNodes();

                if (nodes.size() == 0) {
                    registerService().thenRunAsync(() -> {
                        try {
                            List<Node> nodes0 = doGetNodes();

                            if (nodes0.size() == 0) {
                                throw new Exception("Could not find nodes");
                            }

                            future.complete(doGetNodes());

                        } catch (Exception exception) {
                            future.completeExceptionally(exception);
                        }
                    }, executor);

                    return;
                }

                future.complete(doGetNodes());

{'fraction_non_alphanumeric': 0.09015328656793972, 'fraction_numerical': 0.0018186541958950377, 'mean_word_length': 1.9887422360248448, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016788', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1889, 'n_words': 415}