Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine send after close exception #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.aliyun.openservices.aliyun.log.producer.errors.LogSizeTooLargeException;
import com.aliyun.openservices.aliyun.log.producer.errors.MaxBatchCountExceedException;
import com.aliyun.openservices.aliyun.log.producer.errors.ProducerException;
import com.aliyun.openservices.aliyun.log.producer.errors.SendAfterCloseException;
import com.aliyun.openservices.aliyun.log.producer.internals.*;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
Expand Down Expand Up @@ -75,6 +76,8 @@ public class LogProducer implements Producer {

private final ClientConfiguration clientConfiguration;

private volatile boolean closed = false;

/**
* Start up a LogProducer instance.
*
Expand Down Expand Up @@ -366,6 +369,9 @@ public ListenableFuture<Result> send(
List<LogItem> logItems,
Callback callback)
throws InterruptedException, ProducerException {
if (closed) {
throw new SendAfterCloseException("Can't send logs after the producer is closed");
}
Utils.assertArgumentNotNullOrEmpty(project, "project");
Utils.assertArgumentNotNullOrEmpty(logStore, "logStore");
if (topic == null) {
Expand Down Expand Up @@ -428,6 +434,7 @@ public void close(long timeoutMs) throws InterruptedException, ProducerException
throw new IllegalArgumentException(
"timeoutMs must be greater than or equal to 0, got " + timeoutMs);
}
this.closed = true;
ProducerException firstException = null;
LOGGER.info("Closing the log producer, timeoutMs={}", timeoutMs);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.aliyun.openservices.aliyun.log.producer.errors;

/** Send logs after producer is closed. */
public class SendAfterCloseException extends ProducerException {
public SendAfterCloseException() {
super();
}

public SendAfterCloseException(String message, Throwable cause) {
super(message, cause);
}

public SendAfterCloseException(String message) {
super(message);
}

public SendAfterCloseException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.aliyun.openservices.aliyun.log.producer.errors.ProducerException;
import com.aliyun.openservices.aliyun.log.producer.errors.ResultFailedException;
import com.aliyun.openservices.aliyun.log.producer.errors.RetriableErrors;
import com.aliyun.openservices.aliyun.log.producer.errors.SendAfterCloseException;
import com.aliyun.openservices.aliyun.log.producer.internals.LogSizeCalculator;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.auth.DefaultCredentials;
Expand Down Expand Up @@ -117,6 +118,52 @@ public void testSendWithCredentialsProvider() throws InterruptedException, Produ
assertProducerFinalState(producer);
}

@Test
public void testSendAfterClose() throws InterruptedException, ProducerException, ExecutionException {
ProducerConfig producerConfig = new ProducerConfig();
final Producer producer = new LogProducer(producerConfig);
producer.putProjectConfig(buildCredentialsProjectConfig());
ListenableFuture<Result> f = producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), buildLogItem());
Result result = f.get();
Assert.assertTrue(result.isSuccessful());
Assert.assertEquals("", result.getErrorCode());
Assert.assertEquals("", result.getErrorMessage());
Assert.assertEquals(1, result.getReservedAttempts().size());
Assert.assertTrue(!result.getReservedAttempts().get(0).getRequestId().isEmpty());

f = producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), null, null, buildLogItem());
result = f.get();
Assert.assertTrue(result.isSuccessful());
Assert.assertEquals("", result.getErrorCode());
Assert.assertEquals("", result.getErrorMessage());
Assert.assertEquals(1, result.getReservedAttempts().size());
Assert.assertTrue(!result.getReservedAttempts().get(0).getRequestId().isEmpty());

f = producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), "", "", buildLogItem());
result = f.get();
Assert.assertTrue(result.isSuccessful());
Assert.assertEquals("", result.getErrorCode());
Assert.assertEquals("", result.getErrorMessage());
Assert.assertEquals(1, result.getReservedAttempts().size());
Assert.assertTrue(!result.getReservedAttempts().get(0).getRequestId().isEmpty());

f = producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), "topic", "source", buildLogItem());
result = f.get();
Assert.assertTrue(result.isSuccessful());
Assert.assertEquals("", result.getErrorCode());
Assert.assertEquals("", result.getErrorMessage());
Assert.assertEquals(1, result.getReservedAttempts().size());
Assert.assertTrue(!result.getReservedAttempts().get(0).getRequestId().isEmpty());

producer.close();
assertProducerFinalState(producer);
try {
producer.send(System.getenv("PROJECT"), System.getenv("LOG_STORE"), "topic", "source", buildLogItem());
Assert.fail();
} catch (SendAfterCloseException e) {

}
}
@Test
public void testSendWithCallback()
throws InterruptedException, ProducerException, ExecutionException {
Expand Down