Пример #1
0
  @SuppressWarnings("rawtypes")
  private boolean retryFailedEntries(InputStream content, TrackingBytesArray data)
      throws IOException {
    ObjectReader r = mapper.reader(Map.class);
    JsonParser parser = mapper.getJsonFactory().createJsonParser(content);
    if (ParsingUtils.seek("items", new JacksonJsonParser(parser)) == null) {
      return false;
    }

    int entryToDeletePosition = 0; // head of the list
    for (Iterator<Map> iterator = r.readValues(parser); iterator.hasNext(); ) {
      Map map = iterator.next();
      Map values = (Map) map.values().iterator().next();
      String error = (String) values.get("error");
      if (error != null) {
        // status - introduced in 1.0.RC1
        Integer status = (Integer) values.get("status");
        if (status != null && HttpStatus.canRetry(status)
            || error.contains("EsRejectedExecutionException")) {
          entryToDeletePosition++;
        } else {
          String message =
              (status != null
                  ? String.format("%s(%s) - %s", HttpStatus.getText(status), status, error)
                  : error);
          throw new IllegalStateException(
              String.format("Found unrecoverable error [%s]; Bailing out..", message));
        }
      } else {
        data.remove(entryToDeletePosition);
      }
    }

    return entryToDeletePosition > 0;
  }
Пример #2
0
  public void bulk(Resource resource, TrackingBytesArray data) throws IOException {
    Retry retry = retryPolicy.init();
    int httpStatus = 0;

    do {
      Response response = execute(PUT, resource.bulk(), data);
      httpStatus =
          (retryFailedEntries(response.body(), data)
              ? HttpStatus.SERVICE_UNAVAILABLE
              : HttpStatus.OK);
    } while (data.length() > 0 && retry.retry(httpStatus));
  }