From owner-svn-src-head@FreeBSD.ORG Sun Aug 29 22:55:22 2010 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F29E810656BB; Sun, 29 Aug 2010 22:55:21 +0000 (UTC) (envelope-from pjd@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id E21988FC22; Sun, 29 Aug 2010 22:55:21 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o7TMtLrM076827; Sun, 29 Aug 2010 22:55:21 GMT (envelope-from pjd@svn.freebsd.org) Received: (from pjd@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o7TMtLHY076825; Sun, 29 Aug 2010 22:55:21 GMT (envelope-from pjd@svn.freebsd.org) Message-Id: <201008292255.o7TMtLHY076825@svn.freebsd.org> From: Pawel Jakub Dawidek Date: Sun, 29 Aug 2010 22:55:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r211981 - head/sbin/hastd X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Aug 2010 22:55:22 -0000 Author: pjd Date: Sun Aug 29 22:55:21 2010 New Revision: 211981 URL: http://svn.freebsd.org/changeset/base/211981 Log: - Move functionality responsible for checking one connection to separate function to make code more readable. - Be sure not to reconnect too often in case of signal delivery, etc. MFC after: 2 weeks Obtained from: Wheel Systems Sp. z o.o. http://www.wheelsystems.com Modified: head/sbin/hastd/primary.c Modified: head/sbin/hastd/primary.c ============================================================================== --- head/sbin/hastd/primary.c Sun Aug 29 22:22:15 2010 (r211980) +++ head/sbin/hastd/primary.c Sun Aug 29 22:55:21 2010 (r211981) @@ -1903,6 +1903,67 @@ keepalive_send(struct hast_resource *res pjdlog_debug(2, "keepalive_send: Request sent."); } +static void +guard_one(struct hast_resource *res, unsigned int ncomp) +{ + struct proto_conn *in, *out; + + if (!ISREMOTE(ncomp)) + return; + + rw_rlock(&hio_remote_lock[ncomp]); + + if (!real_remote(res)) { + rw_unlock(&hio_remote_lock[ncomp]); + return; + } + + if (ISCONNECTED(res, ncomp)) { + assert(res->hr_remotein != NULL); + assert(res->hr_remoteout != NULL); + keepalive_send(res, ncomp); + } + + if (ISCONNECTED(res, ncomp)) { + assert(res->hr_remotein != NULL); + assert(res->hr_remoteout != NULL); + rw_unlock(&hio_remote_lock[ncomp]); + pjdlog_debug(2, "remote_guard: Connection to %s is ok.", + res->hr_remoteaddr); + return; + } + + assert(res->hr_remotein == NULL); + assert(res->hr_remoteout == NULL); + /* + * Upgrade the lock. It doesn't have to be atomic as no other thread + * can change connection status from disconnected to connected. + */ + rw_unlock(&hio_remote_lock[ncomp]); + pjdlog_debug(2, "remote_guard: Reconnecting to %s.", + res->hr_remoteaddr); + in = out = NULL; + if (init_remote(res, &in, &out)) { + rw_wlock(&hio_remote_lock[ncomp]); + assert(res->hr_remotein == NULL); + assert(res->hr_remoteout == NULL); + assert(in != NULL && out != NULL); + res->hr_remotein = in; + res->hr_remoteout = out; + rw_unlock(&hio_remote_lock[ncomp]); + pjdlog_info("Successfully reconnected to %s.", + res->hr_remoteaddr); + sync_start(); + } else { + /* Both connections should be NULL. */ + assert(res->hr_remotein == NULL); + assert(res->hr_remoteout == NULL); + assert(in == NULL && out == NULL); + pjdlog_debug(2, "remote_guard: Reconnect to %s failed.", + res->hr_remoteaddr); + } +} + /* * Thread guards remote connections and reconnects when needed, handles * signals, etc. @@ -1911,11 +1972,12 @@ static void * guard_thread(void *arg) { struct hast_resource *res = arg; - struct proto_conn *in, *out; unsigned int ii, ncomps; + time_t lastcheck, now; int timeout; ncomps = HAST_NCOMPONENTS; + lastcheck = time(NULL); for (;;) { if (sigexit_received) { @@ -1930,63 +1992,24 @@ guard_thread(void *arg) if (sigchld_received) sigchld_received = false; - timeout = KEEPALIVE_SLEEP; pjdlog_debug(2, "remote_guard: Checking connections."); mtx_lock(&hio_guard_lock); + timeout = KEEPALIVE_SLEEP; for (ii = 0; ii < ncomps; ii++) { - if (!ISREMOTE(ii)) - continue; - rw_rlock(&hio_remote_lock[ii]); - if (ISCONNECTED(res, ii)) { - assert(res->hr_remotein != NULL); - assert(res->hr_remoteout != NULL); - keepalive_send(res, ii); + if (!ISCONNECTED(res, ii)) { + timeout = RECONNECT_SLEEP; + break; } - if (ISCONNECTED(res, ii)) { - assert(res->hr_remotein != NULL); - assert(res->hr_remoteout != NULL); - rw_unlock(&hio_remote_lock[ii]); - pjdlog_debug(2, - "remote_guard: Connection to %s is ok.", - res->hr_remoteaddr); - } else if (real_remote(res)) { - assert(res->hr_remotein == NULL); - assert(res->hr_remoteout == NULL); - /* - * Upgrade the lock. It doesn't have to be - * atomic as no other thread can change - * connection status from disconnected to - * connected. - */ - rw_unlock(&hio_remote_lock[ii]); - pjdlog_debug(2, - "remote_guard: Reconnecting to %s.", - res->hr_remoteaddr); - in = out = NULL; - if (init_remote(res, &in, &out)) { - rw_wlock(&hio_remote_lock[ii]); - assert(res->hr_remotein == NULL); - assert(res->hr_remoteout == NULL); - assert(in != NULL && out != NULL); - res->hr_remotein = in; - res->hr_remoteout = out; - rw_unlock(&hio_remote_lock[ii]); - pjdlog_info("Successfully reconnected to %s.", - res->hr_remoteaddr); - sync_start(); - } else { - /* Both connections should be NULL. */ - assert(res->hr_remotein == NULL); - assert(res->hr_remoteout == NULL); - assert(in == NULL && out == NULL); - pjdlog_debug(2, - "remote_guard: Reconnect to %s failed.", - res->hr_remoteaddr); + } + now = time(NULL); + if (lastcheck + timeout <= now) { + timeout = KEEPALIVE_SLEEP; + for (ii = 0; ii < ncomps; ii++) { + guard_one(res, ii); + if (!ISCONNECTED(res, ii)) timeout = RECONNECT_SLEEP; - } - } else { - rw_unlock(&hio_remote_lock[ii]); } + lastcheck = now; } /* Sleep only if a signal wasn't delivered in the meantime. */ if (!sigexit_received && !sighup_received && !sigchld_received)