private OperationContext sendReplicatePacket(final Packet packet, boolean lineUp) { if (!enabled) return null; boolean runItNow = false; OperationContext repliToken = OperationContextImpl.getContext(executorFactory); if (lineUp) { repliToken.replicationLineUp(); } synchronized (replicationLock) { if (enabled) { pendingTokens.add(repliToken); replicatingChannel.send(packet); } else { // Already replicating channel failed, so just play the action now runItNow = true; } } // Execute outside lock if (runItNow) { repliToken.replicationDone(); } return repliToken; }
/** * @throws IllegalStateException By default, all replicated packets generate a replicated * response. If your packets are triggering this exception, it may be because the packets were * not sent with {@link #sendReplicatePacket(Packet)}. */ private void replicated() { OperationContext ctx = pendingTokens.poll(); if (ctx == null) { throw new IllegalStateException("Missing replication token on the queue."); } ctx.replicationDone(); }
/** * Completes any pending operations. * * <p>This can be necessary in case the live loses connection to the backup (network failure, or * backup crashing). */ public void clearReplicationTokens() { synchronized (replicationLock) { while (!pendingTokens.isEmpty()) { OperationContext ctx = pendingTokens.poll(); try { ctx.replicationDone(); } catch (Throwable e) { HornetQServerLogger.LOGGER.errorCompletingCallbackOnReplicationManager(e); } } } }
public void waitContextCompletion() { try { if (!context.waitCompletion(10000)) { HornetQServerLogger.LOGGER.errorCompletingContext(new Exception("warning")); } } catch (Exception e) { HornetQServerLogger.LOGGER.warn(e.getMessage(), e); } }
@Test public void testOrderOnNonPersistency() throws Exception { setupServer(true); final ArrayList<Integer> executions = new ArrayList<Integer>(); StorageManager storage = getStorage(); manager = liveServer.getReplicationManager(); Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager); int numberOfAdds = 200; final CountDownLatch latch = new CountDownLatch(numberOfAdds); OperationContext ctx = storage.getContext(); for (int i = 0; i < numberOfAdds; i++) { final int nAdd = i; if (i % 2 == 0) { replicatedJournal.appendPrepareRecord(i, new FakeData(), false); } ctx.executeOnCompletion( new IOAsyncTask() { public void onError(final int errorCode, final String errorMessage) {} public void done() { executions.add(nAdd); latch.countDown(); } }); } Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); for (int i = 0; i < numberOfAdds; i++) { Assert.assertEquals(i, executions.get(i).intValue()); } Assert.assertEquals(0, manager.getActiveTokens().size()); }
public void close(final boolean failed) { if (closed) return; context.executeOnCompletion( new IOAsyncTask() { public void onError(int errorCode, String errorMessage) {} public void done() { try { doClose(failed); } catch (Exception e) { HornetQServerLogger.LOGGER.errorClosingSession(e); } } }); }
@Test public void testExceptionSettingActionBefore() throws Exception { OperationContext ctx = OperationContextImpl.getContext(factory); ctx.storeLineUp(); String msg = "I'm an exception"; ctx.onError(HornetQExceptionType.UNBLOCKED.getCode(), msg); final AtomicInteger lastError = new AtomicInteger(0); final List<String> msgsResult = new ArrayList<String>(); final CountDownLatch latch = new CountDownLatch(1); ctx.executeOnCompletion( new IOAsyncTask() { public void onError(final int errorCode, final String errorMessage) { lastError.set(errorCode); msgsResult.add(errorMessage); latch.countDown(); } public void done() {} }); Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); Assert.assertEquals(5, lastError.get()); Assert.assertEquals(1, msgsResult.size()); Assert.assertEquals(msg, msgsResult.get(0)); final CountDownLatch latch2 = new CountDownLatch(1); // Adding the Task after the exception should still throw an exception ctx.executeOnCompletion( new IOAsyncTask() { public void onError(final int errorCode, final String errorMessage) { lastError.set(errorCode); msgsResult.add(errorMessage); latch2.countDown(); } public void done() {} }); Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS)); Assert.assertEquals(2, msgsResult.size()); Assert.assertEquals(msg, msgsResult.get(0)); Assert.assertEquals(msg, msgsResult.get(1)); final CountDownLatch latch3 = new CountDownLatch(1); ctx.executeOnCompletion( new IOAsyncTask() { public void onError(final int errorCode, final String errorMessage) {} public void done() { latch3.countDown(); } }); Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS)); }