trnman.c 25.8 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2006 MySQL AB
unknown's avatar
unknown committed
2 3 4

   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
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7 8 9 10 11 12 13 14 15 16 17 18

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include <my_global.h>
#include <my_sys.h>
unknown's avatar
unknown committed
19
#include <m_string.h>
unknown's avatar
unknown committed
20
#include "trnman.h"
unknown's avatar
unknown committed
21
#include "ma_checkpoint.h"
unknown's avatar
unknown committed
22
#include "ma_control_file.h"
unknown's avatar
unknown committed
23

unknown's avatar
unknown committed
24 25 26 27 28 29 30
/*
  status variables:
  how many trns in the active list currently,
  in the committed list currently, allocated since startup.
*/
uint trnman_active_transactions, trnman_committed_transactions,
  trnman_allocated_transactions;
unknown's avatar
unknown committed
31

unknown's avatar
unknown committed
32 33 34 35
/* list of active transactions in the trid order */
static TRN active_list_min, active_list_max;
/* list of committed transactions in the trid order */
static TRN committed_list_min, committed_list_max;
unknown's avatar
unknown committed
36

unknown's avatar
unknown committed
37
/* a counter, used to generate transaction ids */
unknown's avatar
unknown committed
38 39
static TrID global_trid_generator;

unknown's avatar
unknown committed
40 41 42 43 44 45 46
/* the mutex for everything above */
static pthread_mutex_t LOCK_trn_list;

/* LIFO pool of unused TRN structured for reuse */
static TRN *pool;

/* a hash for committed transactions that maps trid to a TRN structure */
Sergei Golubchik's avatar
Sergei Golubchik committed
47
static LF_HASH trid_to_trn;
unknown's avatar
unknown committed
48

49
/* an array that maps short_id of an active transaction to a TRN structure */
unknown's avatar
unknown committed
50 51 52
static TRN **short_trid_to_active_trn;

/* locks for short_trid_to_active_trn and pool */
unknown's avatar
unknown committed
53
static my_atomic_rwlock_t LOCK_short_trid_to_trn, LOCK_pool;
54 55 56 57
static my_bool default_trnman_end_trans_hook(TRN *, my_bool, my_bool);

my_bool (*trnman_end_trans_hook)(TRN *, my_bool, my_bool)=
  default_trnman_end_trans_hook;
unknown's avatar
unknown committed
58

59
/*
60
  Simple interface functions
61
  QQ: if they stay so simple, should we make them inline?
62
*/
63 64 65 66 67 68

uint trnman_increment_locked_tables(TRN *trn)
{
  return trn->locked_tables++;
}

unknown's avatar
unknown committed
69
uint trnman_has_locked_tables(TRN *trn)
70
{
unknown's avatar
unknown committed
71
  return trn->locked_tables;
72 73 74 75 76 77 78
}

uint trnman_decrement_locked_tables(TRN *trn)
{
  return --trn->locked_tables;
}

unknown's avatar
unknown committed
79
void trnman_reset_locked_tables(TRN *trn, uint locked_tables)
80
{
unknown's avatar
unknown committed
81
  trn->locked_tables= locked_tables;
82 83
}

84 85 86 87 88 89
static void wt_thd_release_self(TRN *trn)
{
  if (trn->wt)
  {
    WT_RESOURCE_ID rc;
    rc.type= &ma_rc_dup_unique;
90
    rc.value= (intptr)trn;
91
    wt_thd_release(trn->wt, & rc);
92
    trn->wt= 0;
93 94
  }
}
unknown's avatar
unknown committed
95

96 97 98 99 100 101 102 103 104 105
static my_bool
default_trnman_end_trans_hook(TRN *trn __attribute__ ((unused)),
                              my_bool commit __attribute__ ((unused)),
                              my_bool active_transactions
                              __attribute__ ((unused)))
{
  return 0;
}


106
static uchar *trn_get_hash_key(const uchar *trn, size_t *len,
unknown's avatar
unknown committed
107
                              my_bool unused __attribute__ ((unused)))
unknown's avatar
unknown committed
108
{
109
  *len= sizeof(TrID);
unknown's avatar
unknown committed
110
  return (uchar *) & ((*((TRN **)trn))->trid);
111
}
unknown's avatar
unknown committed
112

unknown's avatar
unknown committed
113 114 115 116 117 118 119 120 121 122 123 124

/**
   @brief Initializes transaction manager.

   @param  initial_trid        Generated TrIDs will start from initial_trid+1.

   @return Operation status
     @retval 0      OK
     @retval !=0    Error
*/

