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

The following examples show how to use com.aliyun.openservices.aliyun.log.producer.errors.ProducerException. 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: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
private long closeFailureBatchHandler(long timeoutMs)
    throws InterruptedException, ProducerException {
  long startMs = System.currentTimeMillis();
  failureBatchHandler.close();
  boolean invokedFromCallback =
      Thread.currentThread() == this.successBatchHandler
          || Thread.currentThread() == this.failureBatchHandler;
  if (invokedFromCallback) {
    LOGGER.warn(
        "Skip join failure batch handler since you have incorrectly invoked close from the producer call-back");
    return timeoutMs;
  }
  failureBatchHandler.join(timeoutMs);
  if (failureBatchHandler.isAlive()) {
    LOGGER.warn("The failure batch handler thread is still alive");
    throw new ProducerException("the failure batch handler thread is still alive");
  }
  long nowMs = System.currentTimeMillis();
  return Math.max(0, timeoutMs - nowMs + startMs);
}
 
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: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
private long closeSuccessBatchHandler(long timeoutMs)
    throws InterruptedException, ProducerException {
  long startMs = System.currentTimeMillis();
  successBatchHandler.close();
  boolean invokedFromCallback = Thread.currentThread() == this.successBatchHandler;
  if (invokedFromCallback) {
    LOGGER.warn(
        "Skip join success batch handler since you have incorrectly invoked close from the producer call-back");
    return timeoutMs;
  }
  successBatchHandler.join(timeoutMs);
  if (successBatchHandler.isAlive()) {
    LOGGER.warn("The success batch handler thread is still alive");
    throw new ProducerException("the success batch handler thread is still alive");
  }
  long nowMs = System.currentTimeMillis();
  return Math.max(0, timeoutMs - nowMs + startMs);
}
 
Example #4
Source File: LogAccumulator.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
public ListenableFuture<Result> append(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException {
  appendsInProgress.incrementAndGet();
  try {
    return doAppend(project, logStore, topic, source, shardHash, logItems, callback);
  } finally {
    appendsInProgress.decrementAndGet();
  }
}
 
Example #5
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendLogsThrownLogSizeTooLargeException()
    throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  producerConfig.setTotalSizeInBytes(30);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(LogSizeTooLargeException.class);
  thrown.expectMessage(
      "the logs is 36 bytes which is larger than the totalSizeInBytes you specified");
  List<LogItem> logItems = new ArrayList<LogItem>();
  logItems.add(ProducerTest.buildLogItem());
  logItems.add(ProducerTest.buildLogItem());
  logItems.add(ProducerTest.buildLogItem());
  producer.send("project", "logStore", logItems);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #6
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 #7
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendLogsThrownMaxBatchCountExceedException()
    throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(MaxBatchCountExceedException.class);
  thrown.expectMessage(
      "the log list size is 40961 which exceeds the MAX_BATCH_COUNT "
          + ProducerConfig.MAX_BATCH_COUNT);
  List<LogItem> logItems = new ArrayList<LogItem>();
  for (int i = 0; i < ProducerConfig.MAX_BATCH_COUNT + 1; ++i) {
    logItems.add(ProducerTest.buildLogItem());
  }
  producer.send("project", "logStore", logItems);
}
 
Example #8
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
/**
 * Send a log asynchronously. It will convert the log to a log list which only contains one log,
 * and then invoke <code>send(project, logStore, topic, source, logItems, logItem,
 * callback)</code>. See {@link #send(String, String, String, String, String, List, Callback)} for
 * details.
 */
@Override
public ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException {
  Utils.assertArgumentNotNull(logItem, "logItem");
  List<LogItem> logItems = new ArrayList<LogItem>();
  logItems.add(logItem);
  return send(project, logStore, topic, source, shardHash, logItems, callback);
}
 
Example #9
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, List, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #10
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendLogThrownLogSizeTooLargeException()
    throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  producerConfig.setTotalSizeInBytes(10);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(LogSizeTooLargeException.class);
  thrown.expectMessage(
      "the logs is 12 bytes which is larger than the totalSizeInBytes you specified");
  producer.send("project", "logStore", ProducerTest.buildLogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #11
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, LogItem)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem)
    throws InterruptedException, ProducerException;
 
Example #12
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, List)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems)
    throws InterruptedException, ProducerException;
 
