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
|
/*-
* Copyright (c) 2008-2013 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __wt_session_add_btree --
* Add a btree handle to the session's cache.
*/
int
__wt_session_add_btree(
WT_SESSION_IMPL *session, WT_BTREE_SESSION **btree_sessionp)
{
WT_BTREE_SESSION *btree_session;
WT_RET(__wt_calloc_def(session, 1, &btree_session));
btree_session->btree = session->btree;
TAILQ_INSERT_HEAD(&session->btrees, btree_session, q);
if (btree_sessionp != NULL)
*btree_sessionp = btree_session;
return (0);
}
/*
* __wt_session_lock_btree --
* Lock a btree handle.
*/
int
__wt_session_lock_btree(WT_SESSION_IMPL *session, uint32_t flags)
{
WT_BTREE *btree;
uint32_t special_flags;
btree = session->btree;
/*
* Special operation flags will cause the handle to be reopened.
* For example, a handle opened with WT_BTREE_BULK cannot use the same
* internal data structures as a handle opened for ordinary access.
*/
special_flags = LF_ISSET(WT_BTREE_SPECIAL_FLAGS);
WT_ASSERT(session, special_flags == 0 || LF_ISSET(WT_BTREE_EXCLUSIVE));
if (LF_ISSET(WT_BTREE_EXCLUSIVE)) {
/*
* Try to get an exclusive handle lock and fail immediately if
* it's unavailable. We don't expect exclusive operations on
* trees to be mixed with ordinary cursor access, but if there
* is a use case in the future, we could make blocking here
* configurable.
*
* Special flags will cause the handle to be reopened, which
* will get the necessary lock, so don't bother here.
*/
if (LF_ISSET(WT_BTREE_LOCK_ONLY) || special_flags == 0) {
WT_RET(__wt_try_writelock(session, btree->rwlock));
F_SET(btree, WT_BTREE_EXCLUSIVE);
}
} else if (F_ISSET(btree, WT_BTREE_SPECIAL_FLAGS))
return (EBUSY);
else
WT_RET(__wt_readlock(session, btree->rwlock));
/*
* At this point, we have the requested lock -- if that is all that was
* required, we're done. Otherwise, check that the handle is open and
* that no special flags are required.
*/
if (LF_ISSET(WT_BTREE_LOCK_ONLY) ||
(F_ISSET(btree, WT_BTREE_OPEN) && special_flags == 0))
return (0);
/*
* The handle needs to be opened. If we locked the handle above,
* unlock it before returning.
*/
if (!LF_ISSET(WT_BTREE_EXCLUSIVE) || special_flags == 0) {
F_CLR(btree, WT_BTREE_EXCLUSIVE);
WT_RET(__wt_rwunlock(session, btree->rwlock));
}
/* Treat an unopened handle just like a non-existent handle. */
return (WT_NOTFOUND);
}
/*
* __wt_session_release_btree --
* Unlock a btree handle.
*/
int
__wt_session_release_btree(WT_SESSION_IMPL *session)
{
WT_BTREE *btree;
WT_DECL_RET;
btree = session->btree;
/*
* If we had no cache flag set, close and free the btree handle. It was
* never added to the handle cache.
*/
if (F_ISSET(btree, WT_BTREE_NO_CACHE)) {
/* A write lock has been held since the handle was created. */
WT_RET(__wt_rwunlock(session, btree->rwlock));
WT_RET(__wt_conn_btree_discard_single(session, btree));
} else {
/*
* If we had special flags set, close the handle so that future
* access can get a handle without special flags.
*/
if (F_ISSET(btree, WT_BTREE_DISCARD | WT_BTREE_SPECIAL_FLAGS)) {
WT_ASSERT(session, F_ISSET(btree, WT_BTREE_EXCLUSIVE));
F_CLR(btree, WT_BTREE_DISCARD);
WT_RET(__wt_conn_btree_sync_and_close(session));
}
if (F_ISSET(btree, WT_BTREE_EXCLUSIVE))
F_CLR(btree, WT_BTREE_EXCLUSIVE);
ret = __wt_rwunlock(session, btree->rwlock);
}
session->btree = NULL;
return (ret);
}
/*
* __wt_session_get_btree_ckpt --
* Check the configuration strings for a checkpoint name, get a btree
* handle for the given name, set session->btree.
*/
int
__wt_session_get_btree_ckpt(WT_SESSION_IMPL *session,
const char *uri, const char *cfg[], uint32_t flags)
{
WT_CONFIG_ITEM cval;
WT_DECL_RET;
int last_ckpt;
const char *checkpoint;
last_ckpt = 0;
checkpoint = NULL;
/*
* This function exists to handle checkpoint configuration. Callers
* that never open a checkpoint call the underlying function directly.
*/
WT_RET_NOTFOUND_OK(
__wt_config_gets_defno(session, cfg, "checkpoint", &cval));
if (cval.len != 0) {
/*
* The internal checkpoint name is special, find the last
* unnamed checkpoint of the object.
*/
if (WT_STRING_MATCH(WT_CHECKPOINT, cval.str, cval.len)) {
last_ckpt = 1;
retry: WT_RET(__wt_meta_checkpoint_last_name(
session, uri, &checkpoint));
} else
WT_RET(__wt_strndup(
session, cval.str, cval.len, &checkpoint));
}
ret = __wt_session_get_btree(session, uri, checkpoint, cfg, flags);
__wt_free(session, checkpoint);
/*
* There's a potential race: we get the name of the most recent unnamed
* checkpoint, but if it's discarded (or locked so it can be discarded)
* by the time we try to open it, we'll fail the open. Retry in those
* cases, a new "last" checkpoint should surface, and we can't return an
* error, the application will be justifiably upset if we can't open the
* last checkpoint instance of an object.
*
* The check against WT_NOTFOUND is correct: if there was no checkpoint
* for the object (that is, the object has never been in a checkpoint),
* we returned immediately after the call to search for that name.
*/
if (last_ckpt && (ret == WT_NOTFOUND || ret == EBUSY))
goto retry;
return (ret);
}
/*
* __wt_session_get_btree --
* Get a btree handle for the given name, set session->btree.
*/
int
__wt_session_get_btree(WT_SESSION_IMPL *session,
const char *uri, const char *checkpoint, const char *cfg[], uint32_t flags)
{
WT_BTREE *btree;
WT_BTREE_SESSION *btree_session;
WT_DECL_RET;
btree = NULL;
btree_session = NULL;
/*
* If the no cache flag is set, we never use the handle cache to
* store or retrieve the handle.
*/
if (!LF_ISSET(WT_BTREE_NO_CACHE)) {
TAILQ_FOREACH(btree_session, &session->btrees, q) {
btree = btree_session->btree;
if (strcmp(uri, btree->name) != 0)
continue;
if ((checkpoint == NULL && btree->checkpoint == NULL) ||
(checkpoint != NULL && btree->checkpoint != NULL &&
strcmp(checkpoint, btree->checkpoint) == 0))
break;
}
if (btree_session == NULL)
session->btree = NULL;
else {
session->btree = btree;
/*
* Try and lock the file; if we succeed, our "exclusive"
* state must match.
*/
if ((ret = __wt_session_lock_btree(
session, flags)) != WT_NOTFOUND) {
WT_ASSERT(session, ret != 0 ||
LF_ISSET(WT_BTREE_EXCLUSIVE) == F_ISSET(
session->btree, WT_BTREE_EXCLUSIVE));
return (ret);
}
ret = 0;
}
}
/*
* If we don't already hold the schema lock, get it now so that we
* can find and/or open the handle.
*/
WT_WITH_SCHEMA_LOCK_OPT(session,
ret = __wt_conn_btree_get(session, uri, checkpoint, cfg, flags));
WT_RET(ret);
if (btree_session == NULL && !LF_ISSET(WT_BTREE_NO_CACHE))
WT_RET(__wt_session_add_btree(session, NULL));
WT_ASSERT(session, LF_ISSET(WT_BTREE_LOCK_ONLY) ||
F_ISSET(session->btree, WT_BTREE_OPEN));
WT_ASSERT(session, LF_ISSET(WT_BTREE_EXCLUSIVE) ==
F_ISSET(session->btree, WT_BTREE_EXCLUSIVE));
return (0);
}
/*
* __wt_session_lock_checkpoint --
* Lock the btree handle for the given checkpoint name.
*/
int
__wt_session_lock_checkpoint(WT_SESSION_IMPL *session, const char *checkpoint)
{
WT_BTREE *btree;
WT_DECL_RET;
btree = session->btree;
WT_ERR(__wt_session_get_btree(session, btree->name,
checkpoint, NULL, WT_BTREE_EXCLUSIVE | WT_BTREE_LOCK_ONLY));
/*
* We lock checkpoint handles that we are overwriting, so the handle
* must be closed when we release it.
*/
F_SET(session->btree, WT_BTREE_DISCARD);
WT_ASSERT(session, WT_META_TRACKING(session));
WT_ERR(__wt_meta_track_handle_lock(session, 0));
/* Restore the original btree in the session. */
err: session->btree = btree;
return (ret);
}
/*
* __wt_session_discard_btree --
* Discard our reference to the btree.
*/
int
__wt_session_discard_btree(
WT_SESSION_IMPL *session, WT_BTREE_SESSION *btree_session)
{
WT_BTREE *btree;
WT_DECL_RET;
TAILQ_REMOVE(&session->btrees, btree_session, q);
btree = session->btree;
session->btree = btree_session->btree;
__wt_overwrite_and_free(session, btree_session);
ret = __wt_conn_btree_close(session, 0);
/* Restore the original btree in the session. */
session->btree = btree;
return (ret);
}
|