summaryrefslogtreecommitdiff
path: root/sql/ha_sequence.cc
blob: bab0614706d1f7aad1cc89d28967565969cb8835 (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
/*
   Copyright (c) 2017, Aliyun and/or its affiliates.
   Copyright (c) 2017, 2020, MariaDB Corporation.

   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 "mariadb.h"
#include "sql_list.h"
#include "table.h"
#include "sql_table.h"
#include "sql_sequence.h"
#include "ha_sequence.h"
#include "sql_plugin.h"
#include "mysql/plugin.h"
#include "sql_priv.h"
#include "sql_parse.h"
#include "sql_update.h"
#include "sql_base.h"
#include "log_event.h"

#ifdef WITH_WSREP
#include "wsrep_trans_observer.h" /* wsrep_start_transaction() */
#endif

/*
  Table flags we should inherit and disable from the original engine.
  We add HA_STATS_RECORDS_IS_EXACT as ha_sequence::info() will ensure
  that records is always 1
*/

#define SEQUENCE_ENABLED_TABLE_FLAGS  (HA_STATS_RECORDS_IS_EXACT | \
                                       HA_PERSISTENT_TABLE)
#define SEQUENCE_DISABLED_TABLE_FLAGS  (HA_CAN_SQL_HANDLER | \
                                        HA_CAN_INSERT_DELAYED | \
                                        HA_BINLOG_STMT_CAPABLE)
handlerton *sql_sequence_hton;

/*
  Create a sequence handler
*/

ha_sequence::ha_sequence(handlerton *hton, TABLE_SHARE *share)
  :handler(hton, share), write_locked(0)
{
  sequence= share->sequence;
  DBUG_ASSERT(share->sequence);
}

/**
   Destructor method must remove the underlying handler
*/
ha_sequence::~ha_sequence()
{
  delete file;
}

/**
   Sequence table open method

   @param name            Path to file (dbname and tablename)
   @param mode            mode
   @param flags           Flags how to open file

   RETURN VALUES
   @retval 0              Success
   @retval != 0           Failure
*/

int ha_sequence::open(const char *name, int mode, uint flags)
{
  int error;
  DBUG_ENTER("ha_sequence::open");
  DBUG_ASSERT(table->s == table_share && file);

  file->table= table;
  if (likely(!(error= file->open(name, mode, flags))))
  {
    /*
      Allocate ref in table's mem_root. We can't use table's ref
      as it's allocated by ha_ caller that allocates this.
     */
    ref_length= file->ref_length;
    if (!(ref= (uchar*) alloc_root(&table->mem_root,ALIGN_SIZE(ref_length)*2)))
    {
      file->ha_close();
      error=HA_ERR_OUT_OF_MEM;
      DBUG_RETURN(error);
    }
    file->ref= ref;
    file->dup_ref= dup_ref= ref+ALIGN_SIZE(file->ref_length);

    /*
      ha_open() sets the following for us. We have to set this for the
      underlying handler
    */
    file->cached_table_flags= (file->table_flags() | HA_REUSES_FILE_NAMES);

    file->reset_statistics();
    internal_tmp_table= file->internal_tmp_table=
      MY_TEST(flags & HA_OPEN_INTERNAL_TABLE);
    reset_statistics();

    /*
      Don't try to read the initial row if the call is part of CREATE, REPAIR
      or FLUSH
    */
    if (!(flags & (HA_OPEN_FOR_CREATE | HA_OPEN_FOR_REPAIR |
                   HA_OPEN_FOR_FLUSH)))
    {
      if (unlikely((error= table->s->sequence->read_initial_values(table))))
        file->ha_close();
    }
    else if (!table->s->tmp_table)
      table->internal_set_needs_reopen(true);

    /*
      The following is needed to fix comparison of rows in
      ha_update_first_row() for InnoDB
    */
    if (!error)
      memcpy(table->record[1], table->s->default_values, table->s->reclength);
  }
  DBUG_RETURN(error);
}

/*
  Clone the sequence. Needed if table is used by range optimization
  (Very, very unlikely)
*/

handler *ha_sequence::clone(const char *name, MEM_ROOT *mem_root)
{
  ha_sequence *new_handler;
  DBUG_ENTER("ha_sequence::clone");
  if (!(new_handler= new (mem_root) ha_sequence(ht, table_share)))
    DBUG_RETURN(NULL);

  /*
    Allocate new_handler->ref here because otherwise ha_open will allocate it
    on this->table->mem_root and we will not be able to reclaim that memory
    when the clone handler object is destroyed.
  */

  if (!(new_handler->ref= (uchar*) alloc_root(mem_root,
                                              ALIGN_SIZE(ref_length)*2)))
    goto err;

  if (new_handler->ha_open(table, name,
                           table->db_stat,
                           HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_NO_PSI_CALL))
    goto err;

  /* Reuse original storage engine data for duplicate key reference */
  new_handler->ref=        file->ref;
  new_handler->ref_length= file->ref_length;
  new_handler->dup_ref=    file->dup_ref;

  DBUG_RETURN((handler*) new_handler);

err:
  delete new_handler;
  DBUG_RETURN(NULL);
}


