cli.c 15.8 KB
Newer Older
1
2
3
4
5
6
7
8
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
mahadevkonar's avatar
mahadevkonar committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <zookeeper.h>
#include <proto.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
28
#include <errno.h>
29
#include <assert.h>
mahadevkonar's avatar
mahadevkonar committed
30
31
32
33
34
35
36
37
38
39
40

#ifdef YCA
#include <yca/yca.h>
#endif

#define _LL_CAST_ (long long)

static zhandle_t *zh;
static clientid_t myid;
static const char *clientIdFile = 0;
struct timeval startTime;
41
42
static char cmd[1024];
static int batchMode=0;
mahadevkonar's avatar
mahadevkonar committed
43
44
45
46
47
48
49

static int to_send=0;
static int sent=0;
static int recvd=0;

static int shutdownThisThing=0;

50
51
52
53
54
55
56
57
58
static __attribute__ ((unused)) void 
printProfileInfo(struct timeval start, struct timeval end,int thres,const char* msg)
{
  int delay=(end.tv_sec*1000+end.tv_usec/1000)-
    (start.tv_sec*1000+start.tv_usec/1000);
  if(delay>thres)
    fprintf(stderr,"%s: execution time=%dms\n",msg,delay);
}

59
void watcher(zhandle_t *zzh, int type, int state, const char *path,void* context) {
60
    fprintf(stderr,"Watcher %d state = %d for %s\n", type, state, (path ? path: "null"));
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
61
62
    if (type == ZOO_SESSION_EVENT) {
        if (state == ZOO_CONNECTED_STATE) {
63
64
65
66
67
68
69
70
71
            const clientid_t *id = zoo_client_id(zh);
            if (myid.client_id == 0|| myid.client_id != id->client_id) {
                myid = *id;
                fprintf(stderr, "Got a new id: %llx\n", _LL_CAST_ myid.client_id);
                if (clientIdFile) {
                    FILE *fh = fopen(clientIdFile, "w");
                    if (!fh) {
                        perror(clientIdFile);
                    } else {
72
73
74
75
                        int rc = fwrite(&myid, sizeof(myid), 1, fh);
                        if (rc != sizeof(myid)) {
                            perror("writing client id");
                        }
76
77
78
79
                        fclose(fh);
                    }
                }
            }
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
80
        } else if (state == ZOO_AUTH_FAILED_STATE) {
81
82
83
84
            fprintf(stderr, "Authentication failure. Shutting down...\n");
            zookeeper_close(zh);
            shutdownThisThing=1;
            zh=0;
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
85
        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
86
87
88
89
90
91
            fprintf(stderr, "Session expired. Shutting down...\n");
            zookeeper_close(zh);
            shutdownThisThing=1;
            zh=0;
        }
    }
mahadevkonar's avatar
mahadevkonar committed
92
93
}

94
95
96
97
98
void dumpStat(const struct Stat *stat) {
    char tctimes[40];
    char tmtimes[40];
    time_t tctime;
    time_t tmtime;
mahadevkonar's avatar
mahadevkonar committed
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
    if (!stat) {
        fprintf(stderr,"null\n");
        return;
    }
    tctime = stat->ctime/1000;
    tmtime = stat->mtime/1000;
    fprintf(stderr, "\tctime = %s\tczxid=%llx\n"
    "\tmtime=%s\tmzxid=%llx\n"
    "\tversion=%x\taversion=%x\n"
    "\tephemeralOwner = %llx\n",
    ctime_r(&tctime, tctimes), _LL_CAST_ stat->czxid, ctime_r(&tmtime, tmtimes),
    _LL_CAST_ stat->mzxid,
    (unsigned int)stat->version, (unsigned int)stat->aversion,
    _LL_CAST_ stat->ephemeralOwner);
mahadevkonar's avatar
mahadevkonar committed
114
115
}

116
117
118
119
120
void my_string_completion(int rc, const char *name, const void *data) {
    fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc);
    if (!rc) {
        fprintf(stderr, "\tname = %s\n", name);
    }
121
122
    if(batchMode)
      shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
123
124
125
}

void my_data_completion(int rc, const char *value, int value_len,
126
127
128
129
130
131
132
133
134
135
136
        const struct Stat *stat, const void *data) {
    struct timeval tv;
    int sec;
    int usec;
    gettimeofday(&tv, 0);
    sec = tv.tv_sec - startTime.tv_sec;
    usec = tv.tv_usec - startTime.tv_usec;
    fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000);
    fprintf(stderr, "%s: rc = %d\n", (char*)data, rc);
    if (value) {
        fprintf(stderr, " value_len = %d\n", value_len);
137
        assert(write(2, value, value_len) == value_len);
138
139
140
141
    }
    fprintf(stderr, "\nStat:\n");
    dumpStat(stat);
    free((void*)data);
