1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
|
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999-2001 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> *
* Based on: *
* Version: $Revision: 1.13 $ *
***************************************************************************/
#include "jk_pool.h"
#include "jk_service.h"
#include "jk_util.h"
#include "jk_worker.h"
#include "jk_lb_worker.h"
/*
* The load balancing code in this
*/
/*
* Time to wait before retry...
*/
#define WAIT_BEFORE_RECOVER (60*1)
#define ADDITINAL_WAIT_LOAD (20)
struct worker_record {
char *name;
double lb_factor;
double lb_value;
int is_local_worker;
int in_error_state;
int in_recovering;
time_t error_time;
jk_worker_t *w;
};
typedef struct worker_record worker_record_t;
struct lb_worker {
worker_record_t *lb_workers;
unsigned num_of_workers;
jk_pool_t p;
jk_pool_atom_t buf[TINY_POOL_SIZE];
char *name;
jk_worker_t worker;
int in_local_worker_mode;
int local_worker_only;
int sticky_session;
};
typedef struct lb_worker lb_worker_t;
struct lb_endpoint {
jk_endpoint_t *e;
lb_worker_t *worker;
jk_endpoint_t endpoint;
};
typedef struct lb_endpoint lb_endpoint_t;
/* ========================================================================= */
/* Retrieve the parameter with the given name */
static char *get_path_param(jk_ws_service_t *s,
const char *name)
{
char *id_start = NULL;
for(id_start = strstr(s->req_uri, name) ;
id_start ;
id_start = strstr(id_start + 1, name)) {
if('=' == id_start[strlen(name)]) {
/*
* Session path-cookie was found, get it's value
*/
id_start += (1 + strlen(name));
if(strlen(id_start)) {
char *id_end;
id_start = jk_pool_strdup(s->pool, id_start);
/*
* The query string is not part of req_uri, however
* to be on the safe side lets remove the trailing query
* string if appended...
*/
if(id_end = strchr(id_start, '?')) {
*id_end = '\0';
}
return id_start;
}
}
}
return NULL;
}
/* ========================================================================= */
/* Retrieve the cookie with the given name */
static char *get_cookie(jk_ws_service_t *s,
const char *name)
{
unsigned i;
for(i = 0 ; i < s->num_headers ; i++) {
if(0 == strcasecmp(s->headers_names[i], "cookie")) {
char *id_start;
for(id_start = strstr(s->headers_values[i], name) ;
id_start ;
id_start = strstr(id_start + 1, name)) {
if('=' == id_start[strlen(name)]) {
/*
* Session cookie was found, get it's value
*/
id_start += (1 + strlen(name));
if(strlen(id_start)) {
char *id_end;
id_start = jk_pool_strdup(s->pool, id_start);
if(id_end = strchr(id_start, ';')) {
*id_end = '\0';
}
return id_start;
}
}
}
}
}
return NULL;
}
/* ========================================================================= */
/* Retrieve session id from the cookie or the parameter */
/* (parameter first) */
static char *get_sessionid(jk_ws_service_t *s)
{
char *val;
val = get_path_param(s, JK_PATH_SESSION_IDENTIFIER);
if(!val) {
val = get_cookie(s, JK_SESSION_IDENTIFIER);
}
return val;
}
static char *get_session_route(jk_ws_service_t *s)
{
char *sessionid = get_sessionid(s);
char *ch;
if(!sessionid) {
return NULL;
}
/*
* Balance parameter is appended to the end
*/
ch = strrchr(sessionid, '.');
if(!ch) {
return 0;
}
ch++;
if(*ch == '\0') {
return NULL;
}
return ch;
}
static void close_workers(lb_worker_t *p,
int num_of_workers,
jk_logger_t *l)
{
int i = 0;
for(i = 0 ; i < num_of_workers ; i++) {
p->lb_workers[i].w->destroy(&(p->lb_workers[i].w),
l);
}
}
static double get_max_lb(lb_worker_t *p)
{
unsigned i;
double rc = 0.0;
for(i = 0 ; i < p->num_of_workers ; i++) {
if(!p->lb_workers[i].in_error_state) {
if(p->lb_workers[i].lb_value > rc) {
rc = p->lb_workers[i].lb_value;
}
}
}
return rc;
}
static worker_record_t *get_most_suitable_worker(lb_worker_t *p,
jk_ws_service_t *s, int attempt)
{
worker_record_t *rc = NULL;
double lb_min = 0.0;
unsigned i;
char *session_route = NULL;
if (p->sticky_session) {
session_route = get_session_route(s);
}
if(session_route) {
for(i = 0 ; i < p->num_of_workers ; i++) {
if(0 == strcmp(session_route, p->lb_workers[i].name)) {
/* First attempt will allways be to the
correct host. If this is indeed down and no
hope of recovery, we'll go to fail-over
*/
if(attempt>0 && p->lb_workers[i].in_error_state) {
break;
} else {
return &(p->lb_workers[i]);
}
}
}
}
for(i = 0 ; i < p->num_of_workers ; i++) {
if (!p->in_local_worker_mode || p->lb_workers[i].is_local_worker || !p->local_worker_only) {
if(p->lb_workers[i].in_error_state) {
if(!p->lb_workers[i].in_recovering) {
time_t now = time(0);
if((now - p->lb_workers[i].error_time) > WAIT_BEFORE_RECOVER) {
p->lb_workers[i].in_recovering = JK_TRUE;
p->lb_workers[i].error_time = now;
rc = &(p->lb_workers[i]);
break;
}
}
} else {
if(p->lb_workers[i].lb_value < lb_min || !rc) {
lb_min = p->lb_workers[i].lb_value;
rc = &(p->lb_workers[i]);
if (rc->is_local_worker) break;
}
}
}
}
if(rc && !rc->is_local_worker) {
rc->lb_value += rc->lb_factor;
}
return rc;
}
static int JK_METHOD service(jk_endpoint_t *e,
jk_ws_service_t *s,
jk_logger_t *l,
int *is_recoverable_error)
{
jk_log(l, JK_LOG_DEBUG,
"Into jk_endpoint_t::service\n");
if(e && e->endpoint_private && s && is_recoverable_error) {
lb_endpoint_t *p = e->endpoint_private;
jk_endpoint_t *end = NULL;
int attempt=0;
/* you can not recover on another load balancer */
*is_recoverable_error = JK_FALSE;
while(1) {
worker_record_t *rec = get_most_suitable_worker(p->worker, s, attempt++);
int rc;
if(rec) {
int is_recoverable = JK_FALSE;
s->jvm_route = jk_pool_strdup(s->pool, rec->name);
rc = rec->w->get_endpoint(rec->w, &end, l);
if(rc && end) {
int src = end->service(end, s, l, &is_recoverable);
end->done(&end, l);
if(src ) {
if(rec->in_recovering && rec->lb_value != 0) {
rec->lb_value = get_max_lb(p->worker) + ADDITINAL_WAIT_LOAD;
}
rec->in_error_state = JK_FALSE;
rec->in_recovering = JK_FALSE;
rec->error_time = 0;
return JK_TRUE;
}
}
/*
* Service failed !!!
*
* Time for fault tolerance (if possible)...
*/
rec->in_error_state = JK_TRUE;
rec->in_recovering = JK_FALSE;
rec->error_time = time(0);
if(!is_recoverable) {
/*
* Error is not recoverable - break with an error.
*/
jk_log(l, JK_LOG_ERROR,
"lb: unrecoverable error, request failed. Tomcat failed in the middle of request, we can't recover to another instance.\n");
break;
}
/*
* Error is recoverable by submitting the request to
* another worker... Lets try to do that.
*/
jk_log(l, JK_LOG_DEBUG,
"In jk_endpoint_t::service, recoverable error... will try to recover on other host\n");
} else {
/* NULL record, no more workers left ... */
jk_log(l, JK_LOG_ERROR,
"lb: All tomcat instances failed, no more workers left.\n");
return JK_FALSE;
break;
}
}
}
jk_log(l, JK_LOG_ERROR,
"In jk_endpoint_t::service: NULL Parameters\n");
return JK_FALSE;
}
static int JK_METHOD done(jk_endpoint_t **e,
jk_logger_t *l)
{
jk_log(l, JK_LOG_DEBUG,
"Into jk_endpoint_t::done\n");
if(e && *e && (*e)->endpoint_private) {
lb_endpoint_t *p = (*e)->endpoint_private;
if(p->e) {
p->e->done(&p->e, l);
}
free(p);
*e = NULL;
return JK_TRUE;
}
jk_log(l, JK_LOG_ERROR,
"In jk_endpoint_t::done: NULL Parameters\n");
return JK_FALSE;
}
static int JK_METHOD validate(jk_worker_t *pThis,
jk_map_t *props,
jk_worker_env_t *we,
jk_logger_t *l)
{
jk_log(l, JK_LOG_DEBUG,
"Into jk_worker_t::validate\n");
if(pThis && pThis->worker_private) {
lb_worker_t *p = pThis->worker_private;
char **worker_names;
unsigned num_of_workers;
p->in_local_worker_mode = JK_FALSE;
p->local_worker_only = jk_get_local_worker_only_flag(props, p->name);
p->sticky_session = jk_get_is_sticky_session(props, p->name);
if(jk_get_lb_worker_list(props,
p->name,
&worker_names,
&num_of_workers) && num_of_workers) {
unsigned i = 0;
unsigned j = 0;
p->lb_workers = jk_pool_alloc(&p->p,
num_of_workers * sizeof(worker_record_t));
if(!p->lb_workers) {
return JK_FALSE;
}
for(i = 0 ; i < num_of_workers ; i++) {
p->lb_workers[i].name = jk_pool_strdup(&p->p, worker_names[i]);
p->lb_workers[i].lb_factor = jk_get_lb_factor(props,
worker_names[i]);
if(p->lb_workers[i].lb_factor < 0) {
p->lb_workers[i].lb_factor = -1 * p->lb_workers[i].lb_factor;
}
if (0 < p->lb_workers[i].lb_factor) {
p->lb_workers[i].lb_factor = 1/p->lb_workers[i].lb_factor;
}
p->lb_workers[i].is_local_worker = jk_get_is_local_worker(props, worker_names[i]);
if (p->lb_workers[i].is_local_worker) p->in_local_worker_mode = JK_TRUE;
/*
* Allow using lb in fault-tolerant mode.
* A value of 0 means the worker will be used for all requests without
* sessions
*/
p->lb_workers[i].lb_value = p->lb_workers[i].lb_factor;
p->lb_workers[i].in_error_state = JK_FALSE;
p->lb_workers[i].in_recovering = JK_FALSE;
if(!wc_create_worker(p->lb_workers[i].name,
props,
&(p->lb_workers[i].w),
we,
l) || !p->lb_workers[i].w) {
break;
} else if (p->lb_workers[i].is_local_worker) {
/*
* If lb_value is 0 than move it at the beginning of the list
*/
if (i != j) {
worker_record_t tmp_worker;
tmp_worker = p->lb_workers[j];
p->lb_workers[j] = p->lb_workers[i];
p->lb_workers[i] = tmp_worker;
}
j++;
}
}
if (!p->in_local_worker_mode) {
p->local_worker_only = JK_FALSE;
}
if(i != num_of_workers) {
close_workers(p, i, l);
jk_log(l, JK_LOG_DEBUG,
"In jk_worker_t::validate: Failed to create worker %s\n",
p->lb_workers[i].name);
} else {
for (i = 0; i < num_of_workers; i++) {
jk_log(l, JK_LOG_DEBUG,
"Balanced worker %i has name %s\n",
i, p->lb_workers[i].name);
}
jk_log(l, JK_LOG_DEBUG,
"in_local_worker_mode: %s\n",
(p->in_local_worker_mode ? "true" : "false"));
jk_log(l, JK_LOG_DEBUG,
"local_worker_only: %s\n",
(p->local_worker_only ? "true" : "false"));
p->num_of_workers = num_of_workers;
return JK_TRUE;
}
}
}
jk_log(l, JK_LOG_ERROR,
"In jk_worker_t::validate: NULL Parameters\n");
return JK_FALSE;
}
static int JK_METHOD init(jk_worker_t *pThis,
jk_map_t *props,
jk_worker_env_t *we,
jk_logger_t *log)
{
/* Nothing to do for now */
return JK_TRUE;
}
static int JK_METHOD get_endpoint(jk_worker_t *pThis,
jk_endpoint_t **pend,
jk_logger_t *l)
{
jk_log(l, JK_LOG_DEBUG,
"Into jk_worker_t::get_endpoint\n");
if(pThis && pThis->worker_private && pend) {
lb_endpoint_t *p = (lb_endpoint_t *)malloc(sizeof(lb_endpoint_t));
if(p) {
p->e = NULL;
p->worker = pThis->worker_private;
p->endpoint.endpoint_private = p;
p->endpoint.service = service;
p->endpoint.done = done;
*pend = &p->endpoint;
return JK_TRUE;
}
jk_log(l, JK_LOG_ERROR,
"In jk_worker_t::get_endpoint, malloc failed\n");
} else {
jk_log(l, JK_LOG_ERROR,
"In jk_worker_t::get_endpoint, NULL parameters\n");
}
return JK_FALSE;
}
static int JK_METHOD destroy(jk_worker_t **pThis,
jk_logger_t *l)
{
jk_log(l, JK_LOG_DEBUG,
"Into jk_worker_t::destroy\n");
if(pThis && *pThis && (*pThis)->worker_private) {
lb_worker_t *private_data = (*pThis)->worker_private;
close_workers(private_data,
private_data->num_of_workers,
l);
jk_close_pool(&private_data->p);
free(private_data);
return JK_TRUE;
}
jk_log(l, JK_LOG_ERROR,
"In jk_worker_t::destroy, NULL parameters\n");
return JK_FALSE;
}
int JK_METHOD lb_worker_factory(jk_worker_t **w,
const char *name,
jk_logger_t *l)
{
jk_log(l, JK_LOG_DEBUG,
"Into lb_worker_factory\n");
if(NULL != name && NULL != w) {
lb_worker_t *private_data =
(lb_worker_t *)malloc(sizeof(lb_worker_t));
if(private_data) {
jk_open_pool(&private_data->p,
private_data->buf,
sizeof(jk_pool_atom_t) * TINY_POOL_SIZE);
private_data->name = jk_pool_strdup(&private_data->p, name);
if(private_data->name) {
private_data->lb_workers = NULL;
private_data->num_of_workers = 0;
private_data->worker.worker_private = private_data;
private_data->worker.validate = validate;
private_data->worker.init = init;
private_data->worker.get_endpoint = get_endpoint;
private_data->worker.destroy = destroy;
*w = &private_data->worker;
return JK_TRUE;
}
jk_close_pool(&private_data->p);
free(private_data);
}
jk_log(l, JK_LOG_ERROR,
"In lb_worker_factory, malloc failed\n");
} else {
jk_log(l, JK_LOG_ERROR,
"In lb_worker_factory, NULL parameters\n");
}
return JK_FALSE;
}
|