Search in sources :

Example 1 with Item

use of com.nextdoor.bender.ipc.es.EsResponse.Item in project bender by Nextdoor.

the class ElasticSearchTransport method checkResponse.

@Override
public void checkResponse(HttpResponse resp, String responseString) throws TransportException {
    /*
     * Check responses status code of the overall bulk call. The call can succeed but have
     * individual failures which are checked later.
     */
    if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase());
    }
    /*
     * Short circuit deserializing the response by just looking if there are any errors
     */
    if (responseString.contains("\"errors\":false")) {
        return;
    }
    /*
     * Convert response text to a POJO. Only tested with ES 2.4.x and 5.x.
     */
    Gson gson = new GsonBuilder().create();
    EsResponse esResp = null;
    try {
        esResp = gson.fromJson(responseString, EsResponse.class);
    } catch (JsonSyntaxException e) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase(), e);
    }
    /*
     * Look for the errors per index request
     */
    int failures = 0;
    if (esResp.items == null) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase());
    }
    HashSet<String> errorTypes = new HashSet<String>();
    for (Item item : esResp.items) {
        Index index = item.index;
        if (index == null || index.error == null || index.error.reason == null) {
            continue;
        }
        /*
       * For now just allow 200's and 400's. Both are considered non-fatal errors from the lambda's
       * perspective.
       */
        switch(index.status) {
            case HttpStatus.SC_OK:
            case HttpStatus.SC_BAD_REQUEST:
                continue;
            default:
                failures++;
                if (index.error != null && index.error.reason != null && index.error.type != null) {
                    if (!errorTypes.contains(index.error.type)) {
                        logger.error("Indexing Error Reason: " + index.error.reason);
                        if (index.error.caused_by != null) {
                            logger.error("Indexing Error Cause: " + index.error.caused_by.reason);
                        }
                        errorTypes.add(index.error.type);
                    }
                }
        }
    }
    errorTypes.clear();
    if (failures != 0) {
        throw new TransportException("es index failure count is " + failures);
    }
}
Also used : Item(com.nextdoor.bender.ipc.es.EsResponse.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Index(com.nextdoor.bender.ipc.es.EsResponse.Index) TransportException(com.nextdoor.bender.ipc.TransportException) HashSet(java.util.HashSet)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TransportException (com.nextdoor.bender.ipc.TransportException)1 Index (com.nextdoor.bender.ipc.es.EsResponse.Index)1 Item (com.nextdoor.bender.ipc.es.EsResponse.Item)1 HashSet (java.util.HashSet)1