jplana / python-etcd

A python client for etcd
Other
522 stars 210 forks source link

Refresh TTL will flush the value of the key #224

Closed tobegit3hub closed 7 years ago

tobegit3hub commented 7 years ago

Now we use etcd and python-etcd for leader election.

We want to write the ip or hostname into master key to become the master. Once it's successfully, it will refresh the key periodically. But if I call refresh with TTL, it flushes the value of the master key and I don't know who's the master now.

The way to re-produce the result. We can get the value after step 4 but no value after step 5.

In [1]: import etcd

In [2]: etcd_client = etcd.Client(host="127.0.0.1", port=2379)

In [3]: etcd_master_key = "/foo/master"

In [4]: result = etcd_client.write(etcd_master_key, "bar", ttl=10, prevExist=False)

In [5]: etcd_client.refresh(etcd_master_key, ttl=10)

Out[5]: <class 'etcd.EtcdResult'>({'newKey': False, '_prev_node': <class 'etcd.EtcdResult'>({'newKey': False, '_children': [], 'createdIndex': 186, 'modifiedIndex': 186, 'value': u'tomi', 'expiration': u'2017-01-13T07:35:38.746033249Z', 'key': u'/cloud-ml/run_queue_consumer/master', 'ttl': 1, 'action': None, 'dir': False}), 'raft_index': 33521683, '_children': [], 'createdIndex': 186, 'modifiedIndex': 187, 'value': u'', 'etcd_index': 187, 'expiration': u'2017-01-13T07:35:48.470047006Z', 'key': u'/cloud-ml/run_queue_consumer/master', 'ttl': 10, 'action': u'update', 'dir': False})
tobegit3hub commented 7 years ago

I have also try etcd_client.write(etcd_master_key, socket.gethostname(), ttl=10, prevExist=False, refresh=True) but get same result.

tobegit3hub commented 7 years ago

Seems that it's related to https://github.com/coreos/etcd/issues/5651 and have fixed in etcd 3.0.

Not the bug of python-etcd and upgrading etcd works like a charm.