/*
  Map the create table to the original storage engine
*/

int ha_sequence::create(const char *name, TABLE *form,
                        HA_CREATE_INFO *create_info)
{
  DBUG_ASSERT(create_info->sequence);
  /* Sequence tables has one and only one row */
  create_info->max_rows= create_info->min_rows= 1;
  return (file->create(name, form, create_info));
}

/**
  Sequence write row method.

  A sequence table has only one row. Any inserts in the table
  will update this row.

  @retval 0     Success
  @retval != 0  Failure

  NOTES:
    write_locked is set if we are called from SEQUENCE::next_value
    In this case the mutex is already locked and we should not update
    the sequence with 'buf' as the sequence object is already up to date.
*/

int ha_sequence::write_row(const uchar *buf)
{
  int error;
  sequence_definition tmp_seq;
  bool sequence_locked;
  THD *thd= table->in_use;
  DBUG_ENTER("ha_sequence::write_row");
  DBUG_ASSERT(table->record[0] == buf);

  /*
    Log to binary log even if this function has been called before
    (The function ends by setting row_logging to 0)
  */
  row_logging= row_logging_init;
  if (unlikely(sequence->initialized == SEQUENCE::SEQ_IN_PREPARE))
  {
    /* This calls is from ha_open() as part of create table */
    DBUG_RETURN(file->write_row(buf));
  }
  if (unlikely(sequence->initialized == SEQUENCE::SEQ_IN_ALTER))
  {
    int error= 0;
    /* This is called from alter table */
    tmp_seq.read_fields(table);
    if (tmp_seq.check_and_adjust(0))
      DBUG_RETURN(HA_ERR_SEQUENCE_INVALID_DATA);
    sequence->copy(&tmp_seq);
    if (likely(!(error= file->write_row(buf))))
      sequence->initialized= SEQUENCE::SEQ_READY_TO_USE;
    row_logging= 0;
    DBUG_RETURN(error);
  }
  if (unlikely(sequence->initialized != SEQUENCE::SEQ_READY_TO_USE))
    DBUG_RETURN(HA_ERR_WRONG_COMMAND);

  sequence_locked= write_locked;
  if (!write_locked)                         // If not from next_value()
  {
    /*
      User tries to write a full row directly to the sequence table with
      INSERT or LOAD DATA.

      - Get an exclusive lock for the table. This is needed to ensure that
        we excute all full inserts (same as ALTER SEQUENCE) in same order
        on master and slaves
      - Check that the new row is an accurate SEQUENCE object
    */
    /* mark a full binlog image insert to force non-parallel slave */
    thd->transaction->stmt.mark_trans_did_ddl();
    if (table->s->tmp_table == NO_TMP_TABLE &&
        thd->mdl_context.upgrade_shared_lock(table->mdl_ticket,
                                             MDL_EXCLUSIVE,
                                             thd->variables.
                                             lock_wait_timeout))
        DBUG_RETURN(ER_LOCK_WAIT_TIMEOUT);

    tmp_seq.read_fields(table);
    if (tmp_seq.check_and_adjust(0))
      DBUG_RETURN(HA_ERR_SEQUENCE_INVALID_DATA);

    /*
      Lock sequence to ensure that no one can come in between
      while sequence, table and binary log are updated.
    */
    sequence->write_lock(table);
  }

#ifdef WITH_WSREP
  /* We need to start Galera transaction for select NEXT VALUE FOR
  sequence if it is not yet started. Note that ALTER is handled
  as TOI. */
  if (WSREP_ON && WSREP(thd) &&
      !thd->wsrep_trx().active() &&
      wsrep_thd_is_local(thd))
    wsrep_start_transaction(thd, thd->wsrep_next_trx_id());
#endif

  if (likely(!(error= file->update_first_row(buf))))
  {
    Log_func *log_func= Write_rows_log_event::binlog_row_logging_function;
    if (!sequence_locked)
      sequence->copy(&tmp_seq);
    rows_changed++;
    /* We have to do the logging while we hold the sequence mutex */
    if (row_logging)
      error= binlog_log_row(table, 0, buf, log_func);
  }

  /* Row is already logged, don't log it again in ha_write_row() */
  row_logging= 0;
  sequence->all_values_used= 0;
  if (!sequence_locked)
    sequence->write_unlock(table);
  DBUG_RETURN(error);
}


/*
  Inherit the sequence base table flags.
*/

handler::Table_flags ha_sequence::table_flags() const
{
  DBUG_ENTER("ha_sequence::table_flags");
  DBUG_RETURN((file->table_flags() & ~SEQUENCE_DISABLED_TABLE_FLAGS) |
              SEQUENCE_ENABLED_TABLE_FLAGS);
}


int ha_sequence::info(uint flag)
{
  DBUG_ENTER("ha_sequence::info");
  file->info(flag);
  /* Inform optimizer that we have always only one record */
  stats= file->stats;
  stats.records= 1;
  DBUG_RETURN(false);
}


