org.reborndb.reborn.TestRoundRobinJedisPool.java Source code

Java tutorial

Introduction

Here is the source code for org.reborndb.reborn.TestRoundRobinJedisPool.java

Source

/**
 * @(#)TestRoundRobinJedisPool.java, 2014-12-1.
 *
 * Copyright (c) 2015 Reborndb Org.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.reborndb.reborn;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.io.Closeables;

/**
 * @author Apache9
 */
public class TestRoundRobinJedisPool {

    private ObjectMapper mapper = new ObjectMapper();

    private int zkPort = 34125;

    private File testDir = new File(getClass().getName());

    private ZooKeeperServerWapper zkServer;

    private int redisPort1 = 24689;

    private RedisServer redis1;

    private int redisPort2 = 24789;

    private RedisServer redis2;

    private Jedis jedis1;

    private Jedis jedis2;

    private String zkPath = "/" + getClass().getName();

    private RoundRobinJedisPool jodisPool;

    private void deleteDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            return;
        }
        Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }

        });
    }

    public ZooKeeper connect(String host, int timeout) throws Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(1);

        try {
            ZooKeeper zk = new ZooKeeper(host, timeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    if (event.getState() == Event.KeeperState.SyncConnected) {
                        connectedLatch.countDown();
                    }
                }
            });

            if (ZooKeeper.States.CONNECTING == zk.getState()) {
                connectedLatch.await();
            }

            return zk;
        } catch (Exception e) {
            return null;
        }
    }

    private void addNode(String name, int port, String state) {
        ZooKeeper zk = null;

        try {
            zk = connect("localhost:" + zkPort, 5000);

            if (zk.exists(zkPath, null) == null) {
                zk.create(zkPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            ObjectNode node = mapper.createObjectNode();
            node.put("addr", "127.0.0.1:" + port);
            node.put("state", state);
            zk.create(zkPath + "/" + name, mapper.writer().writeValueAsBytes(node), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        } catch (Exception e) {
            // do nothing
        } finally {
            try {
                zk.close();
            } catch (InterruptedException e) {
                // do nothing
            }
        }
    }

    private void removeNode(String name) {
        ZooKeeper zk = null;

        try {
            zk = connect("localhost:" + zkPort, 5000);

            zk.delete(zkPath + "/" + name, -1);
        } catch (Exception e) {
            // do nothing
        } finally {
            try {
                zk.close();
            } catch (InterruptedException e) {
                // do nothing
            }
        }
    }

    @Before
    public void setUp() throws Exception {
        deleteDirectory(testDir);
        testDir.mkdirs();
        zkServer = new ZooKeeperServerWapper(zkPort, testDir);
        zkServer.start();
        redis1 = new RedisServer(redisPort1);
        redis1.start();
        redis2 = new RedisServer(redisPort2);
        redis2.start();

        Thread.sleep(2000);

        jedis1 = new Jedis("localhost", redisPort1);
        jedis2 = new Jedis("localhost", redisPort2);
        addNode("node1", redisPort1, "online");
        jodisPool = new RoundRobinJedisPool("localhost:" + zkPort, 5000, zkPath, new JedisPoolConfig());
    }

    @After
    public void tearDown() throws IOException {
        Closeables.close(jodisPool, true);
        Closeables.close(jedis1, true);
        Closeables.close(jedis2, true);
        if (redis1 != null) {
            redis1.stop();
        }
        if (redis2 != null) {
            redis2.stop();
        }
        if (zkServer != null) {
            zkServer.stop();
        }
        deleteDirectory(testDir);
    }

    @Test
    public void test() throws IOException, InterruptedException, KeeperException {
        try (Jedis jedis = jodisPool.getResource()) {
            jedis.set("k1", "v1");
        }
        assertEquals("v1", jedis1.get("k1"));

        // fake node
        addNode("node2", 12345, "offline");
        Thread.sleep(3000);
        try (Jedis jedis = jodisPool.getResource()) {
            jedis.set("k2", "v2");
        }
        assertEquals("v2", jedis1.get("k2"));

        addNode("node3", redisPort2, "online");
        Thread.sleep(3000);
        try (Jedis jedis = jodisPool.getResource()) {
            jedis.set("k3", "v3");
        }
        assertEquals("v3", jedis2.get("k3"));

        removeNode("node1");
        Thread.sleep(3000);
        try (Jedis jedis = jodisPool.getResource()) {
            jedis.set("k4", "v4");
        }
        assertEquals("v4", jedis2.get("k4"));
    }
}