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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
#include "test_utils.h"
#include "swarmkv/swarmkv.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <asm/errno.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
__attribute__((__unused__)) static int exec_prog(const char **argv)
{
pid_t my_pid;
if (0 == (my_pid = fork())) {
if (-1 == execve(argv[0], (char **)argv , NULL)) {
perror("child process execve failed [%m]");
return -1;
}
}
#ifdef WAIT_FOR_COMPLETION
int status, timeout /* unused ifdef WAIT_FOR_COMPLETION */;
timeout = 1000;
while (0 == waitpid(my_pid , &status , WNOHANG)) {
if ( --timeout < 0 ) {
perror("timeout");
return -1;
}
sleep(1);
}
printf("%s WEXITSTATUS %d WIFEXITED %d [status %d]\n",
argv[0], WEXITSTATUS(status), WIFEXITED(status), status);
if (1 != WIFEXITED(status) || 0 != WEXITSTATUS(status)) {
perror("%s failed, halt system");
return -1;
}
#endif
return 0;
}
int swarmkv_cli_create_cluster(const char *cluster_name, const char *node_string)
{
char cmd_string[1024];
snprintf(cmd_string, sizeof(cmd_string), "../tools/swarmkv-cli --cluster-create %s %s", cluster_name, node_string);
return system(cmd_string);
}
int swarmkv_cli_add_slot_owner(const char *cluster_name, const char *node_string)
{
char cmd_string[1024];
snprintf(cmd_string, sizeof(cmd_string), "../tools/swarmkv-cli -n %s --exec cluster addslotowner %s", cluster_name, node_string);
return system(cmd_string);
// https://stackoverflow.com/questions/16744785/c-threads-using-execl-suicides-itself
// const char *argv[]={"../tools/swarmkv-cli", "-n", cluster_name,
// "--exec", "cluster", "addslotowner", "127.0.0.1:7312", "127.0.0.1:7313", NULL};
// exec_prog(argv);
// return 0;
}
void wait_for_sync()
{
usleep(50*1000);
}
struct cmd_exec_arg* cmd_exec_arg_new(void)
{
struct cmd_exec_arg* arg=(struct cmd_exec_arg*)calloc(sizeof(struct cmd_exec_arg), 1);
pthread_cond_init(&arg->cond, NULL);
pthread_mutex_init(&arg->mutex, NULL);
arg->callback_invoking_tid=syscall(SYS_gettid);
return arg;
}
void cmd_exec_arg_set_cb(struct cmd_exec_arg *arg, proc_result_callback_t *cb, void* cb_arg)
{
arg->cb=cb;
arg->cb_uarg=cb_arg;
}
void cmd_exec_arg_free(struct cmd_exec_arg* arg)
{
pthread_cond_destroy(&arg->cond);
pthread_mutex_destroy(&arg->mutex);
if(arg->expected_reply.str)
{
free(arg->expected_reply.str);
arg->expected_reply.str=NULL;
}
free(arg);
}
void cmd_exec_arg_diable_sync_check(struct cmd_exec_arg* arg)
{
arg->diable_sync_check=1;
return;
}
void cmd_exec_arg_clear(struct cmd_exec_arg* arg)
{
arg->success=0;
arg->is_callback_executed=0;
if(!arg->diable_sync_check)
{
arg->api_invoking_tid = syscall(SYS_gettid);
}
arg->callback_invoking_tid=0;
arg->is_sync_callback=0;
return;
}
void cmd_exec_arg_expect_cstring(struct cmd_exec_arg* arg, const char* string)
{
arg->expected_reply.type=SWARMKV_REPLY_STRING;
if(arg->expected_reply.str)
{
free(arg->expected_reply.str);
arg->expected_reply.str=NULL;
}
arg->expected_reply.str=strdup(string);
arg->expected_reply.len=strlen(string);
}
void cmd_exec_arg_expect_integer(struct cmd_exec_arg* arg, long long integer)
{
arg->expected_reply.type=SWARMKV_REPLY_INTEGER;
arg->expected_reply.integer=integer;
}
void cmd_exec_arg_expect_array(struct cmd_exec_arg* arg, size_t n_element)
{
arg->expected_reply.type=SWARMKV_REPLY_ARRAY;
arg->expected_reply.n_element=n_element;
}
void cmd_exec_arg_expect_OK(struct cmd_exec_arg* arg)
{
arg->expected_reply.type=SWARMKV_REPLY_STATUS;
if(arg->expected_reply.str)
{
free(arg->expected_reply.str);
arg->expected_reply.str=NULL;
}
arg->expected_reply.str=strdup("OK");
arg->expected_reply.len=2;
}
void cmd_exec_arg_expect_NIL(struct cmd_exec_arg* arg)
{
arg->expected_reply.type=SWARMKV_REPLY_NIL;
}
void error_pthread_cond_timedwait(const int timed_wait_rv)
{
fprintf(stderr, "Conditional timed wait, failed.\n");
switch (timed_wait_rv)
{
case ETIMEDOUT:
fprintf(stderr, "The time specified by abstime to pthread_cond_timedwait() has passed.\n");
break;
case EINVAL:
fprintf(stderr, "The value specified by abstime, cond or mutex is invalid.\n");
break;
case EPERM:
fprintf(stderr, "The mutex was not owned by the current thread at the time of the call.\n");
break;
default:
break;
}
}
int cmd_exec_arg_wait(struct cmd_exec_arg *arg, long timeout_ms)
{
struct timespec max_wait = {0, 0};
clock_gettime(CLOCK_REALTIME, &max_wait);
max_wait.tv_sec+=timeout_ms/1000;
max_wait.tv_nsec+=(timeout_ms%1000)*1000*1000;
int timed_wait_rv=0;
if(arg->is_callback_executed)
{
return arg->success;
}
pthread_mutex_lock(&arg->mutex);
timed_wait_rv=pthread_cond_timedwait(&arg->cond, &arg->mutex, &max_wait);
pthread_mutex_unlock(&arg->mutex);
if(timed_wait_rv&& !(arg->is_callback_executed))
{
error_pthread_cond_timedwait(timed_wait_rv);
return -1;
}
else
{
return arg->success;
}
}
void cmd_exec_arg_success(struct cmd_exec_arg* arg)
{
arg->is_callback_executed=1;
arg->success=1;
if(!arg->diable_sync_check)
{
arg->callback_invoking_tid = syscall(SYS_gettid);
}
if(arg->callback_invoking_tid==arg->api_invoking_tid)
{
arg->is_sync_callback=1;
}
if(arg->cb)
{
arg->cb(arg, arg->cb_uarg);
}
pthread_cond_signal(&arg->cond);
}
void cmd_exec_arg_failed(struct cmd_exec_arg* arg)
{
arg->is_callback_executed=1;
arg->success=0;
if(!arg->diable_sync_check)
{
arg->callback_invoking_tid = syscall(SYS_gettid);
}
if(arg->callback_invoking_tid==arg->api_invoking_tid)
{
arg->is_sync_callback=1;
}
if(arg->cb)
{
arg->cb(arg, arg->cb_uarg);
}
pthread_cond_signal(&arg->cond);
}
void cmd_exec_generic_callback(const struct swarmkv_reply* reply, void * cb_arg)
{
struct cmd_exec_arg* arg=(struct cmd_exec_arg*)cb_arg;
if(0==reply_compare(reply, &(arg->expected_reply)))
{
cmd_exec_arg_success(arg);
}
else
{
if(arg->print_reply_on_fail)
{
swarmkv_reply_print(reply, stdout);
}
cmd_exec_arg_failed(arg);
}
return;
}
int reply_compare(const struct swarmkv_reply *r1, const struct swarmkv_reply *r2)
{
size_t i=0;
int ret=0;
if(r1->type!=r2->type) return r1->type-r2->type;
switch(r1->type)
{
case SWARMKV_REPLY_INTEGER:
return r1->integer - r2->integer;
break;
case SWARMKV_REPLY_ERROR:
case SWARMKV_REPLY_STATUS:
case SWARMKV_REPLY_STRING:
case SWARMKV_REPLY_NODE:
if(r1->len!=r2->len)
{
return r1->len - r2->len;
}
else
{
return memcmp(r1->str, r2->str, r1->len);
}
break;
case SWARMKV_REPLY_NIL:
return 0;
break;
case SWARMKV_REPLY_ARRAY:
return (r1->n_element - r2->n_element);
if(r1->n_element != r2->n_element)
{
return r1->n_element - r2->n_element;
}
for(i=0; i<r1->n_element; i++)
{
ret=reply_compare(r1->elements[i], r2->elements[i]);
if(ret!=0)
{
return ret;
}
}
break;
default:
ret=0;
}
return 0;
}
double stddev(long long arr[], size_t size)
{
if (size <= 1) {
return 0.0;
}
double sum = 0.0;
double sumOfSquares = 0.0;
size_t i=0;
// Calculate sum of the array elements
for (i = 0; i < size; i++) {
sum += arr[i];
}
double mean = sum / size;
// Calculate sum of squares of differences from mean
for (i = 0; i < size; i++) {
sumOfSquares += pow(arr[i] - mean, 2);
}
// Calculate standard deviation
return sqrt(sumOfSquares / (size - 1));
}
|