142
143
    if(batchMode)
      shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
144
145
146
}

void my_silent_data_completion(int rc, const char *value, int value_len,
147
        const struct Stat *stat, const void *data) {
mahadevkonar's avatar
mahadevkonar committed
148
149
150
    recvd++;
    fprintf(stderr, "Data completion %s rc = %d\n",(char*)data,rc);
    free((void*)data);
151
    if (recvd==to_send) {
mahadevkonar's avatar
mahadevkonar committed
152
        fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,to_send);
153
154
        if(batchMode)
          shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
155
156
157
158
    }
}

void my_strings_completion(int rc, const struct String_vector *strings,
159
160
161
162
163
        const void *data) {
    struct timeval tv;
    int sec;
    int usec;
    int i;
mahadevkonar's avatar
mahadevkonar committed
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
    gettimeofday(&tv, 0);
    sec = tv.tv_sec - startTime.tv_sec;
    usec = tv.tv_usec - startTime.tv_usec;
    fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000);
    fprintf(stderr, "%s: rc = %d\n", (char*)data, rc);
    if (strings)
        for (i=0; i < strings->count; i++) {
            fprintf(stderr, "\t%s\n", strings->data[i]);
        }
    free((void*)data);
    gettimeofday(&tv, 0);
    sec = tv.tv_sec - startTime.tv_sec;
    usec = tv.tv_usec - startTime.tv_usec;
    fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000);
179
180
    if(batchMode)
      shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
181
182
}

183
184
185
void my_void_completion(int rc, const void *data) {
    fprintf(stderr, "%s: rc = %d\n", (char*)data, rc);
    free((void*)data);
186
187
    if(batchMode)
      shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
188
189
}

190
191
192
193
void my_stat_completion(int rc, const struct Stat *stat, const void *data) {
    fprintf(stderr, "%s: rc = %d Stat:\n", (char*)data, rc);
    dumpStat(stat);
    free((void*)data);
194
195
    if(batchMode)
      shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
196
197
198
}

void my_silent_stat_completion(int rc, const struct Stat *stat,
199
200
        const void *data) {
    //    fprintf(stderr, "State completion: [%s] rc = %d\n", (char*)data, rc);
mahadevkonar's avatar
mahadevkonar committed
201
202
203
204
    sent++;
    free((void*)data);
}

205
static void sendRequest(const char* data) {
mahadevkonar's avatar
mahadevkonar committed
206
207
208
209
210
    zoo_aset(zh, "/od", data, strlen(data), -1, my_silent_stat_completion,
            strdup("/od"));
    zoo_aget(zh, "/od", 1, my_silent_data_completion, strdup("/od"));
}

211
void od_completion(int rc, const struct Stat *stat, const void *data) {
mahadevkonar's avatar
mahadevkonar committed
212
213
214
215
    int i;
    fprintf(stderr, "od command response: rc = %d Stat:\n", rc);
    dumpStat(stat);
    // send a whole bunch of requests
216
217
218
    recvd=0;
    sent=0;
    to_send=200;
219
    for (i=0; i<to_send; i++) {
mahadevkonar's avatar
mahadevkonar committed
220
        char buf[4096*16];
221
        memset(buf, -1, sizeof(buf)-1);
222
        buf[sizeof(buf)-1]=0;
mahadevkonar's avatar
mahadevkonar committed
223
224
225
226
        sendRequest(buf);
    }
}

227
228
229
int startsWith(const char *line, const char *prefix) {
    int len = strlen(prefix);
    return strncmp(line, prefix, len) == 0;
mahadevkonar's avatar
mahadevkonar committed
230
231
}

