summaryrefslogtreecommitdiff
path: root/src/session/session_api.c
blob: 2a285b258103ed0654258e347df6a539e5a770b6 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __session_close --
 *	WT_SESSION->close method.
 */
static int
__session_close(WT_SESSION *wt_session, const char *config)
{
	WT_BTREE_SESSION *btree_session;
	WT_CONNECTION_IMPL *conn;
	WT_CURSOR *cursor;
	WT_DECL_RET;
	WT_SESSION_IMPL *session, **tp;

	conn = (WT_CONNECTION_IMPL *)wt_session->connection;
	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, close, config, cfg);
	WT_UNUSED(cfg);

	while ((cursor = TAILQ_FIRST(&session->cursors)) != NULL)
		WT_TRET(cursor->close(cursor));

	while ((btree_session = TAILQ_FIRST(&session->btrees)) != NULL)
		WT_TRET(__wt_session_discard_btree(session, btree_session));

	WT_TRET(__wt_schema_close_tables(session));

	__wt_spin_lock(session, &conn->spinlock);

	/* Discard metadata tracking. */
	__wt_meta_track_discard(session);

	/* Discard scratch buffers. */
	__wt_scr_discard(session);

	/* Confirm we're not holding any hazard references. */
	__wt_hazard_empty(session);

	/* Free the reconciliation information. */
	__wt_rec_destroy(session);

	/* Free the eviction exclusive-lock information. */
	__wt_free(session, session->excl);

	/* Destroy the thread's mutex. */
	if (session->cond != NULL)
		(void)__wt_cond_destroy(session, session->cond);

	/*
	 * Replace the session reference we're closing with the last entry in
	 * the table, then clear the last entry.  As far as the walk of the
	 * server threads is concerned, it's OK if the session appears twice,
	 * or if it doesn't appear at all, so these lines can race all they
	 * want.
	 */
	for (tp = conn->sessions; *tp != session; ++tp)
		;
	--conn->session_cnt;
	*tp = conn->sessions[conn->session_cnt];
	conn->sessions[conn->session_cnt] = NULL;

	/*
	 * Publish, making the session array entry available for re-use.  There
	 * must be a barrier here to ensure the cleanup above completes before
	 * the entry is re-used.
	 */
	WT_PUBLISH(session->iface.connection, NULL);

	session = conn->default_session;
	__wt_spin_unlock(session, &conn->spinlock);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_open_cursor --
 *	WT_SESSION->open_cursor method.
 */
static int
__session_open_cursor(WT_SESSION *wt_session,
    const char *uri, WT_CURSOR *to_dup, const char *config, WT_CURSOR **cursorp)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;
	SESSION_API_CALL(session, open_cursor, config, cfg);

	if (uri != NULL && to_dup != NULL)
		WT_ERR_MSG(session, EINVAL,
		    "should be passed either a URI or a cursor, but not both");

	if (to_dup != NULL)
		ret = __wt_cursor_dup(session, to_dup, config, cursorp);
	else if (WT_PREFIX_MATCH(uri, "colgroup:"))
		ret = __wt_curfile_open(session, uri, NULL, cfg, cursorp);
	else if (WT_PREFIX_MATCH(uri, "config:"))
		ret = __wt_curconfig_open(session, uri, cfg, cursorp);
	else if (WT_PREFIX_MATCH(uri, "file:"))
		ret = __wt_curfile_open(session, uri, NULL, cfg, cursorp);
	else if (WT_PREFIX_MATCH(uri, "index:"))
		ret = __wt_curindex_open(session, uri, cfg, cursorp);
	else if (WT_PREFIX_MATCH(uri, "statistics:"))
		ret = __wt_curstat_open(session, uri, cfg, cursorp);
	else if (WT_PREFIX_MATCH(uri, "table:"))
		ret = __wt_curtable_open(session, uri, cfg, cursorp);
	else {
		__wt_err(session, EINVAL, "Unknown cursor type '%s'", uri);
		ret = EINVAL;
	}

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __wt_session_create_strip --
 *	Discard any configuration information from a schema entry that is not
 * applicable to an session.create call, here for the wt dump command utility,
 * which only wants to dump the schema information needed for load.
 */
int
__wt_session_create_strip(
    WT_SESSION *session, const char *v1, const char *v2, const char **value_ret)
{
	WT_SESSION_IMPL *session_impl = (WT_SESSION_IMPL *)session;
	const char *cfg[] = { __wt_confdfl_session_create, v1, v2, NULL };

	return (__wt_config_collapse(session_impl, cfg, value_ret));
}

/*
 * __session_create --
 *	WT_SESSION->create method.
 */
