<commit_before>package org.apache.mesos.elasticsearch.systemtest;

import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static com.jayway.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.is;

/**
 * Response for Elasticsearch nodes
 */
public class ElasticsearchNodesResponse {

    private List<JSONObject> tasks;

    private int nodesCount;

    public ElasticsearchNodesResponse(List<JSONObject> tasks, int nodesCount) {
        this.tasks = tasks;
        this.nodesCount = nodesCount;
        await().atMost(5, TimeUnit.MINUTES).pollInterval(1, TimeUnit.SECONDS).until(new ElasticsearchNodesCall(), is(true));
    }

    private boolean discoverySuccessful;

    class ElasticsearchNodesCall implements Callable<Boolean> {

        @Override
        public Boolean call() throws Exception {
            try {
                for (JSONObject task : tasks) {
                    if (!(Unirest.get("http://" + task.getString("http_address") + "/_nodes").asJson().getBody().getObject().getJSONObject("nodes").length() == nodesCount)) {
                        discoverySuccessful = false;
                    }
                }
                discoverySuccessful = true;
                return true;
            } catch (UnirestException e) {
                DiscoverySystemTest.LOGGER.info("Polling Elasticsearch _nodes endpoints...");
                return false;
            }
        }
    }

    public boolean isDiscoverySuccessful() {
        return discoverySuccessful;
    }
}<commit_msg>more logging of failures when polling elasticsearch endpoint<commit_after>package org.apache.mesos.elasticsearch.systemtest;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest;
import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static com.jayway.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.is;

/**
 * Response for Elasticsearch nodes
 */
public class ElasticsearchNodesResponse {

    private List<JSONObject> tasks;

    private int nodesCount;

    public ElasticsearchNodesResponse(List<JSONObject> tasks, int nodesCount) {
        this.tasks = tasks;
        this.nodesCount = nodesCount;
        await().atMost(5, TimeUnit.MINUTES).pollInterval(1, TimeUnit.SECONDS).until(new ElasticsearchNodesCall(), is(true));
    }

    private boolean discoverySuccessful = false;

    class ElasticsearchNodesCall implements Callable<Boolean> {

        // Returns whether to stop polling
        // Queries the Elasticsearch nodes list https://www.elastic.co/guide/en/elasticsearch/reference/1.6/cluster-nodes-info.html
        // `discoverySuccessful` is set to `true` iff return value is `true`
        @Override
        public Boolean call() throws Exception {
                for (JSONObject task : tasks) {
                    if (!endpointIsOk(task)) {
                        DiscoverySystemTest.LOGGER.info("At least one endpoint is not yet OK; will try again.");
                        return false;
                    }
                }
                DiscoverySystemTest.LOGGER.info("All Elasticsearch endpoints succeeded");
                discoverySuccessful = true;
                return true;
        }

        // Returns `true` iff endpoint is OK.
        // Returns `false` iff endpoint is definitely not OK or it is not yet determined and we should continue polling.
        private boolean endpointIsOk(JSONObject task) {
            String url = "http://" + task.getString("http_address") + "/_nodes";

            GetRequest request = Unirest.get(url);

            HttpResponse<String> response = null;
            try {
                response = request.asString();
            }
            catch (UnirestException e) {
                DiscoverySystemTest.LOGGER.info("Polling Elasticsearch endpoint '" + url + "' threw exception: " + e.getMessage());
                return false;
            }
            // response != null

            if (200 <= response.getStatus() && response.getStatus() < 400) {
                JsonNode body = null;

                try {
                    body = new JsonNode(response.getBody());
                } catch (RuntimeException e) {
                    DiscoverySystemTest.LOGGER.info("Polling Elasticsearch endpoint '" + url + "' returned bad response body: " + e.getMessage());
                    return false;
                }
                // body != null

                if (body.getObject().getJSONObject("nodes").length() != nodesCount) {
                    DiscoverySystemTest.LOGGER.info("Polling Elasticsearch endpoint '" + url + "' returned wrong number of nodes (Expected " + nodesCount + " but got " + body.getObject().getJSONObject("nodes").length() + ")");
                    return false;
                }
                else {
                    DiscoverySystemTest.LOGGER.info("Polling Elasticsearch endpoint '" + url + "' succeeded");
                    return true;
                }
            }
            else {
                DiscoverySystemTest.LOGGER.info("Polling Elasticsearch endpoint '" + url + "' returned bad status: " + response.getStatus() + " " + response.getStatusText());
                return false;
            }
        }
    }

    public boolean isDiscoverySuccessful() {
        return discoverySuccessful;
    }
}
{'fraction_non_alphanumeric': 0.10010445682451254, 'fraction_numerical': 0.0020891364902506965, 'mean_word_length': 2.2257158899494667, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '5017851', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1473, 'n_words': 341}