diff options
author | Jeff Darcy <jdarcy@redhat.com> | 2014-01-16 22:36:34 +0000 |
---|---|---|
committer | Jeff Darcy <jdarcy@redhat.com> | 2014-01-22 15:51:29 +0000 |
commit | 3920b73526e2e2a4bf3187c5d9d68bb140f85d7f (patch) | |
tree | ad3e81834b5a811e2318f9b4739fcca58b5b4c7d /xlators/cluster/nsr-server/src/etcd-sim.c | |
parent | edcc056558e5031c84e82810250337153fa1de47 (diff) |
cluster/nsr-server: fix etcd-sim date handling
There was just no particularly good reason to be doing all of those conversions
for comparison on the same machine, and it was actually causing failures on a
test machine where strptime was sporadically creating a structure where the
"hours" field was off by one. Also added missing functions to allow unit
testing, fixed some headers and indentation, etc.
Change-Id: I3b1a80365d72bdb5afb0119a8e6505e2bc47a94f
Signed-off-by: Jeff Darcy <jdarcy@redhat.com>
Diffstat (limited to 'xlators/cluster/nsr-server/src/etcd-sim.c')
-rw-r--r-- | xlators/cluster/nsr-server/src/etcd-sim.c | 125 |
1 files changed, 59 insertions, 66 deletions
diff --git a/xlators/cluster/nsr-server/src/etcd-sim.c b/xlators/cluster/nsr-server/src/etcd-sim.c index c2b8fb863..3cf73a78a 100644 --- a/xlators/cluster/nsr-server/src/etcd-sim.c +++ b/xlators/cluster/nsr-server/src/etcd-sim.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Red Hat + * Copyright (c) 2014, Red Hat * All rights reserved. * Redistribution and use in source and binary forms, with or without @@ -27,18 +27,12 @@ #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 "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> @@ -46,10 +40,9 @@ */ #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 +#define MAX_EXPIRE_LEN 16 etcd_session etcd_open (etcd_server *server_list) @@ -79,8 +72,9 @@ 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; + unsigned long expires; + char *ret; + lockf(sim->fd, F_LOCK, 0 ); if (fseek(sim->stream, 0, SEEK_SET) == -1) { lockf(sim->fd, F_ULOCK, 0 ); @@ -96,23 +90,18 @@ etcd_get (etcd_session this, char *key) 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); - } + 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); + lockf(sim->fd, F_ULOCK, 0 ); + return(ret); } } lockf(sim->fd, F_ULOCK, 0 ); @@ -125,13 +114,11 @@ 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; + unsigned long expires; + lockf(sim->fd, F_LOCK, 0 ); if (fseek(sim->stream, 0, SEEK_SET) == -1) { lockf(sim->fd, F_ULOCK, 0 ); @@ -146,27 +133,23 @@ etcd_set (etcd_session this, char *key, char *value, 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); + char k[256], s[256]; + sscanf(str,"%s %s %lu",k, s, &expires); // check if the present key is expired - if ( (!ttl) || ((delta >= 0) && (delta < ttl))) { - /* - * 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); - lockf(sim->fd, F_ULOCK, 0 ); - return ETCD_WTF; - } - } + 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); + lockf(sim->fd, F_ULOCK, 0 ); + return ETCD_WTF; + } fseek(sim->stream, -strlen(str), SEEK_CUR); free(str); goto here; @@ -174,18 +157,9 @@ etcd_set (etcd_session this, char *key, char *value, } 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); + 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), sim->stream) != strlen(tp)) { lockf(sim->fd, F_ULOCK, 0 ); return ETCD_WTF; @@ -221,3 +195,22 @@ 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; +} |