You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Ruediger Pluem <rp...@apache.org> on 2015/03/05 17:14:42 UTC

error_time reset in proxy_util.c

I am currently hunting down an issue where a balancer member that is set to error is reused before the retry time runs out.
I think the reason is some race condition around line 2900 in proxy_util.c

    /*
     * Put the entire worker to error state if
     * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
     * Altrough some connections may be alive
     * no further connections to the worker could be made
     */
    if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
        !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
        worker->s->error_time = apr_time_now();
        worker->s->status |= PROXY_WORKER_IN_ERROR;
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
            "ap_proxy_connect_backend disabling worker for (%s) for %"
            APR_TIME_T_FMT "s",
            worker->s->hostname, apr_time_sec(worker->s->retry));
    }
    else {
        if (worker->s->retries) {
            /*
             * A worker came back. So here is where we need to
             * either reset all params to initial conditions or
             * apply some sort of aging
             */
        }
        worker->s->error_time = 0;
        worker->s->retries = 0;
    }

I suspect that the worker was already set to error by a parallel thread / process and hence
PROXY_WORKER_IS_USABLE(worker) is false and causes worker->s->error_time to be reset which causes the worker to be open
for retry immediately. This has been the case since r104624
(http://svn.apache.org/viewvc?view=revision&revision=r104624) 10,5 years ago and the commit messages offers no hint at
least to be why we reset these values.
Can anybody think of a good reason why we do this?
Another question is if we shouldn't do

worker->s->error_time = apr_time_now();

also in case the worker is already in error state to restart the retry clock as we just faced an error with connecting
to the backend.


Regards

Rüdiger

Re: error_time reset in proxy_util.c

Posted by Yann Ylavic <yl...@gmail.com>.
On Fri, Mar 6, 2015 at 5:46 PM, Yann Ylavic <yl...@gmail.com> wrote:
>
> Actually since r1650028 (in trunk), non balancer-member workers are
> not put in error state by default (PROXY_WORKER_IGNORE_ERRORS).
> This has not been backported (reviewed?) yet, but comes from PR 48388.
>
> So this should not affect them (by default), +1.

Let's say that if r1650028 should not be backported (as per PR 48388
discussion), you're patch is correct ;)

>
> Regards,
> Yann.

Re: error_time reset in proxy_util.c

Posted by Yann Ylavic <yl...@gmail.com>.
On Fri, Mar 6, 2015 at 5:24 PM, Plüm, Rüdiger, Vodafone Group
<ru...@vodafone.com> wrote:
>
>
>> -----Ursprüngliche Nachricht-----
>> Von: Yann Ylavic [mailto:ylavic.dev@gmail.com]
>> Gesendet: Freitag, 6. März 2015 17:02
>> An: httpd
>> Betreff: Re: error_time reset in proxy_util.c
>>
>>
>> If the worker is *not a BalancerMember*, couldn't we simply mark the
>> connection as close = 1 here, and continue with it until the end of
>> this request?
>> This is to avoid responding 503 whereas it succeed for this particular
>> request (since this is a race, the worker was put in error state a
>> very little time ago, not at ap_proxy_acquire_connection() time for
>> this request at least).
>
> IMHO the situation does not really differ between BalancerMember and non BalancerMembers.
> Another process / thread detected an error condition that might not be caused by
> a failed connect on TCP layer, but by a failonstatus / failontimeout by a previous
> request. We should respect this here as we would when this worker was marked as down
> a few moments earlier which would have prevented us to the connection at all.

Actually since r1650028 (in trunk), non balancer-member workers are
not put in error state by default (PROXY_WORKER_IGNORE_ERRORS).
This has not been backported (reviewed?) yet, but comes from PR 48388.

So this should not affect them (by default), +1.

Regards,
Yann.

AW: error_time reset in proxy_util.c

Posted by Plüm, Rüdiger, Vodafone Group <ru...@vodafone.com>.

