summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/nsr-server/src/etcd-api.h
blob: 66275d40dbca2fa5711ff9c4a667799b59f716e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright (c) 2013, Red Hat
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.  Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials
 * provided with the distribution.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Description of an etcd server.  For now it just includes the name and
 * port, but some day it might include other stuff like SSL certificate
 * information.
 */

typedef enum {
        ETCD_OK = 0,
        ETCD_PROTOCOL_ERROR,
                                /* TBD: add other error categories here */
        ETCD_WTF                /* anything we can't easily categorize */
} etcd_result;

typedef struct {
        char            *host;
        unsigned short  port;
} etcd_server;

typedef void *etcd_session;

/*
 * etcd_open
 *
 * Establish a session to an etcd cluster, with automatic reconnection and
 * so on.
 *
 *      server_list
 *      Array of etcd_server structures, with the last having host=NULL.  The
 *      caller is responsible for ensuring that this remains valid as long as
 *      the session exists.
 */
etcd_session    etcd_open       (etcd_server *server_list);


/*
 * etcd_open_str
 *
 * Same as etcd_open, except that the servers are specified as a list of
 * host:port strings, separated by comma/semicolon or whitespace.
 */
etcd_session    etcd_open_str   (char *server_names);


/*
 * etcd_close
 *
 * Terminate a session, closing connections and freeing memory (or any other
 * resources) associated with it.
 */
void            etcd_close      (etcd_session session);


/*
 * etcd_close
 *
 * Same as etcd_close, but also free the server list as etcd_open_str would
 * have allocated it.
 */
void            etcd_close_str  (etcd_session session);


/*
 * etcd_get
 *
 * Fetch a key from one of the servers in a session.  The return value is a
 * newly allocated string, which must be freed by the caller.
 *
 *      key
 *      The etcd key (path) to fetch.
 */
char *          etcd_get (etcd_session session, char *key);


/*
 * etcd_watch
 * Watch the set of keys matching a prefix.
 *
 *      pfx
 *      The etcd key prefix (like a path) to watch.
 *
 *      keyp
 *      Space for a pointer to the key that was added/modified/deleted.
 *
 *      valuep
 *      Space for a pointer to the value if a key was added/modified.  A delete
 *      is signified by this being set to NULL.
 *
 *      index_in
 *      Pointer to an index to be used for *issuing* the watch request, or
 *      NULL for a watch without an index.
 *
 *      index_out
 *      Pointer to space for an index *returned* by etcd, or NULL to mean don't
 *      bother.
 *
 * In normal usage, index_in will be NULL and index_out will be set to receive
 * the index for the first watch.  Subsequently, index_in will be set to
 * provide the previous index (plus one) and index_out will be set to receive
 * the next.  It's entirely legitimate to point both at the same variable.
 */

etcd_result     etcd_watch (etcd_session session, char *pfx,
                            char **keyp, char **valuep,
                            int *index_in, int *index_out);


/*
 * etcd_set
 *
 * Write a key, with optional TTL and/or previous value (as a precondition).
 *
 *      key
 *      The etcd key (path) to set.
 *
 *      value
 *      New value as a null-terminated string.  Unlike etcd_get, we can derive
 *      the length ourselves instead of needing it to be passed in separately.
 *
 *      precond
 *      Required previous value as a null-terminated string, or NULL to mean
 *      an unconditional set.
 *
 *      ttl
 *      Time in seconds after which the value will automatically expire and be
 *      deleted, or zero to mean no auto-expiration.
 */

etcd_result     etcd_set        (etcd_session session, char *key, char *value,
                                 char *precond, unsigned int ttl);


/*
 * etcd_delete
 *
 * Delete a key from one of the servers in a session.
 *
 *      key
 *      The etcd key (path) to delete.
 */

etcd_result     etcd_delete     (etcd_session session, char *key);


/*
 * etcd_leader
 *
 * Get the identify of the current leader.
 */

char *          etcd_leader     (etcd_session session);

/*
 * etcd_lock
 *
 * Take or renew a lock - really a lease but the etcd folks call it a lock so
 * we'll follow suit.
 *
 *      key
 *      The path (in the "locks" namespace) for the lock.
 *
 *      ttl
 *      Time in seconds for the lock.
 *
 *      index_in (optional, indicates renewal)
 *      Lock index from previous lock call.
 *
 *      index_out (only used for initial lock)
 *      Place for the new lock index.  You must free this.
 */

etcd_result     etcd_lock (etcd_session session_as_void, char *key,
                           unsigned int ttl, char *index_in, char **index_out);

/*
 * etcd_unlock
 *
 * Release a lock (see etcd_lock regarding terminology).
 *
 *      key
 *      The path (in the "locks" namespace) for the lock.
 *
 *      index
 *      Lock index from previous lock call.
 */

etcd_result     etcd_unlock (etcd_session session_as_void, char *key,
                             char *index);