com.aliyun.openservices.aliyun.log.producer.errors.ResultFailedException Java Examples

The following examples show how to use com.aliyun.openservices.aliyun.log.producer.errors.ResultFailedException. 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: ProducerTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendWithInvalidAccessKeySecret() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  final Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildInvalidAccessKeySecretProjectConfig());
  ListenableFuture<Result> f =
      producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), buildLogItem());
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals("SignatureNotMatch", result.getErrorCode());
    Assert.assertTrue(!result.getErrorMessage().isEmpty());
    List<Attempt> attempts = result.getReservedAttempts();
    Assert.assertEquals(1, attempts.size());
    for (Attempt attempt : attempts) {
      Assert.assertFalse(attempt.isSuccess());
      Assert.assertEquals("SignatureNotMatch", attempt.getErrorCode());
      Assert.assertTrue(!attempt.getErrorMessage().isEmpty());
      Assert.assertTrue(!attempt.getRequestId().isEmpty());
    }
  }
}
 
Example #2
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendWithNotExistProject() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  ListenableFuture<Result> f = producer.send("projectNotExist", "logStore", new LogItem(), null);
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals(Errors.PROJECT_CONFIG_NOT_EXIST, result.getErrorCode());
    Assert.assertEquals(
        "Cannot get the projectConfig for project projectNotExist", result.getErrorMessage());
  }
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #3
Source File: SlsOutputFormat.java    From alibaba-flink-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable throwable) {
	if (callBackException == null) {
		LOG.error("loghub-callback: send failed, exception:", throwable);
		if (throwable instanceof ResultFailedException) {
			ResultFailedException exception = ((ResultFailedException) throwable);
			callBackException = new RuntimeException("An exception was thrown, result: " + exception.getResult().toString(), throwable);
		} else {
			callBackException = new RuntimeException("An exception was thrown", throwable);
		}
	}
	numCommitted.incrementAndGet();
}
 
Example #4
Source File: SampleProducerWithFuture.java    From aliyun-log-producer-sample with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable t) {
  if (t instanceof ResultFailedException) {
    Result result = ((ResultFailedException) t).getResult();
    LOGGER.error(
        "Failed to send logs, project={}, logStore={}, result={}", project, logStore, result);
  } else {
    LOGGER.error("Failed to send log, e=", t);
  }
  completed.getAndIncrement();
}
 
Example #5
Source File: ProducerBatch.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
private void setFutures(Result result) {
  for (Thunk thunk : thunks) {
    try {
      if (result.isSuccessful()) {
        thunk.future.set(result);
      } else {
        thunk.future.setException(new ResultFailedException(result));
      }
    } catch (Exception e) {
      LOGGER.error("Failed to set future, groupKey={}, e=", groupKey, e);
    }
  }
}
 
Example #6
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithRequestError2() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  int retries = 5;
  int maxReservedAttempts = 2;
  producerConfig.setRetries(retries);
  producerConfig.setMaxReservedAttempts(maxReservedAttempts);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  ListenableFuture<Result> f = producer.send("project", "logStore", ProducerTest.buildLogItem());
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals("RequestError", result.getErrorCode());
    Assert.assertTrue(
        result.getErrorMessage().startsWith("Web request failed: project.endpoint"));
    List<Attempt> attempts = result.getReservedAttempts();
    Assert.assertEquals(maxReservedAttempts, attempts.size());
    Assert.assertEquals(retries + 1, result.getAttemptCount());
    for (Attempt attempt : attempts) {
      Assert.assertFalse(attempt.isSuccess());
      Assert.assertEquals("RequestError", attempt.getErrorCode());
      Assert.assertTrue(
          attempt.getErrorMessage().startsWith("Web request failed: project.endpoint"));
      Assert.assertEquals("", attempt.getRequestId());
    }
  }
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #7
Source File: ProducerMultiShardTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidSend() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  producerConfig.setAdjustShardHash(false);
  final Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(
      new ProjectConfig(
          System.getenv("PROJECT"),
          System.getenv("ENDPOINT"),
          System.getenv("ACCESS_KEY_ID"),
          System.getenv("ACCESS_KEY_SECRET")));
  producer.putProjectConfig(
      new ProjectConfig(
          System.getenv("OTHER_PROJECT"),
          System.getenv("ENDPOINT"),
          System.getenv("ACCESS_KEY_ID"),
          System.getenv("ACCESS_KEY_SECRET")));
  ListenableFuture<Result> f =
      producer.send(
          System.getenv("OTHER_PROJECT"),
          System.getenv("OTHER_LOG_STORE"),
          "",
          "",
          "0",
          ProducerTest.buildLogItem());
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Assert.assertEquals("ShardNotExist", resultFailedException.getErrorCode());
  }
}
 
Example #8
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendWithRequestError() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  int retries = 5;
  producerConfig.setRetries(retries);
  producerConfig.setMaxReservedAttempts(retries + 1);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  ListenableFuture<Result> f = producer.send("project", "logStore", ProducerTest.buildLogItem());
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals("RequestError", result.getErrorCode());
    Assert.assertTrue(
        result.getErrorMessage().startsWith("Web request failed: project.endpoint"));
    List<Attempt> attempts = result.getReservedAttempts();
    Assert.assertEquals(retries + 1, attempts.size());
    long t1;
    long t2 = -1;
    for (int i = 0; i < attempts.size(); ++i) {
      Attempt attempt = attempts.get(i);
      Assert.assertFalse(attempt.isSuccess());
      Assert.assertEquals("RequestError", attempt.getErrorCode());
      Assert.assertTrue(
          attempt.getErrorMessage().startsWith("Web request failed: project.endpoint"));
      Assert.assertEquals("", attempt.getRequestId());
      t1 = t2;
      t2 = attempt.getTimestampMs();
      if (i == 0) {
        continue;
      }
      long diff = t2 - t1;
      long retryBackoffMs = producerConfig.getBaseRetryBackoffMs() * LongMath.pow(2, i - 1);
      long low = retryBackoffMs - (long) (producerConfig.getBaseRetryBackoffMs() * 0.1);
      long high = retryBackoffMs + (long) (producerConfig.getBaseRetryBackoffMs() * 0.2);
      if (i == 1) {
        Assert.assertTrue(low <= diff);
      } else {
        Assert.assertTrue(low <= diff && diff <= high);
      }
    }
  }
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}