org.influxdb.InfluxDBException Java Examples

The following examples show how to use org.influxdb.InfluxDBException. 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: MessagePackTraverser.java    From influxdb-java with MIT License 6 votes vote down vote up
/**
 * Traverse over the whole message pack stream.
 * This method can be used for converting query results in chunk.
 *
 * @param is
 *          The MessagePack format input stream
 * @return an Iterable over the QueryResult objects
 *
 */
public Iterable<QueryResult> traverse(final InputStream is) {
  MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(is);

  return () -> {
    return new Iterator<QueryResult>() {
      @Override
      public boolean hasNext() {
        try {
          return unpacker.hasNext();
        } catch (IOException e) {
          throw new InfluxDBException(e);
        }
      }

      @Override
      public QueryResult next() {
        return parse(unpacker);
      }
    };
  };

}
 
Example #2
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 #3
Source File: InfluxHistory.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void write_(List<Map.Entry<DateTime, TimeSeriesValue>> tsvList) {
    try {
        final Point[] points = tsvList.stream()
                .flatMap(timestampedTsv -> timeSeriesValueToPoint(timestampedTsv.getValue(), timestampedTsv.getKey()))
                .toArray(Point[]::new);
        getInfluxDB().write(BatchPoints.database(getDatabase()).points(points).build());
    } catch (InfluxDBException ex) {
        if (ex.getCause() instanceof SocketTimeoutException)
            LOG.log(Level.WARNING, "influx write timed out"); // Influx documentation says this might happen
        else
            throw ex;
    }
}
 
Example #4
Source File: InfluxDBMethodInterceptorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterceptWithException() throws Throwable {
    interceptor.beforeMethod(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, null);
    interceptor.handleMethodException(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, new InfluxDBException("test exception"));
    interceptor.afterMethod(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, null);

    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertThat(spans.size(), is(1));
    assertWriteInfluxDBSpan(spans.get(0));

    assertLogData(SpanHelper.getLogs(spans.get(0)));
}
 
Example #5
Source File: InfluxDBMethodInterceptorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void assertLogData(List<LogDataEntity> logDataEntities) {
    assertThat(logDataEntities.size(), is(1));
    LogDataEntity logData = logDataEntities.get(0);
    Assert.assertThat(logData.getLogs().size(), is(4));
    Assert.assertThat(logData.getLogs().get(0).getValue(),
                    CoreMatchers.<Object>is("error"));
    Assert.assertThat(logData.getLogs().get(1).getValue(),
                    CoreMatchers.<Object>is(InfluxDBException.class.getName()));
    Assert.assertEquals("test exception", logData.getLogs().get(2).getValue());
    assertNotNull(logData.getLogs().get(3).getValue());
}
 
Example #6
Source File: MessagePackTraverser.java    From influxdb-java with MIT License 5 votes vote down vote up
private QueryResult parse(final MessageUnpacker unpacker) {
  QueryResult queryResult = new QueryResult();
  QueryResultModelPath queryResultPath = new QueryResultModelPath();
  queryResultPath.add("queryResult", queryResult);
  try {
    traverse(unpacker, queryResultPath, 1);
  } catch (IOException e) {
    throw new InfluxDBException(e);
  }
  return queryResult;
}
 
Example #7
Source File: RetryCapableBatchWriter.java    From influxdb-java with MIT License 5 votes vote down vote up
private WriteResult(final InfluxDBException e) {
  this.throwable = e;
  if (e.isRetryWorth()) {
    this.outcome = WriteResultOutcome.FAILED_RETRY_POSSIBLE;
  } else {
    this.outcome = WriteResultOutcome.FAILED_RETRY_IMPOSSIBLE;
  }
}
 
Example #8
Source File: RetryCapableBatchWriter.java    From influxdb-java with MIT License 5 votes vote down vote up
private void evictTooOldFailedWrites() {
  while (usedRetryBufferCapacity > retryBufferCapacity && batchQueue.size() > 0) {
    List<Point> points = batchQueue.removeFirst().getPoints();
    usedRetryBufferCapacity -= points.size();
    exceptionHandler.accept(points,
            new InfluxDBException.RetryBufferOverrunException(
                    "Retry buffer overrun, current capacity: " + retryBufferCapacity));
  }
}
 