int trnman_init(TrID initial_trid)
125
{
126
  DBUG_ENTER("trnman_init");
127 128 129 130 131

  short_trid_to_active_trn= (TRN **)my_malloc(SHORT_TRID_MAX*sizeof(TRN*),
                                     MYF(MY_WME|MY_ZEROFILL));
  if (unlikely(!short_trid_to_active_trn))
    DBUG_RETURN(1);
132
  short_trid_to_active_trn--; /* min short_id is 1 */
133

unknown's avatar
unknown committed
134 135 136 137 138 139 140 141 142 143
  /*
    Initialize lists.
    active_list_max.min_read_from must be larger than any trid,
    so that when an active list is empty we would could free
    all committed list.
    And  committed_list_max itself can not be freed so
    committed_list_max.commit_trid must not be smaller that
    active_list_max.min_read_from
  */

unknown's avatar
unknown committed
144
  active_list_max.trid= active_list_min.trid= 0;
145
  active_list_max.min_read_from= ~(TrID) 0;
unknown's avatar
unknown committed
146 147 148 149
  active_list_max.next= active_list_min.prev= 0;
  active_list_max.prev= &active_list_min;
  active_list_min.next= &active_list_max;

150
  committed_list_max.commit_trid= ~(TrID) 0;
unknown's avatar
unknown committed
151 152 153 154
  committed_list_max.next= committed_list_min.prev= 0;
  committed_list_max.prev= &committed_list_min;
  committed_list_min.next= &committed_list_max;

unknown's avatar
unknown committed
155
  trnman_active_transactions= 0;
unknown's avatar
unknown committed
156
  trnman_committed_transactions= 0;
unknown's avatar
unknown committed
157 158
  trnman_allocated_transactions= 0;

unknown's avatar
unknown committed
159
  pool= 0;
unknown's avatar
unknown committed
160
  global_trid_generator= initial_trid;
Sergei Golubchik's avatar
Sergei Golubchik committed
161
  lf_hash_init(&trid_to_trn, sizeof(TRN*), LF_HASH_UNIQUE,
unknown's avatar
unknown committed
162
               0, 0, trn_get_hash_key, 0);
163
  DBUG_PRINT("info", ("pthread_mutex_init LOCK_trn_list"));
164
  pthread_mutex_init(&LOCK_trn_list, MY_MUTEX_INIT_FAST);
unknown's avatar
unknown committed
165 166 167
  my_atomic_rwlock_init(&LOCK_short_trid_to_trn);
  my_atomic_rwlock_init(&LOCK_pool);

168
  DBUG_RETURN(0);
unknown's avatar
unknown committed
169 170
}

unknown's avatar
unknown committed
171 172 173 174 175
/*
  NOTE
    this could only be called in the "idle" state - no transaction can be
    running. See asserts below.
*/
176
void trnman_destroy()
unknown's avatar
unknown committed
177
{
178
  DBUG_ENTER("trnman_destroy");
179 180 181

  if (short_trid_to_active_trn == NULL) /* trnman already destroyed */
    DBUG_VOID_RETURN;
Sergei Golubchik's avatar
Sergei Golubchik committed
182
  DBUG_ASSERT(trid_to_trn.count == 0);
unknown's avatar
unknown committed
183
  DBUG_ASSERT(trnman_active_transactions == 0);
unknown's avatar
unknown committed
184
  DBUG_ASSERT(trnman_committed_transactions == 0);
unknown's avatar
unknown committed
185 186 187 188 189 190 191 192
  DBUG_ASSERT(active_list_max.prev == &active_list_min);
  DBUG_ASSERT(active_list_min.next == &active_list_max);
  DBUG_ASSERT(committed_list_max.prev == &committed_list_min);
  DBUG_ASSERT(committed_list_min.next == &committed_list_max);
  while (pool)
  {
    TRN *trn= pool;
    pool= pool->next;
193
    pthread_mutex_destroy(&trn->state_lock);
unknown's avatar
unknown committed
194 195
    my_free((void *)trn, MYF(0));
  }
Sergei Golubchik's avatar
Sergei Golubchik committed
196
  lf_hash_destroy(&trid_to_trn);
197
  DBUG_PRINT("info", ("pthread_mutex_destroy LOCK_trn_list"));
unknown's avatar
unknown committed
198 199 200
  pthread_mutex_destroy(&LOCK_trn_list);
  my_atomic_rwlock_destroy(&LOCK_short_trid_to_trn);
  my_atomic_rwlock_destroy(&LOCK_pool);
unknown's avatar
unknown committed
201
  my_free((void *)(short_trid_to_active_trn+1), MYF(0));
202
  short_trid_to_active_trn= NULL;
203

204
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
205 206
}

unknown's avatar
unknown committed
207 208 209 210 211 212
/*
  NOTE
    TrID is limited to 6 bytes. Initial value of the generator
    is set by the recovery code - being read from the last checkpoint
    (or 1 on a first run).
*/
unknown's avatar
unknown committed
213 214
static TrID new_trid()
{
215
  DBUG_ENTER("new_trid");
unknown's avatar
unknown committed
216
  DBUG_ASSERT(global_trid_generator < 0xffffffffffffLL);
217
  DBUG_PRINT("info", ("safe_mutex_assert_owner LOCK_trn_list"));
unknown's avatar
unknown committed
218
  safe_mutex_assert_owner(&LOCK_trn_list);
219
  DBUG_RETURN(++global_trid_generator);
unknown's avatar
unknown committed
220 221
}

