Author: mahadev Date: Tue Jan 26 01:06:33 2010 New Revision: 903032 URL: http://svn.apache.org/viewvc?rev=903032&view=rev Log: ZOOKEEPER-612. Make Zookeeper C client can be compiled by gcc of early version (qian via mahadev)
Modified: hadoop/zookeeper/trunk/CHANGES.txt hadoop/zookeeper/trunk/src/c/include/zookeeper.h hadoop/zookeeper/trunk/src/c/src/cli.c hadoop/zookeeper/trunk/src/c/src/load_gen.c hadoop/zookeeper/trunk/src/c/src/mt_adaptor.c hadoop/zookeeper/trunk/src/c/src/zk_hashtable.c hadoop/zookeeper/trunk/src/c/src/zk_log.c hadoop/zookeeper/trunk/src/c/src/zookeeper.c hadoop/zookeeper/trunk/src/c/tests/TestZookeeperInit.cc Modified: hadoop/zookeeper/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/CHANGES.txt?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/CHANGES.txt (original) +++ hadoop/zookeeper/trunk/CHANGES.txt Tue Jan 26 01:06:33 2010 @@ -251,6 +251,9 @@ ZOOKEEPER-655. StringBuffer -> StringBuilder - conversion of references as necessary (Kay Kay via henry) + ZOOKEEPER-612. Make Zookeeper C client can be compiled by gcc of early + version (qian via mahadev) + NEW FEATURES: ZOOKEEPER-539. generate eclipse project via ant target. (phunt via mahadev) Modified: hadoop/zookeeper/trunk/src/c/include/zookeeper.h URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/include/zookeeper.h?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/include/zookeeper.h (original) +++ hadoop/zookeeper/trunk/src/c/include/zookeeper.h Tue Jan 26 01:06:33 2010 @@ -22,6 +22,7 @@ #include <stdlib.h> #include <sys/time.h> #include <stdio.h> +#include <ctype.h> #include "zookeeper_version.h" #include "recordio.h" Modified: hadoop/zookeeper/trunk/src/c/src/cli.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/cli.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/cli.c (original) +++ hadoop/zookeeper/trunk/src/c/src/cli.c Tue Jan 26 01:06:33 2010 @@ -415,6 +415,9 @@ fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "exists ")) { +#ifdef THREADED + struct Stat stat; +#endif line += 7; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); @@ -423,7 +426,6 @@ #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) { @@ -446,8 +448,8 @@ if (rc) fprintf(stderr, "od command failed: %d\n", rc); } else if (startsWith(line, "addauth ")) { - line += 8; char *ptr; + line += 8; ptr = strchr(line, ' '); if (ptr) { *ptr = '\0'; Modified: hadoop/zookeeper/trunk/src/c/src/load_gen.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/load_gen.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/load_gen.c (original) +++ hadoop/zookeeper/trunk/src/c/src/load_gen.c Tue Jan 26 01:06:33 2010 @@ -90,9 +90,10 @@ char nodeName[1024]; int i; for(i=0; i<count;i++){ + int rc = 0; snprintf(nodeName, sizeof(nodeName),"%s/%d",root,i); incCounter(1); - int rc=zoo_acreate(zh, nodeName, "first", 5, &ZOO_OPEN_ACL_UNSAFE, 0, + rc=zoo_acreate(zh, nodeName, "first", 5, &ZOO_OPEN_ACL_UNSAFE, 0, create_completion, 0); if(i%1000==0){ LOG_INFO(("Created %s",nodeName)); @@ -118,9 +119,10 @@ int i; counter=0; for(i=0; i<count;i++){ + int rc = 0; snprintf(nodeName, sizeof(nodeName),"%s/%d",root,i); incCounter(1); - int rc=zoo_aset(zh, nodeName, "second", 6,-1,write_completion, 0); + rc=zoo_aset(zh, nodeName, "second", 6,-1,write_completion, 0); if(rc!=ZOK) return rc; } } @@ -145,9 +147,10 @@ int i; counter=0; for(i=0; i<count;i++){ + int rc = 0; snprintf(nodeName, sizeof(nodeName),"%s/%d",root,i); incCounter(1); - int rc=zoo_aget(zh, nodeName,0,read_completion, 0); + rc=zoo_aget(zh, nodeName,0,read_completion, 0); if(rc!=ZOK) return rc; } } @@ -161,9 +164,10 @@ int i; counter=0; for(i=0; i<count;i++){ + int rc = 0; snprintf(nodeName, sizeof(nodeName),"%s/%d",root,i); incCounter(1); - int rc=zoo_adelete(zh, nodeName,-1,delete_completion, 0); + rc=zoo_adelete(zh, nodeName,-1,delete_completion, 0); if(rc!=ZOK) return rc; } } @@ -192,9 +196,10 @@ return rc; } for(i=0;i<children.count; i++){ + int rc = 0; char nodeName[2048]; snprintf(nodeName, sizeof(nodeName),"%s/%s",root,children.data[i]); - int rc=recursiveDelete(nodeName); + rc=recursiveDelete(nodeName); if(rc!=ZOK){ free_String_vector(&children); return rc; @@ -237,8 +242,9 @@ LOG_INFO(("Checking server connection...")); ensureConnected(); if(cleaning==1){ + int rc = 0; deletedCounter=0; - int rc=recursiveDelete(argv[2]); + rc=recursiveDelete(argv[2]); if(rc==ZOK){ LOG_INFO(("Succesfully deleted a subtree starting at %s (%d nodes)", argv[2],deletedCounter)); Modified: hadoop/zookeeper/trunk/src/c/src/mt_adaptor.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/mt_adaptor.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/mt_adaptor.c (original) +++ hadoop/zookeeper/trunk/src/c/src/mt_adaptor.c Tue Jan 26 01:06:33 2010 @@ -140,6 +140,7 @@ void start_threads(zhandle_t* zh) { + int rc = 0; struct adaptor_threads* adaptor=zh->adaptor_priv; pthread_cond_init(&adaptor->cond,0); pthread_mutex_init(&adaptor->lock,0); @@ -149,7 +150,7 @@ // while initialization is in progress api_prolog(zh); LOG_DEBUG(("starting threads...")); - int rc=pthread_create(&adaptor->io, 0, do_io, zh); + rc=pthread_create(&adaptor->io, 0, do_io, zh); assert("pthread_create() failed for the IO thread"&&!rc); rc=pthread_create(&adaptor->completion, 0, do_completion, zh); assert("pthread_create() failed for the completion thread"&&!rc); Modified: hadoop/zookeeper/trunk/src/c/src/zk_hashtable.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/zk_hashtable.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/zk_hashtable.c (original) +++ hadoop/zookeeper/trunk/src/c/src/zk_hashtable.c Tue Jan 26 01:06:33 2010 @@ -99,9 +99,11 @@ static void destroy_watcher_object_list(watcher_object_list_t* list) { + watcher_object_t* e = NULL; + if(list==0) return; - watcher_object_t* e=list->head; + e=list->head; while(e!=0){ watcher_object_t* this=e; e=e->next; Modified: hadoop/zookeeper/trunk/src/c/src/zk_log.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/zk_log.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/zk_log.c (original) +++ hadoop/zookeeper/trunk/src/c/src/zk_log.c Tue Jan 26 01:06:33 2010 @@ -91,21 +91,23 @@ static const char* time_now(){ struct timeval tv; + struct tm lt; + time_t now = 0; + size_t len = 0; char* now_str=get_time_buffer(); + if(!now_str) return "time_now(): Failed to allocate memory buffer"; gettimeofday(&tv,0); - const time_t now = tv.tv_sec; - struct tm lt; + now = tv.tv_sec; localtime_r(&now, <); // clone the format used by log4j ISO8601DateFormat // specifically: "yyyy-MM-dd HH:mm:ss,SSS" - size_t len = strftime(now_str, - TIME_NOW_BUF_SIZE, + len = strftime(now_str, TIME_NOW_BUF_SIZE, "%F %H:%M:%S", <); Modified: hadoop/zookeeper/trunk/src/c/src/zookeeper.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/zookeeper.c?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/src/zookeeper.c (original) +++ hadoop/zookeeper/trunk/src/c/src/zookeeper.c Tue Jan 26 01:06:33 2010 @@ -316,10 +316,11 @@ { auth_info *auth = auth_list->auth; while (auth != NULL) { + auth_info* old_auth = NULL; if(auth->scheme!=NULL) free(auth->scheme); deallocate_Buffer(&auth->auth); - auth_info *old_auth = auth; + old_auth = auth; auth = auth->next; free(old_auth); } @@ -449,11 +450,18 @@ } memset(&hints, 0, sizeof(hints)); +#ifdef AI_ADDRCONFIG hints.ai_flags = AI_ADDRCONFIG; +#else + hints.ai_flags = 0; +#endif hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; + while(isspace(*host) && host != strtok_last) + host++; + if (getaddrinfo(host, port_spec, &hints, &res0) != 0) { LOG_ERROR(("getaddrinfo: %s\n", strerror(errno))); rc=ZSYSTEMERROR; @@ -543,6 +551,15 @@ static void log_env() { char buf[2048]; +#ifdef HAVE_SYS_UTSNAME_H + struct utsname utsname; +#endif + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R) + struct passwd pw; + struct passwd *pwp = NULL; + uid_t uid = 0; +#endif LOG_INFO(("Client environment:zookeeper.version=%s", PACKAGE_STRING)); @@ -554,7 +571,6 @@ #endif #ifdef HAVE_SYS_UTSNAME_H - struct utsname utsname; uname(&utsname); LOG_INFO(("Client environment:os.name=%s", utsname.sysname)); LOG_INFO(("Client environment:os.arch=%s", utsname.release)); @@ -572,9 +588,7 @@ #endif #if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R) - uid_t uid = getuid(); - struct passwd pw; - struct passwd *pwp; + uid = getuid(); if (!getpwuid_r(uid, &pw, buf, sizeof(buf), &pwp)) { LOG_INFO(("Client environment:user.home=%s", pw.pw_dir)); } else { @@ -601,6 +615,10 @@ zhandle_t *zookeeper_init(const char *host, watcher_fn watcher, int recv_timeout, const clientid_t *clientid, void *context, int flags) { + int errnosave = 0; + zhandle_t *zh = NULL; + char *index_chroot = NULL; + log_env(); LOG_INFO(("Initiating client connection, host=%s sessionTimeout=%d watcher=%p" @@ -614,9 +632,7 @@ context, flags)); - int errnosave; - zhandle_t *zh = calloc(1, sizeof(*zh)); - char *index_chroot; + zh = calloc(1, sizeof(*zh)); if (!zh) { return 0; } @@ -1122,8 +1138,9 @@ /** send all auths, not just the last one **/ static int send_auth_info(zhandle_t *zh) { int rc = 0; + auth_info *auth = NULL; + zoo_lock_auth(zh); - auth_info *auth; auth = zh->auth_h.auth; if (auth == NULL) { zoo_unlock_auth(zh); @@ -1140,9 +1157,11 @@ static int send_last_auth_info(zhandle_t *zh) { - int rc; + int rc = 0; + auth_info *auth = NULL; + zoo_lock_auth(zh); - auth_info *auth = get_last_auth(&zh->auth_h); + auth = get_last_auth(&zh->auth_h); if(auth==NULL) { zoo_unlock_auth(zh); return ZOK; // there is nothing to send @@ -1786,17 +1805,18 @@ } if (hdr.xid == WATCHER_EVENT_XID) { + struct WatcherEvent evt; + int type = 0; + char *path = NULL; + completion_list_t *c = NULL; + LOG_DEBUG(("Processing WATCHER_EVENT")); - struct WatcherEvent evt; - int type; - char *path; deserialize_WatcherEvent(ia, "event", &evt); type = evt.type; path = evt.path; /* We are doing a notification, so there is no pending request */ - completion_list_t *c = - create_completion_entry(WATCHER_EVENT_XID,-1,0,0,0); + c = create_completion_entry(WATCHER_EVENT_XID,-1,0,0,0); c->buffer = bptr; c->c.watcher_result = collectWatchers(zh, type, path); @@ -1844,9 +1864,10 @@ if (cptr->c.void_result != SYNCHRONOUS_MARKER) { if(hdr.xid == PING_XID){ + int elapsed = 0; struct timeval now; gettimeofday(&now, 0); - int elapsed = calculate_interval(&zh->last_ping, &now); + elapsed = calculate_interval(&zh->last_ping, &now); LOG_DEBUG(("Got ping response in %d ms", elapsed)); // Nothing to do with a ping response @@ -2175,9 +2196,14 @@ } static int isValidPath(const char* path, const int flags) { + int len = 0; + char lastc = '/'; + char c; + int i = 0; + if (path == 0) return 0; - const int len = strlen(path); + len = strlen(path); if (len == 0) return 0; if (path[0] != '/') @@ -2187,9 +2213,7 @@ if (path[len - 1] == '/' && !(flags & ZOO_SEQUENCE)) return 0; - char lastc = '/'; - char c; - int i = 1; + i = 1; for (; i < len; lastc = path[i], i++) { c = path[i]; Modified: hadoop/zookeeper/trunk/src/c/tests/TestZookeeperInit.cc URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/tests/TestZookeeperInit.cc?rev=903032&r1=903031&r2=903032&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/c/tests/TestZookeeperInit.cc (original) +++ hadoop/zookeeper/trunk/src/c/tests/TestZookeeperInit.cc Tue Jan 26 01:06:33 2010 @@ -168,6 +168,27 @@ CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port)); } } + void testMultipleAddressWithSpace() + { + const string EXPECTED_HOST("127.0.0.1:2121, 127.0.0.2:3434"); + const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}}; + const int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS); + + zoo_deterministic_conn_order(1); + zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs_count); + + for(int i=0;i<zh->addrs_count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[i]; + CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0); + if(i<1) + CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port)); + else + CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port)); + } + } void testNullAddressString() { zh=zookeeper_init(NULL,0,0,0,0,0);