CUBRID Engine  latest
log_comm.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  * log_comm.c - log and recovery manager (at client & server)
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <assert.h>
28 #if !defined(WINDOWS)
29 #include <sys/time.h>
30 #endif /* !WINDOWS */
31 
32 #include "log_comm.h"
33 
34 #include "memory_alloc.h"
35 #include "storage_common.h"
36 #include "error_manager.h"
37 #include "porting.h"
38 #include "environment_variable.h"
39 #include "system_parameter.h"
40 #include "misc_string.h"
41 #include "intl_support.h"
42 #include "log_common_impl.h"
43 #if defined (SERVER_MODE)
44 #include "vacuum.h"
45 #endif /* SERVER_MODE */
46 #if !defined(WINDOWS)
47 #if defined(CS_MODE)
48 #include "db.h"
49 #endif /* CS_MODE */
50 #if defined(SERVER_MODE)
51 #include "server_support.h"
52 #include "connection_defs.h"
53 #endif /* SERVER_MODE */
54 #endif /* !WINDOWS */
55 #if defined (SERVER_MODE)
56 #include "thread_manager.hpp"
57 #endif // SERVER_MODE
58 
60 {
62  const char *name;
63 };
65 
68  "TRAN_RECOVERY"},
69  {TRAN_ACTIVE,
70  "TRAN_ACTIVE"},
72  "TRAN_UNACTIVE_COMMITTED"},
74  "TRAN_UNACTIVE_WILL_COMMIT"},
76  "TRAN_UNACTIVE_COMMITTED_WITH_POSTPONE"},
78  "TRAN_UNACTIVE_TOPOPE_COMMITTED_WITH_POSTPONE"},
80  "TRAN_UNACTIVE_ABORTED"},
82  "TRAN_UNACTIVE_UNILATERALLY_ABORTED"},
84  "TRAN_UNACTIVE_2PC_PREPARE"},
86  "TRAN_UNACTIVE_2PC_COLLECTING_PARTICIPANT_VOTES"},
88  "TRAN_UNACTIVE_2PC_ABORT_DECISION"},
90  "TRAN_UNACTIVE_2PC_COMMIT_DECISION"},
92  "TRAN_UNACTIVE_COMMITTED_INFORMING_PARTICIPANTS"},
94  "TRAN_UNACTIVE_ABORTED_INFORMING_PARTICIPANTS"},
96  "TRAN_STATE_UNKNOWN"}
97 };
98 
100 {
102  const char *name;
103 };
105 
107  {TRAN_SERIALIZABLE, "SERIALIZABLE"},
108  {TRAN_REPEATABLE_READ, "REPEATABLE READ"},
109  {TRAN_READ_COMMITTED, "COMMITTED READ"},
110  {TRAN_UNKNOWN_ISOLATION, "TRAN_UNKNOWN_ISOLATION"}
111 };
112 
113 const int LOG_MIN_NBUFFERS = 3;
114 
115 /*
116  * log_state_string - Translate state into string representation
117  *
118  * return:
119  *
120  * state(in): Transaction state
121  *
122  * NOTE: Translate state into a string representation.
123  */
124 const char *
126 {
127  int num = sizeof (log_Tran_state_names) / sizeof (TRAN_STATE_NAME);
128  int i;
129 
130  for (i = 0; i < num; i++)
131  {
132  if (log_Tran_state_names[i].state == state)
133  {
134  return log_Tran_state_names[i].name;
135  }
136  }
137 
138  return "TRAN_STATE_UNKNOWN";
139 
140 }
141 
142 
143 /*
144  * log_state_short_string - Translate state into string representation
145  * without TRAN_ prefix
146  *
147  * return:
148  *
149  * state(in): Transaction state
150  */
151 const char *
153 {
154  const char *state_string;
155  int skip_len;
156 
157  skip_len = sizeof ("TRAN_") - 1;
158  state_string = log_state_string (state) + skip_len;
159 
160  return state_string;
161 }
162 
163 /*
164  * log_isolation_string - Translate isolation level into string representation
165  *
166  * return:
167  *
168  * isolation(in): Isolation level. One of the following:
169  * TRAN_REPEATABLE_READ
170  * TRAN_READ_COMMITTED
171  * TRAN_SERIALIZABLE
172  *
173  * NOTE:Translate degree of consistency into a string representation.
174  */
175 const char *
177 {
178  int num = sizeof (log_Isolation_names) / sizeof (TRAN_ISOLATION_NAME);
179  int i;
180 
181  for (i = 0; i < num; i++)
182  {
183  if (log_Isolation_names[i].isolation == isolation)
184  {
185  return log_Isolation_names[i].name;
186  }
187  }
188 
189  return "TRAN_UNKNOWN_ISOLATION";
190 }
191 
192 /*
193  * log_dump_log_info - Dump log information
194  *
195  * return: nothing
196  *
197  * logname_info(in): Name of the log information file
198  * also_stdout(in):
199  * fmt(in): Format for the variable list of arguments (like printf)
200  * va_alist: Variable number of arguments
201  *
202  * NOTE:Dump some log information
203  */
204 int
205 log_dump_log_info (const char *logname_info, bool also_stdout, const char *fmt, ...)
206 {
207  FILE *fp; /* Pointer to file */
208  va_list ap; /* Point to each unnamed arg in turn */
209  time_t log_time;
210  struct tm log_tm;
211  struct tm *log_tm_p = &log_tm;
212  struct timeval tv;
213  char time_array[128];
214  char time_array_of_log_info[255];
215 
216  va_start (ap, fmt);
217 
218  if (logname_info == NULL)
219  {
220  return ER_FAILED;
221  }
222 
223  fp = fopen (logname_info, "a");
224  if (fp == NULL)
225  {
227  va_end (ap);
228  return ER_LOG_MOUNT_FAIL;
229  }
230 
231  log_time = time (NULL);
232  log_tm_p = localtime_r (&log_time, &log_tm);
233  if (log_tm_p == NULL)
234  {
235  strcpy (time_array_of_log_info, "Time: 00/00/00 00:00:00.000 - ");
236  }
237  else
238  {
239  gettimeofday (&tv, NULL);
240  strftime (time_array, 128, "%m/%d/%y %H:%M:%S", log_tm_p);
241  snprintf (time_array_of_log_info, 255, "Time: %s.%03ld - ", time_array, tv.tv_usec / 1000);
242  }
243 
244  if (strlen (time_array_of_log_info) != TIME_SIZE_OF_DUMP_LOG_INFO)
245  {
246  strcpy (time_array_of_log_info, "Time: 00/00/00 00:00:00.000 - ");
247  }
248 
249  fprintf (fp, "%s", time_array_of_log_info);
250 
251  (void) vfprintf (fp, fmt, ap);
252  fflush (fp);
253  fclose (fp);
254 
255 #if !defined(NDEBUG)
256  if (also_stdout && prm_get_bool_value (PRM_ID_LOG_TRACE_DEBUG))
257  {
258  va_start (ap, fmt);
259  (void) vfprintf (stdout, fmt, ap);
260  fflush (stdout);
261  }
262 #endif
263 
264  va_end (ap);
265 
266  return NO_ERROR;
267 }
268 
269 bool
271 {
272 #if defined(WINDOWS) || defined(SA_MODE)
273  return false;
274 
275 #elif defined(CS_MODE) /* WINDOWS || SA_MODE */
276  int client_type;
277 
278  client_type = db_get_client_type ();
279  if (client_type == DB_CLIENT_TYPE_LOG_COPIER || client_type == DB_CLIENT_TYPE_LOG_APPLIER)
280  {
281  return false;
282  }
283 
284  return true;
285 
286 #elif defined(SERVER_MODE) /* CS_MODE */
287  HA_SERVER_STATE ha_state;
288 
289  /* Vacuum workers are not allowed to reach this code */
290  if (LOG_FIND_CURRENT_TDES () == NULL || !LOG_FIND_CURRENT_TDES ()->is_active_worker_transaction ())
291  {
292  return false;
293  }
294 
295  if (HA_DISABLED ())
296  {
297  return false;
298  }
299 
300  ha_state = css_ha_server_state ();
301  if (ha_state != HA_SERVER_STATE_ACTIVE && ha_state != HA_SERVER_STATE_TO_BE_STANDBY)
302  {
303  return false;
304  }
305 
307 
308  return true;
309 #else /* SERVER_MODE */
310 
311  return false;
312 #endif
313 }
#define NO_ERROR
Definition: error_code.h:46
#define ER_FAILED
Definition: error_code.h:47
const char * log_state_short_string(TRAN_STATE state)
Definition: log_comm.c:152
#define ER_LOG_MOUNT_FAIL
Definition: error_code.h:141
const char * log_state_string(TRAN_STATE state)
Definition: log_comm.c:125
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
TRAN_ISOLATION isolation
Definition: log_comm.c:101
#define assert(x)
TRAN_STATE state
Definition: log_comm.c:61
DB_TRAN_ISOLATION
Definition: dbtran_def.h:26
int db_Disable_modifications
Definition: db_macro.c:90
#define NULL
Definition: freelistheap.h:34
#define TIME_SIZE_OF_DUMP_LOG_INFO
Definition: log_comm.h:31
static TRAN_STATE_NAME log_Tran_state_names[]
Definition: log_comm.c:66
const char * name
Definition: log_comm.c:62
const char * log_isolation_string(TRAN_ISOLATION isolation)
Definition: log_comm.c:176
LOG_TDES * LOG_FIND_CURRENT_TDES(THREAD_ENTRY *thread_p=NULL)
Definition: log_impl.h:1115
#define ARG_FILE_LINE
Definition: error_manager.h:44
const int LOG_MIN_NBUFFERS
Definition: log_comm.c:113
const char * name
Definition: log_comm.c:102
int db_get_client_type(void)
Definition: db_admin.c:489
#define strlen(s1)
Definition: intl_support.c:43
bool prm_get_bool_value(PARAM_ID prm_id)
HA_SERVER_STATE css_ha_server_state(void)
int i
Definition: dynamic_load.c:954
int log_dump_log_info(const char *logname_info, bool also_stdout, const char *fmt,...)
Definition: log_comm.c:205
#define HA_DISABLED()
TRAN_STATE
Definition: log_comm.h:36
enum ha_server_state HA_SERVER_STATE
Definition: boot.h:126
bool log_does_allow_replication(void)
Definition: log_comm.c:270
static TRAN_ISOLATION_NAME log_Isolation_names[]
Definition: log_comm.c:106