summaryrefslogtreecommitdiff
path: root/src/session/session_api.c
blob: 493cdb6a1ecbd75f01be9edcd60ac9aa869d7362 (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 */

#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_SESSION_IMPL *session, **tp;
	int ret;

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

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

	/*
	 * We first close all public cursors, then all remaining file cursors.
	 * File cursors are special because the btree code looks inside them
	 * to work out whether pages are in use.
	 */
	while ((cursor = TAILQ_FIRST(&session->public_cursors)) != NULL)
		WT_TRET(cursor->close(cursor, config));

	while ((cursor = TAILQ_FIRST(&session->file_cursors)) != NULL)
		WT_TRET(cursor->close(cursor, config));

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

	WT_TRET(__wt_schema_close_tables(session));

	__wt_spin_lock(session, &conn->spinlock);
	/* Unpin the current session buffer. */
	if (session->sb != NULL)
		__wt_sb_decrement(session, session->sb, NULL);

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

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

	/* Free any reconciliation state. */
	__wt_rec_destroy(session);

	/* 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(session);

	return (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_SESSION_IMPL *session;
	int ret;

	WT_UNUSED(to_dup);

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

	if (WT_PREFIX_MATCH(uri, "colgroup:"))
		ret = __wt_curfile_open(session, uri, 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, 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_errx(session, "Unknown cursor type '%s'", uri);
		ret = EINVAL;
	}

err:	API_END(session);
	return (ret);
}

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

	session = (WT_SESSION_IMPL *)wt_session;
	ret = 0;
	SESSION_API_CALL(session, create, config, cfg);
	WT_UNUSED(cfg);
	WT_ERR(__wt_schema_create(session, name, config));
err:	API_END(session);
	return (ret);
}

/*
 * __session_rename --
 *	WT_SESSION->rename method.
 */
static int
__session_rename(WT_SESSION *wt_session,
    const char *oldname, const char *newname, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(oldname);
	WT_UNUSED(newname);
	WT_UNUSED(config);

	return (ENOTSUP);
}

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

	force = 0;

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, drop, config, cfg);

	WT_ERR(__wt_config_gets(session, cfg, "force", &cval));
	force = (cval.val != 0);

	ret = __wt_schema_drop(session, name, cfg);
err:	API_END(session);

	return (force ? 0 : ret);
}

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

	session = (WT_SESSION_IMPL *)wt_session;
	ret = 0;

	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(session);
	return (ret);
}

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

	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(session);
	return (ret);
}

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

	session = (WT_SESSION_IMPL *)wt_session;

	SESSION_API_CALL(session, sync, config, cfg);
	ret = __wt_schema_worker(session, uri, cfg, __wt_btree_sync, 0);
err:	API_END(session);
	return (ret);
}

/*
 * __session_truncate --
 *	WT_SESSION->truncate method.
 */
static int
__session_truncate(WT_SESSION *wt_session,
    const char *name, WT_CURSOR *start, WT_CURSOR *end, const char *config)
{
	WT_UNUSED(wt_session);
	WT_UNUSED(name);
	WT_UNUSED(start);
	WT_UNUSED(end);
	WT_UNUSED(config);

	return (ENOTSUP);
}

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

	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(session);
	return (ret);
}

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

	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(session);
	return (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_SESSION_IMPL *session;
	va_list ap;

	session = (WT_SESSION_IMPL *)wt_session;

	va_start(ap, fmt);
	__wt_msgv(session, fmt, ap);
	va_end(ap);

	return (0);
}

/*
 * __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_SESSION_IMPL *session, *session_ret;
	uint32_t slot;
	int ret;

	WT_UNUSED(config);
	ret = 0;
	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_errx(session,
		    "WiredTiger only configured to support %d thread contexts",
		    conn->session_size);
		ret = WT_ERROR;
		goto err;
	}

	/*
	 * 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;
	WT_ASSERT(session, session->event_handler != NULL);
	session_ret->event_handler = session->event_handler;
	session_ret->hazard = conn->hazard + slot * conn->hazard_size;

	TAILQ_INIT(&session_ret->file_cursors);
	TAILQ_INIT(&session_ret->public_cursors);
	TAILQ_INIT(&session_ret->btrees);
	if (event_handler != NULL)
		session_ret->event_handler = event_handler;

	/*
	 * 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);
}