Example #13
Source File: SlsOutputFormat.java    From alibaba-flink-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void writeRecord(T row) throws IOException {

	if (null != row) {
		long start = System.currentTimeMillis();
		List<LogItem> tmpLogGroup = new ArrayList<>(1);
		tmpLogGroup.add(serializationSchema.getLogItem(row));

		/** calc the partition key, if not set, use null as random shard **/
		String partitionKey = serializationSchema.getPartitionKey(row);
		String topic = serializationSchema.getTopic(row);
		String source = serializationSchema.getSource(row);
		try {
			ListenableFuture<Result> future = logProducerProvider.getClient().send(
					this.projectName, this.logstore, topic, source, partitionKey, tmpLogGroup);
			Futures.addCallback(future, sendFutureCallback, executor);
			numSent.incrementAndGet();
		} catch (InterruptedException | ProducerException e) {
			callBackException = new RuntimeException(e);
		}

		if (null != callBackException) {
			LOG.warn("Fail in write to SLS", callBackException);
			if (failOnError) {
				throw callBackException;
			}
			callBackException = null;
		}

		// report metrics
		long end = System.currentTimeMillis();
		latencyGauge.report(end - start, 1);
		outTps.markEvent();
	}

}
 
Example #14
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyLogItems() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logItems cannot be empty");
  List<LogItem> logItems = new ArrayList<LogItem>();
  producer.send("project", "logStore", logItems);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #15
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, LogItem, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #16
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyProject() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("project cannot be empty");
  producer.send("", "logStore", new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #17
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, LogItem, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #18
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, List, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #19
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithNullLogItems() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logItems cannot be null");
  producer.send("project", "logStore", (List<LogItem>) null);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #20
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithNullLogItem() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logItem cannot be null");
  producer.send("project", "logStore", (LogItem) null);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #21
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyLogStore() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logStore cannot be empty");
  producer.send("project", "", new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #22
Source File: ProducerTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxBatchSizeInBytes() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  producerConfig.setBatchSizeThresholdInBytes(27);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  LogItem logItem = new LogItem();
  logItem.PushBack("key1", "val1");
  logItem.PushBack("key2", "val2");
  logItem.PushBack("key3", "val3");
  int sizeInBytes = LogSizeCalculator.calculate(logItem);
  Assert.assertEquals(28, sizeInBytes);
  producer.send("project", "logStore", new LogItem());
}
 
Example #23
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithNullLogStore() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logStore cannot be null");
  producer.send("project", null, new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #24
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithNullProject() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(
      new ProjectConfig("project", "endpoint", "accessKeyId", "accessKeySecret"));
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("project cannot be null");
  producer.send(null, "logStore", new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #25
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
private long closeTimeoutThreadPool(long timeoutMs)
    throws InterruptedException, ProducerException {
  long startMs = System.currentTimeMillis();
  timeoutThreadPool.shutdown();
  if (timeoutThreadPool.awaitTermination(timeoutMs, TimeUnit.MILLISECONDS)) {
    LOGGER.debug("The timeoutThreadPool is terminated");
  } else {
    LOGGER.warn("The timeoutThreadPool is not fully terminated");
    throw new ProducerException("the timeoutThreadPool is not fully terminated");
  }
  long nowMs = System.currentTimeMillis();
  return Math.max(0, timeoutMs - nowMs + startMs);
}
 
Example #26
Source File: LogProducerProviderTest.java    From alibaba-flink-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void testClose() throws ProducerException, InterruptedException {
	LogProducerProvider producerProvider = new LogProducerProvider(
			"test-project", "test-endpoint", "test-ak", "test-secret", 1, 100);

	LogProducer producer = Mockito.mock(LogProducer.class);
	Whitebox.setInternalState(producerProvider, "client", producer);

	Mockito.doThrow(new InterruptedException()).doNothing().when(producer).close();
	producerProvider.closeClient();

	Mockito.verify(producer, Mockito.times(2)).close();
}
 
Example #27
Source File: LoghubAppender.java    From aliyun-log-logback-appender with Apache License 2.0 5 votes vote down vote up
private void doStop() throws InterruptedException, ProducerException {
    if (!isStarted()) {
        return;
    }

    super.stop();
    producer.close();
}
 
Example #28
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 #29
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a log asynchronously. Equivalent to <code>send(project, logStore, topic, source, "",
 * logItem, null)</code>. See {@link #send(String, String, String, String, String, LogItem,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project, String logStore, String topic, String source, LogItem logItem)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, null, logItem, null);
}
 
Example #30
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a list of logs asynchronously. Equivalent to <code>send(project, logStore, topic, source,
 * "", logItems, null)</code>. See {@link #send(String, String, String, String, String, List,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project, String logStore, String topic, String source, List<LogItem> logItems)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, null, logItems, null);
}