use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.
the class TimedBufferTest method testTimeOnTimedBuffer.
@Test
public void testTimeOnTimedBuffer() throws Exception {
final ReusableLatch latchFlushed = new ReusableLatch(0);
final AtomicInteger flushes = new AtomicInteger(0);
class TestObserver implements TimedBufferObserver {
@Override
public void flushBuffer(final ByteBuffer buffer, final boolean sync, final List<IOCallback> callbacks) {
for (IOCallback callback : callbacks) {
callback.done();
}
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.utils.timedbuffer.TimedBufferObserver#newBuffer(int, int)
*/
@Override
public ByteBuffer newBuffer(final int minSize, final int maxSize) {
return ByteBuffer.allocate(maxSize);
}
@Override
public int getRemainingBytes() {
return 1024 * 1024;
}
}
TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false);
timedBuffer.start();
TestObserver observer = new TestObserver();
timedBuffer.setObserver(observer);
int x = 0;
byte[] bytes = new byte[10];
for (int j = 0; j < 10; j++) {
bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
}
ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes);
IOCallback callback = new IOCallback() {
@Override
public void done() {
System.out.println("done");
latchFlushed.countDown();
}
@Override
public void onError(int errorCode, String errorMessage) {
}
};
try {
latchFlushed.setCount(2);
// simulating a low load period
timedBuffer.addBytes(buff, true, callback);
Thread.sleep(1000);
timedBuffer.addBytes(buff, true, callback);
Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
latchFlushed.setCount(5);
flushes.set(0);
// Sending like crazy... still some wait (1 millisecond) between each send..
long time = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
timedBuffer.addBytes(buff, true, callback);
Thread.sleep(1);
}
Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
// The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer.
Assert.assertTrue("Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450);
// ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself
// for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test
// it should be in fact only writing once..
// i will set for 3 just in case there's a GC or anything else happening on the test
Assert.assertTrue("Too many writes were called", flushes.get() <= 3);
} finally {
timedBuffer.stop();
}
}
use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.
the class OperationContextUnitTest method testCaptureExceptionOnFailure.
@Test
public void testCaptureExceptionOnFailure() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory());
final CountDownLatch latch = new CountDownLatch(1);
final OperationContextImpl context = new OperationContextImpl(executor) {
@Override
public void complete() {
super.complete();
latch.countDown();
}
};
context.storeLineUp();
final AtomicInteger failures = new AtomicInteger(0);
Thread t = new Thread() {
@Override
public void run() {
try {
context.waitCompletion(5000);
} catch (Throwable e) {
e.printStackTrace();
failures.incrementAndGet();
}
}
};
t.start();
// Need to wait complete to be called first or the test would be invalid.
// We use a latch instead of forcing a sleep here
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
context.onError(ActiveMQExceptionType.UNSUPPORTED_PACKET.getCode(), "Poop happens!");
t.join();
Assert.assertEquals(1, failures.get());
failures.set(0);
final AtomicInteger operations = new AtomicInteger(0);
// We should be up to date with lineUps and executions. this should now just finish processing
context.executeOnCompletion(new IOCallback() {
@Override
public void done() {
operations.incrementAndGet();
}
@Override
public void onError(final int errorCode, final String errorMessage) {
failures.incrementAndGet();
}
});
Assert.assertEquals(1, failures.get());
Assert.assertEquals(0, operations.get());
}
use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.
the class OperationContextUnitTest method testCompleteTaskAfterPaging.
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testCompleteTaskAfterPaging() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory());
try {
OperationContextImpl impl = new OperationContextImpl(executor);
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
impl.executeOnCompletion(new IOCallback() {
@Override
public void onError(int errorCode, String errorMessage) {
}
@Override
public void done() {
latch1.countDown();
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
for (int i = 0; i < 10; i++) impl.storeLineUp();
for (int i = 0; i < 3; i++) impl.pageSyncLineUp();
impl.executeOnCompletion(new IOCallback() {
@Override
public void onError(int errorCode, String errorMessage) {
}
@Override
public void done() {
latch2.countDown();
}
});
assertFalse(latch2.await(1, TimeUnit.MILLISECONDS));
for (int i = 0; i < 9; i++) impl.done();
for (int i = 0; i < 2; i++) impl.pageSyncDone();
assertFalse(latch2.await(1, TimeUnit.MILLISECONDS));
impl.done();
impl.pageSyncDone();
assertTrue(latch2.await(10, TimeUnit.SECONDS));
} finally {
executor.shutdown();
}
}
use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.
the class AMQSession method sendShouldBlockProducer.
private void sendShouldBlockProducer(final ProducerInfo producerInfo, final Message messageSend, final boolean sendProducerAck, final PagingStore store, final ActiveMQDestination dest, final AtomicInteger count, final org.apache.activemq.artemis.api.core.Message coreMsg, final SimpleString address) throws ResourceAllocationException {
if (!store.checkMemory(() -> {
Exception exceptionToSend = null;
try {
getCoreSession().send(coreMsg, false, dest.isTemporary());
} catch (Exception e) {
logger.warn(e.getMessage(), e);
exceptionToSend = e;
}
connection.enableTtl();
if (count == null || count.decrementAndGet() == 0) {
if (exceptionToSend != null) {
this.connection.getContext().setDontSendReponse(false);
connection.sendException(exceptionToSend);
} else {
server.getStorageManager().afterCompleteOperations(new IOCallback() {
@Override
public void done() {
if (sendProducerAck) {
try {
ProducerAck ack = new ProducerAck(producerInfo.getProducerId(), messageSend.getSize());
connection.dispatchAsync(ack);
} catch (Exception e) {
connection.getContext().setDontSendReponse(false);
ActiveMQServerLogger.LOGGER.warn(e.getMessage(), e);
connection.sendException(e);
}
} else {
connection.getContext().setDontSendReponse(false);
try {
Response response = new Response();
response.setCorrelationId(messageSend.getCommandId());
connection.dispatchAsync(response);
} catch (Exception e) {
ActiveMQServerLogger.LOGGER.warn(e.getMessage(), e);
connection.sendException(e);
}
}
}
@Override
public void onError(int errorCode, String errorMessage) {
try {
final IOException e = new IOException(errorMessage);
ActiveMQServerLogger.LOGGER.warn(errorMessage);
connection.serviceException(e);
} catch (Exception ex) {
ActiveMQServerLogger.LOGGER.debug(ex);
}
}
});
}
}
})) {
this.connection.getContext().setDontSendReponse(false);
connection.enableTtl();
throw new ResourceAllocationException("Queue is full " + address);
}
}
use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.
the class ReplicationTest method testNoActions.
@Test
public void testNoActions() throws Exception {
setupServer(true);
StorageManager storage = getStorage();
manager = liveServer.getReplicationManager();
waitForComponent(manager);
Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
replicatedJournal.appendPrepareRecord(1, new FakeData(), false);
final CountDownLatch latch = new CountDownLatch(1);
storage.afterCompleteOperations(new IOCallback() {
@Override
public void onError(final int errorCode, final String errorMessage) {
}
@Override
public void done() {
latch.countDown();
}
});
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
Assert.assertEquals("should be empty " + manager.getActiveTokens(), 0, manager.getActiveTokens().size());
}
Aggregations