use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection 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.spi.core.protocol.RemotingConnection 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.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class BridgeReconnectTest method testReconnectSameNode.
// Fail bridge and reconnect same node, no backup specified
@Test
public void testReconnectSameNode() throws Exception {
server0 = createActiveMQServer(0, isNetty(), server0Params);
TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
server0.getConfiguration().setConnectorConfigurations(connectors);
server1.getConfiguration().setConnectorConfigurations(connectors);
BridgeConfiguration bridgeConfiguration = createBridgeConfig();
List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
bridgeConfigs.add(bridgeConfiguration);
server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
queueConfigs0.add(queueConfig0);
server0.getConfiguration().setQueueConfigurations(queueConfigs0);
CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
queueConfigs1.add(queueConfig1);
server1.getConfiguration().setQueueConfigurations(queueConfigs1);
startServers();
locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
session0 = csf0.createSession(false, true, true);
ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
session1 = csf1.createSession(false, true, true);
ClientProducer prod0 = session0.createProducer(testAddress);
ClientConsumer cons1 = session1.createConsumer(queueName);
session1.start();
// Now we will simulate a failure of the bridge connection between server0 and server1
Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
assertNotNull(bridge);
RemotingConnection forwardingConnection = getForwardingConnection(bridge);
InVMConnector.failOnCreateConnection = true;
InVMConnector.numberOfFailures = reconnectAttempts - 1;
forwardingConnection.fail(new ActiveMQNotConnectedException());
forwardingConnection = getForwardingConnection(bridge);
forwardingConnection.fail(new ActiveMQNotConnectedException());
final ManagementService managementService = server0.getManagementService();
QueueControl coreQueueControl = (QueueControl) managementService.getResource(ResourceNames.QUEUE + queueName);
assertEquals(0, coreQueueControl.getDeliveringCount());
final int numMessages = NUM_MESSAGES;
SimpleString propKey = new SimpleString("propkey");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session0.createMessage(false);
message.putIntProperty(propKey, i);
prod0.send(message);
}
for (int i = 0; i < numMessages; i++) {
ClientMessage r1 = cons1.receive(1500);
assertNotNull(r1);
assertEquals(i, r1.getObjectProperty(propKey));
}
closeServers();
assertNoMoreConnections();
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class BridgeReconnectTest method testFailoverThenFailAgainAndReconnect.
@Test
public void testFailoverThenFailAgainAndReconnect() throws Exception {
server0 = createActiveMQServer(0, isNetty(), server0Params);
TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
server0.getConfiguration().setConnectorConfigurations(connectors);
BridgeConfiguration bridgeConfiguration = createBridgeConfig();
List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
bridgeConfigs.add(bridgeConfiguration);
server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
queueConfigs0.add(queueConfig0);
server0.getConfiguration().setQueueConfigurations(queueConfigs0);
CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
queueConfigs1.add(queueConfig1);
server1.getConfiguration().setQueueConfigurations(queueConfigs1);
startServers();
locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
session0 = csf0.createSession(false, true, true);
ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
session1 = csf1.createSession(false, true, true);
ClientProducer prod0 = session0.createProducer(testAddress);
ClientConsumer cons1 = session1.createConsumer(queueName);
session1.start();
Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
RemotingConnection forwardingConnection = getForwardingConnection(bridge);
InVMConnector.failOnCreateConnection = true;
InVMConnector.numberOfFailures = reconnectAttempts - 1;
forwardingConnection.fail(new ActiveMQNotConnectedException());
final int numMessages = NUM_MESSAGES;
SimpleString propKey = new SimpleString("propkey");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session0.createMessage(false);
message.putIntProperty(propKey, i);
prod0.send(message);
}
int outOfOrder = -1;
int supposed = -1;
for (int i = 0; i < numMessages; i++) {
ClientMessage r1 = cons1.receive(1500);
assertNotNull(r1);
if (outOfOrder == -1 && i != r1.getIntProperty(propKey).intValue()) {
outOfOrder = r1.getIntProperty(propKey).intValue();
supposed = i;
}
}
if (outOfOrder != -1) {
fail("Message " + outOfOrder + " was received out of order, it was supposed to be " + supposed);
}
log.info("=========== second failure, sending message");
// Fail again - should reconnect
forwardingConnection = ((BridgeImpl) bridge).getForwardingConnection();
InVMConnector.failOnCreateConnection = true;
InVMConnector.numberOfFailures = reconnectAttempts - 1;
forwardingConnection.fail(new ActiveMQException(ActiveMQExceptionType.UNBLOCKED));
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session0.createMessage(false);
message.putIntProperty(propKey, i);
prod0.send(message);
}
for (int i = 0; i < numMessages; i++) {
ClientMessage r1 = cons1.receive(1500);
assertNotNull("Didn't receive message", r1);
if (outOfOrder == -1 && i != r1.getIntProperty(propKey).intValue()) {
outOfOrder = r1.getIntProperty(propKey).intValue();
supposed = i;
}
}
if (outOfOrder != -1) {
fail("Message " + outOfOrder + " was received out of order, it was supposed to be " + supposed);
}
closeServers();
assertNoMoreConnections();
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection 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();
}
Aggregations