Example #9
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 #10
Source File: RetryCapableBatchWriterTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
public void testClosingWriter() {
  InfluxDB mockInfluxDB = mock(InfluxDB.class);
  BiConsumer<Iterable<Point>, Throwable> errorHandler = mock(BiConsumer.class);

  BatchPoints bp5 = getBP(5);
  BatchPoints bp6 = getBP(6);
  BatchPoints bp90 = getBP(90);
  
  doAnswer(new TestAnswer() {
    int i = 0;
    @Override
    protected void check(InvocationOnMock invocation) {
      //first 4 calls
      if (i++ < 4) {
        throw InfluxDBException.buildExceptionForErrorState("cache-max-memory-size exceeded 104/1400"); 
      }
      return;
    }
  }).when(mockInfluxDB).write(any(BatchPoints.class));
  
  RetryCapableBatchWriter rw = new RetryCapableBatchWriter(mockInfluxDB, errorHandler,
      150, 100);
  
  rw.write(Collections.singletonList(bp5));
  rw.write(Collections.singletonList(bp6));
  rw.write(Collections.singletonList(bp90));
  //recoverable exception -> never errorHandler
  verify(errorHandler, never()).accept(any(), any());
  verify(mockInfluxDB, times(3)).write(any(BatchPoints.class));
  
  rw.close();
  
  ArgumentCaptor<BatchPoints> captor4Write = ArgumentCaptor.forClass(BatchPoints.class);
  ArgumentCaptor<List<Point>> captor4Accept = ArgumentCaptor.forClass(List.class);
  verify(errorHandler, times(1)).accept(captor4Accept.capture(), any());
  verify(mockInfluxDB, times(5)).write(captor4Write.capture());
  
  //bp5 and bp6 were merged and writing of the merged batch points on closing should be failed
  Assertions.assertEquals(11, captor4Accept.getValue().size());
  //bp90 was written because no more exception thrown
  Assertions.assertEquals(90, captor4Write.getAllValues().get(4).getPoints().size());
}
 
Example #11
Source File: RetryCapableBatchWriterTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
public void testRetryingKeepChronologicalOrder() {
  
  BatchPoints.Builder b = BatchPoints.database("d1");
  for (int i = 0; i < 200; i++) {
    b.point(Point.measurement("x1").time(1,TimeUnit.HOURS).
        addField("x", 1).
        tag("t", "v1").build()).build();
  }
  
  BatchPoints bp1 = b.build();
  
  b = BatchPoints.database("d1");
  
  b.point(Point.measurement("x1").time(1,TimeUnit.HOURS).
      addField("x", 2).
      tag("t", "v2").build()).build();
  
  for (int i = 0; i < 199; i++) {
    b.point(Point.measurement("x1").time(2,TimeUnit.HOURS).
        addField("x", 2).
        tag("t", "v2").build()).build();
  }
  BatchPoints bp2 = b.build();
  
  InfluxDB mockInfluxDB = mock(InfluxDB.class);
  BiConsumer<Iterable<Point>, Throwable> errorHandler = mock(BiConsumer.class);
  RetryCapableBatchWriter rw = new RetryCapableBatchWriter(mockInfluxDB, errorHandler,
      450, 150);
  doAnswer(new TestAnswer() {
    int i = 0;
    @Override
    protected void check(InvocationOnMock invocation) {
      if (i++ < 1) {
        throw InfluxDBException.buildExceptionForErrorState("cache-max-memory-size exceeded 104/1400"); 
      }
      return;
    }
  }).when(mockInfluxDB).write(any(BatchPoints.class));
  
  rw.write(Collections.singletonList(bp1));
  rw.write(Collections.singletonList(bp2));
  
  ArgumentCaptor<BatchPoints> captor4Write = ArgumentCaptor.forClass(BatchPoints.class);
  verify(mockInfluxDB, times(3)).write(captor4Write.capture());
  
  //bp1 written but failed because of recoverable cache-max-memory-size error
  Assertions.assertEquals(bp1, captor4Write.getAllValues().get(0));
  //bp1 rewritten on writing of bp2 
  Assertions.assertEquals(bp1, captor4Write.getAllValues().get(1));
  //bp2 written
  Assertions.assertEquals(bp2, captor4Write.getAllValues().get(2));
}
 
Example #12
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 #13
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);
    }
  }
}