bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_lb_worker.h
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /***************************************************************************
19  * Description: load balance worker header file                            *
20  * Author:      Gal Shachor <shachor@il.ibm.com>                           *
21  * Author:      Rainer Jung <rjung@apache.org>                             *
22  * Version:     $Revision: 751916 $                                           *
23  ***************************************************************************/
24
25 #ifndef JK_LB_WORKER_H
26 #define JK_LB_WORKER_H
27
28 #include "jk_logger.h"
29 #include "jk_service.h"
30 #include "jk_mt.h"
31 #include "jk_shm.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif                          /* __cplusplus */
37
38 #define JK_LB_WORKER_NAME     ("lb")
39 #define JK_LB_WORKER_TYPE     (5)
40 #define JK_LB_SUB_WORKER_TYPE (7)
41 #define JK_LB_DEF_DOMAIN_NAME ("unknown")
42
43 #define JK_LB_METHOD_REQUESTS          (0)
44 #define JK_LB_METHOD_TRAFFIC           (1)
45 #define JK_LB_METHOD_BUSYNESS          (2)
46 #define JK_LB_METHOD_SESSIONS          (3)
47 #define JK_LB_METHOD_DEF               (JK_LB_METHOD_REQUESTS)
48 #define JK_LB_METHOD_MAX               (JK_LB_METHOD_SESSIONS)
49 #define JK_LB_METHOD_TEXT_REQUESTS     ("Request")
50 #define JK_LB_METHOD_TEXT_TRAFFIC      ("Traffic")
51 #define JK_LB_METHOD_TEXT_BUSYNESS     ("Busyness")
52 #define JK_LB_METHOD_TEXT_SESSIONS     ("Sessions")
53 #define JK_LB_METHOD_TEXT_DEF          (JK_LB_METHOD_TEXT_REQUESTS)
54 #define JK_LB_LOCK_OPTIMISTIC          (0)
55 #define JK_LB_LOCK_PESSIMISTIC         (1)
56 #define JK_LB_LOCK_DEF                 (JK_LB_LOCK_OPTIMISTIC)
57 #define JK_LB_LOCK_MAX                 (JK_LB_LOCK_PESSIMISTIC)
58 #define JK_LB_LOCK_TEXT_OPTIMISTIC     ("Optimistic")
59 #define JK_LB_LOCK_TEXT_PESSIMISTIC    ("Pessimistic")
60 #define JK_LB_LOCK_TEXT_DEF            (JK_LB_LOCK_TEXT_OPTIMISTIC)
61 /*
62  * The following definitions for state and activation
63  * need to be kept in sync with the two macros 
64  * JK_WORKER_USABLE() and JK_WORKER_USABLE_STICKY() in jk_lb_worker.c.
65  * Since we use ordered comparisons there instead of multiple
66  * equal/unequal compares, order of the values is critical here.
67  */
68 #define JK_LB_STATE_IDLE               (0)
69 #define JK_LB_STATE_OK                 (1)
70 #define JK_LB_STATE_RECOVER            (2)
71 #define JK_LB_STATE_FORCE              (3)
72 #define JK_LB_STATE_BUSY               (4)
73 #define JK_LB_STATE_ERROR              (5)
74 #define JK_LB_STATE_PROBE              (6)
75 #define JK_LB_STATE_DEF                (JK_LB_STATE_IDLE)
76 #define JK_LB_STATE_TEXT_IDLE          ("OK/IDLE")
77 #define JK_LB_STATE_TEXT_OK            ("OK")
78 #define JK_LB_STATE_TEXT_RECOVER       ("ERR/REC")
79 #define JK_LB_STATE_TEXT_FORCE         ("ERR/FRC")
80 #define JK_LB_STATE_TEXT_BUSY          ("OK/BUSY")
81 #define JK_LB_STATE_TEXT_ERROR         ("ERR")
82 #define JK_LB_STATE_TEXT_PROBE         ("ERR/PRB")
83 #define JK_LB_STATE_TEXT_MAX           (JK_LB_STATE_PROBE)
84 #define JK_LB_STATE_TEXT_DEF           (JK_LB_STATE_TEXT_IDLE)
85 /* All JK_LB_ACTIVATION_* values must be single digit. */
86 /* Otherwise the string encoding of the activation array */
87 /* fails e.g. in the isapi redirector. */
88 /* JK_LB_ACTIVATION_UNSET is not allowed as an actual worker state. */
89 /* It will not work e.g. when the status worker tries to show the state. */
90 /* It is only used in rule extension data to indicate, that the */
91 /* activation state should not be overwritten. */
92 #define JK_LB_ACTIVATION_ACTIVE        (0)
93 #define JK_LB_ACTIVATION_DISABLED      (1)
94 #define JK_LB_ACTIVATION_STOPPED       (2)
95 #define JK_LB_ACTIVATION_UNSET         (9)
96 #define JK_LB_ACTIVATION_DEF           (JK_LB_ACTIVATION_ACTIVE)
97 #define JK_LB_ACTIVATION_MAX           (JK_LB_ACTIVATION_STOPPED)
98 #define JK_LB_ACTIVATION_TEXT_ACTIVE   ("ACT")
99 #define JK_LB_ACTIVATION_TEXT_DISABLED ("DIS")
100 #define JK_LB_ACTIVATION_TEXT_STOPPED  ("STP")
101 #define JK_LB_ACTIVATION_TEXT_DEF      (JK_LB_ACTIVATION_TEXT_ACTIVE)
102
103 #define JK_LB_UINT64_STR_SZ          (21)
104 #define JK_LB_NOTES_COUNT            (9)
105 #define JK_NOTE_LB_FIRST_NAME        ("JK_LB_FIRST_NAME")
106 #define JK_NOTE_LB_FIRST_VALUE       ("JK_LB_FIRST_VALUE")
107 #define JK_NOTE_LB_FIRST_ACCESSED    ("JK_LB_FIRST_ACCESSED")
108 #define JK_NOTE_LB_FIRST_READ        ("JK_LB_FIRST_READ")
109 #define JK_NOTE_LB_FIRST_TRANSFERRED ("JK_LB_FIRST_TRANSFERRED")
110 #define JK_NOTE_LB_FIRST_ERRORS      ("JK_LB_FIRST_ERRORS")
111 #define JK_NOTE_LB_FIRST_BUSY        ("JK_LB_FIRST_BUSY")
112 #define JK_NOTE_LB_FIRST_ACTIVATION  ("JK_LB_FIRST_ACTIVATION")
113 #define JK_NOTE_LB_FIRST_STATE       ("JK_LB_FIRST_STATE")
114 #define JK_NOTE_LB_LAST_NAME         ("JK_LB_LAST_NAME")
115 #define JK_NOTE_LB_LAST_VALUE        ("JK_LB_LAST_VALUE")
116 #define JK_NOTE_LB_LAST_ACCESSED     ("JK_LB_LAST_ACCESSED")
117 #define JK_NOTE_LB_LAST_READ         ("JK_LB_LAST_READ")
118 #define JK_NOTE_LB_LAST_TRANSFERRED  ("JK_LB_LAST_TRANSFERRED")
119 #define JK_NOTE_LB_LAST_ERRORS       ("JK_LB_LAST_ERRORS")
120 #define JK_NOTE_LB_LAST_BUSY         ("JK_LB_LAST_BUSY")
121 #define JK_NOTE_LB_LAST_ACTIVATION   ("JK_LB_LAST_ACTIVATION")
122 #define JK_NOTE_LB_LAST_STATE        ("JK_LB_LAST_STATE")
123
124 /* Time to wait before retry. */
125 #define WAIT_BEFORE_RECOVER   (60)
126 /* We accept doing global maintenance if we are */
127 /* JK_LB_MAINTAIN_TOLERANCE seconds early. */
128 #define JK_LB_MAINTAIN_TOLERANCE (2)
129 /* We divide load values by 2^x during global maintenance. */
130 /* The exponent x is JK_LB_DECAY_MULT*#MAINT_INTV_SINCE_LAST_MAINT */
131 #define JK_LB_DECAY_MULT         (1)
132
133 struct lb_sub_worker
134 {
135     jk_worker_t *worker;
136     /* Shared memory worker data */
137     jk_shm_lb_sub_worker_t *s;
138
139     char         name[JK_SHM_STR_SIZ+1];
140     /* Sequence counter starting at 0 and increasing
141      * every time we change the config
142      */
143     volatile unsigned int sequence;
144
145     /* route */
146     char    route[JK_SHM_STR_SIZ+1];
147     /* worker domain */
148     char    domain[JK_SHM_STR_SIZ+1];
149     /* worker redirect route */
150     char    redirect[JK_SHM_STR_SIZ+1];
151     /* worker distance */
152     int distance;
153     /* current activation state (config) of the worker */
154     int activation;
155     /* Current lb factor */
156     int lb_factor;
157     /* Current worker index */
158     int i;
159     /* Current lb reciprocal factor */
160     jk_uint64_t lb_mult;
161 };
162 typedef struct lb_sub_worker lb_sub_worker_t;
163
164 struct lb_worker
165 {
166     jk_worker_t worker;
167     /* Shared memory worker data */
168     jk_shm_lb_worker_t *s;
169
170     char         name[JK_SHM_STR_SIZ+1];
171     /* Sequence counter starting at 0 and increasing
172      * every time we change the config
173      */
174     volatile unsigned int sequence;
175
176     jk_pool_t p;
177     jk_pool_atom_t buf[TINY_POOL_SIZE];
178
179     JK_CRIT_SEC cs;
180
181     lb_sub_worker_t *lb_workers;
182     unsigned int num_of_workers;
183     int          sticky_session;
184     int          sticky_session_force;
185     int          recover_wait_time;
186     int          error_escalation_time;
187     int          max_reply_timeouts;
188     int          retries;
189     int          retry_interval;
190     int          lbmethod;
191     int          lblock;
192     int          maintain_time;
193     unsigned int max_packet_size;
194     unsigned int next_offset;
195     /* Session cookie */
196     char         session_cookie[JK_SHM_STR_SIZ+1];
197     /* Session path */
198     char         session_path[JK_SHM_STR_SIZ+1];
199 };
200 typedef struct lb_worker lb_worker_t;
201
202 int JK_METHOD lb_worker_factory(jk_worker_t **w,
203                                 const char *name, jk_logger_t *l);
204
205 const char *jk_lb_get_lock(lb_worker_t *p, jk_logger_t *l);
206 int jk_lb_get_lock_code(const char *v);
207 const char *jk_lb_get_method(lb_worker_t *p, jk_logger_t *l);
208 int jk_lb_get_method_code(const char *v);
209 const char *jk_lb_get_state(lb_sub_worker_t *p, jk_logger_t *l);
210 int jk_lb_get_state_code(const char *v);
211 const char *jk_lb_get_activation_direct(int activation, jk_logger_t *l);
212 const char *jk_lb_get_activation(lb_sub_worker_t *p, jk_logger_t *l);
213 int jk_lb_get_activation_code(const char *v);
214 void reset_lb_values(lb_worker_t *p, jk_logger_t *l);
215 void jk_lb_pull(lb_worker_t * p, int locked, jk_logger_t *l);
216 void jk_lb_push(lb_worker_t * p, int locked, jk_logger_t *l);
217 void update_mult(lb_worker_t * p, jk_logger_t *l);
218
219 #ifdef __cplusplus
220 }
221 #endif                          /* __cplusplus */
222 #endif                          /* JK_LB_WORKER_H */