static int
__session_create(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;
	SESSION_API_CALL(session, create, config, cfg);
	WT_UNUSED(cfg);

	/* Disallow objects in the WiredTiger name space. */
	WT_ERR(__wt_schema_name_check(session, uri));

	WT_ERR(__wt_schema_create(session, uri, config));

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_rename --
 *	WT_SESSION->rename method.
 */
static int
__session_rename(WT_SESSION *wt_session,
    const char *uri, const char *newname, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;
	SESSION_API_CALL(session, rename, config, cfg);
	ret = __wt_schema_rename(session, uri, newname, cfg);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_drop --
 *	WT_SESSION->drop method.
 */
static int
__session_drop(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_CONFIG_ITEM cval;
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;
	SESSION_API_CALL(session, drop, config, cfg);

	WT_ERR(__wt_meta_track_on(session));

	/* If dropping snapshots, that's a different code path. */
	WT_ERR(__wt_config_gets(session, cfg, "snapshot", &cval));
	ret = (cval.len != 0) ?
	    __wt_schema_worker(session, uri, cfg,
		__wt_btree_snapshot_drop, WT_BTREE_SNAPSHOT_OP) :
	    __wt_schema_drop(session, uri, cfg);

err:    WT_TRET(__wt_meta_track_off(session, ret != 0));
	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_dumpfile --
 *	WT_SESSION->dumpfile method.
 */
static int
__session_dumpfile(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;
	SESSION_API_CALL(session, dumpfile, config, cfg);
	ret = __wt_schema_worker(session, uri, cfg,
	    __wt_dumpfile, WT_BTREE_EXCLUSIVE | WT_BTREE_VERIFY);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_salvage --
 *	WT_SESSION->salvage method.
 */
static int
__session_salvage(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, salvage, config, cfg);
	ret = __wt_schema_worker(session, uri, cfg,
	    __wt_salvage, WT_BTREE_EXCLUSIVE | WT_BTREE_SALVAGE);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_sync --
 *	WT_SESSION->sync method.
 */
static int
__session_sync(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, sync, config, cfg);
	WT_ERR(__wt_meta_track_on(session));

	ret = __wt_schema_worker(session, uri, cfg,
	    __wt_btree_snapshot, WT_BTREE_SNAPSHOT_OP);

err:    WT_TRET(__wt_meta_track_off(session, ret != 0));
	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_truncate --
 *	WT_SESSION->truncate method.
 */
static int
__session_truncate(WT_SESSION *wt_session,
    const char *uri, WT_CURSOR *start, WT_CURSOR *stop, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, truncate, config, cfg);
	/*
	 * If the URI is specified, we don't need a start/stop, if start/stop
	 * is specified, we don't need a URI.
	 *
	 * If no URI is specified, and both cursors are specified, start/stop
	 * must reference the same object.
	 *
	 * Any specified cursor must have been initialized.
	 */
	if ((uri == NULL && start == NULL && stop == NULL) ||
	    (uri != NULL && (start != NULL || stop != NULL)))
		WT_ERR_MSG(session, EINVAL,
		    "the truncate method should be passed either a URI or "
		    "start/stop cursors, but not both");
	if (start != NULL && stop != NULL && strcmp(start->uri, stop->uri) != 0)
		WT_ERR_MSG(session, EINVAL,
		    "truncate method cursors must reference the same object");
	if ((start != NULL && !F_ISSET(start, WT_CURSTD_KEY_SET)) ||
	    (stop != NULL && !F_ISSET(stop, WT_CURSTD_KEY_SET)))
		WT_ERR_MSG(session, EINVAL,
		    "the truncate method cursors must have their keys set");

	if (uri == NULL) {
		/*
		 * From a starting/stopping cursor to the begin/end of the
		 * object is easy, walk the object.
		 */
		if (start == NULL)
			for (;;) {
				WT_ERR(stop->remove(stop));
				if ((ret = stop->prev(stop)) != 0) {
					if (ret == WT_NOTFOUND)
						ret = 0;
					break;
				}
			}
		else
			for (;;) {
				WT_ERR(start->remove(start));
				if (stop != NULL &&
				    start->equals(start, stop))
					break;
				if ((ret = start->next(start)) != 0) {
					if (ret == WT_NOTFOUND)
						ret = 0;
					break;
				}
			}
	} else
		ret = __wt_schema_truncate(session, uri, cfg);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_upgrade --
 *	WT_SESSION->upgrade method.
 */
static int
__session_upgrade(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, upgrade, config, cfg);
	ret = __wt_schema_worker(session, uri, cfg,
	    __wt_upgrade, WT_BTREE_EXCLUSIVE | WT_BTREE_UPGRADE);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_verify --
 *	WT_SESSION->verify method.
 */
static int
__session_verify(WT_SESSION *wt_session, const char *uri, const char *config)
{
	WT_DECL_RET;
	WT_SESSION_IMPL *session;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, verify, config, cfg);
	ret = __wt_schema_worker(session, uri, cfg,
	    __wt_verify, WT_BTREE_EXCLUSIVE | WT_BTREE_VERIFY);

err:	API_END_NOTFOUND_MAP(session, ret);
}

/*
 * __session_begin_transaction --
 *	WT_SESSION->begin_transaction method.
 */
static int
__session_begin_transaction(WT_SESSION *wt_session, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(config);

	return (ENOTSUP);
}

/*
 * __session_commit_transaction --
 *	WT_SESSION->commit_transaction method.
 */
static int
__session_commit_transaction(WT_SESSION *wt_session, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(config);

	return (ENOTSUP);
}

/*
 * __session_rollback_transaction --
 *	WT_SESSION->rollback_transaction method.
 */
static int
__session_rollback_transaction(WT_SESSION *wt_session, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(config);

	return (ENOTSUP);
}

/*
 * __session_checkpoint --
 *	WT_SESSION->checkpoint method.
 */
static int
__session_checkpoint(WT_SESSION *wt_session, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(config);

	return (ENOTSUP);
}

/*
 * __session_msg_printf --
 *	WT_SESSION->msg_printf method.
 */
static int
__session_msg_printf(WT_SESSION *wt_session, const char *fmt, ...)
{
	WT_DECL_RET;
	va_list ap;

	va_start(ap, fmt);
	ret = __wt_vmsg((WT_SESSION_IMPL *)wt_session, fmt, ap);
	va_end(ap);

	return (ret);
}

/*
 * __wt_open_session --
 *	Allocate a session handle.  The internal parameter is used for sessions
 *	opened by WiredTiger for its own use.
 */
int
__wt_open_session(WT_CONNECTION_IMPL *conn, int internal,
    WT_EVENT_HANDLER *event_handler, const char *config,
    WT_SESSION_IMPL **sessionp)
{
	static WT_SESSION stds = {
		NULL,
		__session_close,
		__session_open_cursor,
		__session_create,
		__session_drop,
		__session_rename,
		__session_salvage,
		__session_sync,
		__session_truncate,
		__session_upgrade,
		__session_verify,
		__session_begin_transaction,
		__session_commit_transaction,
		__session_rollback_transaction,
		__session_checkpoint,
		__session_dumpfile,
		__session_msg_printf
	};
	WT_DECL_RET;
	WT_SESSION_IMPL *session, *session_ret;
	uint32_t slot;

	WT_UNUSED(config);

	session = conn->default_session;
	session_ret = NULL;

	__wt_spin_lock(session, &conn->spinlock);

	/* Check to see if there's an available session slot. */
	if (conn->session_cnt == conn->session_size - 1)
		WT_ERR_MSG(session, WT_ERROR,
		    "WiredTiger only configured to support %d thread contexts",
		    conn->session_size);

	/*
	 * The session reference list is compact, the session array is not.
	 * Find the first empty session slot.
	 */
	for (slot = 0, session_ret = conn->session_array;
	    session_ret->iface.connection != NULL;
	    ++session_ret, ++slot)
		;

	/* Session entries are re-used, clear the old contents. */
	WT_CLEAR(*session_ret);

	WT_ERR(__wt_cond_alloc(session, "session", 1, &session_ret->cond));
	session_ret->iface = stds;
	session_ret->iface.connection = &conn->iface;
	session_ret->event_handler =
	    event_handler == NULL ? session->event_handler : event_handler;
	session_ret->hazard = conn->hazard + slot * conn->hazard_size;

	TAILQ_INIT(&session_ret->cursors);
	TAILQ_INIT(&session_ret->btrees);

	/*
	 * Public sessions are automatically closed during WT_CONNECTION->close.
	 * If the session handles for internal threads were to go on the public
	 * list, there would be complex ordering issues during close.  Set a
	 * flag to avoid this: internal sessions are not closed automatically.
	 */
	if (internal)
		F_SET(session_ret, WT_SESSION_INTERNAL);

	/*
	 * Publish: make the entry visible to server threads.  There must be a
	 * barrier to ensure the structure fields are set before any other
	 * thread can see the session.
	 */
	WT_PUBLISH(conn->sessions[conn->session_cnt++], session_ret);

	STATIC_ASSERT(offsetof(WT_CONNECTION_IMPL, iface) == 0);
	*sessionp = session_ret;

err:	__wt_spin_unlock(session, &conn->spinlock);
	return (ret);
}