📄 jk_worker_lb.c
字号:
/* ========================================================================= * * * * The Apache Software License, Version 1.1 * * * * Copyright (c) 1999-2002 The Apache Software Foundation. * * All rights reserved. * * * * ========================================================================= * * * * Redistribution and use in source and binary forms, with or without modi- * * fication, are permitted provided that the following conditions are met: * * * * 1. Redistributions of source code must retain the above copyright notice * * notice, this list of conditions and the following disclaimer. * * * * 2. Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * * * 3. The end-user documentation included with the redistribution, if any, * * must include the following acknowlegement: * * * * "This product includes software developed by the Apache Software * * Foundation <http://www.apache.org/>." * * * * Alternately, this acknowlegement may appear in the software itself, if * * and wherever such third-party acknowlegements normally appear. * * * * 4. The names "The Jakarta Project", "Jk", and "Apache Software * * Foundation" must not be used to endorse or promote products derived * * from this software without prior written permission. For written * * permission, please contact <apache@apache.org>. * * * * 5. Products derived from this software may not be called "Apache" nor may * * "Apache" appear in their names without prior written permission of the * * Apache Software Foundation. * * * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES * * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * * THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY * * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * * POSSIBILITY OF SUCH DAMAGE. * * * * ========================================================================= * * * * This software consists of voluntary contributions made by many indivi- * * duals on behalf of the Apache Software Foundation. For more information * * on the Apache Software Foundation, please see <http://www.apache.org/>. * * * * ========================================================================= *//*************************************************************************** * Description: Load balancer worker, knows how to load balance among * * several workers. * * Author: Gal Shachor <shachor@il.ibm.com> * * Author: Costin Manolache ***************************************************************************/#include "jk_pool.h"#include "jk_service.h"#include "jk_worker.h"#include "jk_logger.h"#include "jk_config.h"#include "jk_env.h"#include "jk_requtil.h"#ifdef HAS_APR#include "apr_thread_proc.h"#endif#define DEFAULT_LB_FACTOR (1.0)/* Time to wait before retry... XXX make it configurable*/#define WAIT_BEFORE_RECOVER 60 #define MAX_ATTEMPTS 3#define NO_WORKER_MSG "The servlet container is temporary unavailable or being upgraded\n";#define STICKY_SESSION 1typedef struct { struct jk_mutex *cs; int attempts; int recovery; int timeout; int sticky_session; time_t error_time;} jk_worker_lb_private_t;/** Find the best worker. In process, check if timeout expired for workers that failed in the past and give them another chance. This will check the JSESSIONID and forward to the right worker if in a session. It'll also adjust the load balancing factors.*/static jk_worker_t *jk2_get_most_suitable_worker(jk_env_t *env, jk_worker_t *lb, jk_ws_service_t *s, int attempt){ jk_worker_t *rc = NULL; int lb_min = 0; int lb_max = 0; int i; int j; int level; int currentLevel=JK_LB_LEVELS - 1; char *session_route = NULL; time_t now = 0; jk_worker_lb_private_t *lb_priv = lb->worker_private; if(lb_priv->sticky_session) { session_route = jk2_requtil_getSessionRoute(env, s); } if(session_route) { for( level=0; level<JK_LB_LEVELS; level++ ) { for(i = 0 ; i < lb->workerCnt[level]; i++) { jk_worker_t *w=lb->workerTables[level][i]; if(w->route != NULL && 0 == strcmp(session_route, w->route)) { if(attempt > 0 && w->in_error_state) { /* We already tried to revive this worker. */ break; } else { return w; } } } } } /** Get one worker that is ready */ for( level=0; level<JK_LB_LEVELS; level++ ) { for(i = 0 ; i < lb->workerCnt[level] ; i++) { jk_worker_t *w=lb->workerTables[level][i]; if( w->mbean->disabled ) continue; if( w->in_error_state ) continue; if( w->lb_disabled ) continue; if( rc==NULL ) { rc=w; currentLevel=level; lb_min=w->lb_value; continue; } if( w->lb_value < lb_min ) { lb_min = w->lb_value; rc = w; currentLevel=level; } } if( rc!=NULL ) { /* We found a valid worker on the current level, don't worry about the higher levels. */ break; } if( lb->hwBalanceErr > 0 ) { /* don't go to higher levels - we'll return an error */ currentLevel=0; break; } } /** Reenable workers in error state if the timeout has passed. * Don't bother with 'higher' levels, since we'll never try them. */ for( level=0; level<=currentLevel; level++ ) { for(i = 0 ; i < lb->workerCnt[level] ; i++) { jk_worker_t *w=lb->workerTables[level][i]; if( w->mbean->disabled ) continue; if( w->lb_disabled ) continue; if(w->in_error_state) { /* Check if it's ready for recovery */ if( now==0 ) now = time(NULL); if((now - w->error_time) > lb_priv->recovery) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "lb.getWorker() reenable %s\n", w->mbean->name); w->in_error_state = JK_FALSE; /* Find max lb */ if( lb_max ==0 ) { for( j=0; j<lb->workerCnt[level]; j++ ) { if( lb->workerTables[level][j]->lb_value > lb_max ) { lb_max=lb->workerTables[level][j]->lb_value; } } } w->lb_value = lb_max; } } } } /* If no active worker is found, we'll try all workers in error_state, */ if ( rc==NULL ) { /* no workers found (rc is null), now try as hard as possible to get a worker anyway, pick one with largest error time.. */ int error_workers=0; env->l->jkLog(env, env->l, JK_LOG_INFO, "lb.getWorker() All workers in error state, use the one with oldest error\n"); for( level=0; level<JK_LB_LEVELS; level++ ) { for(i = 0 ; i < lb->workerCnt[level] ; i++) { jk_worker_t *w=lb->workerTables[level][i]; if( w->mbean->disabled == JK_TRUE ) continue; if( w->lb_disabled ) continue; error_workers++; if( rc==NULL ) { rc= w; currentLevel=level; continue; } /* pick the oldest failed worker */ if ( w->error_time < rc->error_time ) { currentLevel=level; rc = w; } } if( lb->hwBalanceErr > 0 ) { /* Don't try higher levels, only level=0 */ break; } } if( attempt >= error_workers ) { env->l->jkLog(env, env->l, JK_LOG_INFO, "lb.getWorker() We tried all possible workers %d\n", attempt ); return NULL; } } if(rc!=NULL) { /* It it's the default, it'll remain the default - we don't increase the factor */ rc->in_error_state = JK_FALSE; if( rc->lb_value != 0 ) { int newValue=rc->lb_value + rc->lb_factor; if( newValue > 255 ) { rc->lb_value=rc->lb_factor; /* Roll over. This has 2 goals: - avoid the lb factor becoming too big, and give a chance to run to workers that were in error state ( I think it's cleaner than looking for "max" ) - the actual lb_value will be 1 byte. Even on the craziest platform, that will be an atomic write. We do a lot of operations on lb_value in a MT environment, and the chance of reading something inconsistent is considerable. Since APR will not support atomic - and adding a CS would cost too much, this is actually a good solution. Note that lb_value is not used for anything critical - just to balance the load, the worst that may happen is having a worker stay idle for 255 requests. */ for(i = 0 ; i < lb->workerCnt[currentLevel] ; i++) { jk_worker_t *w=lb->workerTables[currentLevel][i]; w->lb_value=w->lb_factor; } } else { rc->lb_value=newValue; } } } return rc;}/** Get the best worker and forward to it. Since we don't directly connect to anything, there's no need for an endpoint.*/static int JK_METHOD jk2_lb_service(jk_env_t *env, jk_worker_t *lb, jk_ws_service_t *s){ int attempt=0; jk_workerEnv_t *wEnv=lb->workerEnv; jk_worker_lb_private_t *lb_priv = lb->worker_private; jk_worker_t *rec = NULL; if( s==NULL ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -