forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphys_rep_lsn.c
348 lines (294 loc) · 10.2 KB
/
phys_rep_lsn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <build/db.h>
#include <strings.h>
#include <logmsg.h>
#include <string.h>
#include "bdb_int.h"
#include <dbinc/db_swap.h>
#include "phys_rep_lsn.h"
#include "tranlog.h"
#include <cdb2api.h>
#include <parse_lsn.h>
#include "locks.h"
extern bdb_state_type *bdb_state;
int matchable_log_type(int rectype);
extern int gbl_verbose_physrep;
LOG_INFO get_last_lsn(bdb_state_type *bdb_state)
{
int rc;
/* get db internals */
DB_LOGC *logc;
DBT logrec;
DB_LSN last_log_lsn;
LOG_INFO log_info = {0};
rc = bdb_state->dbenv->log_cursor(bdb_state->dbenv, &logc, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: can't get log cursor rc %d\n", __func__, rc);
return log_info;
}
bzero(&logrec, sizeof(DBT));
logrec.flags = DB_DBT_MALLOC;
rc = logc->get(logc, &last_log_lsn, &logrec, DB_LAST);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: can't get last log record rc %d\n", __func__,
rc);
logc->close(logc, 0);
return log_info;
}
if (gbl_verbose_physrep)
logmsg(LOGMSG_USER, "%s: LSN %u:%u\n", __func__, last_log_lsn.file,
last_log_lsn.offset);
log_info.file = last_log_lsn.file;
log_info.offset = last_log_lsn.offset;
log_info.size = logrec.size;
if (logrec.data)
free(logrec.data);
logc->close(logc, 0);
return log_info;
}
int compare_log(DBT *logrec, void *blob, unsigned int blob_len)
{
int rc;
if (logrec->size != blob_len) {
rc = 1;
} else {
rc = memcmp(blob, logrec->data, blob_len);
}
return rc;
}
int find_log_timestamp(bdb_state_type *bdb_state, time_t time,
unsigned int *file, unsigned int *offset)
{
int rc;
u_int64_t my_time;
/* get db internals */
DB_LOGC *logc;
DBT logrec;
DB_LSN rec_lsn;
u_int32_t rectype;
/* get last record then iterate */
rc = bdb_state->dbenv->log_cursor(bdb_state->dbenv, &logc, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: can't get log cursor rc %d\n", __func__, rc);
return 1;
}
bzero(&logrec, sizeof(DBT));
logrec.flags = DB_DBT_REALLOC;
do {
do {
rc = logc->get(logc, &rec_lsn, &logrec, DB_PREV);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: can't get log record rc %d\n",
__func__, rc);
logc->close(logc, 0);
if (logrec.data)
free(logrec.data);
return 1;
}
LOGCOPY_32(&rectype, logrec.data);
} while (!matchable_log_type(rectype));
my_time = get_timestamp_from_matchable_record(logrec.data);
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s my ts is %"PRIu64", {%u:%u}\n", __func__,
my_time, rec_lsn.file, rec_lsn.offset);
}
} while (time < my_time);
if (logrec.data) {
free(logrec.data);
logrec.data = NULL;
}
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s ts is %"PRIu64", {%u:%u}\n", __func__, my_time,
rec_lsn.file, rec_lsn.offset);
}
*file = rec_lsn.file;
*offset = rec_lsn.offset;
return 0;
}
static int get_next_matchable(DB_LOGC *logc, LOG_INFO *info, int check_current,
DBT *logrec)
{
int rc;
u_int32_t rectype;
/* get db internals */
DB_LSN match_lsn;
LOG_INFO log_info;
log_info.file = log_info.offset = log_info.size = 0;
match_lsn.file = info->file;
match_lsn.offset = info->offset;
if (check_current) {
if ((rc = logc->get(logc, &match_lsn, logrec, DB_SET)) != 0) {
logmsg(LOGMSG_ERROR, "%s: can't find log record {%d:%d}, rc %d\n",
__func__, info->file, info->offset, rc);
return 1;
}
LOGCOPY_32(&rectype, logrec->data);
if (matchable_log_type(rectype)) {
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s: initial rec {%u:%u} is matchable\n",
__func__, info->file, info->offset);
}
assert(info->file == match_lsn.file);
assert(info->offset == match_lsn.offset);
info->size = logrec->size;
return 0;
}
}
do {
rc = logc->get(logc, &match_lsn, logrec, DB_PREV);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s: can't get prev log record for {%d:%d} rc"
"%d\n",
__func__, match_lsn.file, match_lsn.offset, rc);
free(logrec->data);
logrec->data = NULL;
return 1;
}
LOGCOPY_32(&rectype, logrec->data);
} while (!matchable_log_type(rectype));
info->file = match_lsn.file;
info->offset = match_lsn.offset;
info->size = logrec->size;
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s: Found matchable {%u:%u}\n", __func__,
info->file, info->offset);
}
return rc;
}
/* generator code */
uint32_t get_next_offset(DB_ENV *dbenv, LOG_INFO log_info)
{
return log_info.offset + log_info.size + dbenv->get_log_header_size(dbenv);
}
int apply_log(DB_ENV *dbenv, unsigned int file, unsigned int offset,
int64_t rectype, void *blob, int blob_len)
{
return dbenv->apply_log(dbenv, file, offset, rectype, blob, blob_len);
}
int truncate_log_lock(bdb_state_type *bdb_state, unsigned int file,
unsigned int offset, uint32_t flags)
{
extern int gbl_online_recovery;
char *msg = "truncate log";
int online = gbl_online_recovery;
if (flags &&
bdb_state->repinfo->master_host != bdb_state->repinfo->myhost) {
return send_truncate_to_master(bdb_state, file, offset);
}
if (online) {
BDB_READLOCK(msg);
} else {
BDB_WRITELOCK(msg);
}
bdb_state->dbenv->rep_verify_match(bdb_state->dbenv, file, offset, online);
/* have to get lock for recovery */
BDB_RELLOCK();
return 0;
}
LOG_INFO find_match_lsn(void *in_bdb_state, cdb2_hndl_tp *repl_db,
LOG_INFO start_info)
{
int rc;
char sql_cmd[128];
bdb_state_type *bdb_state = (bdb_state_type *)in_bdb_state;
void *blob;
char *lsn;
int blob_len;
int match_current = 1;
unsigned int match_file, match_offset;
LOG_INFO info = {0};
DB_LOGC *logc;
DBT logrec = {0};
rc = bdb_state->dbenv->log_cursor(bdb_state->dbenv, &logc, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: can't get log cursor rc %d\n", __func__, rc);
return info;
}
logrec.flags = DB_DBT_REALLOC;
while (
!(rc = get_next_matchable(logc, &start_info, match_current, &logrec))) {
match_current = 0;
snprintf(
sql_cmd, sizeof(sql_cmd),
"select * from comdb2_transaction_logs('{%d:%d}','{%d:%d}', 0)",
start_info.file, start_info.offset, start_info.file,
start_info.offset);
if ((rc = cdb2_run_statement(repl_db, sql_cmd)) == 0) {
if ((rc = cdb2_next_record(repl_db)) == CDB2_OK) {
lsn = (char *)cdb2_column_value(repl_db, 0);
if (!lsn) {
logmsg(LOGMSG_ERROR,
"%s: null lsn for probe of {%d:%d}."
" going to next record\n",
__func__, start_info.file, start_info.offset);
continue;
}
if ((rc = char_to_lsn(lsn, &match_file, &match_offset)) != 0) {
logmsg(LOGMSG_ERROR, "Could not parse lsn? %s\n", lsn);
continue;
}
/* check if lsns match, if not, then get next matchable */
if (match_file != start_info.file ||
match_offset != start_info.offset) {
logmsg(LOGMSG_ERROR,
"%s not same lsn{%u:%u} vs "
"{%u:%u}??? \n",
__func__, start_info.file, start_info.offset,
match_file, match_offset);
continue;
}
/* here lsns match, thus we can now compare them */
blob = cdb2_column_value(repl_db, 4);
blob_len = cdb2_column_size(repl_db, 4);
if ((rc = compare_log(&logrec, blob, blob_len)) == 0) {
info.file = match_file;
info.offset = match_offset;
info.size = blob_len;
info.gen = *(int64_t *)cdb2_column_value(repl_db, 2);
logc->close(logc, 0);
free(logrec.data);
logrec.data = NULL;
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s: found match at {%d:%d}\n",
__func__, start_info.file, start_info.offset);
}
return info;
} else {
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER, "%s: memcmp failed for {%d:%d}\n",
__func__, start_info.file, start_info.offset);
}
}
} else {
/* Didn't find a record: just go to previous */
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER,
"%s: probe of {%d:%d} failed, going to "
"previous\n",
__func__, start_info.file, start_info.offset);
}
}
} else {
/* Run statement failure: close cursor and handle & return */
if (gbl_verbose_physrep) {
logmsg(LOGMSG_USER,
"%s: %s returns %d, '%s': closing "
"connection\n",
__func__, sql_cmd, rc, cdb2_errstr(repl_db));
}
logc->close(logc, 0);
if (logrec.data) {
free(logrec.data);
logrec.data = NULL;
}
return info;
}
}
logmsg(LOGMSG_WARN, "No matchable lsns in the log\n");
logc->close(logc, 0);
if (logrec.data) {
free(logrec.data);
logrec.data = NULL;
}
return info;
}