This response contains five fields. We will introduce three more fields as we try more commands.
1. The action of the request; we set the value via a post request, thus the action is “SET”
2. The key of the request; we set “/message” to “Hello world!”, so the key field is “/message”.
Notice we use a file system like structure to represent the key-value pairs. So each key starts with “/”.
3. The current value of the key; we set the value to “Hello world!”.
4. If we set a new key; “/message” did not exist before, so this is a new key.
5. Index field is the unique request index of the set request. Each sensitive request we send to the server will have a unique request index. The current sensitive request are “SET”, “DELETE” and “TESTANDSET”. All of these request will change the state of the key-value store system, thus they are sensitive. “GET”, “LIST” and “WATCH” are non-sensitive commands. Those commands will not change the state of the key-value store system.
You may notice that in this example the index is 3, although it is the first request you sent to the server. This is because there are some internal commands that also change the state of the server, we also need to assign them command indexes(Command used to add a server and sync the servers).
OK. Watch command can do more than this. We have index and in etcd we store the most recent 1000 responses by default, which allow us to watch for previous commands.
Let us try to watch for the set command of index 6 again.
You should see the watch command return immediately with the same response as previous.
#### Trying TestAndSet
Etcd servers will process all the command in sequence atomically, thus it can be used as a centralized decision making cluster.
TestAndSet is the most basic operation to build distributed lock service and more interesting stuff.
What it does is to test whether the given previous value is equal to the value of the key, if equal etcd will change the value of the key to the given value.
which meas foo is a key under /foo and the value is “barbar” and a key foo_dir is a directory.
### Setting up a cluster of three machines
Next we can explore the power of etcd cluster. We use go-raft as the underlay distributed protocol which provide consistency and persistence of all the machines in the cluster. The will allow if the minor machine dies, the cluster will still be able to performance correctly. Also if most of the machines dead and restart, we will recover from the previous state of the cluster.
Let us create 3 new machines.
The first one will be
We use -s to specify server port and -c to specify client port and -d to specify the directory to store the log and info of the node in the cluster