Java Code Examples for org.influxdb.InfluxDBException#buildExceptionForErrorState()

The following examples show how to use org.influxdb.InfluxDBException#buildExceptionForErrorState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 6 votes vote down vote up
private <T> T execute(final Call<T> call) {
  try {
    Response<T> response = call.execute();
    if (response.isSuccessful()) {
      return response.body();
    }
    try (ResponseBody errorBody = response.errorBody()) {
      if (messagePack) {
        throw InfluxDBException.buildExceptionForErrorState(errorBody.byteStream());
      } else {
        throw InfluxDBException.buildExceptionForErrorState(errorBody.string());
      }
    }
  } catch (IOException e) {
    throw new InfluxDBIOException(e);
  }
}
 
Example 2
Source File: RetryCapableBatchWriterTest.java    From influxdb-java with MIT License 5 votes vote down vote up
@Test
public void testAllNonRecoverableExceptions() {
  
  InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
  BiConsumer errorHandler = mock(BiConsumer.class);
  RetryCapableBatchWriter rw = new RetryCapableBatchWriter(mockInfluxDB, errorHandler,
          150, 100);

  InfluxDBException nonRecoverable1 = InfluxDBException.buildExceptionForErrorState(createErrorBody("database not found: cvfdgf"));
  InfluxDBException nonRecoverable2 = InfluxDBException.buildExceptionForErrorState(createErrorBody("points beyond retention policy 'abc'"));
  InfluxDBException nonRecoverable3 = InfluxDBException.buildExceptionForErrorState(createErrorBody("unable to parse 'abc'"));
  InfluxDBException nonRecoverable4 = InfluxDBException.buildExceptionForErrorState(createErrorBody("hinted handoff queue not empty service='abc'"));
  InfluxDBException nonRecoverable5 = InfluxDBException.buildExceptionForErrorState(createErrorBody("field type conflict 'abc'"));
  InfluxDBException nonRecoverable6 = new InfluxDBException.RetryBufferOverrunException(createErrorBody("Retry BufferOverrun Exception"));
  InfluxDBException nonRecoverable7 = InfluxDBException.buildExceptionForErrorState(createErrorBody("user is not authorized to write to database"));
  InfluxDBException nonRecoverable8 = InfluxDBException.buildExceptionForErrorState(createErrorBody("authorization failed"));
  InfluxDBException nonRecoverable9 = InfluxDBException.buildExceptionForErrorState(createErrorBody("username required"));

  List<InfluxDBException> exceptions = Arrays.asList(nonRecoverable1, nonRecoverable2, nonRecoverable3,
      nonRecoverable4, nonRecoverable5, nonRecoverable6, nonRecoverable7, nonRecoverable8, nonRecoverable9);
  int size = exceptions.size();
  doAnswer(new TestAnswer() {
    int i = 0;
    @Override
    protected void check(InvocationOnMock invocation) {
      if (i < size) {
        throw exceptions.get(i++);
      }
    }
  }).when(mockInfluxDB).write(any(BatchPoints.class));
  
  BatchPoints bp = getBP(8);
  for (int i = 0; i < size; i++) {
    rw.write(Collections.singletonList(bp));
  }
  verify(errorHandler, times(size)).accept(any(), any());;
}
 
Example 3
Source File: RetryCapableBatchWriterTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
void defaultExceptionIsRecoverable() {
  InfluxDBException unknownError = InfluxDBException.buildExceptionForErrorState(createErrorBody("unknown error"));

  Assertions.assertTrue(unknownError.isRetryWorth());
}
 
Example 4
Source File: RetryCapableBatchWriterTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
public void testBufferCountConsistency() throws Exception {
  InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
  BiConsumer errorHandler = mock(BiConsumer.class);
  int MAX_BUFFER_CAPACITY = 3000;
  RetryCapableBatchWriter rw = new RetryCapableBatchWriter(mockInfluxDB, errorHandler,
          MAX_BUFFER_CAPACITY, 1000);

  Exception nonRecoverable = InfluxDBException.buildExceptionForErrorState("{ \"error\": \"database not found: cvfdgf\" }");
  Exception recoverable = InfluxDBException.buildExceptionForErrorState("{ \"error\": \"cache-max-memory-size exceeded 104/1400\" }");

  // need access to private properties for quality testing
  Field localUsedRetryBufferCapacity = RetryCapableBatchWriter.class.
          getDeclaredField("usedRetryBufferCapacity");
  Field localBatchQueue = RetryCapableBatchWriter.class.
          getDeclaredField("batchQueue");

  localUsedRetryBufferCapacity.setAccessible(true);
  localBatchQueue.setAccessible(true);

  // cycle test with all possible outcomes: non retry, with retry, write pass
  // "with retry" will cover https://github.com/influxdata/influxdb-java/issues/541
  Exception[] tryExceptionList = new Exception[]{nonRecoverable, recoverable, null};

  for (Exception exception : tryExceptionList) {
    // try for 100 times with random number of points each time
    for (int i=0; i < 100; i++) {
      int count = 200 + ((i * 777) & 511);
      BatchPoints bps = getBP(count);
      if (exception != null) {
          Mockito.doThrow(exception).when(mockInfluxDB).write(bps);
      }
      else {
          Mockito.reset(mockInfluxDB);
      }
      rw.write(Collections.singletonList(bps));

      // count actual number of points in batchQueue
      @SuppressWarnings("unchecked")
      LinkedList<BatchPoints> batchQueue = (LinkedList<BatchPoints>)localBatchQueue.get(rw);
      int sum = 0;
      for (BatchPoints b : batchQueue) {
        sum += b.getPoints().size();
      }

      // compare with value of usedRetryBufferCapacity
      int localUsedRetryBufferCapacityVal = (int) localUsedRetryBufferCapacity.get(rw);

      Assertions.assertTrue(sum == localUsedRetryBufferCapacityVal,
              "batchSize usage counter mismatch UsedRetryBufferCapacityVal, "
              + sum + " != " + localUsedRetryBufferCapacityVal);
      Assertions.assertTrue(sum < MAX_BUFFER_CAPACITY, "batchSize usage outside of allowed range " + sum);
    }
  }
}