FYI, etcd provides a transaction mechanism which supports if-else like commits.[1]
Best, tison. [1] https://godoc.org/github.com/coreos/etcd/clientv3#OpTxn Zili Chen <[email protected]> 于2019年8月14日周三 下午11:52写道: > Hi Andor, > > Thanks for your attention. > > The problem is that in concurrent scenario zk.setData() could still failed > if there is another thread delete the node. I know with proper lock > strategy > and ownership separation this can be avoid but it's said that ZooKeeper has > a transaction mechanism so I'd like to see whether I can make use of it. > > There is where I turn to > > zk.multi(Op.check(path1), Op.setData(path2, data)); // path1 == or != > path2 is irrelevant > > when the existence of a mark node(path1) guarded a condition and I want to > make > sure that setData successes only if the mark node exist. If I check the > existence > first and commit setData, a remove to the node could break the guard. In > other > words, I want to tell ZooKeeper that the check and setData should be > successful > committed or fail to be committed atomically. > > Best, > tison. > > > Andor Molnar <[email protected]> 于2019年8月14日周三 下午11:12写道: > >> Hi Zili, >> >> There’s no such functionality in ZooKeeper as far as I’m concerned. I >> think your multi example (zk.multi(Op.check(path), Op.setData(path, data))) >> is already a usage pattern which multi is not designed to support. >> >> Why do you need to do this in “transactions” (there’s no transaction in >> ZK)? >> >> In Java you can do: >> >> try { >> zk.create(); >> } catch (NodeExistsException e) { >> // swallow exception >> } >> zk.setData(); >> … >> >> Regards, >> Andor >> >> >> >> >> > On 2019. Aug 6., at 14:44, Zili Chen <[email protected]> wrote: >> > >> > Hi Enrico, >> > >> > Thanks for your reply. >> > >> >> In this case usually you use conditional setData, using the 'version' >> of >> >> thr znode >> > >> > >> > what if the caller has no idea on whether the node exist?(see also >> > my if-else pseudo-code above.) >> > >> > IIRC if we call `setData` on a non-exist path a NoNodeException >> > will be thrown. >> > Best, >> > tison. >> > >> > >> > Enrico Olivelli <[email protected]> 于2019年8月6日周二 下午8:27写道: >> > >> >> Il mar 6 ago 2019, 13:47 Zili Chen <[email protected]> ha scritto: >> >> >> >>> Any ideas? >> >>> >> >>> >> >>> Zili Chen <[email protected]> 于2019年7月29日周一 上午11:12写道: >> >>> >> >>>> Hi ZooKeepers, >> >>>> >> >>>> Currently our transaction mechanism supports doing >> >>>> create/setData/checkExist/delete in transaction. However, taking this >> >>>> scenario into consideration, we want to put data in path "/path" but >> >>>> don't know whether the znode exists or not. Let's say we program as >> >>>> below >> >>>> >> >>>> if (zk.exist(path)) { >> >>>> zk.setData(path, data); >> >>>> } else { >> >>>> zk.create(path, data); >> >>>> } >> >>> >> >> >> >> Do you need to perform other ops in the same transaction? >> >> In this case usually you use conditional setData, using the 'version' >> of >> >> thr znode >> >> >> >> >> >> Enrico >> >> >> >>> >> >>>> if we want to do the check and "put" in transaction, it would be like >> >>>> >> >>>> zk.multi(Op.check(path), Op.setData(path, data)); >> >>>> >> >>>> but we cannot add a "else" branch. ZooKeeper's transaction would all >> >>>> success or fail. >> >>>> >> >>>> Is there any way to do an "if-else" transaction? >> >>>> >> >>>> Best, >> >>>> tison. >> >>>> >> >>> >> >> >> >>
