CUBRID Engine  latest
connection_globals.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  * connection_globals.c - The global variable, function definitions used by connection
22  *
23  * Note:
24  */
25 
26 #ident "$Id$"
27 
28 #include "config.h"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <assert.h>
33 
34 #include "porting.h"
35 #include "memory_alloc.h"
36 #include "boot.h"
37 #include "connection_globals.h"
38 #include "utility.h"
39 #include "system_parameter.h"
40 
41 const char *css_Service_name = "cubrid";
42 int css_Service_id = 1523;
43 
44 SOCKET css_Pipe_to_master = INVALID_SOCKET; /* socket for Master->Slave communication */
45 
46 /* Stuff for the new client/server/master protocol */
49 
50 /* For Windows, we only support the new style of connection protocol. */
51 #if defined(WINDOWS)
53 #else /* WINDOWS */
55 #endif /* WINDOWS */
56 
57 /* do not change first 4 bytes of css_Net_magic */
58 char css_Net_magic[CSS_NET_MAGIC_SIZE] = { 0x00, 0x00, 0x00, 0x01, 0x20, 0x08, 0x11, 0x22 };
59 
60 static bool css_Is_conn_rules_initialized = false;
61 
62 static int css_get_normal_client_max_conn (void);
63 static int css_get_admin_client_max_conn (void);
64 static int css_get_ha_client_max_conn (void);
65 
66 static bool css_is_normal_client (BOOT_CLIENT_TYPE client_type);
67 static bool css_is_admin_client (BOOT_CLIENT_TYPE client_type);
68 static bool css_is_ha_client (BOOT_CLIENT_TYPE client_type);
69 
70 static int css_get_required_conn_num_for_ha (void);
71 
76 };
77 
78 const int css_Conn_rules_size = DIM (css_Conn_rules);
79 
80 /*
81  * css_get_normal_client_max_conn() -
82  * return: max_clients set by user
83  */
84 static int
86 {
88 }
89 
90 /*
91  * css_get_admin_client_max_conn() -
92  * return: a num of reserved conn for admin clients
93  */
94 static int
96 {
97  return 1;
98 }
99 
100 /*
101  * css_get_ha_client_max_conn() -
102  * return: a num of reserved conn for HA clients
103  */
104 static int
106 {
108 }
109 
110 /*
111  * css_is_normal_client() -
112  * return: whether a client is a normal client or not
113  */
114 static bool
116 {
117  int i;
118 
119  for (i = 0; i < css_Conn_rules_size; i++)
120  {
121  if (i == CSS_CR_NORMAL_ONLY_IDX)
122  {
123  continue;
124  }
125 
126  if (css_Conn_rules[i].check_client_type_fn (client_type))
127  {
128  return false;
129  }
130  }
131  return true;
132 }
133 
134 /*
135  * css_is_admin_client() -
136  * return: whether a client is a admin client or not
137  */
138 static bool
140 {
141  return BOOT_ADMIN_CLIENT_TYPE (client_type);
142 }
143 
144 /*
145  * css_is_ha_client() -
146  * return: whether a client is a HA client or not
147  */
148 static bool
150 {
151  return BOOT_LOG_REPLICATOR_TYPE (client_type);
152 }
153 
154 /*
155  * css_get_required_conn_num_for_ha() - calculate the number
156  * of connections required for HA
157  * return: the number of connections required for HA
158  */
159 static int
161 {
162  int required_conn_num = 0, num_of_nodes = 0;
163  char *ha_node_list_p = NULL;
164  char *ha_replica_list_p = NULL;
165  int curr_ha_mode;
166 
167 #if defined (SA_MODE)
168  curr_ha_mode = util_get_ha_mode_for_sa_utils ();
169 #else /* SA_MODE */
170  curr_ha_mode = HA_GET_MODE ();
171 #endif /* !SA_MODE */
172 
173  if (curr_ha_mode == HA_MODE_OFF)
174  {
175  return 0;
176  }
177 
178  ha_node_list_p = prm_get_string_value (PRM_ID_HA_NODE_LIST);
179  num_of_nodes = util_get_num_of_ha_nodes (ha_node_list_p);
180 
181  if (HA_GET_MODE () == HA_MODE_REPLICA)
182  {
183  /* one applylogdb for each node */
184  return num_of_nodes * 2;
185  }
186 
187  ha_replica_list_p = prm_get_string_value (PRM_ID_HA_REPLICA_LIST);
188  /* one copylogdb for each replica */
189  required_conn_num = util_get_num_of_ha_nodes (ha_replica_list_p);
190 
191  /* applylogdb and copylogdb for each node */
192  required_conn_num += (num_of_nodes - 1) * 3;
193 
194  return required_conn_num;
195 }
196 
197 /*
198  * css_init_conn_rules() - initialize connection rules
199  * which are determined in runtime
200  * return:
201  */
202 void
204 {
205  int i;
206 
207  for (i = 0; i < css_Conn_rules_size; i++)
208  {
209  assert (css_Conn_rules[i].get_max_conn_num_fn != NULL);
210 
211  css_Conn_rules[i].max_num_conn = css_Conn_rules[i].get_max_conn_num_fn ();
212  }
213 
215 
216  return;
217 }
218 
219 /*
220  * css_get_max_conn() -
221  * return: max_clients + a total num of reserved connections
222  */
223 int
225 {
226  int i, total = 0;
227 
229  {
231  }
232 
233  for (i = 0; i < css_Conn_rules_size; i++)
234  {
235  total += css_Conn_rules[i].max_num_conn;
236  }
237 
238  return total;
239 }
static int css_get_ha_client_max_conn(void)
CSS_GET_MAX_CONN_NUM get_max_conn_num_fn
int SOCKET
Definition: porting.h:482
const int css_Conn_rules_size
#define BOOT_LOG_REPLICATOR_TYPE(client_type)
Definition: boot.h:57
enum db_client_type BOOT_CLIENT_TYPE
#define BOOT_ADMIN_CLIENT_TYPE(client_type)
Definition: boot.h:50
SOCKET css_Pipe_to_master
CSS_CONN_RULE_INFO css_Conn_rules[]
int css_Server_inhibit_connection_socket
int css_Server_use_new_connection_protocol
static int css_get_required_conn_num_for_ha(void)
static int css_get_admin_client_max_conn(void)
#define INVALID_SOCKET
Definition: porting.h:483
#define HA_GET_MODE()
#define assert(x)
int prm_get_integer_value(PARAM_ID prm_id)
char css_Net_magic[CSS_NET_MAGIC_SIZE]
static bool css_is_ha_client(BOOT_CLIENT_TYPE client_type)
static int css_get_normal_client_max_conn(void)
#define NULL
Definition: freelistheap.h:34
SOCKET css_Server_connection_socket
#define CSS_NET_MAGIC_SIZE
void css_init_conn_rules(void)
int css_get_max_conn(void)
static bool css_is_normal_client(BOOT_CLIENT_TYPE client_type)
static bool css_Is_conn_rules_initialized
char * prm_get_string_value(PARAM_ID prm_id)
int css_Service_id
static bool css_is_admin_client(BOOT_CLIENT_TYPE client_type)
int i
Definition: dynamic_load.c:954
int util_get_num_of_ha_nodes(const char *node_list)
Definition: util_common.c:478
int util_get_ha_mode_for_sa_utils(void)
Definition: util_common.c:872
#define CSS_CR_NORMAL_ONLY_IDX
const char * css_Service_name