222
static uint get_short_trid(TRN *trn)
unknown's avatar
unknown committed
223
{
224 225
  int i= (int) ((global_trid_generator + (intptr)trn) * 312089 %
                SHORT_TRID_MAX + 1);
226 227 228
  uint res=0;

  for ( ; !res ; i= 1)
unknown's avatar
unknown committed
229
  {
unknown's avatar
unknown committed
230 231 232 233 234 235 236
    my_atomic_rwlock_wrlock(&LOCK_short_trid_to_trn);
    for ( ; i <= SHORT_TRID_MAX; i++) /* the range is [1..SHORT_TRID_MAX] */
    {
      void *tmp= NULL;
      if (short_trid_to_active_trn[i] == NULL &&
          my_atomic_casptr((void **)&short_trid_to_active_trn[i], &tmp, trn))
      {
237
        res= i;
unknown's avatar
unknown committed
238 239 240 241
        break;
      }
    }
    my_atomic_rwlock_wrunlock(&LOCK_short_trid_to_trn);
unknown's avatar
unknown committed
242
  }
243
  return res;
unknown's avatar
unknown committed
244 245
}

unknown's avatar
unknown committed
246 247 248 249 250
/*
  DESCRIPTION
    start a new transaction, allocate and initialize transaction object
    mutex and cond will be used for lock waits
*/
251

252
TRN *trnman_new_trn(WT_THD *wt)
unknown's avatar
unknown committed
253
{
Sergei Golubchik's avatar
Sergei Golubchik committed
254
  int res;
unknown's avatar
unknown committed
255
  TRN *trn;
256
  DBUG_ENTER("trnman_new_trn");
unknown's avatar
unknown committed
257 258

  /*
unknown's avatar
unknown committed
259 260
    we have a mutex, to do simple things under it - allocate a TRN,
    increment trnman_active_transactions, set trn->min_read_from.
unknown's avatar
unknown committed
261

262
    Note that all the above is fast. generating short_id may be slow,
unknown's avatar
unknown committed
263 264
    as it involves scanning a large array - so it's done outside of the
    mutex.
unknown's avatar
unknown committed
265 266
  */

267
  DBUG_PRINT("info", ("pthread_mutex_lock LOCK_trn_list"));
unknown's avatar
unknown committed
268 269
  pthread_mutex_lock(&LOCK_trn_list);

unknown's avatar
unknown committed
270
  /* Allocating a new TRN structure */
unknown's avatar
unknown committed
271
  trn= pool;
unknown's avatar
unknown committed
272 273 274 275
  /*
    Popping an unused TRN from the pool
    (ABA isn't possible, we're behind a mutex
  */
unknown's avatar
unknown committed
276 277 278 279 280 281
  my_atomic_rwlock_wrlock(&LOCK_pool);
  while (trn && !my_atomic_casptr((void **)&pool, (void **)&trn,
                                  (void *)trn->next))
    /* no-op */;
  my_atomic_rwlock_wrunlock(&LOCK_pool);

unknown's avatar
unknown committed
282
  /* Nothing in the pool ? Allocate a new one */
unknown's avatar
unknown committed
283 284
  if (!trn)
  {
285 286 287 288 289 290 291
    /*
      trn should be completely initalized at create time to allow
      one to keep a known state on it.
      (Like redo_lns, which is assumed to be 0 at start of row handling
      and reset to zero before end of row handling)
    */
    trn= (TRN *)my_malloc(sizeof(TRN), MYF(MY_WME | MY_ZEROFILL));
unknown's avatar
unknown committed
292
    if (unlikely(!trn))
unknown's avatar
unknown committed
293
    {
294
      DBUG_PRINT("info", ("pthread_mutex_unlock LOCK_trn_list"));
unknown's avatar
unknown committed
295 296 297 298
      pthread_mutex_unlock(&LOCK_trn_list);
      return 0;
    }
    trnman_allocated_transactions++;
299
    pthread_mutex_init(&trn->state_lock, MY_MUTEX_INIT_FAST);
unknown's avatar
unknown committed
300
  }
301
  trn->wt= wt;
Sergei Golubchik's avatar
Sergei Golubchik committed
302
  trn->pins= lf_hash_get_pins(&trid_to_trn);
303 304 305 306 307 308
  if (!trn->pins)
  {
    trnman_free_trn(trn);
    return 0;
  }

unknown's avatar
unknown committed
309
  trnman_active_transactions++;
unknown's avatar
unknown committed
310 311 312 313 314 315 316 317

  trn->min_read_from= active_list_min.next->trid;

  trn->trid= new_trid();

  trn->next= &active_list_max;
  trn->prev= active_list_max.prev;
  active_list_max.prev= trn->prev->next= trn;
318
  DBUG_PRINT("info", ("pthread_mutex_unlock LOCK_trn_list"));
unknown's avatar
unknown committed
319 320
  pthread_mutex_unlock(&LOCK_trn_list);

unknown's avatar
unknown committed
321
  if (unlikely(!trn->min_read_from))
unknown's avatar
unknown committed
322 323 324 325 326 327 328
  {
    /*
      We are the only transaction. Set min_read_from so that we can read
      our own rows
    */
    trn->min_read_from= trn->trid + 1;
  }
unknown's avatar
unknown committed
329

Sergei Golubchik's avatar
Sergei Golubchik committed
330
  trn->commit_trid=  ~(TrID)0;
331
  trn->rec_lsn= trn->undo_lsn= trn->first_undo_lsn= 0;
332
  trn->used_tables= 0;
unknown's avatar
unknown committed
333

334
  trn->locked_tables= 0;
unknown's avatar
unknown committed
335

unknown's avatar
unknown committed
336 337 338 339
  /*
    only after the following function TRN is considered initialized,
    so it must be done the last
  */
340 341 342
  pthread_mutex_lock(&trn->state_lock);
  trn->short_id= get_short_trid(trn);
  pthread_mutex_unlock(&trn->state_lock);
unknown's avatar
unknown committed
343

Sergei Golubchik's avatar
Sergei Golubchik committed
344 345 346 347 348 349 350 351
  res= lf_hash_insert(&trid_to_trn, trn->pins, &trn);
  DBUG_ASSERT(res <= 0);
  if (res)
  {
    trnman_end_trn(trn, 0);
    return 0;
  }

352 353 354
  DBUG_PRINT("exit", ("trn: x%lx  trid: 0x%lu",
                      (ulong) trn, (ulong) trn->trid));

355
  DBUG_RETURN(trn);
unknown's avatar
unknown committed
356 357 358
}

