public RestStatus status() {
   RestStatus status = RestStatus.OK;
   for (IndexDeleteByQueryResponse indexResponse : indices.values()) {
     if (indexResponse.getFailedShards() > 0) {
       RestStatus indexStatus = indexResponse.getFailures()[0].status();
       if (indexResponse.getFailures().length > 1) {
         for (int i = 1; i < indexResponse.getFailures().length; i++) {
           if (indexResponse.getFailures()[i].status().getStatus() >= 500) {
             indexStatus = indexResponse.getFailures()[i].status();
           }
         }
       }
       if (status.getStatus() < indexStatus.getStatus()) {
         status = indexStatus;
       }
     }
   }
   return status;
 }
Esempio n. 2
0
 public RestStatus status() {
   if (shardFailures.length == 0) {
     if (successfulShards == 0 && totalShards > 0) {
       return RestStatus.SERVICE_UNAVAILABLE;
     }
     return RestStatus.OK;
   }
   // if total failure, bubble up the status code to the response level
   if (successfulShards == 0 && totalShards > 0) {
     RestStatus status = RestStatus.OK;
     for (int i = 0; i < shardFailures.length; i++) {
       RestStatus shardStatus = shardFailures[i].status();
       if (shardStatus.getStatus() >= status.getStatus()) {
         status = shardFailures[i].status();
       }
     }
     return status;
   }
   return RestStatus.OK;
 }
 @Override
 public void readFrom(StreamInput in) throws IOException {
   id = in.readVInt();
   description = in.readUTF();
   levels = new ClusterBlockLevel[in.readVInt()];
   for (int i = 0; i < levels.length; i++) {
     levels[i] = ClusterBlockLevel.fromId(in.readVInt());
   }
   retryable = in.readBoolean();
   disableStatePersistence = in.readBoolean();
   status = RestStatus.readFrom(in);
 }
 @Override
 public void writeTo(StreamOutput out) throws IOException {
   out.writeVInt(id);
   out.writeUTF(description);
   out.writeVInt(levels.length);
   for (ClusterBlockLevel level : levels) {
     out.writeVInt(level.id());
   }
   out.writeBoolean(retryable);
   out.writeBoolean(disableStatePersistence);
   RestStatus.writeTo(out, status);
 }
  public void testWrapExceptionToPreserveStatus() throws IOException {
    Exception cause = new Exception();

    // Successfully get the status without a body
    RestStatus status = randomFrom(RestStatus.values());
    ElasticsearchStatusException wrapped =
        RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("No error body.", wrapped.getMessage());

    // Successfully get the status without a body
    HttpEntity okEntity = new StringEntity("test body", StandardCharsets.UTF_8);
    wrapped =
        RemoteScrollableHitSource.wrapExceptionToPreserveStatus(
            status.getStatus(), okEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("body=test body", wrapped.getMessage());

    // Successfully get the status with a broken body
    IOException badEntityException = new IOException();
    HttpEntity badEntity = mock(HttpEntity.class);
    when(badEntity.getContent()).thenThrow(badEntityException);
    wrapped =
        RemoteScrollableHitSource.wrapExceptionToPreserveStatus(
            status.getStatus(), badEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);

    // Fail to get the status without a body
    int notAnHttpStatus = -1;
    assertNull(RestStatus.fromCode(notAnHttpStatus));
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals(
        "Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());

    // Fail to get the status without a body
    wrapped =
        RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals(
        "Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());

    // Fail to get the status with a broken body
    wrapped =
        RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals(
        "Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.",
        wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
  }