Skip to content

Commit 36ed46f

Browse files
author
Andrew Kornev
committed
implemeted the batch mode: a zk shell command can now be specified on the command line
git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@670934 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1a85483 commit 36ed46f

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

zookeeper/c/src/cli.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ static zhandle_t *zh;
3535
static clientid_t myid;
3636
static const char *clientIdFile = 0;
3737
struct timeval startTime;
38+
static char cmd[1024];
39+
static int batchMode=0;
3840

3941
static int to_send=0;
4042
static int sent=0;
4143
static int recvd=0;
4244

4345
static int shutdownThisThing=0;
4446

47+
static __attribute__ ((unused)) void
48+
printProfileInfo(struct timeval start, struct timeval end,int thres,const char* msg)
49+
{
50+
int delay=(end.tv_sec*1000+end.tv_usec/1000)-
51+
(start.tv_sec*1000+start.tv_usec/1000);
52+
if(delay>thres)
53+
fprintf(stderr,"%s: execution time=%dms\n",msg,delay);
54+
}
55+
4556
void watcher(zhandle_t *zzh, int type, int state, const char *path) {
4657
fprintf(stderr,"Watcher %d state = %d for %s\n", type, state, (path ? path: "null"));
4758
if (type == SESSION_EVENT) {
@@ -101,6 +112,8 @@ void my_string_completion(int rc, const char *name, const void *data) {
101112
if (!rc) {
102113
fprintf(stderr, "\tname = %s\n", name);
103114
}
115+
if(batchMode)
116+
shutdownThisThing=1;
104117
}
105118

106119
void my_data_completion(int rc, const char *value, int value_len,
@@ -120,25 +133,19 @@ void my_data_completion(int rc, const char *value, int value_len,
120133
fprintf(stderr, "\nStat:\n");
121134
dumpStat(stat);
122135
free((void*)data);
136+
if(batchMode)
137+
shutdownThisThing=1;
123138
}
124139

125140
void my_silent_data_completion(int rc, const char *value, int value_len,
126141
const struct Stat *stat, const void *data) {
127-
// char buf[value_len+1];
128-
// if(value){
129-
// strncpy(buf,value,value_len);buf[value_len]=0;
130-
// }
131-
// fprintf(stderr, "Data completion: %s=\n[%s] rc = %d\n",(char*)data,
132-
// value?buf:"null", rc);
133142
recvd++;
134143
fprintf(stderr, "Data completion %s rc = %d\n",(char*)data,rc);
135144
free((void*)data);
136-
// if(recvd==100){
137-
// fprintf(stderr, "Sleeping for a few moments\n");
138-
// sleep(2);
139-
// }
140145
if (recvd==to_send) {
141146
fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,to_send);
147+
if(batchMode)
148+
shutdownThisThing=1;
142149
}
143150
}
144151

@@ -163,17 +170,23 @@ void my_strings_completion(int rc, const struct String_vector *strings,
163170
sec = tv.tv_sec - startTime.tv_sec;
164171
usec = tv.tv_usec - startTime.tv_usec;
165172
fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000);
173+
if(batchMode)
174+
shutdownThisThing=1;
166175
}
167176

168177
void my_void_completion(int rc, const void *data) {
169178
fprintf(stderr, "%s: rc = %d\n", (char*)data, rc);
170179
free((void*)data);
180+
if(batchMode)
181+
shutdownThisThing=1;
171182
}
172183

173184
void my_stat_completion(int rc, const struct Stat *stat, const void *data) {
174185
fprintf(stderr, "%s: rc = %d Stat:\n", (char*)data, rc);
175186
dumpStat(stat);
176187
free((void*)data);
188+
if(batchMode)
189+
shutdownThisThing=1;
177190
}
178191

179192
void my_silent_stat_completion(int rc, const struct Stat *stat,
@@ -355,6 +368,7 @@ void processline(char *line) {
355368
int main(int argc, char **argv) {
356369
#ifndef THREADED
357370
fd_set rfds, wfds, efds;
371+
int processed=0;
358372
#endif
359373
char buffer[4096];
360374
char p[2048];
@@ -366,16 +380,24 @@ int main(int argc, char **argv) {
366380
FILE *fh;
367381

368382
if (argc < 2) {
369-
fprintf(stderr, "USAGE %s zookeeper_host_list [clientid_file]\n", argv[0]);
383+
fprintf(stderr,
384+
"USAGE %s zookeeper_host_list [clientid_file|cmd:(ls|create|od|...)]\n",
385+
argv[0]);
370386
return 2;
371387
}
372388
if (argc > 2) {
389+
if(strncmp("cmd:",argv[2],4)==0){
390+
strcpy(cmd,argv[2]+4);
391+
batchMode=1;
392+
fprintf(stderr,"Batch mode: %s\n",cmd);
393+
}else{
373394
clientIdFile = argv[2];
374395
fh = fopen(clientIdFile, "r");
375396
if (fh) {
376397
fread(&myid, sizeof(myid), 1, fh);
377398
fclose(fh);
378399
}
400+
}
379401
}
380402
#ifdef YCA
381403
strcpy(appId,"yahoo.example.yca_test");
@@ -463,6 +485,11 @@ int main(int argc, char **argv) {
463485
if (FD_ISSET(fd, &wfds)) {
464486
events |= ZOOKEEPER_WRITE;
465487
}
488+
if(batchMode && processed==0){
489+
//batch mode
490+
processline(cmd);
491+
processed=1;
492+
}
466493
if (FD_ISSET(0, &rfds)) {
467494
int rc;
468495
int len = sizeof(buffer) - bufoff -1;

0 commit comments

Comments
 (0)