/*
unknown's avatar
unknown committed
359 360
  remove a trn from the active list.
  if necessary - move to committed list and set commit_trid
361 362 363 364 365 366 367 368 369

  NOTE
    Locks are released at the end. In particular, after placing the
    transaction in commit list, and after setting commit_trid. It's
    important, as commit_trid affects visibility.  Locks don't affect
    anything they simply delay execution of other threads - they could be
    released arbitrarily late. In other words, when locks are released it
    serves as a start banner for other threads, they start to run. So
    everything they may need must be ready at that point.
370 371 372 373

  RETURN
    0  ok
    1  error
unknown's avatar
unknown committed
374
*/
375
my_bool trnman_end_trn(TRN *trn, my_bool commit)
unknown's avatar
unknown committed
376
{
377
  int res= 1;
unknown's avatar
unknown committed
378 379
  TRN *free_me= 0;
  LF_PINS *pins= trn->pins;
380
  DBUG_ENTER("trnman_end_trn");
unknown's avatar
unknown committed
381

382 383 384
  DBUG_ASSERT(trn->rec_lsn == 0);
  /* if a rollback, all UNDO records should have been executed */
  DBUG_ASSERT(commit || trn->undo_lsn == 0);
385
  DBUG_PRINT("info", ("pthread_mutex_lock LOCK_trn_list"));
386

unknown's avatar
unknown committed
387
  pthread_mutex_lock(&LOCK_trn_list);
unknown's avatar
unknown committed
388 389

  /* remove from active list */
unknown's avatar
unknown committed
390 391 392
  trn->next->prev= trn->prev;
  trn->prev->next= trn->next;

unknown's avatar
unknown committed
393 394 395 396 397
  /*
    if trn was the oldest active transaction, now that it goes away there
    may be committed transactions in the list which no active transaction
    needs to bother about - clean up the committed list
  */
unknown's avatar
unknown committed
398 399
  if (trn->prev == &active_list_min)
  {
unknown's avatar
unknown committed
400
    uint free_me_count;
unknown's avatar
unknown committed
401
    TRN *t;
unknown's avatar
unknown committed
402
    for (t= committed_list_min.next, free_me_count= 0;
unknown's avatar
unknown committed
403
         t->commit_trid < active_list_min.next->min_read_from;
unknown's avatar
unknown committed
404
         t= t->next, free_me_count++) /* no-op */;
unknown's avatar
unknown committed
405

unknown's avatar
unknown committed
406 407
    DBUG_ASSERT((t != committed_list_min.next && free_me_count > 0) ||
                (t == committed_list_min.next && free_me_count == 0));
unknown's avatar
unknown committed
408
    /* found transactions committed before the oldest active one */
unknown's avatar
unknown committed
409 410 411 412 413 414
    if (t != committed_list_min.next)
    {
      free_me= committed_list_min.next;
      committed_list_min.next= t;
      t->prev->next= 0;
      t->prev= &committed_list_min;
unknown's avatar
unknown committed
415
      trnman_committed_transactions-= free_me_count;
unknown's avatar
unknown committed
416 417 418
    }
  }

419 420 421 422 423
  pthread_mutex_lock(&trn->state_lock);
  trn->commit_trid= global_trid_generator;
  wt_thd_release_self(trn);
  pthread_mutex_unlock(&trn->state_lock);

unknown's avatar
unknown committed
424 425
  /*
    if transaction is committed and it was not the only active transaction -
Sergei Golubchik's avatar
Sergei Golubchik committed
426
    add it to the committed list
unknown's avatar
unknown committed
427
  */
unknown's avatar
unknown committed
428 429 430 431
  if (commit && active_list_min.next != &active_list_max)
  {
    trn->next= &committed_list_max;
    trn->prev= committed_list_max.prev;
unknown's avatar
unknown committed
432
    trnman_committed_transactions++;
Sergei Golubchik's avatar
Sergei Golubchik committed
433
    committed_list_max.prev= trn->prev->next= trn;
unknown's avatar
unknown committed
434
  }
Sergei Golubchik's avatar
Sergei Golubchik committed
435
  else
unknown's avatar
unknown committed
436 437 438 439
  {
    trn->next= free_me;
    free_me= trn;
  }
440 441 442
  if ((*trnman_end_trans_hook)(trn, commit,
                               active_list_min.next != &active_list_max))
    res= -1;
unknown's avatar
unknown committed
443
  trnman_active_transactions--;
444

unknown's avatar
unknown committed
445 446
  pthread_mutex_unlock(&LOCK_trn_list);

unknown's avatar
unknown committed
447
  /* the rest is done outside of a critical section */
unknown's avatar
unknown committed
448
  my_atomic_rwlock_rdlock(&LOCK_short_trid_to_trn);
449
  my_atomic_storeptr((void **)&short_trid_to_active_trn[trn->short_id], 0);
unknown's avatar
unknown committed
450 451
  my_atomic_rwlock_rdunlock(&LOCK_short_trid_to_trn);

unknown's avatar
unknown committed
452 453 454 455 456 457
  /*
    we, under the mutex, removed going-in-free_me transactions from the
    active and committed lists, thus nobody else may see them when it scans
    those lists, and thus nobody may want to free them. Now we don't
    need a mutex to access free_me list
  */
unknown's avatar
unknown committed
458
  /* QQ: send them to the purge thread */
459
  while (free_me)
unknown's avatar
unknown committed
460 461 462 463
  {
    TRN *t= free_me;
    free_me= free_me->next;

464
    /* ignore OOM. it's harmless, and we can do nothing here anyway */
Sergei Golubchik's avatar
Sergei Golubchik committed
465
    (void)lf_hash_delete(&trid_to_trn, pins, &t->trid, sizeof(TrID));
unknown's avatar
unknown committed
466 467 468 469 470

    trnman_free_trn(t);
  }

  lf_hash_put_pins(pins);
471 472

  DBUG_RETURN(res < 0);
unknown's avatar
unknown committed
473 474 475 476
}

