summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/nsr-server/src/etcd-sim.c
blob: d0bea12c7ac23d27921c1eb027189b704080e6e5 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2014, 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>

#include "mem-pool.h"

/*
 * Mock implementation of etcd
 * The etcd file is simulated in /tmp/<server-names>
 * Writes from Multiple writers are protected using file lock.
*/

#include "etcd-api.h"
#define MAX_KEY_LEN 64
#define MAX_VALUE_LEN 64
#define MAX_EXPIRE_LEN 16

etcd_session
etcd_open (etcd_server *server_list)
{
        return NULL;
}

typedef struct _etcd_sim_s {
        char *path;
} etcd_sim_t;

void
etcd_close (etcd_session this)
{
        etcd_sim_t *sim = (etcd_sim_t *)this;
        free(sim->path);
        free(this);
}


char *
etcd_get_1 (FILE *stream, char *key)
{
        char *str = NULL;
        size_t len;
        unsigned long expires;
	char *ret;

        // Read the file
        while(1) {
                if(str) {
                        free(str);
                        str = NULL;
                }
                if (getline((char **)&str, &len,stream) == -1) {
                        break;
                }
                if (!strncmp(str, key, strlen(key))) {
			char k[256], s[256];
			sscanf(str,"%s %s %lu",k, s, &expires);
			// check if key is expired.
			if (time(NULL) > expires) {
				/* Keep looking for an unexpired entry. */
				continue;
			}
		        ret = calloc(1, strlen(s) + 1);
		        strcpy(ret,s);
		        free(str);
		        return(ret);
                }
        }
        return NULL;
}


char *
etcd_get (etcd_session this, char *key)
{
        etcd_sim_t      *sim            = (etcd_sim_t *)this;
        int             fd;
        FILE            *stream;
        char            *retval;

        fd = open(sim->path,O_RDONLY);
        if (!fd) {
                return NULL;
        }

        stream = fdopen(fd,"r");
        (void)flock(fd,LOCK_SH);
        retval = etcd_get_1(stream,key);
        (void)flock(fd,LOCK_UN);
        fclose(stream); /* closes fd as well */

        return retval;
}


etcd_result
etcd_set_1 (FILE *stream, char *key, char *value,
            char *precond, unsigned int ttl)
{
        char *str = NULL;
        char tp[255];
        size_t len;
        unsigned long expires;

        while(1) {
                if(str) {
                        free(str);
                        str = NULL;
                }
                if (getline((char **)&str, &len,stream) == -1) {
                        break;
                }
                if (!strncmp(str, key, strlen(key))) {
                        char k[256], s[256];
                        sscanf(str,"%s %s %lu",k, s, &expires);
                        // check if the present key is expired
                        if (time(NULL) > expires) {
				/* Keep looking for an unexpired entry. */
				continue;
			}
			/*
			 * The only case in which we should fail here is if a
			 * precondition was specified and does not match the
                         * current (non-expired) value.
			 */
			if (precond && strcmp(precond, s)) {
				free(str);
				return ETCD_WTF;
			}
                        fseek(stream, -strlen(str), SEEK_CUR);               
                        free(str);
                        goto here;
                }
        }
here:
        memset(tp, 0, 255);
        sprintf(tp,"%*s %*s %*lu\n",
                -MAX_KEY_LEN, key, -MAX_VALUE_LEN, value,
                -MAX_EXPIRE_LEN, ttl ? time(NULL) + ttl : ~0);
        if (fwrite(tp, 1,strlen(tp), stream) != strlen(tp)) {
                return ETCD_WTF;
        }
        fflush(stream);
        fsync(fileno(stream));
        return ETCD_OK;
}


etcd_result
etcd_set (etcd_session this, char *key, char *value,
          char *precond, unsigned int ttl)
{
        etcd_sim_t      *sim = (etcd_sim_t *)this;
        int             fd;
        FILE            *stream;
        etcd_result     retval;

        fd = open(sim->path,O_RDWR);
        if (fd < 0) {
                return ETCD_WTF;
        }

        stream = fdopen(fd,"r+");
        (void)flock(fd,LOCK_EX);
        retval = etcd_set_1(stream,key,value,precond,ttl);
        (void)flock(fd,LOCK_UN);
        fclose(stream); /* closes fd as well */

        return retval;
}


etcd_session
etcd_open_str (char *server_names)
{
        etcd_sim_t      *sim;
        int             fd;

        sim = calloc(1, sizeof(etcd_sim_t));
        (void)asprintf(&sim->path,"/tmp/%s",server_names);

        fd = open(sim->path, O_RDWR | O_CREAT, 0777);
        if (fd == -1) {
                free(sim->path);
                free(sim);
                return NULL;
        }

        close(fd);
        return ((void *)sim);
}


void
etcd_close_str (etcd_session this)
{
        etcd_close(this);
}

etcd_result
etcd_delete (etcd_session this, char *key)
{
	return ETCD_WTF;
}

char *
etcd_leader (etcd_session this_as_void)
{
	return NULL;
}

etcd_result
etcd_watch (etcd_session this, char *pfx, char **keyp, char **valuep,
	    int *index_in, int *index_out)
{
	return ETCD_WTF;
}

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

        if (!index_in) {
                if (gf_asprintf(&path,"/var/tmp/%s",key) < 0) {
                        return ETCD_WTF;
                }
                fd = open(path,O_RDWR|O_CREAT,0666);
                GF_FREE(path);
                if (fd < 0) {
                        return ETCD_WTF;
                }
                if (flock(fd,LOCK_EX) < 0) {
                        close(fd);
                        return ETCD_WTF;
                }
                *index_out = strdup("42");
        }

        /*
         * Yes, we leak an fd by not closing it here (and nobody else even
         * knows about it).  That would be awful in any other context, but
         * for test scripts it won't matter.
         */
        return ETCD_OK;
}