Search in sources :

Example 1 with DistributedSequenceHandler

use of io.mycat.route.sequence.handler.DistributedSequenceHandler in project Mycat-Server by MyCATApache.

the class DistributedSequenceHandlerTest method testFailOver.

/**
     * 测试ZK容灾
     *
     * @throws Exception
     */
@Test
public void testFailOver() {
    Set<Long> idSet = new HashSet<>();
    try {
        int leader = failLeader(17);
        System.out.println("***断掉一个leader节点后(curator会抛对应的异常断链异常,不用在意)***:");
        for (int i = 0; i < 16; i++) {
            if (i == leader) {
                System.out.println("Node [" + i + "] used to be leader");
                continue;
            }
            distributedSequenceHandler[i].nextId("");
            System.out.println("Node [" + i + "]is leader:" + distributedSequenceHandler[i].getLeaderSelector().hasLeadership());
            System.out.println(" InstanceID:" + distributedSequenceHandler[i].getInstanceId());
            idSet.add(distributedSequenceHandler[i].getInstanceId());
        }
        Assert.assertEquals(idSet.size(), 15);
        idSet = new HashSet<>();
        int leader2 = failLeader(leader);
        System.out.println("***断掉两个leader节点后(curator会抛对应的异常断链异常,不用在意)***:");
        for (int i = 0; i < 16; i++) {
            if (i == leader || i == leader2) {
                System.out.println("Node [" + i + " used to be leader");
                continue;
            }
            distributedSequenceHandler[i].nextId("");
            System.out.println("Node [" + i + "]is leader:" + distributedSequenceHandler[i].getLeaderSelector().hasLeadership());
            System.out.println(" InstanceID:" + distributedSequenceHandler[i].getInstanceId());
            idSet.add(distributedSequenceHandler[i].getInstanceId());
        }
        Assert.assertEquals(idSet.size(), 14);
        idSet = new HashSet<>();
        MycatConfig mycatConfig = new MycatConfig();
        distributedSequenceHandler[leader] = new DistributedSequenceHandler(mycatConfig.getSystem());
        distributedSequenceHandler[leader].initializeZK(testingServer.getConnectString());
        distributedSequenceHandler[leader].nextId("");
        distributedSequenceHandler[leader2] = new DistributedSequenceHandler(mycatConfig.getSystem());
        distributedSequenceHandler[leader2].initializeZK(testingServer.getConnectString());
        distributedSequenceHandler[leader2].nextId("");
        System.out.println("新加入两个节点后");
        for (int i = 0; i < 16; i++) {
            System.out.println("Node [" + i + "]is leader:" + distributedSequenceHandler[i].getLeaderSelector().hasLeadership());
            System.out.println(" InstanceID:" + distributedSequenceHandler[i].getInstanceId());
            idSet.add(distributedSequenceHandler[i].getInstanceId());
        }
    } catch (Exception e) {
    } finally {
        Assert.assertEquals(idSet.size(), 16);
    }
}
Also used : DistributedSequenceHandler(io.mycat.route.sequence.handler.DistributedSequenceHandler) MycatConfig(io.mycat.config.MycatConfig) IOException(java.io.IOException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with DistributedSequenceHandler

use of io.mycat.route.sequence.handler.DistributedSequenceHandler in project Mycat-Server by MyCATApache.

the class DistributedSequenceHandlerTest method initialize.

@Before
public void initialize() throws Exception {
    distributedSequenceHandler = new DistributedSequenceHandler[16];
    MycatConfig mycatConfig = new MycatConfig();
    testingServer = new TestingServer();
    testingServer.start();
    for (int i = 0; i < 16; i++) {
        distributedSequenceHandler[i] = new DistributedSequenceHandler(mycatConfig.getSystem());
        distributedSequenceHandler[i].initializeZK(testingServer.getConnectString());
        distributedSequenceHandler[i].nextId("");
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) DistributedSequenceHandler(io.mycat.route.sequence.handler.DistributedSequenceHandler) MycatConfig(io.mycat.config.MycatConfig) Before(org.junit.Before)

Aggregations

MycatConfig (io.mycat.config.MycatConfig)2 DistributedSequenceHandler (io.mycat.route.sequence.handler.DistributedSequenceHandler)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 TestingServer (org.apache.curator.test.TestingServer)1 Before (org.junit.Before)1 Test (org.junit.Test)1