232
233
static const char *hostPort;

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
void processline(char *line) {
    int rc;
    int async = (line[0] == 'a');
    if (async) {
        line++;
    }
    if (startsWith(line, "get ")) {
        line += 4;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
        gettimeofday(&startTime, 0);
        rc = zoo_aget(zh, line, 1, my_data_completion, strdup(line));
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "set ")) {
        char *ptr;
        line += 4;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
        ptr = strchr(line, ' ');
        if (!ptr) {
            fprintf(stderr, "No data found after path\n");
            return;
        }
        *ptr = '\0';
        ptr++;
        if (async) {
            rc = zoo_aset(zh, line, ptr, strlen(ptr), -1, my_stat_completion,
                    strdup(line));
        } else {
            rc = zoo_set(zh, line, ptr, strlen(ptr), -1);
        }
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "ls ")) {
        line += 3;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
        gettimeofday(&startTime, 0);
        rc= zoo_aget_children(zh, line, 1, my_strings_completion, strdup(line));
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "create ")) {
        int flags = 0;
        line += 7;
        if (line[0] == '+') {
            line++;
            if (line[0] == 'e') {
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
291
                flags |= ZOO_EPHEMERAL;
292
293
294
                line++;
            }
            if (line[0] == 's') {
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
295
                flags |= ZOO_SEQUENCE;
296
297
298
299
300
301
302
303
                line++;
            }
            line++;
        }
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
mahadevkonar's avatar
mahadevkonar committed
304
        fprintf(stderr, "Creating [%s] node\n", line);
305
//        {
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
306
//            struct ACL _CREATE_ONLY_ACL_ACL[] = {{ZOO_PERM_CREATE, ZOO_ANYONE_ID_UNSAFE}};
307
308
309
310
//            struct ACL_vector CREATE_ONLY_ACL = {1,_CREATE_ONLY_ACL_ACL};
//            rc = zoo_acreate(zh, line, "new", 3, &CREATE_ONLY_ACL, flags,
//                    my_string_completion, strdup(line));
//        }
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
311
        rc = zoo_acreate(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, flags,
312
                my_string_completion, strdup(line));
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "delete ")) {
        line += 7;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
        if (async) {
            rc = zoo_adelete(zh, line, -1, my_void_completion, strdup(line));
        } else {
            rc = zoo_delete(zh, line, -1);
        }
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "sync ")) {
        line += 5;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
        rc = zoo_async(zh, line, my_string_completion, strdup(line));
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (startsWith(line, "exists ")) {
        line += 7;
        if (line[0] != '/') {
            fprintf(stderr, "Path must start with /, found: %s\n", line);
            return;
        }
#ifndef THREADED
        rc = zoo_aexists(zh, line, 1, my_stat_completion, strdup(line));
#else
        struct Stat stat;
        rc = zoo_exists(zh, line, 1, &stat);
#endif
        if (rc) {
            fprintf(stderr, "Error %d for %s\n", rc, line);
        }
    } else if (strcmp(line, "myid") == 0) {
        printf("session Id = %llx\n", _LL_CAST_ zoo_client_id(zh)->client_id);
    } else if (strcmp(line, "reinit") == 0) {
        zookeeper_close(zh);
        // we can't send myid to the server here -- zookeeper_close() removes 
        // the session on the server. We must start anew.
        zh = zookeeper_init(hostPort, watcher, 10000, 0, 0, 0);
    } else if (startsWith(line, "quit")) {
        fprintf(stderr, "Quitting...\n");
        shutdownThisThing=1;
    } else if (startsWith(line, "od")) {
        const char val[]="fire off";
mahadevkonar's avatar
mahadevkonar committed
367
        fprintf(stderr, "Overdosing...\n");
368
        rc = zoo_aset(zh, "/od", val, sizeof(val)-1, -1, od_completion, 0);
mahadevkonar's avatar
mahadevkonar committed
369
370
371
372
373
        if (rc)
            fprintf(stderr, "od command failed: %d\n", rc);
    }
}

