s3: Slightly increase parallelism in g_lock
[abartlet/samba.git/.git] / source3 / lib / g_lock.c
index 8a44b22e11fb78e5b37d93fbbf7ffb71d8e86274..add670c154dee1b749c92c14bc258f96fd4cadba 100644 (file)
@@ -108,6 +108,34 @@ static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
                              (locks[i].lock_type & G_LOCK_PENDING) ?
                              "(pending)" : "(owner)"));
 
+               if (((locks[i].lock_type & G_LOCK_PENDING) == 0)
+                   && !process_exists(locks[i].pid)) {
+
+                       DEBUGADD(10, ("lock owner %s died -- discarding\n",
+                                     procid_str(talloc_tos(),
+                                                &locks[i].pid)));
+
+                       if (i < (num_locks-1)) {
+                               locks[i] = locks[num_locks-1];
+                       }
+                       num_locks -= 1;
+               }
+       }
+
+       *plocks = locks;
+       *pnum_locks = num_locks;
+       return true;
+}
+
+static void g_lock_cleanup(int *pnum_locks, struct g_lock_rec *locks)
+{
+       int i, num_locks;
+
+       num_locks = *pnum_locks;
+
+       DEBUG(10, ("g_lock_cleanup: %d locks\n", num_locks));
+
+       for (i=0; i<num_locks; i++) {
                if (process_exists(locks[i].pid)) {
                        continue;
                }
@@ -119,19 +147,18 @@ static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
                }
                num_locks -= 1;
        }
-
-       *plocks = locks;
        *pnum_locks = num_locks;
