use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.
the class BridgeTest method internalTestMessageLoss.
/**
* This test will ignore messages
* What will cause the bridge to fail with a timeout
* The bridge should still recover the failure and reconnect on that case
*/
public void internalTestMessageLoss(final boolean largeMessage) throws Exception {
class MyInterceptor implements Interceptor {
public boolean ignoreSends = true;
public CountDownLatch latch;
MyInterceptor(int numberOfIgnores) {
latch = new CountDownLatch(numberOfIgnores);
}
@Override
public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
if (ignoreSends && packet instanceof SessionSendMessage || ignoreSends && packet instanceof SessionSendLargeMessage || ignoreSends && packet instanceof SessionSendContinuationMessage && !((SessionSendContinuationMessage) packet).isContinues()) {
IntegrationTestLogger.LOGGER.info("IGNORED: " + packet);
latch.countDown();
return false;
} else {
IntegrationTestLogger.LOGGER.info(packet);
return true;
}
}
}
MyInterceptor myInterceptor = new MyInterceptor(3);
Map<String, Object> server0Params = new HashMap<>();
server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);
Map<String, Object> server1Params = new HashMap<>();
addTargetParameters(server1Params);
server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
final String testAddress = "testAddress";
final String queueName0 = "queue0";
final String forwardAddress = "forwardAddress";
final String queueName1 = "queue1";
TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
HashMap<String, TransportConfiguration> connectors = new HashMap<>();
connectors.put(server1tc.getName(), server1tc);
server0.getConfiguration().setConnectorConfigurations(connectors);
final int messageSize = 1024;
final int numMessages = 1;
ArrayList<String> connectorConfig = new ArrayList<>();
connectorConfig.add(server1tc.getName());
BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(100).setReconnectAttempts(-1).setReconnectAttemptsOnSameNode(-1).setUseDuplicateDetection(false).setConfirmationWindowSize(numMessages * messageSize / 2).setStaticConnectors(connectorConfig).setCallTimeout(500);
List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
bridgeConfigs.add(bridgeConfiguration);
server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
queueConfigs0.add(queueConfig0);
server0.getConfiguration().setQueueConfigurations(queueConfigs0);
CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
queueConfigs1.add(queueConfig1);
server1.getConfiguration().setQueueConfigurations(queueConfigs1);
server1.start();
server1.getRemotingService().addIncomingInterceptor(myInterceptor);
server0.start();
locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc));
ClientSessionFactory sf0 = addSessionFactory(locator.createSessionFactory(server0tc));
ClientSessionFactory sf1 = addSessionFactory(locator.createSessionFactory(server1tc));
ClientSession session0 = sf0.createSession(false, true, true);
ClientSession session1 = sf1.createSession(false, true, true);
ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
ClientConsumer consumer1 = session1.createConsumer(queueName1);
session1.start();
final byte[] bytes = new byte[messageSize];
final SimpleString propKey = new SimpleString("testkey");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session0.createMessage(true);
if (largeMessage) {
message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(10 * 1024));
}
message.putIntProperty(propKey, i);
message.getBodyBuffer().writeBytes(bytes);
producer0.send(message);
}
assertTrue("where is the countDown?", myInterceptor.latch.await(30, TimeUnit.SECONDS));
myInterceptor.ignoreSends = false;
server1.getRemotingService().removeIncomingInterceptor(myInterceptor);
IntegrationTestLogger.LOGGER.info("No longer ignoring packets.");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer1.receive(30000);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getObjectProperty(propKey));
if (largeMessage) {
readLargeMessages(message, 10);
}
message.acknowledge();
}
Assert.assertNull(consumer1.receiveImmediate());
session0.close();
session1.close();
sf0.close();
sf1.close();
closeFields();
assertEquals("there should be no queues", 0, loadQueues(server0).size());
}
use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.
the class TemporaryQueueTest method testDeleteTemporaryQueueWhenClientCrash.
@Test
public void testDeleteTemporaryQueueWhenClientCrash() throws Exception {
session.close();
sf.close();
final SimpleString queue = RandomUtil.randomSimpleString();
SimpleString address = RandomUtil.randomSimpleString();
// server must received at least one ping from the client to pass
// so that the server connection TTL is configured with the client value
final CountDownLatch pingOnServerLatch = new CountDownLatch(1);
server.getRemotingService().addIncomingInterceptor(new Interceptor() {
@Override
public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
if (packet.getType() == PacketImpl.PING) {
pingOnServerLatch.countDown();
}
return true;
}
});
ServerLocator locator = createInVMNonHALocator();
locator.setConnectionTTL(TemporaryQueueTest.CONNECTION_TTL);
sf = addSessionFactory(createSessionFactory(locator));
session = sf.createSession(false, true, true);
session.createTemporaryQueue(address, queue);
assertTrue("server has not received any ping from the client", pingOnServerLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
assertEquals(1, server.getConnectionCount());
RemotingConnection remotingConnection = server.getRemotingService().getConnections().iterator().next();
final CountDownLatch serverCloseLatch = new CountDownLatch(1);
remotingConnection.addCloseListener(new CloseListener() {
@Override
public void connectionClosed() {
serverCloseLatch.countDown();
}
});
((ClientSessionInternal) session).getConnection().fail(new ActiveMQInternalErrorException("simulate a client failure"));
// let some time for the server to clean the connections
assertTrue("server has not closed the connection", serverCloseLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval() + 2 * TemporaryQueueTest.CONNECTION_TTL, TimeUnit.MILLISECONDS));
// The next getCount will be asynchronously done at the end of failure. We will wait some time until it has reached there.
for (long timeout = System.currentTimeMillis() + 5000; timeout > System.currentTimeMillis() && server.getConnectionCount() > 0; ) {
Thread.sleep(1);
}
assertEquals(0, server.getConnectionCount());
session.close();
sf.close();
ServerLocator locator2 = createInVMNonHALocator();
sf = addSessionFactory(createSessionFactory(locator2));
session = sf.createSession(false, true, true);
session.start();
ActiveMQAction activeMQAction = new ActiveMQAction() {
@Override
public void run() throws ActiveMQException {
session.createConsumer(queue);
}
};
ActiveMQTestBase.expectActiveMQException("temp queue must not exist after the server detected the client crash", ActiveMQExceptionType.QUEUE_DOES_NOT_EXIST, activeMQAction);
session.close();
locator2.close();
}
use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.
the class PingStressTest method internalTest.
/*
* Test the client triggering failure due to no pong received in time
*/
private void internalTest() throws Exception {
Interceptor noPongInterceptor = new Interceptor() {
@Override
public boolean intercept(final Packet packet, final RemotingConnection conn) throws ActiveMQException {
PingStressTest.log.info("In interceptor, packet is " + packet.getType());
if (packet.getType() == PacketImpl.PING) {
PingStressTest.log.info("Ignoring Ping packet.. it will be dropped");
return false;
} else {
return true;
}
}
};
server.getRemotingService().addIncomingInterceptor(noPongInterceptor);
ServerLocator locator = createNettyNonHALocator().setClientFailureCheckPeriod(PingStressTest.PING_INTERVAL).setConnectionTTL((long) (PingStressTest.PING_INTERVAL * 1.5)).setCallTimeout(PingStressTest.PING_INTERVAL * 10);
final ClientSessionFactory csf1 = createSessionFactory(locator);
final int numberOfSessions = 1;
final int numberOfThreads = 30;
final CountDownLatch flagStart = new CountDownLatch(1);
final CountDownLatch flagAligned = new CountDownLatch(numberOfThreads);
class LocalThread extends Thread {
Throwable failure;
int threadNumber;
LocalThread(final int i) {
super("LocalThread i = " + i);
threadNumber = i;
}
@Override
public void run() {
try {
ServerLocator locator = createNettyNonHALocator().setClientFailureCheckPeriod(PingStressTest.PING_INTERVAL).setConnectionTTL((long) (PingStressTest.PING_INTERVAL * 1.5)).setCallTimeout(PingStressTest.PING_INTERVAL * 10);
final ClientSessionFactory csf2 = createSessionFactory(locator);
// Start all at once to make concurrency worst
flagAligned.countDown();
flagStart.await();
for (int i = 0; i < numberOfSessions; i++) {
System.out.println(getName() + " Session = " + i);
ClientSession session;
// them)
if (RandomUtil.randomBoolean()) {
session = csf1.createSession(false, false, false);
} else {
session = csf2.createSession(false, false, false);
}
// We will wait to anything between 0 to PING_INTERVAL * 2
Thread.sleep(PingStressTest.PING_INTERVAL * (threadNumber % 3));
session.close();
csf2.close();
locator.close();
}
} catch (Throwable e) {
e.printStackTrace();
failure = e;
}
}
}
LocalThread[] threads = new LocalThread[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new LocalThread(i);
threads[i].start();
}
assertTrue(flagAligned.await(10, TimeUnit.SECONDS));
flagStart.countDown();
Throwable e = null;
for (LocalThread t : threads) {
t.join();
if (t.failure != null) {
e = t.failure;
}
}
if (e != null) {
throw new Exception("Test Failed", e);
}
csf1.close();
locator.close();
}
use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.
the class SharedNothingLiveActivation method getActivationChannelHandler.
@Override
public ChannelHandler getActivationChannelHandler(final Channel channel, final Acceptor acceptorUsed) {
return new ChannelHandler() {
@Override
public void handlePacket(Packet packet) {
if (packet.getType() == PacketImpl.BACKUP_REGISTRATION) {
BackupRegistrationMessage msg = (BackupRegistrationMessage) packet;
ClusterConnection clusterConnection = acceptorUsed.getClusterConnection();
try {
startReplication(channel.getConnection(), clusterConnection, getPair(msg.getConnector(), true), msg.isFailBackRequest());
} catch (ActiveMQAlreadyReplicatingException are) {
channel.send(new BackupReplicationStartFailedMessage(BackupReplicationStartFailedMessage.BackupRegistrationProblem.ALREADY_REPLICATING));
} catch (ActiveMQException e) {
logger.debug("Failed to process backup registration packet", e);
channel.send(new BackupReplicationStartFailedMessage(BackupReplicationStartFailedMessage.BackupRegistrationProblem.EXCEPTION));
}
}
}
};
}
use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.
the class JMSFailoverTest method testSendReceiveLargeMessages.
@Test
public void testSendReceiveLargeMessages() throws Exception {
SimpleString QUEUE = new SimpleString("somequeue");
ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc, backuptc);
jbcf.setReconnectAttempts(-1);
jbcf.setBlockOnDurableSend(true);
jbcf.setBlockOnNonDurableSend(true);
jbcf.setMinLargeMessageSize(1024);
// jbcf.setConsumerWindowSize(0);
// jbcf.setMinLargeMessageSize(1024);
final CountDownLatch flagAlign = new CountDownLatch(1);
final CountDownLatch waitToKill = new CountDownLatch(1);
final AtomicBoolean killed = new AtomicBoolean(false);
jbcf.getServerLocator().addIncomingInterceptor(new Interceptor() {
int count = 0;
@Override
public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
if (packet instanceof SessionReceiveContinuationMessage) {
if (count++ == 300 && !killed.get()) {
System.out.println("sending countDown on latch waitToKill");
killed.set(true);
waitToKill.countDown();
}
}
return true;
}
});
Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
final ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
// The thread that will fail the server
Thread spoilerThread = new Thread() {
@Override
public void run() {
flagAlign.countDown();
// a large timeout just to help in case of debugging
try {
waitToKill.await(120, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Killing server...");
JMSUtil.crash(liveServer, coreSession);
} catch (Exception e) {
e.printStackTrace();
}
}
};
coreSession.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, true);
Queue queue = sess.createQueue("somequeue");
MessageProducer producer = sess.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 100; i++) {
TextMessage message = sess.createTextMessage(new String(new byte[10 * 1024]));
producer.send(message);
if (i % 10 == 0) {
sess.commit();
}
}
sess.commit();
conn.start();
spoilerThread.start();
assertTrue(flagAlign.await(10, TimeUnit.SECONDS));
MessageConsumer consumer = sess.createConsumer(queue);
// this test is not meant to validate transactionality during Failover as that would require XA and recovery
for (int i = 0; i < 90; i++) {
TextMessage message = null;
int retryNrs = 0;
do {
retryNrs++;
try {
message = (TextMessage) consumer.receive(5000);
assertNotNull(message);
break;
} catch (JMSException e) {
new Exception("Exception on receive message", e).printStackTrace();
}
} while (retryNrs < 10);
assertNotNull(message);
try {
sess.commit();
} catch (Exception e) {
new Exception("Exception during commit", e);
sess.rollback();
}
}
conn.close();
spoilerThread.join();
}
Aggregations