374
int main(int argc, char **argv) {
mahadevkonar's avatar
mahadevkonar committed
375
#ifndef THREADED
376
    fd_set rfds, wfds, efds;
377
    int processed=0;
mahadevkonar's avatar
mahadevkonar committed
378
#endif
379
380
    char buffer[4096];
    char p[2048];
mahadevkonar's avatar
mahadevkonar committed
381
#ifdef YCA  
382
383
    char *cert=0;
    char appId[64];
mahadevkonar's avatar
mahadevkonar committed
384
#endif
385
386
    int bufoff = 0;
    FILE *fh;
mahadevkonar's avatar
mahadevkonar committed
387

388
    if (argc < 2) {
389
390
391
        fprintf(stderr,
                "USAGE %s zookeeper_host_list [clientid_file|cmd:(ls|create|od|...)]\n", 
                argv[0]);
392
393
394
        return 2;
    }
    if (argc > 2) {
395
396
397
398
399
      if(strncmp("cmd:",argv[2],4)==0){
        strcpy(cmd,argv[2]+4);
        batchMode=1;
        fprintf(stderr,"Batch mode: %s\n",cmd);
      }else{
400
401
402
        clientIdFile = argv[2];
        fh = fopen(clientIdFile, "r");
        if (fh) {
403
404
405
            if (fread(&myid, sizeof(myid), 1, fh) != sizeof(myid)) {
                memset(&myid, 0, sizeof(myid));
            }
406
407
            fclose(fh);
        }
408
      }
409
    }
mahadevkonar's avatar
mahadevkonar committed
410
#ifdef YCA
411
412
413
414
415
416
417
418
419
420
    strcpy(appId,"yahoo.example.yca_test");
    cert = yca_get_cert_once(appId);
    if(cert!=0) {
        fprintf(stderr,"Certificate for appid [%s] is [%s]\n",appId,cert);
        strncpy(p,cert,sizeof(p)-1);
        free(cert);
    } else {
        fprintf(stderr,"Certificate for appid [%s] not found\n",appId);
        strcpy(p,"dummy");
    }
mahadevkonar's avatar
mahadevkonar committed
421
#else
422
    strcpy(p, "dummy");
mahadevkonar's avatar
mahadevkonar committed
423
#endif
Flavio Paiva Junqueira's avatar
Flavio Paiva Junqueira committed
424
    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
425
    zoo_deterministic_conn_order(1); // enable deterministic order
426
    hostPort = argv[1];
427
428
429
430
431
    zh = zookeeper_init(hostPort, watcher, 10000, &myid, 0, 0);
    if (!zh) {
        return errno;
    }

mahadevkonar's avatar
mahadevkonar committed
432
#ifdef YCA
433
434
    if(zoo_add_auth(zh,"yca",p,strlen(p),0,0)!=ZOK)
    return 2;
mahadevkonar's avatar
mahadevkonar committed
435
436
437
#endif

#ifdef THREADED
438
439
    while(!shutdownThisThing) {
        int rc;
mahadevkonar's avatar
mahadevkonar committed
440
441
442
443
444
445
446
447
        int len = sizeof(buffer) - bufoff -1;
        if (len <= 0) {
            fprintf(stderr, "Can't handle lines that long!\n");
            exit(2);
        }
        rc = read(0, buffer+bufoff, len);
        if (rc <= 0) {
            fprintf(stderr, "bye\n");
448
            shutdownThisThing=1;
mahadevkonar's avatar
mahadevkonar committed
449
450
451
452
453
454
455
456
457
458
459
460
            break;
        }
        bufoff += rc;
        buffer[bufoff] = '\0';
        while (strchr(buffer, '\n')) {
            char *ptr = strchr(buffer, '\n');
            *ptr = '\0';
            processline(buffer);
            ptr++;
            memmove(buffer, ptr, strlen(ptr)+1);
            bufoff = 0;
        }
461
    }
mahadevkonar's avatar
mahadevkonar committed
462
#else 
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    FD_ZERO(&efds);
    while (!shutdownThisThing) {
        int fd;
        int interest;
        int events;
        struct timeval tv;
        int rc;
        zookeeper_interest(zh, &fd, &interest, &tv);
        if (fd != -1) {
            if (interest&ZOOKEEPER_READ) {
                FD_SET(fd, &rfds);
            } else {
                FD_CLR(fd, &rfds);
            }
            if (interest&ZOOKEEPER_WRITE) {
                FD_SET(fd, &wfds);
            } else {
                FD_CLR(fd, &wfds);
            }
        } else {
            fd = 0;
        }
        FD_SET(0, &rfds);
        rc = select(fd+1, &rfds, &wfds, &efds, &tv);
        events = 0;
        if (FD_ISSET(fd, &rfds)) {
            events |= ZOOKEEPER_READ;
        }
        if (FD_ISSET(fd, &wfds)) {
            events |= ZOOKEEPER_WRITE;
        }
496
497
498
499
500
        if(batchMode && processed==0){
          //batch mode
          processline(cmd);
          processed=1;
        }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
        if (FD_ISSET(0, &rfds)) {
            int rc;
            int len = sizeof(buffer) - bufoff -1;
            if (len <= 0) {
                fprintf(stderr, "Can't handle lines that long!\n");
                exit(2);
            }
            rc = read(0, buffer+bufoff, len);
            if (rc <= 0) {
                fprintf(stderr, "bye\n");
                break;
            }
            bufoff += rc;
            buffer[bufoff] = '\0';
            while (strchr(buffer, '\n')) {
                char *ptr = strchr(buffer, '\n');
                *ptr = '\0';
                processline(buffer);
                ptr++;
                memmove(buffer, ptr, strlen(ptr)+1);
                bufoff = 0;
            }
        }
        zookeeper_process(zh, events);
    }
mahadevkonar's avatar
mahadevkonar committed
526
#endif
527
528
529
530
    if (to_send!=0)
        fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,sent);
    zookeeper_close(zh);
    return 0;
mahadevkonar's avatar
mahadevkonar committed
531
}