forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrowlocks_util.c
278 lines (239 loc) · 9.06 KB
/
rowlocks_util.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
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "bdb_int.h"
#include <assert.h>
#include "locks.h"
#include "logmsg.h"
/* Get a rowlock. If the caller has provided a cursor, verify the pagelsn and
* return a good rcode only if it hasn't changed. */
int bdb_get_row_lock_pfunc(bdb_state_type *bdb_state, int rowlock_lid, int idx,
DBC *dbcp, int (*pfunc)(void *), void *parg,
unsigned long long genid, DB_LOCK *rlk, DBT *lkname,
int how)
{
DB_LOCK lk = {0}, *lkptr;
int rc;
/* Return the lock if the caller wants it */
if (rlk) {
lkptr = rlk;
} else {
lkptr = &lk;
}
/* Zero */
bzero(lkptr, sizeof(*lkptr));
/* Attempt to get the rowlock, block if cursor is null */
rc = bdb_lock_row_fromlid(bdb_state, rowlock_lid, idx, genid, how, lkptr,
lkname, dbcp || pfunc ? 1 : 0);
/* Return now if cursor is null */
if (!dbcp && !pfunc) {
if (rc && (DB_LOCK_DEADLOCK != rc && BDBERR_DEADLOCK_ROWLOCK != rc)) {
logmsg(LOGMSG_ERROR, "%s:%d rowlock lock error, rc=%d\n", __FILE__,
__LINE__, rc);
}
return rc;
}
/* Would have blocked */
if (rc == DB_LOCK_NOTGRANTED) {
/* Stash the lsn here */
DBCPS plsn = {{0}};
/* Pause all pagelock cursors in this txn */
if (pfunc) {
rc = pfunc(parg);
if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d error running pausefunc, rc=%d\n",
__FILE__, __LINE__, rc);
return rc;
}
}
/* Release pagelock */
if (dbcp) {
rc = dbcp->c_pause(dbcp, &plsn);
if (rc) {
/* Print a message for anything that's not a deadlock */
if (DB_LOCK_DEADLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d error pausing cursor, rc=%d\n",
__FILE__, __LINE__, rc);
}
/* Return CURSOR_STALE so the caller knows the cursor was
* closed. */
return DB_CUR_STALE;
}
}
#if defined DEBUG_ROWLOCKS
int pagelockcount = 0;
/*
* It turns out that there are (apparently) cases where a txn will hold
* onto a pagelock even after a cursor has been closed. I'm not sure
* what conditions cause this, but it seems like a bug. It happens
* rarely, so I'm going to code around it for now.
*/
if ((rc = bdb_state->dbenv->lock_locker_pagelockcount(
bdb_state->dbenv, rowlock_lid, &pagelockcount)) ||
pagelockcount > 0) {
logmsg(LOGMSG_FATAL,
"Error: blocking on rowlock when I'm holding pages!\n");
abort();
}
#endif
/* Blocking call - the LOCK_NOPAGELK means that this will return
* immediately with DEADLOCK if this lockerid is holding any pagelocks.
*/
rc = bdb_lock_row_fromlid_int(bdb_state, rowlock_lid, idx, genid, how,
lkptr, lkname, 0, DB_LOCK_NOPAGELK);
if (rc) {
if (BDBERR_DEADLOCK_ROWLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d rowlock lock error, rc=%d\n", __FILE__,
__LINE__, rc);
}
return rc;
}
/* Unpause */
if (dbcp) {
rc = dbcp->c_unpause(dbcp, &plsn);
/* Page changed from under us */
if (0 != rc) {
/* Remove this */
assert(DB_LOCK_DEADLOCK == rc || DB_CUR_STALE == rc);
/* Unlock row */
bdb_release_row_lock(bdb_state, lkptr);
/* Print errors we don't expect */
if (DB_LOCK_DEADLOCK != rc && DB_CUR_STALE != rc) {
logmsg(LOGMSG_ERROR, "%s:%d rowlock lock error, rc=%d\n",
__FILE__, __LINE__, rc);
}
/* Return CURSOR_STALE so the caller knows the cursor was
* closed. */
return DB_CUR_STALE;
}
}
/* Success */
return 0;
}
/* Another error */
else if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d lock_row on data returned %d.\n", __FILE__,
__LINE__, rc);
return rc;
}
/* Success */
return 0;
}
int bdb_get_row_lock(bdb_state_type *bdb_state, int rowlock_lid, int idx,
DBC *dbcp, unsigned long long genid, DB_LOCK *rlk,
DBT *lkname, int how)
{
return bdb_get_row_lock_pfunc(bdb_state, rowlock_lid, idx, dbcp, NULL, NULL,
genid, rlk, lkname, how);
}
/* Get a minmax lock: return a good rcode if the pagelsn has not changed.
* If the caller has provided a cursor, return a DB_CUR_STALE if cursor's
* page lsn has changed, so the caller can retry the search. */
int bdb_get_row_lock_minmaxlk_pfunc(bdb_state_type *bdb_state, int rowlock_lid,
DBC *dbcp, int (*pfunc)(void *), void *parg,
int ixnum, int stripe, int minmax,
DB_LOCK *rlk, DBT *lkname, int how)
{
DB_LOCK lk, *lkptr;
int rc;
/* Return the lock if the caller wants it */
if (rlk) {
lkptr = rlk;
} else {
lkptr = &lk;
}
/* Zero */
bzero(lkptr, sizeof(*lkptr));
/* Acquire min or max lock, block if cursor is null */
rc = bdb_lock_minmax(bdb_state, ixnum, stripe, minmax, how, lkptr, lkname,
rowlock_lid, dbcp || pfunc ? 1 : 0);
/* Return now if cursor is null */
if (!dbcp && !pfunc) {
if (rc && DB_LOCK_DEADLOCK != rc && BDBERR_DEADLOCK_ROWLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d minmax lock error, rc=%d\n", __FILE__,
__LINE__, rc);
}
return rc;
}
/* Would block */
if (rc == DB_LOCK_NOTGRANTED) {
DBCPS plsn = {{0}};
if (pfunc) {
rc = pfunc(parg);
if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d error running pausefunc, rc=%d\n",
__FILE__, __LINE__, rc);
return rc;
}
}
/* Release pagelock */
if (dbcp) {
rc = dbcp->c_pause(dbcp, &plsn);
if (0 != rc) {
/* Print a message for anything that's not a deadlock */
if (DB_LOCK_DEADLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d error pausing cursor, rc=%d\n",
__FILE__, __LINE__, rc);
}
return rc;
}
}
/* Blocking call */
rc = bdb_lock_minmax(bdb_state, ixnum, stripe, minmax, how, lkptr,
lkname, rowlock_lid, 0);
if (rc) {
if (BDBERR_DEADLOCK_ROWLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d rowlock lock error, rc=%d\n", __FILE__,
__LINE__, rc);
}
return rc;
}
/* Unpause */
if (dbcp) {
rc = dbcp->c_unpause(dbcp, &plsn);
/* Page changed from under us */
if (0 != rc) {
/* Remove this */
assert(DB_LOCK_DEADLOCK == rc || DB_CUR_STALE == rc);
/* Unlock row */
bdb_release_row_lock(bdb_state, lkptr);
/* Print errors we don't expect */
if (DB_LOCK_DEADLOCK != rc && DB_CUR_STALE != rc) {
logmsg(LOGMSG_ERROR, "%s:%d rowlock lock error, rc=%d\n",
__FILE__, __LINE__, rc);
}
return rc;
}
}
/* Success */
return 0;
}
/* Error */
else if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d lock_%s on index returned %d.\n", __FILE__,
__LINE__, (0 == how) ? "min" : "max", rc);
return rc;
}
/* Success */
return 0;
}
int bdb_get_row_lock_minmaxlk(bdb_state_type *bdb_state, int rowlock_lid,
DBC *dbcp, int ixnum, int stripe, int minmax,
DB_LOCK *rlk, DBT *lkname, int how)
{
return bdb_get_row_lock_minmaxlk_pfunc(bdb_state, rowlock_lid, dbcp, NULL,
NULL, ixnum, stripe, minmax, rlk,
lkname, how);
}