etcd icon indicating copy to clipboard operation
etcd copied to clipboard

addToSet command

Open emilisto opened this issue 13 years ago • 5 comments

I think a common use case for etcd would be to keep a set of values of a particular type, where the key you give denotes a set, rather than a key->value pair.

Example scenario: A memcached container spins up and registers its address to the key /backing_services/memcaced. Web server containers listen to this key, and is notified that another memcached server has been added.

I'd like this to work something like:

$ curl http://etcd:4001/v1/keys/backing_services/memcached -d setMember="memcached48.wonderland.com:49152"
    {
        "action": "ADDTOSET",
        "index": 1,
        "key": "/backing_services/memcached",
        "newKey": true,
        "value": "memcached48.wonderland.com:49152"
    }
$ curl http://etcd:4001/v1/keys/backing_services/memcached -d setMember="memcached48.wonderland.com:49152
    {
        "action": "ADDTOSET",
        "index": 1,
        "key": "/backing_services/memcached",
        "newKey": false,
        "value": "memcached48.wonderland.com:49152"
    }
$ curl http://etcd:4001/v1/keys/backing_services/memcached -d setMember="memcached49.wonderland.com:49731"
    {
        "action": "ADDTOSET",
        "index": 2,
        "key": "/backing_services/memcached",
        "newKey": true,
        "value": "memcached49.wonderland.com:49731"
    }

I'm not sure about all details, but first of all I'd like your input. Does this resonate with your vision for etcd? I know this could be accomplished by simply calculating a hash of the value to be stored, and then use this as the key name. But given the practicality of this use case, I think it'd be nice to have it integrated into etcd.

emilisto avatar Aug 03 '13 12:08 emilisto

Hi @emilisto You want to have a key-set pair? As we already have a tree-struct, you can create a node and have the set as its children. Do you mean that you do not want to set the real information in the key rather than the value? In your case, like key /backing_services/memcached/memcached48.wonderland.com:49152 ?

I am not sure why you want to hash it, since for the key we do allow :. But I would like to add function to help application to gain a global unique id in etcd.

It is not too difficult to add the functionality you want, but I may need to change the internal data structure a little bit . I will talk with more people to decide that. @philips @polvi @pquerna Thank you for you feedback!

xiang90 avatar Aug 03 '13 14:08 xiang90

In my humble opinion, etcd should not trade complexity for syntactic sugar, but stay as simple as possible. As @xiangli-cmu already pointed out, the described scenario can be completely achieved with the available operations.

# clients
curl -L http://etcd:4001/v1/watch/backing_services/memcached

# memcached
curl -L http://etcd:4001/v1/keys/backing_services/memcached/memcached48.wonderland.com:49152 -d value="1"
curl -L http://etcd:4001/v1/keys/backing_services/memcached/memcached49.wonderland.com:49731 -d value="1"

If you want to have easier access to the address, you could repeat it in the value -d value="memcached49.wonderland.com:49731". You also might want to set a ttl here, so that the backing serivces have to send the set command including the ttl periodically to indicate that they're still alive.

grobie avatar Aug 04 '13 06:08 grobie

I tend to agree, at lease while we are getting things started. Lets work together on a really great command line client rolling that supports things like this, then we can decide what to move into core. I started https://github.com/coreos/etcdctl as a place to start specing out a command line client.

We really appreciate the discussion. Keep them coming!

polvi avatar Aug 04 '13 17:08 polvi

@xiangli-cmu Hi! Right, my thought behind not wanting to keep the value in the key name is that other possible values would not be valid URL's. An example would be amqp://user:[email protected]

@grobie I absolutely agree with this. The no. of primitives should be kept to the minimum necessary; I started looking into the source and how the file and directory are such solid primitives in the store submodule — complicating them for something like this is not a good enough reason.

@polvi I think that's a great idea! This means use-cases can crystallise, and relevant primitives can take form, and be adopted by the core if they'e actual primitives.

emilisto avatar Aug 04 '13 19:08 emilisto

Closing this one, since we've merged it into etcdctl. If we eventually decide this is a feature for the core, we'll reopen it or create another issue.

emilisto avatar Aug 11 '13 07:08 emilisto