int ha_sequence::extra(enum ha_extra_function operation)
{
  if (operation == HA_EXTRA_PREPARE_FOR_ALTER_TABLE)
  {
    /* In case of ALTER TABLE allow ::write_row() to copy rows */
    sequence->initialized= SEQUENCE::SEQ_IN_ALTER;
  }
  return file->extra(operation);
}

bool ha_sequence::check_if_incompatible_data(HA_CREATE_INFO *create_info,
                                             uint table_changes)
{
  /* Table definition is locked for SEQUENCE tables */
  return(COMPATIBLE_DATA_YES);
}


int ha_sequence::external_lock(THD *thd, int lock_type)
{
  int error= file->external_lock(thd, lock_type);

  /*
    Copy lock flag to satisfy DBUG_ASSERT checks in ha_* functions in
    handler.cc when we later call it with file->ha_..()
  */
  if (!error)
    file->m_lock_type= lock_type;
  return error;
}

/*
  Squence engine error deal method
*/

void ha_sequence::print_error(int error, myf errflag)
{
  const char *sequence_db=   table_share->db.str;
  const char *sequence_name= table_share->table_name.str;
  DBUG_ENTER("ha_sequence::print_error");

  switch (error) {
  case HA_ERR_SEQUENCE_INVALID_DATA:
  {
    my_error(ER_SEQUENCE_INVALID_DATA, MYF(errflag), sequence_db,
             sequence_name);
    DBUG_VOID_RETURN;
  }
  case HA_ERR_SEQUENCE_RUN_OUT:
  {
    my_error(ER_SEQUENCE_RUN_OUT, MYF(errflag), sequence_db, sequence_name);
    DBUG_VOID_RETURN;
  }
  case HA_ERR_WRONG_COMMAND:
    my_error(ER_ILLEGAL_HA, MYF(0), "SEQUENCE", sequence_db, sequence_name);
    DBUG_VOID_RETURN;
  case ER_WRONG_INSERT_INTO_SEQUENCE:
    my_error(error, MYF(0));
    DBUG_VOID_RETURN;
  }
  file->print_error(error, errflag);
  DBUG_VOID_RETURN;
}

/*****************************************************************************
  Sequence plugin interface
*****************************************************************************/

/*
 Create an new handler
*/

static handler *sequence_create_handler(handlerton *hton,
                                        TABLE_SHARE *share,
                                        MEM_ROOT *mem_root)
{
  DBUG_ENTER("sequence_create_handler");
  if (unlikely(!share))
  {
    /*
      This can happen if we call get_new_handler with a non existing share
    */
    DBUG_RETURN(0);
  }
  DBUG_RETURN(new (mem_root) ha_sequence(hton, share));
}


/*
  Sequence engine end.

  SYNOPSIS
    sequence_end()
    p                           handlerton.
    type                        panic type.
  RETURN VALUES
    0           Success
    !=0         Failure
*/
static int sequence_end(handlerton* hton,
                        ha_panic_function type __attribute__((unused)))
{
  DBUG_ENTER("sequence_end");
  DBUG_RETURN(0);
}


/*
  Sequence engine init.

  SYNOPSIS
    sequence_initialize()

    @param p    handlerton.

    retval 0    Success
    retval !=0  Failure
*/

static int sequence_initialize(void *p)
{
  handlerton *local_sequence_hton= (handlerton *)p;
  DBUG_ENTER("sequence_initialize");

  local_sequence_hton->db_type= DB_TYPE_SEQUENCE;
  local_sequence_hton->create= sequence_create_handler;
  local_sequence_hton->panic= sequence_end;
  local_sequence_hton->flags= (HTON_NOT_USER_SELECTABLE |
                               HTON_HIDDEN |
                               HTON_TEMPORARY_NOT_SUPPORTED |
                               HTON_ALTER_NOT_SUPPORTED |
#ifdef WITH_WSREP
                               HTON_WSREP_REPLICATION |
#endif
                               HTON_NO_PARTITION);
  DBUG_RETURN(0);
}


static struct st_mysql_storage_engine sequence_storage_engine=
{ MYSQL_HANDLERTON_INTERFACE_VERSION };

maria_declare_plugin(sql_sequence)
{
  MYSQL_STORAGE_ENGINE_PLUGIN,
  &sequence_storage_engine,
  "SQL_SEQUENCE",
  "jianwei.zhao @ Aliyun & Monty @ MariaDB corp",
  "Sequence Storage Engine for CREATE SEQUENCE",
  PLUGIN_LICENSE_GPL,
  sequence_initialize,        /* Plugin Init */
  NULL,                       /* Plugin Deinit */
  0x0100,                     /* 1.0 */
  NULL,                       /* status variables                */
  NULL,                       /* system variables                */
  "1.0",                      /* string version                  */
  MariaDB_PLUGIN_MATURITY_STABLE /* maturity                     */
}
maria_declare_plugin_end;