/*
  free a trn (add to the pool, that is)
477 478 479 480 481 482 483
  note - we can never really free() a TRN if there's at least one other
  running transaction - see, e.g., how lock waits are implemented in
  lockman.c
  The same is true for other lock-free data structures too. We may need some
  kind of FLUSH command to reset them all - ensuring that no transactions are
  running. It may even be called automatically on checkpoints if no
  transactions are running.
unknown's avatar
unknown committed
484 485 486
*/
void trnman_free_trn(TRN *trn)
{
487 488 489 490 491 492 493
  /*
     union is to solve strict aliasing issue.
     without it gcc 3.4.3 doesn't notice that updating *(void **)&tmp
     modifies the value of tmp.
  */
  union { TRN *trn; void *v; } tmp;

494 495 496 497 498

  pthread_mutex_lock(&trn->state_lock);
  trn->short_id= 0;
  pthread_mutex_unlock(&trn->state_lock);

499
  tmp.trn= pool;
unknown's avatar
unknown committed
500 501 502 503 504

  my_atomic_rwlock_wrlock(&LOCK_pool);
  do
  {
    /*
505
      without this volatile cast gcc-3.4.4 moves the assignment
unknown's avatar
unknown committed
506 507
      down after the loop at -O2
    */
508 509
    *(TRN * volatile *)&(trn->next)= tmp.trn;
  } while (!my_atomic_casptr((void **)&pool, &tmp.v, trn));
unknown's avatar
unknown committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  my_atomic_rwlock_wrunlock(&LOCK_pool);
}

