From commits-return-8470-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Fri Jul 31 14:46:40 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mailroute1-lw-us.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 9FD90180647 for ; Fri, 31 Jul 2020 16:46:40 +0200 (CEST) Received: from mail.apache.org (localhost [127.0.0.1]) by mailroute1-lw-us.apache.org (ASF Mail Server at mailroute1-lw-us.apache.org) with SMTP id CA32C124E2F for ; Fri, 31 Jul 2020 14:46:39 +0000 (UTC) Received: (qmail 76144 invoked by uid 500); 31 Jul 2020 14:46:39 -0000 Mailing-List: contact commits-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list commits@zookeeper.apache.org Received: (qmail 76127 invoked by uid 99); 31 Jul 2020 14:46:39 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Jul 2020 14:46:39 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 5A3368242E; Fri, 31 Jul 2020 14:46:39 +0000 (UTC) Date: Fri, 31 Jul 2020 14:46:39 +0000 To: "commits@zookeeper.apache.org" Subject: [zookeeper] branch branch-3.5 updated: ZOOKEEPER-3885: add locking for watchers hashtables MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <159620679919.18302.16615663018138398225@gitbox.apache.org> From: symat@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: zookeeper X-Git-Refname: refs/heads/branch-3.5 X-Git-Reftype: branch X-Git-Oldrev: f94c3eb2c9e3ad4bd98e2b4b81a60cd6c8d50562 X-Git-Newrev: 17f2fbbca9680d97b80765731258652901f6952f X-Git-Rev: 17f2fbbca9680d97b80765731258652901f6952f X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. symat pushed a commit to branch branch-3.5 in repository https://gitbox.apache.org/repos/asf/zookeeper.git The following commit(s) were added to refs/heads/branch-3.5 by this push: new 17f2fbb ZOOKEEPER-3885: add locking for watchers hashtables 17f2fbb is described below commit 17f2fbbca9680d97b80765731258652901f6952f Author: Tudor Bosman AuthorDate: Fri Jul 31 13:42:18 2020 +0000 ZOOKEEPER-3885: add locking for watchers hashtables See the comments in the JIRA issue. Author: Tudor Bosman Reviewers: Mate Szalay-Beko , Damien Diederen , Enrico Olivelli Closes #1403 from tudor/htlocking1 (cherry picked from commit b776b2360ac282fc4eef1e86fcf185d7a6c3eae5) Signed-off-by: Mate Szalay-Beko --- .../zookeeper-client-c/src/mt_adaptor.c | 24 ++++++++++++++++++++-- .../zookeeper-client-c/src/st_adaptor.c | 10 +++++++++ .../zookeeper-client-c/src/zk_adaptor.h | 5 +++++ .../zookeeper-client-c/src/zookeeper.c | 12 +++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/mt_adaptor.c b/zookeeper-client/zookeeper-client-c/src/mt_adaptor.c index 38cced4..668c2a0 100644 --- a/zookeeper-client/zookeeper-client-c/src/mt_adaptor.c +++ b/zookeeper-client/zookeeper-client-c/src/mt_adaptor.c @@ -256,12 +256,13 @@ int adaptor_init(zhandle_t *zh) pthread_mutex_init(&zh->to_process.lock,0); pthread_mutex_init(&adaptor_threads->zh_lock,0); pthread_mutex_init(&adaptor_threads->reconfig_lock,0); - // to_send must be recursive mutex + pthread_mutex_init(&adaptor_threads->watchers_lock,0); + // to_send must be recursive mutex pthread_mutexattr_init(&recursive_mx_attr); pthread_mutexattr_settype(&recursive_mx_attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&zh->to_send.lock,&recursive_mx_attr); pthread_mutexattr_destroy(&recursive_mx_attr); - + pthread_mutex_init(&zh->sent_requests.lock,0); pthread_cond_init(&zh->sent_requests.cond,0); pthread_mutex_init(&zh->completions_to_process.lock,0); @@ -530,6 +531,25 @@ int unlock_reconfig(struct _zhandle *zh) } } +int lock_watchers(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_lock(&adaptor->watchers_lock); + } else { + return 0; + } +} +int unlock_watchers(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_unlock(&adaptor->watchers_lock); + } else { + return 0; + } +} + int enter_critical(zhandle_t* zh) { struct adaptor_threads *adaptor = zh->adaptor_priv; diff --git a/zookeeper-client/zookeeper-client-c/src/st_adaptor.c b/zookeeper-client/zookeeper-client-c/src/st_adaptor.c index 5e9a4ff..7b633bb 100644 --- a/zookeeper-client/zookeeper-client-c/src/st_adaptor.c +++ b/zookeeper-client/zookeeper-client-c/src/st_adaptor.c @@ -94,6 +94,16 @@ int unlock_reconfig(struct _zhandle *zh) return 0; } +int lock_watchers(struct _zhandle *zh) +{ + return 0; +} + +int unlock_watchers(struct _zhandle *zh) +{ + return 0; +} + int enter_critical(zhandle_t* zh) { return 0; diff --git a/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h b/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h index 97995e3..8bf525d 100644 --- a/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h +++ b/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h @@ -165,6 +165,7 @@ struct adaptor_threads { pthread_mutex_t lock; // ... and a lock pthread_mutex_t zh_lock; // critical section lock pthread_mutex_t reconfig_lock; // lock for reconfiguring cluster's ensemble + pthread_mutex_t watchers_lock; // lock for watcher operations #ifdef WIN32 SOCKET self_pipe[2]; #else @@ -293,6 +294,10 @@ int zoo_unlock_auth(zhandle_t *zh); int lock_reconfig(struct _zhandle *zh); int unlock_reconfig(struct _zhandle *zh); +// watchers hashtable lock +int lock_watchers(struct _zhandle *zh); +int unlock_watchers(struct _zhandle *zh); + // critical section guards int enter_critical(zhandle_t* zh); int leave_critical(zhandle_t* zh); diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 5c2fad6..4b467fb 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1934,9 +1934,11 @@ static int send_set_watches(zhandle_t *zh) int rc; req.relativeZxid = zh->last_zxid; + lock_watchers(zh); req.dataWatches.data = collect_keys(zh->active_node_watchers, (int*)&req.dataWatches.count); req.existWatches.data = collect_keys(zh->active_exist_watchers, (int*)&req.existWatches.count); req.childWatches.data = collect_keys(zh->active_child_watchers, (int*)&req.childWatches.count); + unlock_watchers(zh); // return if there are no pending watches if (!req.dataWatches.count && !req.existWatches.count && @@ -2605,7 +2607,9 @@ static int queue_session_event(zhandle_t *zh, int state) } /* We queued the buffer, so don't free it */ close_buffer_oarchive(&oa, 0); + lock_watchers(zh); cptr->c.watcher_result = collectWatchers(zh, ZOO_SESSION_EVENT, ""); + unlock_watchers(zh); queue_completion(&zh->completions_to_process, cptr, 0); if (process_async(zh->outstanding_sync)) { process_completions(zh); @@ -2911,7 +2915,9 @@ int zookeeper_process(zhandle_t *zh, int events) /* We are doing a notification, so there is no pending request */ c = create_completion_entry(zh, WATCHER_EVENT_XID,-1,0,0,0,0); c->buffer = bptr; + lock_watchers(zh); c->c.watcher_result = collectWatchers(zh, type, path); + unlock_watchers(zh); // We cannot free until now, otherwise path will become invalid deallocate_WatcherEvent(&evt); @@ -2966,8 +2972,10 @@ int zookeeper_process(zhandle_t *zh, int events) // Update last_zxid only when it is a request response zh->last_zxid = hdr.zxid; } + lock_watchers(zh); activateWatcher(zh, cptr->watcher, rc); deactivateWatcher(zh, cptr->watcher_deregistration, rc); + unlock_watchers(zh); if (cptr->c.void_result != SYNCHRONOUS_MARKER) { LOG_DEBUG(LOGCALLBACK(zh), "Queueing asynchronous response"); @@ -4257,19 +4265,23 @@ static int aremove_watches( goto done; } + lock_watchers(zh); if (!pathHasWatcher(zh, server_path, wtype, watcher, watcherCtx)) { rc = ZNOWATCHER; + unlock_watchers(zh); goto done; } if (local) { removeWatchers(zh, server_path, wtype, watcher, watcherCtx); + unlock_watchers(zh); #ifdef THREADED notify_sync_completion((struct sync_completion *)data); #endif rc = ZOK; goto done; } + unlock_watchers(zh); oa = create_buffer_oarchive(); rc = serialize_RequestHeader(oa, "header", &h);