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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <drizzled/server_includes.h>
#include <drizzled/definitions.h>
#include <drizzled/base.h>
#include <drizzled/handler.h>
#include <drizzled/plugin/storage_engine.h>
#include <drizzled/session.h>
#include <drizzled/error.h>
#include <drizzled/gettext.h>
#include <drizzled/registry.h>
#include <drizzled/unireg.h>
#include <drizzled/data_home.h>
#include <string>
#include CSTDINT_H
using namespace std;
drizzled::Registry<StorageEngine *> all_engines;
static void add_storage_engine(StorageEngine *engine)
{
all_engines.add(engine);
}
static void remove_storage_engine(StorageEngine *engine)
{
all_engines.remove(engine);
}
StorageEngine::StorageEngine(const std::string name_arg,
const std::bitset<HTON_BIT_SIZE> &flags_arg,
size_t savepoint_offset_arg,
bool support_2pc)
: name(name_arg), two_phase_commit(support_2pc), enabled(true),
flags(flags_arg),
savepoint_offset(savepoint_alloc_size),
orig_savepoint_offset(savepoint_offset_arg),
slot(0)
{
if (enabled)
{
savepoint_alloc_size+= orig_savepoint_offset;
slot= total_ha++;
if (two_phase_commit)
total_ha_2pc++;
}
}
StorageEngine::~StorageEngine()
{
savepoint_alloc_size-= orig_savepoint_offset;
}
/* args: current_session, db, name */
int StorageEngine::table_exists_in_engine(Session*, const char *, const char *)
{
return HA_ERR_NO_SUCH_TABLE;
}
/**
Return the default storage engine StorageEngine for thread
@param ha_default_storage_engine(session)
@param session current thread
@return
pointer to StorageEngine
*/
StorageEngine *ha_default_storage_engine(Session *session)
{
if (session->variables.storage_engine)
return session->variables.storage_engine;
return global_system_variables.storage_engine;
}
/**
Return the storage engine StorageEngine for the supplied name
@param session current thread
@param name name of storage engine
@return
pointer to storage engine plugin handle
*/
StorageEngine *ha_resolve_by_name(Session *session, const LEX_STRING *name)
{
string find_str(name->str, name->length);
transform(find_str.begin(), find_str.end(),
find_str.begin(), ::tolower);
string default_str("default");
if (find_str == default_str)
return ha_default_storage_engine(session);
StorageEngine *engine= all_engines.find(find_str);
if (engine && engine->is_user_selectable())
return engine;
return NULL;
}
handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
StorageEngine *engine)
{
handler *file;
if (engine && engine->is_enabled())
{
if ((file= engine->create(share, alloc)))
file->init();
return(file);
}
/*
Try the default table type
Here the call to current_session() is ok as we call this function a lot of
times but we enter this branch very seldom.
*/
return(get_new_handler(share, alloc, ha_default_storage_engine(current_session)));
}
class StorageEngineCloseConnection
: public unary_function<StorageEngine *, void>
{
Session *session;
public:
StorageEngineCloseConnection(Session *session_arg) : session(session_arg) {}
/*
there's no need to rollback here as all transactions must
be rolled back already
*/
inline result_type operator() (argument_type engine)
{
if (engine->is_enabled() &&
session_get_ha_data(session, engine))
engine->close_connection(session);
}
};
/**
@note
don't bother to rollback here, it's done already
*/
void ha_close_connection(Session* session)
{
for_each(all_engines.begin(), all_engines.end(),
StorageEngineCloseConnection(session));
}
void ha_drop_database(char* path)
{
for_each(all_engines.begin(), all_engines.end(),
bind2nd(mem_fun(&StorageEngine::drop_database),path));
}
int ha_commit_or_rollback_by_xid(XID *xid, bool commit)
{
vector<int> results;
if (commit)
transform(all_engines.begin(), all_engines.end(), results.begin(),
bind2nd(mem_fun(&StorageEngine::commit_by_xid),xid));
else
transform(all_engines.begin(), all_engines.end(), results.begin(),
bind2nd(mem_fun(&StorageEngine::rollback_by_xid),xid));
if (find_if(results.begin(), results.end(), bind2nd(equal_to<int>(),0))
== results.end())
return 1;
return 0;
}
/**
@details
This function should be called when MySQL sends rows of a SELECT result set
or the EOF mark to the client. It releases a possible adaptive hash index
S-latch held by session in InnoDB and also releases a possible InnoDB query
FIFO ticket to enter InnoDB. To save CPU time, InnoDB allows a session to
keep them over several calls of the InnoDB handler interface when a join
is executed. But when we let the control to pass to the client they have
to be released because if the application program uses mysql_use_result(),
it may deadlock on the S-latch if the application on another connection
performs another SQL query. In MySQL-4.1 this is even more important because
there a connection can have several SELECT queries open at the same time.
@param session the thread handle of the current connection
@return
always 0
*/
int ha_release_temporary_latches(Session *session)
{
for_each(all_engines.begin(), all_engines.end(),
bind2nd(mem_fun(&StorageEngine::release_temporary_latches),session));
return 0;
}
bool ha_flush_logs(StorageEngine *engine)
{
if (engine == NULL)
{
if (find_if(all_engines.begin(), all_engines.end(),
mem_fun(&StorageEngine::flush_logs))
!= all_engines.begin())
return true;
}
else
{
if ((!engine->is_enabled()) ||
(engine->flush_logs()))
return true;
}
return false;
}
/**
recover() step of xa.
@note
there are three modes of operation:
- automatic recover after a crash
in this case commit_list != 0, tc_heuristic_recover==0
all xids from commit_list are committed, others are rolled back
- manual (heuristic) recover
in this case commit_list==0, tc_heuristic_recover != 0
DBA has explicitly specified that all prepared transactions should
be committed (or rolled back).
- no recovery (MySQL did not detect a crash)
in this case commit_list==0, tc_heuristic_recover == 0
there should be no prepared transactions in this case.
*/
class XARecover : unary_function<StorageEngine *, void>
{
int trans_len, found_foreign_xids, found_my_xids;
bool result;
XID *trans_list;
HASH *commit_list;
bool dry_run;
public:
XARecover(XID *trans_list_arg, int trans_len_arg,
HASH *commit_list_arg, bool dry_run_arg)
: trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
trans_list(trans_list_arg), commit_list(commit_list_arg),
dry_run(dry_run_arg)
{}
int getForeignXIDs()
{
return found_foreign_xids;
}
int getMyXIDs()
{
return found_my_xids;
}
result_type operator() (argument_type engine)
{
int got;
if (engine->is_enabled())
{
while ((got= engine->recover(trans_list, trans_len)) > 0 )
{
errmsg_printf(ERRMSG_LVL_INFO,
_("Found %d prepared transaction(s) in %s"),
got, engine->getName().c_str());
for (int i=0; i < got; i ++)
{
my_xid x=trans_list[i].get_my_xid();
if (!x) // not "mine" - that is generated by external TM
{
xid_cache_insert(trans_list+i, XA_PREPARED);
found_foreign_xids++;
continue;
}
if (dry_run)
{
found_my_xids++;
continue;
}
// recovery mode
if (commit_list ?
hash_search(commit_list, (unsigned char *)&x, sizeof(x)) != 0 :
tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
{
engine->commit_by_xid(trans_list+i);
}
else
{
engine->rollback_by_xid(trans_list+i);
}
}
if (got < trans_len)
break;
}
}
}
};
int ha_recover(HASH *commit_list)
{
XID *trans_list= NULL;
int trans_len= 0;
bool dry_run= (commit_list==0 && tc_heuristic_recover==0);
/* commit_list and tc_heuristic_recover cannot be set both */
assert(commit_list==0 || tc_heuristic_recover==0);
/* if either is set, total_ha_2pc must be set too */
if (total_ha_2pc <= 1)
return 0;
#ifndef WILL_BE_DELETED_LATER
/*
for now, only InnoDB supports 2pc. It means we can always safely
rollback all pending transactions, without risking inconsistent data
*/
assert(total_ha_2pc == 2); // only InnoDB and binlog
tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
dry_run=false;
#endif
for (trans_len= MAX_XID_LIST_SIZE ;
trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
{
trans_list=(XID *)malloc(trans_len*sizeof(XID));
}
if (!trans_list)
{
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
return(1);
}
if (commit_list)
errmsg_printf(ERRMSG_LVL_INFO, _("Starting crash recovery..."));
XARecover recover_func(trans_list, trans_len, commit_list, dry_run);
for_each(all_engines.begin(), all_engines.end(), recover_func);
free(trans_list);
if (recover_func.getForeignXIDs())
errmsg_printf(ERRMSG_LVL_WARN,
_("Found %d prepared XA transactions"),
recover_func.getForeignXIDs());
if (dry_run && recover_func.getMyXIDs())
{
errmsg_printf(ERRMSG_LVL_ERROR,
_("Found %d prepared transactions! It means that drizzled "
"was not shut down properly last time and critical "
"recovery information (last binlog or %s file) was "
"manually deleted after a crash. You have to start "
"drizzled with the --tc-heuristic-recover switch to "
"commit or rollback pending transactions."),
recover_func.getMyXIDs(), opt_tc_log_file);
return(1);
}
if (commit_list)
errmsg_printf(ERRMSG_LVL_INFO, _("Crash recovery finished."));
return(0);
}
int ha_start_consistent_snapshot(Session *session)
{
for_each(all_engines.begin(), all_engines.end(),
bind2nd(mem_fun(&StorageEngine::start_consistent_snapshot),session));
return 0;
}
/**
Ask handler if the table exists in engine.
@retval
HA_ERR_NO_SUCH_TABLE Table does not exist
@retval
HA_ERR_TABLE_EXIST Table exists
@retval
\# Error code
*/
class TableExistsInStorageEngine: public unary_function<StorageEngine *,bool>
{
Session *session;
const char *db;
const char *name;
public:
TableExistsInStorageEngine(Session *session_arg,
const char *db_arg, const char *name_arg)
:session(session_arg), db(db_arg), name(name_arg) {}
result_type operator() (argument_type engine)
{
int ret= engine->table_exists_in_engine(session, db, name);
return ret == HA_ERR_TABLE_EXIST;
}
};
/**
Call this function in order to give the handler the possiblity
to ask engine if there are any new tables that should be written to disk
or any dropped tables that need to be removed from disk
*/
int ha_table_exists_in_engine(Session* session,
const char* db, const char* name,
StorageEngine **engine_arg)
{
StorageEngine *engine= NULL;
drizzled::Registry<StorageEngine *>::iterator iter=
find_if(all_engines.begin(), all_engines.end(),
TableExistsInStorageEngine(session, db, name));
if (iter != all_engines.end())
{
engine= *iter;
}
else
{
/* Default way of knowing if a table exists. (checking .frm exists) */
char path[FN_REFLEN];
build_table_filename(path, sizeof(path),
db, name, "", 0);
if (table_proto_exists(path)==EEXIST)
{
drizzled::message::Table table;
build_table_filename(path, sizeof(path),
db, name, ".dfe", 0);
if(drizzle_read_table_proto(path, &table)==0)
{
LEX_STRING engine_name= { (char*)table.engine().name().c_str(),
strlen(table.engine().name().c_str()) };
engine= ha_resolve_by_name(session, &engine_name);
}
}
}
if(engine_arg)
*engine_arg= engine;
if (engine == NULL)
return HA_ERR_NO_SUCH_TABLE;
return HA_ERR_TABLE_EXIST;
}
static const char *check_lowercase_names(handler *file, const char *path,
char *tmp_path)
{
if (lower_case_table_names != 2 || (file->ha_table_flags() & HA_FILE_BASED))
return path;
/* Ensure that table handler get path in lower case */
if (tmp_path != path)
strcpy(tmp_path, path);
/*
we only should turn into lowercase database/table part
so start the process after homedirectory
*/
my_casedn_str(files_charset_info, tmp_path + drizzle_data_home_len);
return tmp_path;
}
/**
An interceptor to hijack the text of the error message without
setting an error in the thread. We need the text to present it
in the form of a warning to the user.
*/
class Ha_delete_table_error_handler: public Internal_error_handler
{
public:
Ha_delete_table_error_handler() : Internal_error_handler() {}
virtual bool handle_error(uint32_t sql_errno,
const char *message,
DRIZZLE_ERROR::enum_warning_level level,
Session *session);
char buff[DRIZZLE_ERRMSG_SIZE];
};
bool
Ha_delete_table_error_handler::
handle_error(uint32_t ,
const char *message,
DRIZZLE_ERROR::enum_warning_level ,
Session *)
{
/* Grab the error message */
strncpy(buff, message, sizeof(buff)-1);
return true;
}
class DeleteTableStorageEngine
: public unary_function<StorageEngine *, void>
{
Session *session;
const char *path;
handler **file;
int *dt_error;
public:
DeleteTableStorageEngine(Session *session_arg, const char *path_arg,
handler **file_arg, int *error_arg)
: session(session_arg), path(path_arg), file(file_arg), dt_error(error_arg) {}
result_type operator() (argument_type engine)
{
handler *tmp_file;
char tmp_path[FN_REFLEN];
if(*dt_error!=ENOENT) /* already deleted table */
return;
if (!engine)
return;
if (!engine->is_enabled())
return;
if ((tmp_file= engine->create(NULL, session->mem_root)))
tmp_file->init();
else
return;
path= check_lowercase_names(tmp_file, path, tmp_path);
int tmp_error= tmp_file->ha_delete_table(path);
if(tmp_error!=ENOENT)
{
*dt_error= tmp_error;
if(*file)
delete *file;
*file= tmp_file;
return;
}
else
delete tmp_file;
return;
}
};
/**
This should return ENOENT if the file doesn't exists.
The .frm file will be deleted only if we return 0 or ENOENT
*/
int ha_delete_table(Session *session, const char *path,
const char *db, const char *alias, bool generate_warning)
{
TABLE_SHARE dummy_share;
Table dummy_table;
memset(&dummy_table, 0, sizeof(dummy_table));
memset(&dummy_share, 0, sizeof(dummy_share));
dummy_table.s= &dummy_share;
int error= ENOENT;
handler *file= NULL;
for_each(all_engines.begin(), all_engines.end(),
DeleteTableStorageEngine(session, path, &file, &error));
if (error && generate_warning)
{
/*
Because file->print_error() use my_error() to generate the error message
we use an internal error handler to intercept it and store the text
in a temporary buffer. Later the message will be presented to user
as a warning.
*/
Ha_delete_table_error_handler ha_delete_table_error_handler;
/* Fill up strucutures that print_error may need */
dummy_share.path.str= (char*) path;
dummy_share.path.length= strlen(path);
dummy_share.db.str= (char*) db;
dummy_share.db.length= strlen(db);
dummy_share.table_name.str= (char*) alias;
dummy_share.table_name.length= strlen(alias);
dummy_table.alias= alias;
if(file != NULL)
{
file->change_table_ptr(&dummy_table, &dummy_share);
session->push_internal_handler(&ha_delete_table_error_handler);
file->print_error(error, 0);
session->pop_internal_handler();
}
else
error= -1; /* General form of fail. maybe bad FRM */
/*
XXX: should we convert *all* errors to warnings here?
What if the error is fatal?
*/
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error,
ha_delete_table_error_handler.buff);
}
if(file)
delete file;
return error;
}
/**
Initiates table-file and calls appropriate database-creator.
@retval
0 ok
@retval
1 error
*/
int ha_create_table(Session *session, const char *path,
const char *db, const char *table_name,
HA_CREATE_INFO *create_info,
bool update_create_info)
{
int error= 1;
Table table;
char name_buff[FN_REFLEN];
const char *name;
TABLE_SHARE share;
init_tmp_table_share(session, &share, db, 0, table_name, path);
if (open_table_def(session, &share, 0) ||
open_table_from_share(session, &share, "", 0, (uint32_t) READ_ALL, 0, &table,
OTM_CREATE))
goto err;
if (update_create_info)
table.updateCreateInfo(create_info);
name= check_lowercase_names(table.file, share.path.str, name_buff);
error= table.file->ha_create(name, &table, create_info);
table.closefrm(false);
if (error)
{
sprintf(name_buff,"%s.%s",db,table_name);
my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), name_buff, error);
}
err:
free_table_share(&share);
return(error != 0);
}
int storage_engine_finalizer(st_plugin_int *plugin)
{
StorageEngine *engine= static_cast<StorageEngine *>(plugin->data);
remove_storage_engine(engine);
if (engine && plugin->plugin->deinit)
(void)plugin->plugin->deinit(engine);
return(0);
}
int storage_engine_initializer(st_plugin_int *plugin)
{
StorageEngine *engine= NULL;
if (plugin->plugin->init)
{
if (plugin->plugin->init(&engine))
{
errmsg_printf(ERRMSG_LVL_ERROR,
_("Plugin '%s' init function returned error."),
plugin->name.str);
return 1;
}
}
if (engine != NULL)
add_storage_engine(engine);
plugin->data= engine;
plugin->isInited= true;
return 0;
}
const string ha_resolve_storage_engine_name(const StorageEngine *engine)
{
return engine == NULL ? string("UNKNOWN") : engine->getName();
}
|