> -----Ursprüngliche Nachricht-----
> Von: Yann Ylavic [mailto:ylavic.dev@gmail.com]
> Gesendet: Freitag, 6. März 2015 17:02
> An: httpd
> Betreff: Re: error_time reset in proxy_util.c
> 
> On Fri, Mar 6, 2015 at 4:25 PM, Ruediger Pluem <rp...@apache.org>
> wrote:
> >
> > Index: proxy_util.c
> > ===================================================================
> > --- proxy_util.c        (revision 1664261)
> > +++ proxy_util.c        (working copy)
> > @@ -2887,14 +2887,15 @@
> >
> >          connected    = 1;
> >      }
> > +    if (PROXY_WORKER_IS_USABLE(worker)) {
> >      /*
> >       * Put the entire worker to error state if
> >       * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
> >       * Altrough some connections may be alive
> >       * no further connections to the worker could be made
> >       */
> > -    if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
> > -        !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
> > +        if (!connected) {
> > +            if (!(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
> >          worker->s->error_time = apr_time_now();
> >          worker->s->status |= PROXY_WORKER_IN_ERROR;
> >          ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
> > @@ -2902,6 +2903,7 @@
> >              APR_TIME_T_FMT "s",
> >              worker->s->hostname, apr_time_sec(worker->s->retry));
> >      }
> > +        }
> >      else {
> >          if (worker->s->retries) {
> >              /*
> > @@ -2915,6 +2917,19 @@
> >      }
> >      return connected ? OK : DECLINED;
> >  }
> > +    else {
> > +        /*
> > +         * The worker is in error likely done by a different thread /
> process
> > +         * e.g. for a timeout or bad status. We should respect this
> and should
> > +         * not continue with a connection via this worker even if we
> got one.
> > +         */
> > +        if (connected) {
> > +            apr_socket_close(conn->sock );
> > +            conn->sock = NULL;
> > +        }
> > +        return DECLINED;
> 
> If the worker is *not a BalancerMember*, couldn't we simply mark the
> connection as close = 1 here, and continue with it until the end of
> this request?
> This is to avoid responding 503 whereas it succeed for this particular
> request (since this is a race, the worker was put in error state a
> very little time ago, not at ap_proxy_acquire_connection() time for
> this request at least).

IMHO the situation does not really differ between BalancerMember and non BalancerMembers.
Another process / thread detected an error condition that might not be caused by
a failed connect on TCP layer, but by a failonstatus / failontimeout by a previous
request. We should respect this here as we would when this worker was marked as down
a few moments earlier which would have prevented us to the connection at all. 

Regards

Rüdiger

Re: error_time reset in proxy_util.c

Posted by Yann Ylavic <yl...@gmail.com>.
On Fri, Mar 6, 2015 at 4:25 PM, Ruediger Pluem <rp...@apache.org> wrote:
>
> Index: proxy_util.c
> ===================================================================
> --- proxy_util.c        (revision 1664261)
> +++ proxy_util.c        (working copy)
> @@ -2887,14 +2887,15 @@
>
>          connected    = 1;
>      }
> +    if (PROXY_WORKER_IS_USABLE(worker)) {
>      /*
>       * Put the entire worker to error state if
>       * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
>       * Altrough some connections may be alive
>       * no further connections to the worker could be made
>       */
> -    if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
> -        !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
> +        if (!connected) {
> +            if (!(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
>          worker->s->error_time = apr_time_now();
>          worker->s->status |= PROXY_WORKER_IN_ERROR;
>          ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
> @@ -2902,6 +2903,7 @@
>              APR_TIME_T_FMT "s",
>              worker->s->hostname, apr_time_sec(worker->s->retry));
>      }
> +        }
>      else {
>          if (worker->s->retries) {
>              /*
> @@ -2915,6 +2917,19 @@
>      }
>      return connected ? OK : DECLINED;
>  }
> +    else {
> +        /*
> +         * The worker is in error likely done by a different thread / process
> +         * e.g. for a timeout or bad status. We should respect this and should
> +         * not continue with a connection via this worker even if we got one.
> +         */
> +        if (connected) {
> +            apr_socket_close(conn->sock );
> +            conn->sock = NULL;
> +        }
> +        return DECLINED;

If the worker is *not a BalancerMember*, couldn't we simply mark the
connection as close = 1 here, and continue with it until the end of
this request?
This is to avoid responding 503 whereas it succeed for this particular
request (since this is a race, the worker was put in error state a
very little time ago, not at ap_proxy_acquire_connection() time for
this request at least).

Otherwise, I agree with the move of :
+            worker->s->error_time = 0;
+            worker->s->retries = 0;
in the correct block.

Regards,
Yann.

Re: error_time reset in proxy_util.c

Posted by Ruediger Pluem <rp...@apache.org>.

On 03/05/2015 06:00 PM, Ruediger Pluem wrote:
> 
> 
> On 03/05/2015 05:29 PM, Eric Covener wrote:
>> On Thu, Mar 5, 2015 at 11:14 AM, Ruediger Pluem <rp...@apache.org> wrote:
>>> I suspect that the worker was already set to error by a parallel thread / process and hence
>>> PROXY_WORKER_IS_USABLE(worker) is false and causes worker->s->error_time to be reset which causes the worker to be open
>>> for retry immediately. This has been the case since r104624
>>> (http://svn.apache.org/viewvc?view=revision&revision=r104624) 10,5 years ago and the commit messages offers no hint at
>>> least to be why we reset these values.
>>> Can anybody think of a good reason why we do this?
>>
>> that sequence sounds plausible
> 
> So do you see any reason why we do this, or should we just stop doing so?
> 
> Another thing:
> 
>     else {
>         if (worker->s->retries) {
>             /*
>              * A worker came back. So here is where we need to
>              * either reset all params to initial conditions or
>              * apply some sort of aging
>              */
>         }
> 
> The comment makes a wrong assumption as we might be here despite the connection failed because the worker was already in
> error. So we might need to split into two ifs above and separate the conditions where the connection failed and where we
> need to do something with the worker because it wasn't in error yet. How about:
> 

Index: proxy_util.c
===================================================================
--- proxy_util.c        (revision 1664261)
+++ proxy_util.c        (working copy)
@@ -2887,14 +2887,15 @@

         connected    = 1;
     }
+    if (PROXY_WORKER_IS_USABLE(worker)) {
     /*
      * Put the entire worker to error state if
      * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
      * Altrough some connections may be alive
      * no further connections to the worker could be made
      */
-    if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
-        !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
+        if (!connected) {
+            if (!(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
         worker->s->error_time = apr_time_now();
         worker->s->status |= PROXY_WORKER_IN_ERROR;
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
@@ -2902,6 +2903,7 @@
             APR_TIME_T_FMT "s",
             worker->s->hostname, apr_time_sec(worker->s->retry));
     }
+        }
     else {
         if (worker->s->retries) {
             /*
@@ -2915,6 +2917,19 @@
     }
     return connected ? OK : DECLINED;
 }
+    else {
+        /*
+         * The worker is in error likely done by a different thread / process
+         * e.g. for a timeout or bad status. We should respect this and should
+         * not continue with a connection via this worker even if we got one.
+         */
+        if (connected) {
+            apr_socket_close(conn->sock );
+            conn->sock = NULL;
+        }
+        return DECLINED;
+    }
+}

 static apr_status_t connection_shutdown(void *theconn)
 {


The above patch fixes my issue (same one as in previous post but without whitespace changes for easier reading).
Objections? Otherwise I would commit.

Regards

Rüdiger

Re: error_time reset in proxy_util.c

Posted by Ruediger Pluem <rp...@apache.org>.

On 03/05/2015 05:29 PM, Eric Covener wrote:
> On Thu, Mar 5, 2015 at 11:14 AM, Ruediger Pluem <rp...@apache.org> wrote:
>> I suspect that the worker was already set to error by a parallel thread / process and hence
>> PROXY_WORKER_IS_USABLE(worker) is false and causes worker->s->error_time to be reset which causes the worker to be open
>> for retry immediately. This has been the case since r104624
>> (http://svn.apache.org/viewvc?view=revision&revision=r104624) 10,5 years ago and the commit messages offers no hint at
>> least to be why we reset these values.
>> Can anybody think of a good reason why we do this?
> 
> that sequence sounds plausible

So do you see any reason why we do this, or should we just stop doing so?

Another thing:

    else {
        if (worker->s->retries) {
            /*
             * A worker came back. So here is where we need to
             * either reset all params to initial conditions or
             * apply some sort of aging
             */
        }

The comment makes a wrong assumption as we might be here despite the connection failed because the worker was already in
error. So we might need to split into two ifs above and separate the conditions where the connection failed and where we
need to do something with the worker because it wasn't in error yet. How about:

Index: proxy_util.c
===================================================================
--- proxy_util.c        (revision 1664261)
+++ proxy_util.c        (working copy)
@@ -2887,33 +2887,48 @@

         connected    = 1;
     }
-    /*
-     * Put the entire worker to error state if
-     * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
-     * Altrough some connections may be alive
-     * no further connections to the worker could be made
-     */
-    if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
-        !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
-        worker->s->error_time = apr_time_now();
-        worker->s->status |= PROXY_WORKER_IN_ERROR;
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
-            "ap_proxy_connect_backend disabling worker for (%s) for %"
-            APR_TIME_T_FMT "s",
-            worker->s->hostname, apr_time_sec(worker->s->retry));
+    if (PROXY_WORKER_IS_USABLE(worker)) {
+        /*
+         * Put the entire worker to error state if
+         * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
+         * Altrough some connections may be alive
+         * no further connections to the worker could be made
+         */
+        if (!connected) {
+            if (!(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
+                worker->s->error_time = apr_time_now();
+                worker->s->status |= PROXY_WORKER_IN_ERROR;
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
+                    "ap_proxy_connect_backend disabling worker for (%s) for %"
+                    APR_TIME_T_FMT "s",
+                    worker->s->hostname, apr_time_sec(worker->s->retry));
+            }
+        }
+        else {
+            if (worker->s->retries) {
+                /*
+                 * A worker came back. So here is where we need to
+                 * either reset all params to initial conditions or
+                 * apply some sort of aging
+                 */
+            }
+            worker->s->error_time = 0;
+            worker->s->retries = 0;
+        }
+        return connected ? OK : DECLINED;
     }
     else {
-        if (worker->s->retries) {
-            /*
-             * A worker came back. So here is where we need to
-             * either reset all params to initial conditions or
-             * apply some sort of aging
-             */
+        /*
+         * The worker is in error likely done by a different thread / processi
+         * e.g. for a timeout or bad status. We should respect this and should
+         * not continue with a connection via this worker even if we got one.
+         */
+        if (connected) {
+            apr_socket_close(conn->sock );
+            conn->sock = NULL;
         }
-        worker->s->error_time = 0;
-        worker->s->retries = 0;
+        return DECLINED;
     }
-    return connected ? OK : DECLINED;
 }

 static apr_status_t connection_shutdown(void *theconn)


> 
>> Another question is if we shouldn't do
>>
>> worker->s->error_time = apr_time_now();
>>
>> also in case the worker is already in error state to restart the retry clock as we just faced an error with connecting
>> to the backend.
> 
> Isn't this already handled by the thread that put the worker in error
> during the race? If there's no race, we already have this line of code
> for the main path.
> 

Valid point, but we might have an error situation here that is more recent then the one by the thread that put it into
error. OTOH as I suspect a race the difference is likely to be rather slim, so it might not be worth the effort.

Regards

Rüdiger

Re: error_time reset in proxy_util.c

Posted by Eric Covener <co...@gmail.com>.
On Thu, Mar 5, 2015 at 11:14 AM, Ruediger Pluem <rp...@apache.org> wrote:
> I suspect that the worker was already set to error by a parallel thread / process and hence
> PROXY_WORKER_IS_USABLE(worker) is false and causes worker->s->error_time to be reset which causes the worker to be open
> for retry immediately. This has been the case since r104624
> (http://svn.apache.org/viewvc?view=revision&revision=r104624) 10,5 years ago and the commit messages offers no hint at
> least to be why we reset these values.
> Can anybody think of a good reason why we do this?

that sequence sounds plausible

> Another question is if we shouldn't do
>
> worker->s->error_time = apr_time_now();
>
> also in case the worker is already in error state to restart the retry clock as we just faced an error with connecting
> to the backend.

Isn't this already handled by the thread that put the worker in error
during the race? If there's no race, we already have this line of code
for the main path.