diff --git a/.travis.yml b/.travis.yml index 7938cc18..195de337 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "8" - "10" - "12" os: diff --git a/Dockerfile.node b/Dockerfile.node index f1445d99..726fcbc9 100644 --- a/Dockerfile.node +++ b/Dockerfile.node @@ -1,4 +1,4 @@ -FROM node:10 +FROM node:12 WORKDIR /app diff --git a/deps/apache-zookeeper-3.5.5.tar.gz b/deps/apache-zookeeper-3.5.5.tar.gz deleted file mode 100644 index 48c2b077..00000000 Binary files a/deps/apache-zookeeper-3.5.5.tar.gz and /dev/null differ diff --git a/deps/apache-zookeeper-3.5.6.tar.gz b/deps/apache-zookeeper-3.5.6.tar.gz new file mode 100644 index 00000000..ab89140b Binary files /dev/null and b/deps/apache-zookeeper-3.5.6.tar.gz differ diff --git a/example.js b/example.js deleted file mode 100755 index 73d525e6..00000000 --- a/example.js +++ /dev/null @@ -1,38 +0,0 @@ -/* eslint-disable no-bitwise,no-console */ -const { constants, ZooKeeper } = require('./lib/index'); - -function onCreate(client, rc, error, path) { - if (rc !== 0) { - console.log(`zk node create result: ${rc}, error: '${error}', path=${path}`); - } else { - console.log(`created zk node ${path}`); - - process.nextTick(() => { - client.close(); - }); - } -} - -function onConnect(client, err) { - if (err) { - throw err; - } - - console.log(`zk session established, id=${client.client_id}`); - client.a_create('/node.js1', 'some value', constants.ZOO_SEQUENCE | constants.ZOO_EPHEMERAL, onCreate.bind(null, client)); -} - -const zk = new ZooKeeper({ - connect: '127.0.0.1:2181', - timeout: 200000, - debug_level: constants.ZOO_LOG_LEVEL_DEBUG, - host_order_deterministic: false, -}); - -zk.setLogger(console.log); - -try { - zk.connect(onConnect.bind(null, zk)); -} catch (e) { - console.error(e); -} diff --git a/patches/CMakeLists.txt b/patches/CMakeLists.txt index 47b161b7..ae6def5f 100644 --- a/patches/CMakeLists.txt +++ b/patches/CMakeLists.txt @@ -51,8 +51,7 @@ set(zookeeper_sources src/recordio.c generated/zookeeper.jute.c src/zk_log.c - src/zk_hashtable.c - src/addrvec.c) + src/zk_hashtable.c) list(APPEND zookeeper_sources src/st_adaptor.c) diff --git a/patches/ZOOKEEPER-3078.patch b/patches/ZOOKEEPER-3078.patch deleted file mode 100644 index 272140c3..00000000 --- a/patches/ZOOKEEPER-3078.patch +++ /dev/null @@ -1,40 +0,0 @@ -Index: zookeeper/src/zookeeper.c -=================================================================== ---- zookeeper/src/zookeeper.c -+++ zookeeper/src/zookeeper.c -@@ -253,8 +253,6 @@ static void cleanup_bufs(zhandle_t *zh,int callCompletion,int rc); - static int disable_conn_permute=0; // permute enabled by default - static struct sockaddr_storage *addr_rw_server = 0; - --static __attribute__((unused)) void print_completion_queue(zhandle_t *zh); -- - static void *SYNCHRONOUS_MARKER = (void*)&SYNCHRONOUS_MARKER; - static int isValidPath(const char* path, const int flags); - -@@ -2530,26 +2528,6 @@ int api_epilog(zhandle_t *zh,int rc) - return rc; - } - --static __attribute__((unused)) void print_completion_queue(zhandle_t *zh) --{ -- completion_list_t* cptr; -- -- if(logLevelsent_requests.head==0) { -- fprintf(LOGSTREAM,"empty\n"); -- return; -- } -- -- cptr=zh->sent_requests.head; -- while(cptr){ -- fprintf(LOGSTREAM,"%d,",cptr->xid); -- cptr=cptr->next; -- } -- fprintf(LOGSTREAM,"end\n"); --} -- - //#ifdef THREADED - // IO thread queues session events to be processed by the completion thread - static int queue_session_event(zhandle_t *zh, int state) diff --git a/scripts/build.js b/scripts/build.js index 51381cae..829f0ae9 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -33,19 +33,14 @@ if (env.isVerbose) { shell.cd(`${env.sourceFolder}`); if (env.isWindows) { - let flags = '-DWANT_SYNCAPI=OFF '; - let output = ''; - - if (!env.isVerbose) { - flags = ''; - output = ' > NUL'; - } - exec(`cmake ${flags}-DCMAKE_GENERATOR_PLATFORM=${process.arch} .${output}`); + const output = env.isVerbose ? '' : ' > NUL'; + exec(`cmake -DWANT_SYNCAPI=OFF -DCMAKE_GENERATOR_PLATFORM=${process.arch} .${output}`); exec(`cmake --build .${output}`); } else { const flags = '-w'; let configureCmd = `./configure CFLAGS='${flags}' --without-syncapi --disable-shared --with-pic --without-cppunit`; let makeCmd = 'make'; + if (!env.isVerbose) { configureCmd += ' --enable-silent-rules --quiet'; makeCmd += ' --no-print-directory --quiet'; diff --git a/scripts/env.js b/scripts/env.js index 9fd18b71..a32528bc 100644 --- a/scripts/env.js +++ b/scripts/env.js @@ -22,14 +22,23 @@ const isWindows = process.platform.toLowerCase().includes('win32'); const isSunOs = process.platform.toLowerCase().includes('sunos'); const rootFolder = setRoot({ isWindows }); -// Don't forget to also update the sha1sum variable when upgrading the Zookeeper version -const zookeeperVersion = '3.5.5'; -const downloadedFolderName = `apache-zookeeper-${zookeeperVersion}`; +let zookeeperVersion; +let downloadedFolderName; +let sha512sum; const suffix = '.tar.gz'; -const downloadedFileName = `${downloadedFolderName}${suffix}`; - -// Update the checksum when upgrading the Zookeeper version -const sha512sum = `4e22df899a83ca3cc15f6d94daadb1a8631fb4108e67b4f56d1f4fcf95f10f89c8ff1fb8a7c84799a3856d8803a8db1e1f2f3fe1b7dc0d6cedf485ef90fd212d ${downloadedFileName}`; +let downloadedFileName; + +if (isWindows) { + zookeeperVersion = '3.4.14'; + downloadedFolderName = `zookeeper-${zookeeperVersion}`; + downloadedFileName = `${downloadedFolderName}${suffix}`; + sha512sum = `b2e03d95f8cf18b97a46e2f53871cef5a5da9d5d80b97009375aed7fb35368c440ca944c7e8b64efabbc065f6fb98bb86239f7c1491f0490efc71876d5a7f424 ${downloadedFileName}`; +} else { + zookeeperVersion = '3.5.6'; + downloadedFolderName = `apache-zookeeper-${zookeeperVersion}`; + downloadedFileName = `${downloadedFolderName}${suffix}`; + sha512sum = `7f45817cbbc42aec5a7817fa2ae99656128e666dc58ace23d86bcfc5ca0dc49e418d1a7d1f082ad80ccb916f9f1b490167d16f836886af1a56fbcf720ad3b9d0 ${downloadedFileName}`; +} const variables = { rootFolder, diff --git a/scripts/prepublish.js b/scripts/prepublish.js index 6d57d2eb..3b4fee65 100644 --- a/scripts/prepublish.js +++ b/scripts/prepublish.js @@ -53,7 +53,7 @@ function validateFile(fileName) { } if (res !== env.sha512sum) { - throw new Error(`Wrong sha512 for ${fileName}! Expected "${env.sha512sum}", got "${res}".`); + throw new Error(`Wrong sha for ${fileName}! Expected "${env.sha512sum}", got "${res}".`); } } @@ -89,8 +89,6 @@ function applyPatches() { shell.cp(`${env.patchesFolder}/${cmakeFile}`, `${env.sourceFolder}/${cmakeFile}`); } } else { - exec(`patch -p0 < ${env.patchesFolder}/ZOOKEEPER-3078.patch`); - decompress(`${env.patchesFolder}/autoreconf.tar.gz`, `${env.patchesFolder}`, { plugins: [ decompressTargz(), diff --git a/src/node-zk.cpp b/src/node-zk.cpp index 3d82bda4..8d7c5139 100644 --- a/src/node-zk.cpp +++ b/src/node-zk.cpp @@ -254,7 +254,7 @@ class ZooKeeper: public Nan::ObjectWrap { } static void New(const Nan::FunctionCallbackInfo& info) { - LOG_DEBUG(NULL, "New: creating a new ZooKeeper"); + LOG_DEBUG("New: creating a new ZooKeeper"); ZooKeeper *zk = new ZooKeeper(); zk->Wrap(info.This()); @@ -264,7 +264,7 @@ class ZooKeeper: public Nan::ObjectWrap { void yield () { if (is_closed) { - LOG_DEBUG(NULL, "yield: was closed"); + LOG_DEBUG("yield: was closed"); return; } @@ -283,7 +283,7 @@ class ZooKeeper: public Nan::ObjectWrap { } if (rc) { - LOG_ERROR(NULL, "yield:zookeeper_interest returned error: %d - %s\n", rc, zerror(rc)); + LOG_ERROR("yield:zookeeper_interest returned error: %d - %s\n", rc, zerror(rc)); return; } @@ -298,7 +298,7 @@ class ZooKeeper: public Nan::ObjectWrap { int64_t delay = tv.tv_sec * 1000 + tv.tv_usec / 1000.; int events = (interest & ZOOKEEPER_READ ? UV_READABLE : 0) | (interest & ZOOKEEPER_WRITE ? UV_WRITABLE : 0); - LOG_DEBUG(NULL, "Interest in (fd=%i, read=%s, write=%s, timeout=%d)", + LOG_DEBUG("Interest in (fd=%i, read=%s, write=%s, timeout=%d)", fd, events & UV_READABLE ? "true" : "false", events & UV_WRITABLE ? "true" : "false", @@ -313,10 +313,10 @@ class ZooKeeper: public Nan::ObjectWrap { zk_io = NULL; } - LOG_DEBUG(NULL, "yield: creating a new poll handle for %lp", this); + LOG_DEBUG("yield: creating a new poll handle for %lp", this); zk_io = (uv_poll_t*)malloc(sizeof(uv_poll_t)); if (!zk_io) { - LOG_ERROR(NULL, "Failed to malloc memory for uv_poll_t"); + LOG_ERROR("Failed to malloc memory for uv_poll_t"); return; } @@ -328,14 +328,14 @@ class ZooKeeper: public Nan::ObjectWrap { #endif } - LOG_DEBUG(NULL, "yield: starting poll for %lp from thread %lp", this); + LOG_DEBUG("yield: starting poll for %lp from thread %lp", this); uv_poll_start(zk_io, events, &zk_io_cb); uv_timer_start(&zk_timer, &zk_timer_cb, delay, delay); } static void zk_io_cb (uv_poll_t *w, int status, int revents) { - LOG_DEBUG(NULL, "zk_io_cb fired, status: %d, revents: %d", status, revents); + LOG_DEBUG("zk_io_cb fired, status: %d, revents: %d", status, revents); ZooKeeper *zk = static_cast(w->data); int events; @@ -352,13 +352,13 @@ class ZooKeeper: public Nan::ObjectWrap { zk->noResponseCounter = 0; } else if (rc == ZNOTHING) { zk->noResponseCounter++; - LOG_WARN(NULL, "yield:zookeeper_process has returned no response %d times\n", zk->noResponseCounter); + LOG_WARN("yield:zookeeper_process has returned no response %d times\n", zk->noResponseCounter); } else { - LOG_ERROR(NULL, "yield:zookeeper_process returned an error: %d - %s\n", rc, zerror(rc)); + LOG_ERROR("yield:zookeeper_process returned an error: %d - %s\n", rc, zerror(rc)); } if (zk->noResponseCounter > 10) { - LOG_ERROR(NULL, "yield:zookeeper_process returned no response too many times: %d\n", zk->noResponseCounter); + LOG_ERROR("yield:zookeeper_process returned no response too many times: %d\n", zk->noResponseCounter); zk->realClose(ZOO_EXPIRED_SESSION_STATE); return; } @@ -372,7 +372,7 @@ class ZooKeeper: public Nan::ObjectWrap { #else static void zk_timer_cb (uv_timer_t *w, int status) { #endif - LOG_DEBUG(NULL, "zk_timer_cb fired"); + LOG_DEBUG("zk_timer_cb fired"); ZooKeeper *zk = static_cast(w->data); int64_t now = uv_now(uv_default_loop()); @@ -380,7 +380,7 @@ class ZooKeeper: public Nan::ObjectWrap { // if last_activity + tv.tv_sec is older than now, we did time out if (timeout < now) { - LOG_DEBUG(NULL, "ping timer went off"); + LOG_DEBUG("ping timer went off"); // timeout occurred, take action zk->yield (); } else { @@ -390,7 +390,7 @@ class ZooKeeper: public Nan::ObjectWrap { int64_t delay = timeout - now + 1; uv_timer_start(w, &zk_timer_cb, delay, delay); - LOG_DEBUG(NULL, "delaying ping timer by %lu", delay); + LOG_DEBUG("delaying ping timer by %lu", delay); } } @@ -404,7 +404,7 @@ class ZooKeeper: public Nan::ObjectWrap { myid = *client_id; zhandle = zookeeper_init(hostPort, main_watcher, session_timeout, &myid, this, 0); if (!zhandle) { - LOG_ERROR(NULL, "zookeeper_init returned 0!"); + LOG_ERROR("zookeeper_init returned 0!"); return false; } Ref(); @@ -461,7 +461,7 @@ class ZooKeeper: public Nan::ObjectWrap { static void main_watcher (zhandle_t *zzh, int type, int state, const char *path, void* context) { Nan::HandleScope scope; - LOG_DEBUG(NULL, "main watcher event: type=%d, state=%d, path=%s", type, state, (path ? path: "null")); + LOG_DEBUG("main watcher event: type=%d, state=%d, path=%s", type, state, (path ? path: "null")); ZooKeeper *zk = static_cast(context); if (type == ZOO_SESSION_EVENT) { @@ -471,10 +471,10 @@ class ZooKeeper: public Nan::ObjectWrap { } else if (state == ZOO_CONNECTING_STATE) { zk->DoEmitPath (Nan::New(on_connecting), path); } else if (state == ZOO_AUTH_FAILED_STATE) { - LOG_ERROR(NULL, "Authentication failure. Shutting down...\n"); + LOG_ERROR("Authentication failure. Shutting down...\n"); zk->realClose(ZOO_AUTH_FAILED_STATE); } else if (state == ZOO_EXPIRED_SESSION_STATE) { - LOG_ERROR(NULL, "Session expired. Shutting down...\n"); + LOG_ERROR("Session expired. Shutting down...\n"); zk->realClose(ZOO_EXPIRED_SESSION_STATE); } } else if (type == ZOO_CREATED_EVENT) { @@ -488,7 +488,7 @@ class ZooKeeper: public Nan::ObjectWrap { } else if (type == ZOO_NOTWATCHING_EVENT) { zk->DoEmitPath(Nan::New(on_event_notwatching), path); } else { - LOG_WARN(NULL, "Unknonwn watcher event type %s",type); + LOG_WARN("Unknonwn watcher event type %s",type); } } @@ -530,10 +530,10 @@ class ZooKeeper: public Nan::ObjectWrap { if (path != 0) { str = LOCAL_STRING(path); - LOG_DEBUG(NULL, "calling Emit(%s, path='%s')", *Nan::Utf8String(event_name), path); + LOG_DEBUG("calling Emit(%s, path='%s')", *Nan::Utf8String(event_name), path); } else { str = Nan::Undefined(); - LOG_DEBUG(NULL, "calling Emit(%s, path=null)", *Nan::Utf8String(event_name)); + LOG_DEBUG("calling Emit(%s, path=null)", *Nan::Utf8String(event_name)); } this->DoEmit(event_name, str); @@ -629,7 +629,7 @@ class ZooKeeper: public Nan::ObjectWrap { value = "null"; } - LOG_DEBUG(NULL, "rc=%d, rc_string=%s, path=%s, data=%lp", rc, zerror(rc), value, cb); + LOG_DEBUG("rc=%d, rc_string=%s, path=%s, data=%lp", rc, zerror(rc), value, cb); CALLBACK_PROLOG(3); argv[2] = LOCAL_STRING(value); @@ -661,7 +661,7 @@ class ZooKeeper: public Nan::ObjectWrap { } CALLBACK_PROLOG(2); - LOG_DEBUG(NULL, "rc=%d, rc_string=%s", rc, zerror(rc)); + LOG_DEBUG("rc=%d, rc_string=%s", rc, zerror(rc)); CALLBACK_EPILOG(); free(d); @@ -701,7 +701,7 @@ class ZooKeeper: public Nan::ObjectWrap { static void stat_completion (int rc, const struct Stat *stat, const void *cb) { CALLBACK_PROLOG(3); - LOG_DEBUG(NULL, "rc=%d, rc_string=%s", rc, zerror(rc)); + LOG_DEBUG("rc=%d, rc_string=%s", rc, zerror(rc)); argv[2] = rc == ZOK ? zkk->createStatObject (stat) : Nan::Null().As(); CALLBACK_EPILOG(); @@ -725,7 +725,7 @@ class ZooKeeper: public Nan::ObjectWrap { static void data_completion (int rc, const char *value, int value_len, const struct Stat *stat, const void *cb) { CALLBACK_PROLOG(4); - LOG_DEBUG(NULL, "rc=%d, rc_string=%s, value=%.*s", rc, zerror(rc), value_len, value); + LOG_DEBUG("rc=%d, rc_string=%s, value=%.*s", rc, zerror(rc), value_len, value); argv[2] = stat != 0 ? zkk->createStatObject (stat) : Nan::Null().As(); @@ -778,7 +778,7 @@ class ZooKeeper: public Nan::ObjectWrap { static void strings_completion (int rc, const struct String_vector *strings, const void *cb) { CALLBACK_PROLOG(3); - LOG_DEBUG(NULL, "rc=%d, rc_string=%s", rc, zerror(rc)); + LOG_DEBUG("rc=%d, rc_string=%s", rc, zerror(rc)); if (strings != NULL) { Local ar = Nan::New((uint32_t)strings->count); @@ -813,7 +813,7 @@ class ZooKeeper: public Nan::ObjectWrap { static void strings_stat_completion (int rc, const struct String_vector *strings, const struct Stat *stat, const void *cb) { CALLBACK_PROLOG(4); - LOG_DEBUG(NULL, "rc=%d, rc_string=%s", rc, zerror(rc)); + LOG_DEBUG("rc=%d, rc_string=%s", rc, zerror(rc)); if (strings != NULL) { Local ar = Nan::New((uint32_t)strings->count); @@ -943,7 +943,7 @@ class ZooKeeper: public Nan::ObjectWrap { } static void acl_completion (int rc, struct ACL_vector *acl, struct Stat *stat, const void *cb) { - LOG_DEBUG(NULL, "rc=%d, rc_string=%s, acl_vector=%lp", rc, zerror(rc), acl); + LOG_DEBUG("rc=%d, rc_string=%s, acl_vector=%lp", rc, zerror(rc), acl); CALLBACK_PROLOG(4); argv[2] = acl != NULL ? zkk->createAclObject(acl) : Nan::Null().As(); @@ -999,15 +999,15 @@ class ZooKeeper: public Nan::ObjectWrap { } if (zhandle) { - LOG_DEBUG(NULL, "call zookeeper_close(%lp)", zhandle); + LOG_DEBUG("call zookeeper_close(%lp)", zhandle); zookeeper_close(zhandle); zhandle = 0; - LOG_DEBUG(NULL, "zookeeper_close() returned"); + LOG_DEBUG("zookeeper_close() returned"); if (zk_io) { int rc = uv_poll_stop(zk_io); - LOG_DEBUG(NULL, "zookeeper_close(%lp) uv_poll_stop result: %d", this, rc); + LOG_DEBUG("zookeeper_close(%lp) uv_poll_stop result: %d", this, rc); uv_close((uv_handle_t*) zk_io, delete_on_close); zk_io = NULL; @@ -1036,7 +1036,7 @@ class ZooKeeper: public Nan::ObjectWrap { virtual ~ZooKeeper() { //realClose (); - LOG_INFO(NULL, "ZooKeeper destructor invoked"); + LOG_INFO("ZooKeeper destructor invoked"); } #ifdef WIN32 diff --git a/src/zk_log.h b/src/zk_log.h index 56a9b73a..b0effb8e 100644 --- a/src/zk_log.h +++ b/src/zk_log.h @@ -16,6 +16,9 @@ * limitations under the License. */ +#include +#include + #ifndef ZK_LOG_H_ #define ZK_LOG_H_ @@ -26,22 +29,25 @@ extern "C" { #endif extern ZOOAPI ZooLogLevel logLevel; -#define LOGCALLBACK(_zh) zoo_get_log_callback(_zh) #define LOGSTREAM NULL -#define LOG_ERROR(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_ERROR) \ - log_message(_cb, ZOO_LOG_LEVEL_ERROR, __LINE__, __func__, __VA_ARGS__) -#define LOG_WARN(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_WARN) \ - log_message(_cb, ZOO_LOG_LEVEL_WARN, __LINE__, __func__, __VA_ARGS__) -#define LOG_INFO(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_INFO) \ - log_message(_cb, ZOO_LOG_LEVEL_INFO, __LINE__, __func__, __VA_ARGS__) -#define LOG_DEBUG(_cb, ...) if(logLevel==ZOO_LOG_LEVEL_DEBUG) \ - log_message(_cb, ZOO_LOG_LEVEL_DEBUG, __LINE__, __func__, __VA_ARGS__) - -ZOOAPI void log_message(log_callback_fn callback, ZooLogLevel curLevel, - int line, const char* funcName, const char* format, ...); - -FILE* zoo_get_log_stream(); +#define LOG_ERROR(...) if(logLevel>=ZOO_LOG_LEVEL_ERROR) \ + WriteFormatted(__VA_ARGS__) +#define LOG_WARN(...) if(logLevel>=ZOO_LOG_LEVEL_WARN) \ + WriteFormatted(__VA_ARGS__) +#define LOG_INFO(...) if(logLevel>=ZOO_LOG_LEVEL_INFO) \ + WriteFormatted(__VA_ARGS__) +#define LOG_DEBUG(...) if(logLevel==ZOO_LOG_LEVEL_DEBUG) \ + WriteFormatted(__VA_ARGS__) + + +void WriteFormatted(const char * format, ...) +{ + va_list args; + va_start(args, format); + vprintf(format, args); + va_end(args); +} #ifdef __cplusplus }