Hi All , I'm evaluating Apache Zookeepers to use in my project where i have a requirement to generate unique and sequential message ids(long) across a cluster. After some web search i found[1] and some other different suggestions. I started with the solution suggested in [1] but even with single client i only got a throughput around 20-30 ids/sec. But according to the performance numbers pointed in the website i think i should be able get better results ( i m expecting to get at least 1000 id/s as i m using this for a MoM where for each message i'll be calling zk id generator.)
In this case I m using ZK version 3.3.4 running in my local machine with default settings and client also running in the same machine. I m attaching my id generator class. Any tips to improve the performance of this is highly appreciated. Am i doing some thing wrong ? [1] http://zookeeper-user.578899.n2.nabble.com/Unique-Id-Generation-td2688494.html cheers, Charith -- Charith Dhanushka Wickramarachchi http://charithwiki.blogspot.com/
/* * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.zookeeper.sample.id; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.wso2.zookeeper.sample.leader.coordination.CoordinationConstants; import org.wso2.zookeeper.sample.leader.coordination.CoordinationException; import org.wso2.zookeeper.sample.leader.coordination.ZooKeeperAgent; import java.nio.ByteBuffer; public class ZookeeperBasedIdGenerator implements IdGenerator { public ZookeeperBasedIdGenerator(ZooKeeperAgent zkAgent) throws CoordinationException { zkAgent.initIdGeneratorCoordination(); zk = zkAgent.getZooKeeper(); } private ZooKeeper zk = null; @Override public long getNextId() throws InterruptedException, KeeperException { Stat s = new Stat(); boolean committed = false; long id = 0; while (!committed) { byte[] data = zk.getData(CoordinationConstants.ID_GEN_DATA_NODE, false, s); ByteBuffer buf = null; if(data.length == 0) { id++; buf = ByteBuffer.allocate(8); buf.putLong(id); buf.flip(); } else { buf = ByteBuffer.wrap(data); id = buf.getLong(); id++; buf.clear(); buf.putLong(id); buf.flip(); } try { zk.setData(CoordinationConstants.ID_GEN_DATA_NODE, buf.array(), s.getVersion()); committed = true; } catch (KeeperException.BadVersionException e) { committed = false; } catch (InterruptedException e) { committed = false; } } return id; } }