-       return true;
+       return;
 }
 
 static struct g_lock_rec *g_lock_addrec(TALLOC_CTX *mem_ctx,
                                        struct g_lock_rec *locks,
-                                       int num_locks,
+                                       int *pnum_locks,
                                        const struct server_id pid,
                                        enum g_lock_type lock_type)
 {
        struct g_lock_rec *result;
+       int num_locks = *pnum_locks;
 
        result = talloc_realloc(mem_ctx, locks, struct g_lock_rec,
                                num_locks+1);
@@ -141,6 +168,7 @@ static struct g_lock_rec *g_lock_addrec(TALLOC_CTX *mem_ctx,
 
        result[num_locks].pid = pid;
        result[num_locks].lock_type = lock_type;
+       *pnum_locks += 1;
        return result;
 }
 
@@ -149,10 +177,6 @@ static void g_lock_got_retry(struct messaging_context *msg,
                             uint32_t msg_type,
                             struct server_id server_id,
                             DATA_BLOB *data);
-static void g_lock_timedout(struct tevent_context *ev,
-                           struct tevent_timer *te,
-                           struct timeval current_time,
-                           void *private_data);
 
 static NTSTATUS g_lock_trylock(struct g_lock_ctx *ctx, const char *name,
                               enum g_lock_type lock_type)
@@ -225,7 +249,7 @@ again:
        if (our_index == -1) {
                /* First round, add ourself */
 
-               locks = g_lock_addrec(talloc_tos(), locks, num_locks,
+               locks = g_lock_addrec(talloc_tos(), locks, &num_locks,
                                      self, lock_type);
                if (locks == NULL) {
                        DEBUG(10, ("g_lock_addrec failed\n"));
@@ -241,7 +265,14 @@ again:
                locks[our_index].lock_type = lock_type;
        }
 
-       data = make_tdb_data((uint8_t *)locks, talloc_get_size(locks));
+       if (NT_STATUS_IS_OK(status) && ((lock_type & G_LOCK_PENDING) == 0)) {
+               /*
+                * Walk through the list of locks, search for dead entries
+                */
+               g_lock_cleanup(&num_locks, locks);
+       }
+
+       data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
        store_status = rec->store(rec, data, 0);
        if (!NT_STATUS_IS_OK(store_status)) {
                DEBUG(1, ("rec->store failed: %s\n",
@@ -267,7 +298,6 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
        NTSTATUS status;
        bool retry = false;
        struct timeval timeout_end;
-       struct timeval timeout_remaining;
        struct timeval time_now;
 
        DEBUG(10, ("Trying to acquire lock %d for %s\n", (int)lock_type,
@@ -302,10 +332,13 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
        timeout_end = timeval_sum(&time_now, &timeout);
 
        while (true) {
+#ifdef CLUSTER_SUPPORT
                fd_set _r_fds;
+#endif
                fd_set *r_fds = NULL;
                int max_fd = 0;
                int ret;
+               struct timeval select_timeout;
 
                status = g_lock_trylock(ctx, name, lock_type);
                if (NT_STATUS_IS_OK(status)) {
@@ -340,7 +373,7 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                 * for writing and some other process already holds a lock
                 * on the registry.tdb.
                 *
-                * So as a quick fix, we act a little corasely here: we do
+                * So as a quick fix, we act a little coarsely here: we do
                 * a select on the ctdb connection fd and when it is readable
                 * or we get EINTR, then we retry without actually parsing
                 * any ctdb packages or dispatching messages. This means that
@@ -362,12 +395,10 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                }
 #endif
 
-               time_now = timeval_current();
-               timeout_remaining = timeval_until(&time_now, &timeout_end);
+               select_timeout = timeval_set(60, 0);
 
                ret = sys_select(max_fd + 1, r_fds, NULL, NULL,
-                                &timeout_remaining);
-
+                                &select_timeout);
                if (ret == -1) {
                        if (errno != EINTR) {
                                DEBUG(1, ("error calling select: %s\n",
@@ -388,7 +419,7 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                                break;
                        } else {
                                DEBUG(10, ("select returned 0 but timeout not "
-                                          "not expired: strange - retrying\n"));
+                                          "not expired, retrying\n"));
                        }
                } else if (ret != 1) {
                        DEBUG(1, ("invalid return code of select: %d\n", ret));
@@ -404,7 +435,9 @@ NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
                 */
        }
 
+#ifdef CLUSTER_SUPPORT
 done:
+#endif
 
        if (!NT_STATUS_IS_OK(status)) {
                NTSTATUS unlock_status;
@@ -437,16 +470,6 @@ static void g_lock_got_retry(struct messaging_context *msg,
        *pretry = true;
 }
 
-static void g_lock_timedout(struct tevent_context *ev,
-                           struct tevent_timer *te,
-                           struct timeval current_time,
-                           void *private_data)
-{
-       bool *ptimedout = (bool *)private_data;
-       *ptimedout = true;
-       TALLOC_FREE(te);
-}
-
 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
                                    struct server_id pid)
 {
@@ -504,14 +527,26 @@ static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
                goto done;
        }
 
+       TALLOC_FREE(rec);
+
        if ((lock_type & G_LOCK_PENDING) == 0) {
+               int num_wakeups = 0;
+
                /*
-                * We've been the lock holder. Tell all others to retry.
+                * We've been the lock holder. Others to retry. Don't
+                * tell all others to avoid a thundering herd. In case
+                * this leads to a complete stall because we miss some
+                * processes, the loop in g_lock_lock tries at least
+                * once a minute.
                 */
+
                for (i=0; i<num_locks; i++) {
                        if ((locks[i].lock_type & G_LOCK_PENDING) == 0) {
                                continue;
                        }
+                       if (!process_exists(locks[i].pid)) {
+                               continue;
+                       }
 
                        /*
                         * Ping all waiters to retry
@@ -524,13 +559,22 @@ static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
                                          procid_str(talloc_tos(),
                                                     &locks[i].pid),
                                          nt_errstr(status)));
+                       } else {
+                               num_wakeups += 1;
+                       }
+                       if (num_wakeups > 5) {
+                               break;
                        }
                }
        }
 done:
+       /*
+        * For the error path, TALLOC_FREE(rec) as well. In the good
+        * path we have already freed it.
+        */
+       TALLOC_FREE(rec);
 
        TALLOC_FREE(locks);
-       TALLOC_FREE(rec);
        return status;
 }