/*
  NOTE
    here we access the hash in a lock-free manner.
    It's safe, a 'found' TRN can never be freed/reused before we access it.
    In fact, it cannot be freed before 'trn' ends, because a 'found' TRN
    can only be removed from the hash when:
                found->commit_trid < ALL (trn->min_read_from)
    that is, at least
                found->commit_trid < trn->min_read_from
    but
                found->trid >= trn->min_read_from
    and
                found->commit_trid > found->trid
526 527 528 529 530

  RETURN
    1   can
    0   cannot
   -1   error (OOM)
unknown's avatar
unknown committed
531
*/
532
int trnman_can_read_from(TRN *trn, TrID trid)
unknown's avatar
unknown committed
533 534 535 536 537 538
{
  TRN **found;
  my_bool can;
  LF_REQUIRE_PINS(3);

  if (trid < trn->min_read_from)
unknown's avatar
unknown committed
539 540 541 542 543 544 545 546 547 548 549 550 551
    return 1; /* Row is visible by all transactions in the system */

  if (trid >= trn->trid)
  {
    /*
      We have now two cases
      trid > trn->trid, in which case the row is from a new transaction
      and not visible, in which case we should return 0.
      trid == trn->trid in which case the row is from the current transaction
      and we should return 1
    */
    return trid == trn->trid;
  }
unknown's avatar
unknown committed
552

Sergei Golubchik's avatar
Sergei Golubchik committed
553
  found= lf_hash_search(&trid_to_trn, trn->pins, &trid, sizeof(trid));
554
  if (found == NULL)
Sergei Golubchik's avatar
Sergei Golubchik committed
555
    return 0; /* not in the hash of transactions = cannot read */
556 557
  if (found == MY_ERRPTR)
    return -1;
unknown's avatar
unknown committed
558 559

  can= (*found)->commit_trid < trn->trid;
560
  lf_hash_search_unpin(trn->pins);
unknown's avatar
unknown committed
561 562 563
  return can;
}

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
TRN *trnman_trid_to_trn(TRN *trn, TrID trid)
{
  TRN **found;
  LF_REQUIRE_PINS(3);

  if (trid < trn->min_read_from)
    return 0; /* it's committed eons ago */

  found= lf_hash_search(&trid_to_trn, trn->pins, &trid, sizeof(trid));
  if (found == NULL || found == MY_ERRPTR)
    return 0; /* no luck */

  /* we've found something */
  pthread_mutex_lock(&(*found)->state_lock);

  if ((*found)->short_id == 0)
  {
    pthread_mutex_unlock(&(*found)->state_lock);
    lf_hash_search_unpin(trn->pins);
    return 0; /* but it was a ghost */
  }
  lf_hash_search_unpin(trn->pins);

  /* Gotcha! */
  return *found; /* note that TRN is returned locked !!! */
}

591 592 593 594 595 596 597 598 599 600
/* TODO: the stubs below are waiting for savepoints to be implemented */

void trnman_new_statement(TRN *trn __attribute__ ((unused)))
{
}

void trnman_rollback_statement(TRN *trn __attribute__ ((unused)))
{
}

unknown's avatar
unknown committed
601

602 603
/**
   @brief Allocates buffers and stores in them some info about transactions
unknown's avatar
unknown committed
604

605 606 607 608
   Does the allocation because the caller cannot know the size itself.
   Memory freeing is to be done by the caller (if the "str" member of the
   LEX_STRING is not NULL).
   The caller has the intention of doing checkpoints.
unknown's avatar
unknown committed
609

610 611 612 613 614 615 616 617
   @param[out]  str_act    pointer to where the allocated buffer,
                           and its size, will be put; buffer will be filled
                           with info about active transactions
   @param[out]  str_com    pointer to where the allocated buffer,
                           and its size, will be put; buffer will be filled
                           with info about committed transactions
   @param[out]  min_first_undo_lsn pointer to where the minimum
                           first_undo_lsn of all transactions will be put
unknown's avatar
unknown committed
618

619 620 621
   @return Operation status
     @retval 0      OK
     @retval 1      Error
unknown's avatar
unknown committed
622
*/
623 624 625

my_bool trnman_collect_transactions(LEX_STRING *str_act, LEX_STRING *str_com,
                                    LSN *min_rec_lsn, LSN *min_first_undo_lsn)
