ZooKeeper-6.6 数据字段和子节点的限制- 高飞网

6.6 数据字段和子节点的限制

2018-06-04 17:07:02.0

    ZooKeeper默认情况下对数据字段的传输限制为1MB,该限制为任何节点数据字段的最大可存储字节数。也限制了任何父节点可以拥有的子节点数(经测试几乎没有限制)。


public class DataTest {
    final static byte[] SIZE_1MB = new byte[1024 * 1024];
    final static byte[] SIZE_1MB_LESS = new byte[1024 * 1023];
    final static byte[] SIZE_2MB = new byte[2 * 1024 * 1024];
    final static ZKClient zkClient = ZKClient.instance();


    public static void main(String[] args) throws KeeperException, InterruptedException {
//        testMaxSizeData();
        testMaxChildren();
    }

    public static void testMaxSizeData() throws KeeperException, InterruptedException {
        if (!zkClient.exists("/test", null)) {
            zkClient.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (!zkClient.exists("/test/testmaxdata", null)) {
            zkClient.create("/test/testmaxdata", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        }
        System.out.println("设置数据...");
        //测试发现,向一个节点传输大于1M数据时,会直接报CONNECTIONLOSS异常
        zkClient.setData("/test/testmaxdata", SIZE_1MB, -1, new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                System.out.println(KeeperException.Code.get(rc));
            }
        }, null);

        Thread.sleep(100000);
    }

    public static void testMaxChildren() throws KeeperException, InterruptedException {
        if (!zkClient.exists("/test", null)) {
            zkClient.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (!zkClient.exists("/test/testmaxchildren", null)) {
            zkClient.create("/test/testmaxchildren", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        }
        System.out.println("添加子节点..");
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            zkClient.create("/test/testmaxchildren/x_" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            System.out.println("x_" + i);
        }

        Thread.sleep(100000);
    }
}



当创建了50多万子节点时,程序仍然正常运行

[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 10] get /test/testmaxchildren

cZxid = 0x40000019c
ctime = Mon Jun 04 18:30:40 CST 2018
mZxid = 0x40000019c
mtime = Mon Jun 04 18:30:40 CST 2018
pZxid = 0x40004d6a2
cversion = 316678
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 526664