CUBRID Engine  latest
shard_proxy.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 
20 /*
21  * shard_proxy.c -
22  *
23  */
24 
25 #ident "$Id$"
26 
27 #include <signal.h>
28 #include <errno.h>
29 
30 /* SHARD TODO : MV include .. */
31 #include "porting.h"
32 #include "shard_proxy.h"
33 #include "shard_proxy_handler.h"
34 #include "shard_key_func.h"
35 
36 #if defined(WINDOWS)
37 #include "broker_wsa_init.h"
38 #endif
39 #define ENDLESS 1
40 
41 /* SHARD SHM */
44 
45 int proxy_id = -1;
46 int proxy_shm_id = -1;
49 
53 
54 static int proxy_shm_initialize (void);
55 /* END OF SHARD SHM */
56 
58 
59 static void cleanup (int signo);
60 
61 static void proxy_set_hang_check_time (void);
62 static void proxy_unset_hang_check_time (void);
63 
64 void
65 proxy_term (void)
66 {
67 #ifdef SOLARIS
68  SLEEP_MILISEC (1, 0);
69 #endif
73 
74  proxy_log_close ();
76 
77  return;
78 }
79 
80 static void
81 cleanup (int signo)
82 {
83  signal (signo, SIG_IGN);
84 
85  proxy_term ();
86  exit (0);
87 
88  return;
89 }
90 
91 int
93 {
94  char *p;
95 
96  p = getenv (PROXY_ID_ENV_STR);
97  if (p == NULL)
98  {
99  SHARD_ERR ("Failed to getenv(PROXY_ID_ENV_STR).");
100  goto return_error;
101  }
102  parse_int (&proxy_id, p, 10);
103 
104  p = getenv (PROXY_SHM_KEY_STR);
105  if (p == NULL)
106  {
107  SHARD_ERR ("Failed to getenv(PROXY_SHM_KEY_STR).");
108  goto return_error;
109  }
110  parse_int (&proxy_shm_id, p, 10);
111 
113  if (shm_proxy_p == NULL)
114  {
115  SHARD_ERR ("Failed to get shm proxy.");
116  goto return_error;
117  }
118 
119  proxy_info_p = shard_shm_find_proxy_info (shm_proxy_p, proxy_id);
120  if (proxy_info_p == NULL)
121  {
122  SHARD_ERR ("Failed to get proxy info.");
123  goto return_error;
124  }
125 
127  if (shm_as_p == NULL)
128  {
129  SHARD_ERR ("Failed to open shared memory. " "(SHM_APPL_SERVER, shm_key:%d).", appl_server_shm_id);
130  goto return_error;
131  }
132 
133  shm_user_p = shard_metadata_get_user (shm_proxy_p);
134  if (shm_user_p == NULL)
135  {
136  SHARD_ERR ("Failed to get shm metadata user info.");
137  goto return_error;
138  }
139 
140  shm_key_p = shard_metadata_get_key (shm_proxy_p);
141  if (shm_key_p == NULL)
142  {
143  SHARD_ERR ("Failed to get shm metadata shard key info.");
144  goto return_error;
145  }
146 
147  shm_conn_p = shard_metadata_get_conn (shm_proxy_p);
148  if (shm_conn_p == NULL)
149  {
150  SHARD_ERR ("Failed to get shm metadata connection info.");
151  goto return_error;
152  }
153 
154  return 0;
155 
156 return_error:
157  SHARD_ERR ("Failed to initialize shard shared memory.");
158 
159  return -1;
160 }
161 
162 int
163 main (int argc, char *argv[])
164 {
165  int error;
166 
167  signal (SIGTERM, cleanup);
168  signal (SIGINT, cleanup);
169 #if !defined(WINDOWS)
170  signal (SIGPIPE, SIG_IGN);
171 #endif
172 
173  error = proxy_shm_initialize ();
174  if (error)
175  {
176  return error;
177  }
178 
179  error = register_fn_get_shard_key ();
180  if (error)
181  {
182  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Failed to register shard hashing function.");
183  return error;
184  }
185 
186 #if defined(WINDOWS)
187  if (wsa_initialize () < 0)
188  {
189  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Failed to initialize WSA.");
190  return error;
191  }
192 #endif
193 
194  /* SHARD TODO : initialize IO */
195  error = proxy_io_initialize ();
196  if (error)
197  {
198  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Failed to initialize proxy IO.");
199  return error;
200  }
201 
202  error = shard_stmt_initialize (proxy_info_p->max_prepared_stmt_count);
203  if (error)
204  {
205  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Failed to initialize proxy statement pool.");
206  return error;
207  }
208 
209  error = proxy_handler_initialize ();
210  if (error)
211  {
212  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Failed to initialize proxy context handler.");
213  return error;
214  }
215 
216  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Shard proxy started.");
217  proxy_Keep_running = true;
218  while (proxy_Keep_running == true)
219  {
220  /*
221  * Since every operation in proxy main is non-blocking
222  * (which is not the case in cas main),
223  * proxy_set_hang_check_time is placed at the beginning and
224  * proxy_unset_hang_check_time is at the end.
225  */
227 
228  /* read or write */
229  proxy_io_process ();
230 
231  /* process message */
233 
234  /* process timer */
236 
238  }
239  PROXY_LOG (PROXY_LOG_MODE_NOTICE, "Shard proxy going down.");
240 
241  proxy_term ();
242  return 0;
243 }
244 
245 static void
247 {
248  if (proxy_info_p != NULL && shm_as_p != NULL && shm_as_p->monitor_hang_flag)
249  {
250  proxy_info_p->claimed_alive_time = time (NULL);
251  }
252 }
253 
254 static void
256 {
257  if (proxy_info_p != NULL && shm_as_p != NULL && shm_as_p->monitor_hang_flag)
258  {
259  proxy_info_p->claimed_alive_time = (time_t) 0;
260  }
261 }
int proxy_shm_id
Definition: shard_proxy.c:46
#define SLEEP_MILISEC(sec, msec)
Definition: util_func.h:40
static void cleanup(int signo)
Definition: shard_proxy.c:81
void proxy_term(void)
Definition: shard_proxy.c:65
int proxy_handler_initialize(void)
static int proxy_shm_initialize(void)
Definition: shard_proxy.c:92
int argc
Definition: dynamic_load.c:951
T_PROXY_INFO * shard_shm_find_proxy_info(T_SHM_PROXY *proxy_p, int proxy_id)
Definition: shard_shm.c:419
int parse_int(int *ret_p, const char *str_p, int base)
Definition: porting.c:2290
int proxy_io_process(void)
int max_prepared_stmt_count
Definition: broker_shm.h:482
#define SHARD_ERR(f, a...)
#define SHM_PROXY
Definition: broker_shm.h:65
void shard_stmt_destroy(void)
int wsa_initialize()
T_SHM_SHARD_CONN * shard_metadata_get_conn(T_SHM_PROXY *shm_proxy_p)
int main(int argc, char *argv[])
Definition: shard_proxy.c:163
void proxy_timer_process(void)
T_SHM_SHARD_USER * shard_metadata_get_user(T_SHM_PROXY *shm_proxy_p)
void proxy_io_destroy(void)
void proxy_handler_process(void)
static void proxy_set_hang_check_time(void)
Definition: shard_proxy.c:246
int proxy_id
Definition: shard_proxy.c:45
#define PROXY_ID_ENV_STR
T_SHM_SHARD_KEY * shm_key_p
Definition: shard_proxy.c:51
T_SHM_SHARD_USER * shm_user_p
Definition: shard_proxy.c:50
int proxy_io_initialize(void)
#define NULL
Definition: freelistheap.h:34
int appl_server_shm_id
Definition: broker_shm.h:516
T_SHM_SHARD_KEY * shard_metadata_get_key(T_SHM_PROXY *shm_proxy_p)
void proxy_handler_destroy(void)
T_PROXY_INFO * proxy_info_p
Definition: shard_proxy.c:48
static void error(const char *msg)
Definition: gencat.c:331
void proxy_access_log_close(void)
const char ** argv
Definition: dynamic_load.c:952
static void proxy_unset_hang_check_time(void)
Definition: shard_proxy.c:255
#define PROXY_SHM_KEY_STR
#define PROXY_LOG(level, fmt, args...)
int register_fn_get_shard_key(void)
void proxy_log_close(void)
int appl_server_shm_id
Definition: shard_proxy.c:42
int shard_stmt_initialize(int initial_size)
bool proxy_Keep_running
Definition: shard_proxy.c:57
T_SHM_APPL_SERVER * shm_as_p
Definition: shard_proxy.c:43
void * uw_shm_open(int shm_key, int which_shm, T_SHM_MODE shm_mode)
Definition: broker_shm.c:139
time_t claimed_alive_time
Definition: broker_shm.h:511
T_SHM_SHARD_CONN * shm_conn_p
Definition: shard_proxy.c:52
T_SHM_PROXY * shm_proxy_p
Definition: shard_proxy.c:47
const char ** p
Definition: dynamic_load.c:945
#define SHM_APPL_SERVER
Definition: broker_shm.h:63