unknown's avatar
unknown committed
626 627 628 629
{
  my_bool error;
  TRN *trn;
  char *ptr;
630
  uint stored_transactions= 0;
unknown's avatar
unknown committed
631
  LSN minimum_rec_lsn= LSN_MAX, minimum_first_undo_lsn= LSN_MAX;
unknown's avatar
unknown committed
632 633 634 635
  DBUG_ENTER("trnman_collect_transactions");

  DBUG_ASSERT((NULL == str_act->str) && (NULL == str_com->str));

636 637
  /* validate the use of read_non_atomic() in general: */
  compile_time_assert((sizeof(LSN) == 8) && (sizeof(LSN_WITH_FLAGS) == 8));
unknown's avatar
unknown committed
638
  pthread_mutex_lock(&LOCK_trn_list);
639 640
  str_act->length= 2 + /* number of active transactions */
    LSN_STORE_SIZE + /* minimum of their rec_lsn */
unknown's avatar
unknown committed
641
    TRANSID_SIZE + /* current TrID generator value */
unknown's avatar
unknown committed
642 643
    (2 + /* short id */
     6 + /* long id */
644 645 646 647 648 649
     LSN_STORE_SIZE + /* undo_lsn */
#ifdef MARIA_VERSIONING /* not enabled yet */
     LSN_STORE_SIZE + /* undo_purge_lsn */
#endif
     LSN_STORE_SIZE /* first_undo_lsn */
     ) * trnman_active_transactions;
unknown's avatar
unknown committed
650
  str_com->length= 4 + /* number of committed transactions */
651 652 653 654 655 656
    (6 + /* long id */
#ifdef MARIA_VERSIONING /* not enabled yet */
     LSN_STORE_SIZE + /* undo_purge_lsn */
#endif
     LSN_STORE_SIZE /* first_undo_lsn */
     ) * trnman_committed_transactions;
unknown's avatar
unknown committed
657 658 659 660
  if ((NULL == (str_act->str= my_malloc(str_act->length, MYF(MY_WME)))) ||
      (NULL == (str_com->str= my_malloc(str_com->length, MYF(MY_WME)))))
    goto err;
  /* First, the active transactions */
661
  ptr= str_act->str + 2 + LSN_STORE_SIZE;
unknown's avatar
unknown committed
662 663
  transid_store(ptr, global_trid_generator);
  ptr+= TRANSID_SIZE;
unknown's avatar
unknown committed
664 665 666
  for (trn= active_list_min.next; trn != &active_list_max; trn= trn->next)
  {
    /*
667 668 669
      trns with a short trid of 0 are not even initialized, we can ignore
      them. trns with undo_lsn==0 have done no writes, we can ignore them
      too. XID not needed now.
unknown's avatar
unknown committed
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
    uint sid;
    LSN rec_lsn, undo_lsn, first_undo_lsn;
    if ((sid= trn->short_id) == 0)
    {
      /*
        Not even inited, has done nothing. Or it is the
        dummy_transaction_object, which does only non-transactional
        immediate-sync operations (CREATE/DROP/RENAME/REPAIR TABLE), and so
        can be forgotten for Checkpoint.
      */
      continue;
    }
      /* needed for low-water mark calculation */
    if (((rec_lsn= lsn_read_non_atomic(trn->rec_lsn)) > 0) &&
        (cmp_translog_addr(rec_lsn, minimum_rec_lsn) < 0))
      minimum_rec_lsn= rec_lsn;
    /*
      trn may have logged REDOs but not yet UNDO, that's why we read rec_lsn
      before deciding to ignore if undo_lsn==0.
    */
    if  ((undo_lsn= trn->undo_lsn) == 0) /* trn can be forgotten */
      continue;
    stored_transactions++;
    int2store(ptr, sid);
unknown's avatar
unknown committed
695
    ptr+= 2;
unknown's avatar
unknown committed
696 697
    int6store(ptr, trn->trid);
    ptr+= 6;
698 699
    lsn_store(ptr, undo_lsn); /* needed for rollback */
    ptr+= LSN_STORE_SIZE;
unknown's avatar
unknown committed
700
    /* needed for low-water mark calculation */
701 702 703 704 705
    if (((first_undo_lsn= lsn_read_non_atomic(trn->first_undo_lsn)) > 0) &&
        (cmp_translog_addr(first_undo_lsn, minimum_first_undo_lsn) < 0))
      minimum_first_undo_lsn= first_undo_lsn;
    lsn_store(ptr, first_undo_lsn);
    ptr+= LSN_STORE_SIZE;
unknown's avatar
unknown committed
706 707 708 709 710
#ifdef MARIA_VERSIONING /* not enabled yet */
    /* to know where purging should start (last delete of this trn) */
    lsn_store(ptr, trn->undo_purge_lsn);
    ptr+= LSN_STORE_SIZE;
#endif
711 712 713 714
    /**
       @todo RECOVERY: add a comment explaining why we can dirtily read some
       vars, inspired by the text of "assumption 8" in WL#3072
    */
unknown's avatar
unknown committed
715
  }
716 717
  str_act->length= ptr - str_act->str; /* as we maybe over-estimated */
  ptr= str_act->str;
unknown's avatar
unknown committed
718 719
  DBUG_PRINT("info",("collected %u active transactions",
                     (uint)stored_transactions));
720 721 722 723 724
  int2store(ptr, stored_transactions);
  ptr+= 2;
  /* this LSN influences how REDOs for any page can be ignored by Recovery */
  lsn_store(ptr, minimum_rec_lsn);
  /* one day there will also be a list of prepared transactions */
unknown's avatar
unknown committed
725 726
  /* do the same for committed ones */
  ptr= str_com->str;
unknown's avatar
unknown committed
727 728 729 730
  int4store(ptr, trnman_committed_transactions);
  ptr+= 4;
  DBUG_PRINT("info",("collected %u committed transactions",
                     (uint)trnman_committed_transactions));
