use of org.jgroups.JChannel in project JGroups by belaban.
the class ClusterSplitLockTest method testClusterSplitImpl.
/**
* Performs a test where the specified downMember goes down when the member
* performed a lock operation on 50% of the locks. The coordinator should
* unlock all locks that where locked by the member that goes down. If the
* member that goes down is the coordinator the new channel coordinator
* should make sure the lock table is up-to-date.
*
* @param downMember
* @throws Exception
*/
private void testClusterSplitImpl(final int downMember) throws Exception {
CountDownLatch doneOnMemberThatWillGoDown = new CountDownLatch(1);
final int numLocks = 10;
final int halfway = numLocks / 2;
List<Future<?>> futures = new ArrayList<>();
/*
* All members perform the specified number of lock() operations. The
* 'downMember' disconnects half way
*/
for (int i = 0; i < MEMBERS; i++) {
final int mbrIdx = i;
final AtomicInteger unlockCount = new AtomicInteger(0);
for (int j = 0; j < numLocks; j++) {
final int lockNr = j;
if (mbrIdx == downMember) {
if (lockNr == halfway) {
futures.add(execs[downMember].submit(() -> {
try {
doneOnMemberThatWillGoDown.await();
log("Disconnecting member %s", memberName(downMember));
disconnectAndDestroy(downMember);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
if (lockNr >= halfway) {
break;
}
}
futures.add(execs[mbrIdx].submit(() -> {
Lock lock = lockServices[mbrIdx].getLock("testlock" + lockNr);
try {
if (!lock.tryLock(5, SECONDS)) {
if (mbrIdx == downMember) {
fail(String.format("Member %s failed to lock %s using tryLock in healthy situation.", memberName(mbrIdx), lockNr));
} else {
log("Failed to tryLock member:%s lock:%d LOCKS:\n%s", memberName(mbrIdx), lockNr, lockServices[mbrIdx].printLocks());
return;
}
} else {
log("Member %s locked %d (threadid: %d)", memberName(mbrIdx), lockNr, Thread.currentThread().getId());
}
} catch (InterruptedException ie) {
log("InterruptedException member:%s, lock:%d", memberName(mbrIdx), lockNr, ie);
fail("Interrupted on tryLock " + memberName(mbrIdx) + " - " + lockNr);
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
fail("Interrupted while sleeping.");
} finally {
lock.unlock();
log("Unlocked lock %d by member %s (threadid: %d)", lockNr, memberName(mbrIdx), Thread.currentThread().getId());
if (mbrIdx == downMember && halfway == unlockCount.incrementAndGet()) {
log("setting doneOnMemberThatWillGoDown flag");
doneOnMemberThatWillGoDown.countDown();
}
}
}));
}
}
/* wait for the chaos to disappear */
for (Future<?> fut : futures) {
fut.get();
}
StringBuilder locksOverview = new StringBuilder("\n==== first run done ====\n");
for (int i = 0; i < MEMBERS; i++) {
locksOverview.append(String.format("Locks on member %s:\n%s\n", memberName(i), lockServices[i].printLocks()));
}
locksOverview.append("\n========================");
log(locksOverview.toString());
/*
* All locks should be unlocked at this point so no try lock request
* should fail
*/
Thread.sleep(2000);
log("==== Checking if tryLock succeeds for all locks on all remaining members =====");
for (int i = 0; i < MEMBERS; i++) {
if (i == downMember) {
continue;
}
for (int j = 0; j < numLocks; j++) {
Lock l = lockServices[i].getLock("testlock" + j);
if (!l.tryLock()) {
logError("Failed to acquire lock on %d by member %s", j, memberName(i));
Address coord = channels[i].getView().getCoord();
int count = 0;
for (JChannel c : channels) {
if (null != c.getAddress() && c.getAddress().equals(coord)) {
logError("Lock table for %s (coord):\n%s", coord, lockServices[count].printLocks());
break;
}
count++;
}
fail(String.format("Member %s can't lock:%d", memberName(i), j));
}
l.unlock();
}
}
}
use of org.jgroups.JChannel in project activemq-artemis by apache.
the class JGroupsBroadcastTest method testRefCount.
@Test
public void testRefCount() throws Exception {
JChannel channel = null;
JChannel newChannel = null;
try {
PlainConfigurator configurator = new PlainConfigurator(jgroupsConfigString);
channel = new JChannel(configurator);
String channelName1 = "channel1";
BroadcastEndpointFactory jgroupsBroadcastCfg1 = new ChannelBroadcastEndpointFactory(channel, channelName1);
BroadcastEndpoint channelEndpoint1 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
BroadcastEndpoint channelEndpoint2 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
BroadcastEndpoint channelEndpoint3 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
channelEndpoint1.close(true);
Assert.assertTrue(channel.isOpen());
channelEndpoint2.close(true);
Assert.assertTrue(channel.isOpen());
channelEndpoint3.close(true);
Assert.assertTrue(channel.isOpen());
channel.close();
// channel wrapper is recreated
try {
channelEndpoint2.openClient();
Assert.fail("this should be closed");
} catch (Exception e) {
}
newChannel = new JChannel(configurator);
jgroupsBroadcastCfg1 = new ChannelBroadcastEndpointFactory(newChannel, channelName1);
channelEndpoint1 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
channelEndpoint1.openClient();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
channel.close();
} catch (Throwable ignored) {
}
try {
newChannel.close();
} catch (Throwable ignored) {
}
}
}
use of org.jgroups.JChannel in project activemq-artemis by apache.
the class ConnectionFactoryWithJGroupsSerializationTest method setUp.
@Override
@Before
public void setUp() throws Exception {
try {
super.setUp();
PlainConfigurator configurator = new PlainConfigurator(jgroupsConfigString);
channel = new JChannel(configurator);
String channelName1 = "channel1";
String channelName2 = "channel2";
BroadcastEndpointFactory jgroupsBroadcastCfg1 = new ChannelBroadcastEndpointFactory(channel, channelName1);
BroadcastEndpointFactory jgroupsBroadcastCfg2 = new JGroupsFileBroadcastEndpointFactory().setChannelName(channelName2).setFile(jgroupsConfigString);
DiscoveryGroupConfiguration dcConfig1 = new DiscoveryGroupConfiguration().setName("dg1").setRefreshTimeout(5000).setDiscoveryInitialWaitTimeout(5000).setBroadcastEndpointFactory(jgroupsBroadcastCfg1);
DiscoveryGroupConfiguration dcConfig2 = new DiscoveryGroupConfiguration().setName("dg2").setRefreshTimeout(5000).setDiscoveryInitialWaitTimeout(5000).setBroadcastEndpointFactory(jgroupsBroadcastCfg2);
jmsServer.getActiveMQServer().getConfiguration().getDiscoveryGroupConfigurations().put(dcConfig1.getName(), dcConfig1);
jmsServer.getActiveMQServer().getConfiguration().getDiscoveryGroupConfigurations().put(dcConfig2.getName(), dcConfig2);
jmsServer.createConnectionFactory("ConnectionFactory1", false, JMSFactoryType.CF, dcConfig1.getName(), "/ConnectionFactory1");
jmsServer.createConnectionFactory("ConnectionFactory2", false, JMSFactoryType.CF, dcConfig2.getName(), "/ConnectionFactory2");
testQueue = createQueue("testQueueFor1389");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
use of org.jgroups.JChannel in project xwiki-platform by xwiki.
the class JGroupsNetworkAdapter method send.
@Override
public void send(RemoteEventData remoteEvent) {
this.logger.debug("Send JGroups remote event [" + remoteEvent + "]");
// Send the message to the whole group
Message message = new Message(null, remoteEvent);
// Send message to JGroups channels
for (Map.Entry<String, JChannel> entry : this.channels.entrySet()) {
try {
entry.getValue().send(message);
} catch (Exception e) {
this.logger.error("Failed to send message [" + remoteEvent + "] to the channel [" + entry.getKey() + "]", e);
}
}
}
use of org.jgroups.JChannel in project xwiki-platform by xwiki.
the class JGroupsNetworkAdapter method createChannel.
/**
* Create a new channel.
*
* @param channelId the identifier of the channel to create
* @return the new channel
* @throws Exception failed to create new channel
*/
private JChannel createChannel(String channelId) throws Exception {
// load configuration
ProtocolStackConfigurator channelConf = loadChannelConfiguration(channelId);
// get Receiver
JGroupsReceiver channelReceiver;
try {
channelReceiver = this.componentManager.getInstance(JGroupsReceiver.class, channelId);
} catch (ComponentLookupException e) {
channelReceiver = this.componentManager.getInstance(JGroupsReceiver.class);
}
// create channel
JChannel channel = new JChannel(channelConf);
channel.setReceiver(channelReceiver);
channel.setDiscardOwnMessages(true);
return channel;
}
Aggregations