[dm-devel] [PATCH v2 31/48] multipathd: uxlsnr: add timeout handling
Benjamin Marzinski
bmarzins at redhat.com
Thu Nov 25 01:23:07 UTC 2021
On Thu, Nov 18, 2021 at 11:58:23PM +0100, mwilck at suse.com wrote:
> From: Martin Wilck <mwilck at suse.com>
>
> Our ppoll() call needs to wake up when a client request times out.
> This logic can be added by determining the first client that's about
> to time out. The logic in handle_client() will then cause a timeout
> reply to be sent to the client. This is more client-friendly
> as the client timing out without receiving a reply.
>
Reviewed-by: Benjamin Marzinski <bmarzins at redhat.com>
> Signed-off-by: Martin Wilck <mwilck at suse.com>
> ---
> multipathd/uxlsnr.c | 57 +++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 52 insertions(+), 5 deletions(-)
>
> diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
> index bf9780d..45fe7b5 100644
> --- a/multipathd/uxlsnr.c
> +++ b/multipathd/uxlsnr.c
> @@ -306,6 +306,35 @@ static void handle_inotify(int fd, struct watch_descriptors *wds)
> }
>
> static const struct timespec ts_zero = { .tv_sec = 0, };
> +static const struct timespec ts_max = { .tv_sec = LONG_MAX, .tv_nsec = 999999999 };
> +
> +/* call with clients lock held */
> +static struct timespec *__get_soonest_timeout(struct timespec *ts)
> +{
> + struct timespec ts_min = ts_max, now;
> + bool any = false;
> + struct client *c;
> +
> + list_for_each_entry(c, &clients, node) {
> + if (timespeccmp(&c->expires, &ts_zero) != 0 &&
> + timespeccmp(&c->expires, &ts_min) < 0) {
> + ts_min = c->expires;
> + any = true;
> + }
> + }
> +
> + if (!any)
> + return NULL;
> +
> + get_monotonic_time(&now);
> + timespecsub(&ts_min, &now, ts);
> + if (timespeccmp(ts, &ts_zero) < 0)
> + *ts = ts_zero;
> +
> + condlog(4, "%s: next client expires in %ld.%03lds", __func__,
> + (long)ts->tv_sec, ts->tv_nsec / 1000000);
> + return ts;
> +}
>
> /* call with clients lock held */
> static bool __need_vecs_lock(void)
> @@ -532,6 +561,24 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
> }
> }
>
> +static void check_timeout(struct client *c)
> +{
> + struct timespec now;
> +
> + if (timespeccmp(&c->expires, &ts_zero) == 0)
> + return;
> +
> + get_monotonic_time(&now);
> + if (timespeccmp(&c->expires, &now) > 0)
> + return;
> +
> + condlog(2, "%s: cli[%d]: timed out at %ld.%03ld", __func__,
> + c->fd, (long)c->expires.tv_sec, c->expires.tv_nsec / 1000000);
> +
> + c->error = -ETIMEDOUT;
> + set_client_state(c, CLT_SEND);
> +}
> +
> static void handle_client(struct client *c, struct vectors *vecs, short revents)
> {
> if (revents & (POLLHUP|POLLERR)) {
> @@ -539,6 +586,7 @@ static void handle_client(struct client *c, struct vectors *vecs, short revents)
> return;
> }
>
> + check_timeout(c);
> while (client_state_machine(c, vecs, revents) == STM_CONT);
> }
>
> @@ -580,6 +628,7 @@ void *uxsock_listen(long ux_sock, void *trigger_data)
> while (1) {
> struct client *c, *tmp;
> int i, n_pfds, poll_count, num_clients;
> + struct timespec __timeout, *timeout;
>
> /* setup for a poll */
> pthread_mutex_lock(&client_lock);
> @@ -647,10 +696,12 @@ void *uxsock_listen(long ux_sock, void *trigger_data)
> break;
> }
> n_pfds = i;
> + timeout = __get_soonest_timeout(&__timeout);
> +
> pthread_cleanup_pop(1);
>
> /* most of our life is spent in this call */
> - poll_count = ppoll(polls, n_pfds, NULL, &mask);
> + poll_count = ppoll(polls, n_pfds, timeout, &mask);
>
> handle_signals(false);
> if (poll_count == -1) {
> @@ -665,10 +716,6 @@ void *uxsock_listen(long ux_sock, void *trigger_data)
> break;
> }
>
> - if (poll_count == 0) {
> - handle_signals(true);
> - continue;
> - }
> if (polls[POLLFD_IDLE].fd != -1 &&
> polls[POLLFD_IDLE].revents & POLLIN)
> drain_idle_fd(idle_fd);
> --
> 2.33.1
More information about the dm-devel
mailing list