Hello Ted,
Your approach appears to be the fastest, so I think I will go with it.  By
the way, it should be buf.rewind() not buf.reset().

Satish

On Thu, Apr 23, 2009 at 6:28 PM, Ted Dunning <ted.dunn...@gmail.com> wrote:

> I don't think you meant ephemeral nodes because it isn't very likely that
> you would have more than a billion sessions attached to a single zookeeper
> cluster.  If you simply want to have a guaranteed unique value among all
> live owners of these id's, then ephemeral sequential nodes are fine and
> integers are also probably fine.
>
> If you want longer-lived uniqueness, then you need something stronger.  If
> your required rate of generating these values is relatively low, then you
> can keep the current maximum (long) value of an id in a file on Zookeeper.
> When you need to generate a new id, do this:
>
>    public long nextId(String state) throws InterruptedException,
> KeeperException {
>        Stat s = new Stat();
>        boolean committed = false;
>        long id = 0;
>        while (!committed) {
>            ByteBuffer buf = ByteBuffer.wrap(zk.getData(state, false, s));
>            id = buf.getLong();
>            id++;
>            buf.reset();
>            buf.putLong(id);
>            try {
>                zk.setData(state, buf.array(), s.getVersion());
>                committed = true;
>            } catch (KeeperException.BadVersionException e) {
>                committed = false;
>            } catch (InterruptedException e) {
>                // at this point, we don't know that our update happened.
> Since it is
>                // not a problem to redo this and because this situation
> should be extremely
>                // rare, we will just pretend the update failed.
>                committed = false;
>            }
>        }
>        return id;
>    }
>
> This gives you long id's that will not repeat.  It will be somewhat limited
> in the number of id's you can create per second, especially if you have
> thousands of nodes all asking for id's.  You could increase that rate by
> randomly selecting one of *n* files each of which corresponds to an
> different disjoint region in the id space.  Even so, with a good zookeeper
> cluster, you should be able to generate 10,000 id's per second or more.
>
> Another way to substantially increase the id generation rate would be to
> allocate 1000 id's per call to zookeeper.  You give up on consecutive id's
> with that approach, but you should be able to generate millions of
> guaranteed unique id's per second and these id's should be pretty dense if
> each process generates lots of id's.
>
> On Thu, Apr 23, 2009 at 4:52 PM, Satish Bhatti <cthd2...@gmail.com> wrote:
>
> > We currently use a database sequence to generate unique ids for use by
> our
> > application.  I was thinking about using ZooKeeper instead so I can get
> rid
> > of the database.  My plan was to use the sequential id from ephemeral
> > nodes,
> > but looking at the code it appears that this is an int, not a long.  Is
> > there any other straightforward way to generate ids using ZooKeeper?
> > Thanks,
> >
> > Satish
> >
>

Reply via email to