summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/nsr-server/src/etcd-sim.c
blob: 5c5cdcec05e9822756b1ccb00109658f54a8c735 (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
/*
 * 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.
 */

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

#include "call-stub.h"
#include "defaults.h"
#include "xlator.h"
#include "api/src/glfs.h"
#include "api/src/glfs-internal.h"
#include "run.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 T_FORMAT "%d-%b-%Y,%H:%M:%S"
#define MAX_KEY_LEN 64
#define MAX_VALUE_LEN 64
#define MAX_TTL_LEN 12

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

typedef struct _etcd_sim_s {
        int fd;
        FILE *stream;
} etcd_sim_t;

void
etcd_close (etcd_session this)
{
        etcd_sim_t *sim = (etcd_sim_t *)this;
        fflush(sim->stream);
        fclose(sim->stream);
        close(sim->fd);
        free(this);
}


char *
etcd_get (etcd_session this, char *key)
{
        char *str = NULL;
        size_t len;
        etcd_sim_t *sim = (etcd_sim_t *)this;
        struct tm tm;
        time_t old, new;
        lockf(sim->fd, F_LOCK, 0 );
        if (fseek(sim->stream, 0, SEEK_SET) == -1) {
                lockf(sim->fd, F_ULOCK, 0 );
                return NULL;
        }
        // Read the file
        while(1) {
                if(str) {
                        free(str);
                        str = NULL;
                }
                if (getline((char **)&str, &len,sim->stream) == -1) {
                        break;
                }
                if (!strncmp(str, key, strlen(key))) {
                       char k[256], s[256], *ret, past[256];
                       unsigned int ttl;
                       double delta;
                       sscanf(str,"%s %s %d %s",k, s, &ttl, past);
                       strptime(past, T_FORMAT, &tm);
                       old = mktime(&tm);
                       new = time(NULL);
                       delta = difftime(new, old);
                       // check if key is expired.
                       // If ttl is 0, it means key has infinite ttk=l.
                       if ((!ttl) || ((delta >= 0) && (delta < ttl))) {
                               ret = calloc(1, strlen(s) + 1);
                               strcpy(ret,s);
                               free(str);
                               lockf(sim->fd, F_ULOCK, 0 );
                               return(ret);
                       }
                }
        }
        lockf(sim->fd, F_ULOCK, 0 );
        return NULL;
}


etcd_result
etcd_set (etcd_session this, char *key, char *value,
          char *precond, unsigned int ttl)
{
        char *str = NULL;
        char buf[255];
        char tp[255];
        char s[255];
        size_t len;
        etcd_sim_t *sim = (etcd_sim_t *)this;
        struct tm tm;
        time_t old, new;
        lockf(sim->fd, F_LOCK, 0 );
        if (fseek(sim->stream, 0, SEEK_SET) == -1) {
                lockf(sim->fd, F_ULOCK, 0 );
                return ETCD_WTF;
        }
        while(1) {
                if(str) {
                        free(str);
                        str = NULL;
                }
                if (getline((char **)&str, &len,sim->stream) == -1) {
                        break;
                }
                if (!strncmp(str, key, strlen(key))) {
                        char k[256], s[256], past[256];
                        unsigned int ttl;
                        double delta;
                        sscanf(str,"%s %s %d %s",k, s, &ttl, past);
                        strptime(past, T_FORMAT, &tm);
                        old = mktime(&tm);
                        new = time(NULL);
                        delta = difftime(new, old);
                        // check if the present key is expired
                        if ( (!ttl) || ((delta >= 0) && (delta < ttl))) {
                                // present key not expired. In case of precondition,
                                // check if it matches. If not return with error
                                // In case of no precond, return error since
                                // present key not yet expired.
                                if ((!precond) || (strcmp(precond, s))) {
                                        free(str);
                                        lockf(sim->fd, F_ULOCK, 0 );
                                        return ETCD_WTF;
                                }
                        }
                        fseek(sim->stream, -strlen(str), SEEK_CUR);               
                        free(str);
                        goto here;
                }
        }
here:
        memset(tp, 0, 255);
        new = time(NULL);
        memcpy(&tm, localtime(&new), sizeof(struct tm));
        strftime(buf, sizeof(buf), T_FORMAT, &tm);
        // what we want to print in the file is something like this
        // key value(at offset of 64) ttl(offset to 128) time(left offset to 140)
        // hence we would want to create a format buf as follows:
        // "%-64s%-64s%-16d%-18s"
        // Hence we construct this first (in string s) and use that to print into tp
        // which gets written to the registry file.
        sprintf(s,"%%-%ds%%-%ds%%-%dd%%s\n",
                MAX_KEY_LEN, MAX_VALUE_LEN, MAX_TTL_LEN);
        sprintf(tp,s,key, value, ttl, buf);
        if (fwrite(tp, 1,strlen(tp), sim->stream) != strlen(tp)) {
                lockf(sim->fd, F_ULOCK, 0 );
                return ETCD_WTF;
        }
        fflush(sim->stream);
        lockf(sim->fd, F_ULOCK, 0 );
        return ETCD_OK;
}



etcd_session
etcd_open_str (char *server_names)
{
        etcd_sim_t *sim;
        char name[256];

        sim = calloc(1, sizeof(etcd_sim_t));
        sprintf(name, "/tmp/%s", server_names);
        sim->fd = open(name, O_RDWR | O_CREAT);
        if (sim->fd == -1)
                return NULL;
        sim->stream = fopen(name, "r+");
        if (sim->stream == NULL) 
                return NULL;

        return ((void *)sim);
}


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