unknown's avatar
unknown committed
731 732 733
  for (trn= committed_list_min.next; trn != &committed_list_max;
       trn= trn->next)
  {
734
    LSN first_undo_lsn;
unknown's avatar
unknown committed
735 736
    int6store(ptr, trn->trid);
    ptr+= 6;
737 738 739 740 741 742 743 744 745
#ifdef MARIA_VERSIONING /* not enabled yet */
    lsn_store(ptr, trn->undo_purge_lsn);
    ptr+= LSN_STORE_SIZE;
#endif
    first_undo_lsn= LSN_WITH_FLAGS_TO_LSN(trn->first_undo_lsn);
    if (cmp_translog_addr(first_undo_lsn, minimum_first_undo_lsn) < 0)
      minimum_first_undo_lsn= first_undo_lsn;
    lsn_store(ptr, first_undo_lsn);
    ptr+= LSN_STORE_SIZE;
unknown's avatar
unknown committed
746 747 748 749 750 751
  }
  /*
    TODO: if we see there exists no transaction (active and committed) we can
    tell the lock-free structures to do some freeing (my_free()).
  */
  error= 0;
752 753
  *min_rec_lsn= minimum_rec_lsn;
  *min_first_undo_lsn= minimum_first_undo_lsn;
unknown's avatar
unknown committed
754 755 756 757 758 759 760
  goto end;
err:
  error= 1;
end:
  pthread_mutex_unlock(&LOCK_trn_list);
  DBUG_RETURN(error);
}
unknown's avatar
unknown committed
761 762 763 764 765 766 767


TRN *trnman_recreate_trn_from_recovery(uint16 shortid, TrID longid)
{
  TrID old_trid_generator= global_trid_generator;
  TRN *trn;
  DBUG_ASSERT(maria_in_recovery && !maria_multi_threaded);
768
  global_trid_generator= longid-1; /* force a correct trid in the new trn */
769
  if (unlikely((trn= trnman_new_trn(NULL)) == NULL))
unknown's avatar
unknown committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    return NULL;
  /* deallocate excessive allocations of trnman_new_trn() */
  global_trid_generator= old_trid_generator;
  set_if_bigger(global_trid_generator, longid);
  short_trid_to_active_trn[trn->short_id]= 0;
  DBUG_ASSERT(short_trid_to_active_trn[shortid] == NULL);
  short_trid_to_active_trn[shortid]= trn;
  trn->short_id= shortid;
  return trn;
}


TRN *trnman_get_any_trn()
{
  TRN *trn= active_list_min.next;
  return (trn != &active_list_max) ? trn : NULL;
}
787 788


789
/**
790 791 792 793
  Returns the minimum existing transaction id

  @notes
    This can only be called when we have at least one running transaction.
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
*/

TrID trnman_get_min_trid()
{
  TrID min_read_from;
  if (short_trid_to_active_trn == NULL)
  {
    /* Transaction manager not initialize; Probably called from maria_chk */
    return ~(TrID) 0;
  }

  pthread_mutex_lock(&LOCK_trn_list);
  min_read_from= active_list_min.next->min_read_from;
  pthread_mutex_unlock(&LOCK_trn_list);
  return min_read_from;
}


812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
/**
  Returns the minimum possible transaction id

  @notes
  If there is no transactions running, returns number for next running
  transaction.
  If one has an active transaction, the returned number will be less or
  equal to this.  If one is not running in a transaction one will ge the
  number for the next started transaction.  This is used in create table
  to get a safe minimum trid to use.
*/

TrID trnman_get_min_safe_trid()
{
  TrID trid;
  pthread_mutex_lock(&LOCK_trn_list);
  trid= min(active_list_min.next->min_read_from,
            global_trid_generator);
  pthread_mutex_unlock(&LOCK_trn_list);
  return trid;
}


835 836 837
/**
  Returns maximum transaction id given to a transaction so far.
*/
838

839 840 841 842 843 844 845 846 847 848
TrID trnman_get_max_trid()
{
  TrID id;
  if (short_trid_to_active_trn == NULL)
    return 0;
  pthread_mutex_lock(&LOCK_trn_list);
  id= global_trid_generator;
  pthread_mutex_unlock(&LOCK_trn_list);
  return id;
}
849 850

/**
851
  @brief Check if there exist an active transaction between two commit_id's
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

  @todo
    Improve speed of this.
      - Store transactions in tree or skip list
      - Have function to copying all active transaction id's to b-tree
        and use b-tree for checking states.  This could be a big win
        for checkpoint that will call this function for a lot of objects.

  @return
    0   No transaction exists
    1   There is at least on active transaction in the given range
*/

my_bool trnman_exists_active_transactions(TrID min_id, TrID max_id,
                                          my_bool trnman_is_locked)
{
  TRN *trn;
  my_bool ret= 0;

  if (!trnman_is_locked)
    pthread_mutex_lock(&LOCK_trn_list);
  for (trn= active_list_min.next; trn != &active_list_max; trn= trn->next)
  {
    if (trn->trid > min_id && trn->trid < max_id)
    {
      ret= 1;
      break;
    }
  }
  if (!trnman_is_locked)
    pthread_mutex_unlock(&LOCK_trn_list);
  return ret;
}
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903


/**
   lock transaction list
*/

void trnman_lock()
{
  pthread_mutex_lock(&LOCK_trn_list);
}

/**
   unlock transaction list
*/

void trnman_unlock()
{
  pthread_mutex_unlock(&LOCK_trn_list);
}