我不認爲有一個簡單的方法來做到這一點。這裏是我會做到這一點:
1. divide the configurations in two znodes [say, /Config and /Config/data-*]
- /config/data-* are the data nodes which will store the configuration
- /config will store the history of all the configs so far
2. /Config/data-* is sequential node
So, every znode will have a strictly increasing number appended to it.
For example, /config/data-1, /config/data-2, and so on. Your configuration object
will be stored in data nodes.
/config/data-1 -> 12345
/config/data-2 -> 34567
/config/data-3 -> 56789
3. /config will look like this:
["/config/data-1","/config/data-2","/config/data-3"] or just
["1", "2", "3"]
這裏是寫算法:
1. create a data node with new config. Which will return the the actual path.
say /config/data-4
2. now try to do conditional update on /config with "/config/data-4" or just "4" appended to
the existing data i.e.
["/config/data-1","/config/data-2","/config/data-3","/config/data-4"] or just
["1", "2", "3","4"]
- if update succeeds, end.
- else, someone else was simultaneously trying to update the config and won the race.
So, either quit or try again starting with step1.
注意:如果更新失敗,你將不得不在樹中,一共可以在清潔有效的孤兒節點以自動化的方式定期
讀會:
1. call sync [if consistency is important]
2. read /config
3. pick the path which is at the last index in the array retrieved from /config
4. read the config from that path
要回滾,無論是德從列表中最後一個條目存儲在/ config或追加最後一個但列表的第二個條目到最後
您可以添加一個「監視」到znode並跟蹤歷史記錄嗎? – LenW