CUBRID Engine  latest
connection_defs.h
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_defs.h - all the #define, the structure defs and the typedefs
22  * for the client/server implementation
23  */
24 
25 #ifndef _CONNECTION_DEFS_H_
26 #define _CONNECTION_DEFS_H_
27 
28 #ident "$Id$"
29 
30 #include "boot.h"
31 #if defined(SERVER_MODE)
32 #include "connection_list_sr.h"
33 #include "critical_section.h"
34 #endif
35 #include "error_manager.h"
36 #include "memory_alloc.h"
37 #include "porting.h"
38 #include "thread_compat.hpp"
39 
40 #if defined(WINDOWS)
41 #include <dos.h>
42 #endif // WINDOWS
43 #include <stdio.h>
44 #if defined(WINDOWS)
45 #include <process.h>
46 #else
47 #include <poll.h>
48 #endif /* WINDOWS */
49 #if !defined(WINDOWS) && defined(SERVER_MODE)
50 #include <pthread.h>
51 #endif /* !WINDOWS && SERVER_MODE */
52 
53 #if defined (__cplusplus)
54 #include <atomic>
55 #endif // C++
56 
57 #define NUM_MASTER_CHANNEL 1
58 
59 /*
60  * These are the types of top-level commands sent to the master server
61  * from the client when initiating a connection. They distinguish the
62  * difference between an information connection and a user connection.
63  */
65 {
67  INFO_REQUEST = 1, /* get runtime info from the master server */
68  DATA_REQUEST = 2, /* get data from the database server */
69  SERVER_REQUEST = 3, /* let new server attach */
70  UNUSED_REQUEST = 4, /* unused request - leave it for compatibility */
71  SERVER_REQUEST_NEW = 5, /* new-style server request */
73 };
74 
75 /*
76  * These are the responses from the master to a server
77  * when it is trying to connect and register itself.
78  */
80 {
85 };
86 
87 /*
88  * These are the types of requests sent by the information client to
89  * the master.
90  */
92 {
110  GET_REPL_LIST = 20, /* REPL: get the info. for a process */
111  GET_ALL_LIST = 21, /* REPL: get the info. for all processes */
112  GET_REPL_COUNT = 22, /* REPL: get the # of repl processes */
113  GET_ALL_COUNT = 23, /* REPL: get the # of all processes */
114  KILL_REPL_SERVER = 24, /* REPL: kill the repl process */
115  GET_SERVER_HA_MODE = 25, /* HA: get server ha mode */
116  GET_HA_NODE_LIST = 26, /* HA: get ha node list */
117  GET_HA_NODE_LIST_VERBOSE = 27, /* HA: get ha node list verbose */
118  GET_HA_PROCESS_LIST = 28, /* HA: get ha process list */
119  GET_HA_PROCESS_LIST_VERBOSE = 29, /* HA: get ha process list verbose */
120  DEREGISTER_HA_PROCESS_BY_PID = 30, /* HA: deregister ha process by pid */
121  RECONFIG_HEARTBEAT = 31, /* HA: reconfigure ha node */
122  DEACTIVATE_HEARTBEAT = 32, /* HA: deactivate */
123  ACTIVATE_HEARTBEAT = 33, /* HA: activate */
124  KILL_ALL_HA_PROCESS = 34, /* HA: kill all ha processes */
125  IS_REGISTERED_HA_PROC = 35, /* HA: check registered ha process */
126  DEREGISTER_HA_PROCESS_BY_ARGS = 36, /* HA: deregister ha process by args */
127  GET_HA_PING_HOST_INFO = 37, /* HA: get ping hosts info */
128  DEACT_STOP_ALL = 38, /* HA: prepare for deactivation */
129  DEACT_CONFIRM_STOP_ALL = 39, /* HA: confirm preparation for deactiavtion */
130  DEACT_CONFIRM_NO_SERVER = 40, /* HA: confirm the completion of deactivation */
131  GET_HA_ADMIN_INFO = 41, /* HA: get administrative info */
132  GET_SERVER_STATE = 42, /* broker: get the server state */
133  START_HA_UTIL_PROCESS = 43 /* HA: start ha utility process */
134 };
135 
136 /*
137  * These are the types of requests sent between the master and the servers.
138  */
140 {
154 };
156 
157 /*
158  * These are the status codes for the connection structure which represent
159  * the state of the connection.
160  */
162 {
166 };
167 
168 /*
169  * These are the types of fds in the socket queue.
170  */
171 enum
172 {
176 };
177 
178 /*
179  * These are the types of "packets" that can be sent over the comm interface.
180  */
182 {
188 };
189 
190 /*
191  * These are the status conditions that can be returned when a client
192  * is trying to get a connection.
193  */
195 {
199  SERVER_IS_RECOVERING = 3, /* not used */
200  SERVER_HAS_SHUT_DOWN = 4, /* not used */
201  ERROR_MESSAGE_FROM_MASTER = 5, /* an error message is returned */
206 };
207 
208 /*
209  * These are the error values returned by the client and server interfaces
210  */
212 {
228  OS_ERROR = 16,
231 };
232 
233 /*
234  * Server's request_handler status codes.
235  * Assigned to error_p in current socket queue entry.
236  */
238 {
242 };
243 
244 /*
245  * There are the modes to check peer-alive.
246  */
248 {
253 };
254 #define CHECK_CLIENT_IS_ALIVE() \
255  (prm_get_integer_value (PRM_ID_CHECK_PEER_ALIVE) == CSS_CHECK_PEER_ALIVE_BOTH \
256  || prm_get_integer_value (PRM_ID_CHECK_PEER_ALIVE) == CSS_CHECK_PEER_ALIVE_SERVER_ONLY)
257 #define CHECK_SERVER_IS_ALIVE() \
258  (prm_get_integer_value (PRM_ID_CHECK_PEER_ALIVE) == CSS_CHECK_PEER_ALIVE_BOTH \
259  || prm_get_integer_value (PRM_ID_CHECK_PEER_ALIVE) == CSS_CHECK_PEER_ALIVE_CLIENT_ONLY)
260 
261 /*
262  * HA mode
263  */
265 {
267  HA_MODE_FAIL_OVER = 1, /* unused */
269  HA_MODE_LAZY_BACK = 3, /* not implemented yet */
272 };
273 typedef enum ha_mode HA_MODE;
274 #define HA_MODE_OFF_STR "off"
275 #define HA_MODE_FAIL_OVER_STR "fail-over"
276 #define HA_MODE_FAIL_BACK_STR "fail-back"
277 #define HA_MODE_LAZY_BACK_STR "lazy-back"
278 #define HA_MODE_ROLE_CHANGE_STR "role-change"
279 #define HA_MODE_REPLICA_STR "replica"
280 #define HA_MODE_ON_STR "on"
281 
282 #define HA_GET_MODE() ((HA_MODE) prm_get_integer_value (PRM_ID_HA_MODE))
283 #define HA_DISABLED() (HA_GET_MODE () == HA_MODE_OFF)
284 
285 /*
286  * HA server mode
287  */
289 {
293  HA_SERVER_MODE_PRIMARY = 0, /* alias of active */
294  HA_SERVER_MODE_SECONDARY = 1, /* alias of standby */
295  HA_SERVER_MODE_TERNARY = 2 /* alias of backup */
296 };
298 #define HA_SERVER_MODE_ACTIVE_STR "active"
299 #define HA_SERVER_MODE_STANDBY_STR "standby"
300 #define HA_SERVER_MODE_BACKUP_STR "backup"
301 #define HA_SERVER_MODE_PRIMARY_STR "primary"
302 #define HA_SERVER_MODE_SECONDARY_STR "secondary"
303 #define HA_SERVER_MODE_TERNARY_STR "ternary"
304 
305 /*
306  * HA log applier state
307  */
309 {
316 };
318 #define HA_LOG_APPLIER_STATE_UNREGISTERED_STR "unregistered"
319 #define HA_LOG_APPLIER_STATE_RECOVERING_STR "recovering"
320 #define HA_LOG_APPLIER_STATE_WORKING_STR "working"
321 #define HA_LOG_APPLIER_STATE_DONE_STR "done"
322 #define HA_LOG_APPLIER_STATE_ERROR_STR "error"
323 
324 #define HA_CHANGE_MODE_DEFAULT_TIMEOUT_IN_SECS 5
325 #define HA_CHANGE_MODE_IMMEDIATELY 0
326 
327 #define HA_DELAY_ERR_CORRECTION 1
328 
329 #define HA_REQUEST_SUCCESS "1\0"
330 #define HA_REQUEST_FAILURE "0\0"
331 #define HA_REQUEST_RESULT_SIZE 2
332 
333 /*
334  * This constant defines the maximum size of a msg from the master to the
335  * server. Every msg between the master and the server will transmit this
336  * many bytes. A constant msg size is necessary since the protocol does
337  * not pre-send the msg length to the server before sending the actual msg.
338  */
339 #define MASTER_TO_SRV_MSG_SIZE 1024
340 
341 #ifdef PRINTING
342 #define TPRINTF(error_string, arg) \
343  do \
344  { \
345  fprintf (stderr, error_string, (arg)); \
346  fflush (stderr); \
347  } \
348  while (0)
349 
350 #define TPRINTF2(error_string, arg1, arg2) \
351  do \
352  { \
353  fprintf (stderr, error_string, (arg1), (arg2)); \
354  fflush (stderr); \
355  } \
356  while (0)
357 #else /* PRINTING */
358 #define TPRINTF(error_string, arg)
359 #define TPRINTF2(error_string, arg1, arg2)
360 #endif /* PRINTING */
361 
362 /* TODO: 64Bit porting */
363 #define HIGH16BITS(X) (((X) >> 16) & 0xffffL)
364 #define LOW16BITS(X) ((X) & 0xffffL)
365 #define DEFAULT_HEADER_DATA {0,0,0,NULL_TRAN_INDEX,0,0,0,0,0}
366 
367 #define CSS_RID_FROM_EID(eid) ((unsigned short) LOW16BITS(eid))
368 #define CSS_ENTRYID_FROM_EID(eid) ((unsigned short) HIGH16BITS(eid))
369 
370 #define NET_HEADER_FLAG_INVALIDATE_SNAPSHOT 0x8000
371 
372 /*
373  * This is the format of the header for each command packet that is sent
374  * across the network.
375  */
376 typedef struct packet_header NET_HEADER;
378 {
379  int type;
380  int version;
381  int host_id;
384  int db_error;
386  unsigned short flags;
388 };
389 
390 /*
391  * These are the data definitions for the queuing routines.
392  */
395 {
397  char *buffer;
398 
399 #if !defined(SERVER_MODE)
400  unsigned int key;
401 #else
402  int key;
403 #endif
404 
405  int size;
406  int rc;
409  int db_error;
410 
411 #if !defined(SERVER_MODE)
412  char lock;
413 #endif
414 };
415 #if defined(SERVER_MODE)
416 struct session_state;
417 #endif
418 /*
419  * This data structure is the interface between the client and the
420  * communication software to identify the data connection.
421  */
424 {
426  unsigned short request_id;
427  int status; /* CONN_OPEN, CONN_CLOSED, CONN_CLOSING = 3 */
430  int db_error;
431  bool in_transaction; /* this client is in-transaction or out-of- */
432  bool reset_on_commit; /* set reset_on_commit when commit/abort */
433 
434 #if defined(SERVER_MODE)
435  int idx; /* connection index */
436  BOOT_CLIENT_TYPE client_type;
437  SYNC_RMUTEX rmutex; /* connection mutex */
438 
439  bool stop_talk; /* block and stop this connection */
440  bool ignore_repl_delay; /* don't do reset_on_commit by the delay of replication */
441  unsigned short stop_phase;
442 
443  char *version_string; /* client version string */
444 
445  CSS_QUEUE_ENTRY *free_queue_list;
446  struct css_wait_queue_entry *free_wait_queue_list;
447  char *free_net_header_list;
448  int free_queue_count;
449  int free_wait_queue_count;
450  int free_net_header_count;
451 
452  CSS_LIST request_queue; /* list of requests */
453  CSS_LIST data_queue; /* list of data packets */
454  CSS_LIST data_wait_queue; /* list of waiters */
455  CSS_LIST abort_queue; /* list of aborted requests */
456  CSS_LIST buffer_queue; /* list of buffers queued for data */
457  CSS_LIST error_queue; /* list of (server) error messages */
458  struct session_state *session_p; /* session object for current request */
459 #else
460  FILE *file;
461  CSS_QUEUE_ENTRY *request_queue; /* the header for unseen requests */
462  CSS_QUEUE_ENTRY *data_queue; /* header for unseen data packets */
463  CSS_QUEUE_ENTRY *abort_queue; /* queue of aborted requests */
464  CSS_QUEUE_ENTRY *buffer_queue; /* header of buffers queued for data */
465  CSS_QUEUE_ENTRY *error_queue; /* queue of (server) error messages */
466  void *cnxn;
467 #endif
470 
471 #if defined __cplusplus
472  // transaction ID manipulation
473  void set_tran_index (int tran_index);
474  int get_tran_index (void);
475 
476  // request count manipulation
477  void add_pending_request ();
478  void start_request ();
479  bool has_pending_request () const;
480  void init_pending_request ();
481 
482 private:
483  // note - I want to protect this.
484  int transaction_id;
485  // *INDENT-OFF*
486  std::atomic<size_t> pending_request_count;
487  // *INDENT-ON*
488 #else // not c++ = c
490 #endif // not c++ = c
491 };
492 
493 /*
494  * This is the mapping entry from a host/key to/from the entry id.
495  */
498 {
499  char *key; /* host name (or some such) */
500  CSS_CONN_ENTRY *conn; /* the connection */
501 #if !defined(SERVER_MODE)
503 #endif
504  unsigned short id; /* host id to help identify the connection */
505 };
506 
507 /*
508  * This data structure is the information of user access status written
509  * when client login server.
510  */
513 {
514  char db_user[DB_MAX_USER_LENGTH];
515  time_t time;
517  char program_name[32];
519 };
520 
521 #endif /* _CONNECTION_DEFS_H_ */
int status
void * cnxn
unsigned short id
CSS_QUEUE_ENTRY * buffer_queue
css_error_code
char * key
int SOCKET
Definition: porting.h:482
enum db_client_type BOOT_CLIENT_TYPE
int db_error
int size
SOCKET fd
ha_mode
CSS_CONN_ENTRY * conn
unsigned short flags
css_client_request
unsigned int key
enum ha_server_mode HA_SERVER_MODE
int transaction_id
css_conn_status
LAST_ACCESS_STATUS * next
CSS_QUEUE_ENTRY * data_queue
int transaction_id
int rc
Definition: connection_sr.c:96
unsigned short request_id
enum ha_log_applier_state HA_LOG_APPLIER_STATE
FILE * file
ha_server_mode
bool in_transaction
CSS_QUEUE_ENTRY * abort_queue
CSS_MAP_ENTRY * next
int invalidate_snapshot
char lock
ha_log_applier_state
bool reset_on_commit
css_master_response
CSS_QUEUE_ENTRY * next
css_status_code
unsigned int SESSION_ID
Definition: dbtype_def.h:480
CSS_QUEUE_ENTRY * error_queue
enum ha_mode HA_MODE
enum css_server_request CSS_SERVER_REQUEST
int invalidate_snapshot
css_server_request
css_check_peer_alive
CSS_QUEUE_ENTRY * request_queue
CSS_CONN_ENTRY * next
const char * program_name
Definition: cas.c:147
#define CUB_MAXHOSTNAMELEN
Definition: porting.h:379
int db_error
static char * host
css_command_type
#define DB_MAX_USER_LENGTH
Definition: dbtype_def.h:498
css_packet_type
SESSION_ID session_id
int client_id
css_status
char * buffer