Search in sources :

Example 1 with GroupLeadershipSession

use of com.linkedin.databus.groupleader.pub.GroupLeadershipSession in project databus by linkedin.

the class LeaderElectSimpleExample method run.

protected void run() {
    String baseName = "/databus2.testGroupLeaderSimple";
    LOG.info("Before joining group: groupName=" + _groupName + "; memberName=" + _memberName + "; leadershipInfo=" + _groupLeadershipConn.getGroupLeadershipInfo(baseName, _groupName));
    GroupLeadershipSession groupLeadershipSession = _groupLeadershipConn.joinGroup(baseName, _groupName, _memberName, new AcceptLeadershipCallback() {

        @Override
        public void doAcceptLeadership(GroupLeadershipSession groupLeadershipSession) {
            LOG.info("Accepting leadership: " + groupLeadershipSession);
        }
    });
    LOG.info("After joining group: " + groupLeadershipSession);
    while (true) {
        try {
            // sleep is OK, this is a demo/example program
            Thread.sleep(3000);
            LOG.debug(groupLeadershipSession);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : AcceptLeadershipCallback(com.linkedin.databus.groupleader.pub.AcceptLeadershipCallback) GroupLeadershipSession(com.linkedin.databus.groupleader.pub.GroupLeadershipSession)

Example 2 with GroupLeadershipSession

use of com.linkedin.databus.groupleader.pub.GroupLeadershipSession in project databus by linkedin.

the class TestLeaderElect method testLeaderElectSimple.

/*
	 * IMPORTANT NOTE : If you add any new test case methods, make sure you wrap it with LockObject.
	 * This class is NOT thread-safe without it but TestNg expects the test class to be thread-safe.
	 */
@Test
public void testLeaderElectSimple() throws Exception {
    synchronized (LockObject) {
        try {
            setUp();
            String baseName = "/databus2.testGroupLeader";
            String groupName = "TestLeaderElect.testLeaderElectSimple";
            cleanZkGroupInfo(baseName, groupName);
            final AtomicReference<String> callerMaintainedCurrentLeaderRef = new AtomicReference<String>();
            AcceptLeadershipCallback saveCurrentLeaderCallback = new AcceptLeadershipCallback() {

                @Override
                public void doAcceptLeadership(GroupLeadershipSession groupLeadershipSession) {
                    callerMaintainedCurrentLeaderRef.set(groupLeadershipSession.getMemberName());
                }
            };
            GroupLeadershipConnection conn002 = _groupLeadershipConnFactory.getConnection();
            Assert.assertNull(conn002.getLeaderName(baseName, groupName), "Leader should be null");
            LOG.info("Group should not exist yet Test");
            assertFalse(conn002.getGroupNames(baseName).contains(groupName));
            GroupLeadershipSession sess002 = conn002.joinGroup(baseName, groupName, "002", saveCurrentLeaderCallback);
            assertEquals(groupName, sess002.getGroupName());
            assertEquals("002", sess002.getMemberName());
            assertEquals("002", (sess002.getLeaderName()));
            LOG.info("002 should be leader Test");
            assertFalse(!sess002.isLeader());
            assertEquals("002", callerMaintainedCurrentLeaderRef.get());
            LOG.info("Group should exist Test");
            assertFalse(!conn002.getGroupNames(baseName).contains(groupName));
            assertEquals(groupName, conn002.getGroupLeadershipInfo(baseName, groupName).getGroupName());
            assertEquals("002", (conn002.getGroupLeadershipInfo(baseName, groupName).getLeaderName()));
            assertEquals(1, conn002.getGroupLeadershipInfo(baseName, groupName).getMemberNames().size());
            System.err.printf("membership info=%s\n", conn002.getGroupLeadershipInfo(baseName, groupName).getMemberNames());
            LOG.info("Member should be in group Test");
            assertFalse(!conn002.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("002"));
            GroupLeadershipConnection conn001 = _groupLeadershipConnFactory.getConnection();
            GroupLeadershipSession sess001 = conn001.joinGroup(baseName, groupName, "001", saveCurrentLeaderCallback);
            assertEquals(groupName, sess001.getGroupName());
            assertEquals("001", sess001.getMemberName());
            assertEquals("002", (sess001.getLeaderName()));
            LOG.info("001 should not be leader");
            assertFalse(sess001.isLeader());
            assertEquals("002", callerMaintainedCurrentLeaderRef.get());
            LOG.info("Group should exist");
            assertFalse(!conn001.getGroupNames(baseName).contains(groupName));
            assertEquals(groupName, conn001.getGroupLeadershipInfo(baseName, groupName).getGroupName());
            assertEquals("002", (conn001.getGroupLeadershipInfo(baseName, groupName).getLeaderName()));
            assertEquals(2, conn001.getGroupLeadershipInfo(baseName, groupName).getMemberNames().size());
            LOG.info("Member should be in group");
            assertFalse(!conn001.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("001"));
            LOG.info("Member should be in group");
            assertFalse(!conn001.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("002"));
            GroupLeadershipConnection conn003 = _groupLeadershipConnFactory.getConnection();
            GroupLeadershipSession sess003 = conn003.joinGroup(baseName, groupName, "003", saveCurrentLeaderCallback);
            assertEquals(groupName, sess003.getGroupName());
            assertEquals("003", sess003.getMemberName());
            assertEquals("002", (sess003.getLeaderName()));
            LOG.info("003 should not be leader");
            assertFalse(sess003.isLeader());
            assertEquals("002", callerMaintainedCurrentLeaderRef.get());
            LOG.info("Group should exist");
            assertTrue(conn003.getGroupNames(baseName).contains(groupName));
            assertEquals(groupName, conn003.getGroupLeadershipInfo(baseName, groupName).getGroupName());
            assertEquals("002", (conn003.getGroupLeadershipInfo(baseName, groupName).getLeaderName()));
            assertEquals(3, conn003.getGroupLeadershipInfo(baseName, groupName).getMemberNames().size());
            LOG.info("Member should be in group");
            assertTrue(conn003.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("001"));
            assertTrue(conn003.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("002"));
            assertTrue(conn003.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("003"));
            sess002.leaveGroup();
            for (int i = 0; i < 60; i++) {
                if ("001".equals((_adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getLeaderName()))) {
                    break;
                }
                Thread.sleep(500);
            }
            assertEquals(groupName, sess001.getGroupName());
            assertEquals("001", sess001.getMemberName());
            assertEquals("001", (sess001.getLeaderName()));
            LOG.info("001 should be leader");
            assertTrue(sess001.isLeader());
            assertEquals("001", callerMaintainedCurrentLeaderRef.get());
            LOG.info("Group should exist");
            assertTrue(_adminLeadershipConn.getGroupNames(baseName).contains(groupName));
            assertEquals(groupName, _adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getGroupName());
            assertEquals("001", (_adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getLeaderName()));
            assertEquals(2, _adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getMemberNames().size());
            LOG.info("Member should be in group");
            assertTrue(_adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("001"));
            assertTrue(_adminLeadershipConn.getGroupLeadershipInfo(baseName, groupName).getMemberNames().contains("003"));
            assertEquals(groupName, sess003.getGroupName());
            assertEquals("003", sess003.getMemberName());
            assertEquals("001", sess003.getLeaderName());
            LOG.info("003 should not be leader");
            assertFalse(sess003.isLeader());
            try {
                sess002.leaveGroup();
                fail("Should have gotten IllegalStateException");
            } catch (IllegalStateException e) {
            // expected
            }
            sess003.leaveGroup();
            sess001.leaveGroup();
            conn003.close();
            conn002.close();
            conn001.close();
        } finally {
            tearDown();
        }
    }
}
Also used : GroupLeadershipConnection(com.linkedin.databus.groupleader.pub.GroupLeadershipConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) AcceptLeadershipCallback(com.linkedin.databus.groupleader.pub.AcceptLeadershipCallback) GroupLeadershipSession(com.linkedin.databus.groupleader.pub.GroupLeadershipSession) Test(org.testng.annotations.Test)

Aggregations

AcceptLeadershipCallback (com.linkedin.databus.groupleader.pub.AcceptLeadershipCallback)2 GroupLeadershipSession (com.linkedin.databus.groupleader.pub.GroupLeadershipSession)2 GroupLeadershipConnection (com.linkedin.databus.groupleader.pub.GroupLeadershipConnection)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.testng.annotations.Test)1