txn_manager.cc 31.3 KB
Newer Older
1 2
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3
#ident "$Id$"
4
#ident "Copyright (c) 2007-2012 Tokutek Inc.  All rights reserved."
5 6
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."

7 8 9
#include <toku_race_tools.h>

#include <util/omt.h>
10

11
#include "log-internal.h"
12 13 14 15
#include "txn.h"
#include "checkpoint.h"
#include "ule.h"
#include "txn_manager.h"
16
#include "rollback.h"
17

Yoni Fogel's avatar
Yoni Fogel committed
18
bool garbage_collection_debug = false;
19

20 21 22
// internal locking functions, should use this instead of accessing lock directly
static void txn_manager_lock(TXN_MANAGER txn_manager);
static void txn_manager_unlock(TXN_MANAGER txn_manager);
23

24
#if 0
Yoni Fogel's avatar
Yoni Fogel committed
25
static bool is_txnid_live(TXN_MANAGER txn_manager, TXNID txnid) {
26 27 28 29
    TOKUTXN result = NULL;
    toku_txn_manager_id2txn_unlocked(txn_manager, txnid, &result);
    return (result != NULL);
}
30
#endif
31

32
//Heaviside function to search through an OMT by a TXNID
33
int find_by_xid (const TOKUTXN &txn, const TXNID &txnidfind);
34

35
static void
36
verify_snapshot_system(TXN_MANAGER txn_manager UU()) {
37
#if 0
38
    uint32_t    num_snapshot_txnids = txn_manager->snapshot_txnids.size();
39
    TXNID       snapshot_txnids[num_snapshot_txnids];
40
    uint32_t    num_live_txns = txn_manager->live_txns.size();
41
    TOKUTXN     live_txns[num_live_txns];
42
    uint32_t    num_referenced_xid_tuples = txn_manager->referenced_xids.size();
43
    struct      referenced_xid_tuple  *referenced_xid_tuples[num_referenced_xid_tuples];
44 45

    int r;
46 47
    uint32_t i;
    uint32_t j;
48 49
    //set up arrays for easier access
    for (i = 0; i < num_snapshot_txnids; i++) {
50
        r = txn_manager->snapshot_txnids.fetch(i, &snapshot_txnids[i]);
51 52 53
        assert_zero(r);
    }
    for (i = 0; i < num_live_txns; i++) {
54
        r = txn_manager->live_txns.fetch(i, &live_txns[i]);
55 56
        assert_zero(r);
    }
57
    for (i = 0; i < num_referenced_xid_tuples; i++) {
58
        r = txn_manager->referenced_xids.fetch(i, &referenced_xid_tuples[i]);
59 60 61 62 63 64 65 66 67 68
        assert_zero(r);
    }

    {
        //Verify snapshot_txnids
        for (i = 0; i < num_snapshot_txnids; i++) {
            TXNID snapshot_xid = snapshot_txnids[i];
            invariant(is_txnid_live(txn_manager, snapshot_xid));
            TOKUTXN snapshot_txn;
            toku_txn_manager_id2txn_unlocked(txn_manager, snapshot_xid, &snapshot_txn);
69
            uint32_t num_live_root_txn_list = snapshot_txn->live_root_txn_list->size();
70 71 72
            TXNID     live_root_txn_list[num_live_root_txn_list];
            {
                for (j = 0; j < num_live_root_txn_list; j++) {
73
                    r = snapshot_txn->live_root_txn_list->fetch(j, &live_root_txn_list[j]);
74 75 76
                    assert_zero(r);
                }
            }
77 78 79 80 81 82 83 84 85
            {
                // Only committed entries have return a youngest.
                TXNID youngest = toku_get_youngest_live_list_txnid_for(
                    snapshot_xid,
                    txn_manager->snapshot_txnids,
                    txn_manager->referenced_xids
                    );
                invariant(youngest == TXNID_NONE);
            }
86 87 88 89
            for (j = 0; j < num_live_root_txn_list; j++) {
                TXNID live_xid = live_root_txn_list[j];
                invariant(live_xid <= snapshot_xid);
                TXNID youngest = toku_get_youngest_live_list_txnid_for(
90 91 92
                    live_xid,
                    txn_manager->snapshot_txnids,
                    txn_manager->referenced_xids
93
                    );
94 95 96 97
                if (is_txnid_live(txn_manager, live_xid)) {
                    // Only committed entries have return a youngest.
                    invariant(youngest == TXNID_NONE);
                }
98 99
                else {
                    invariant(youngest != TXNID_NONE);
100
                    // A committed entry might have been read-only, in which case it won't return anything.
101 102 103
                    // This snapshot reads 'live_xid' so it's youngest cannot be older than snapshot_xid.
                    invariant(youngest >= snapshot_xid);
                }
104 105 106 107
            }
        }
    }
    {
108 109 110 111 112
        // Verify referenced_xids.
        for (i = 0; i < num_referenced_xid_tuples; i++) {
            struct referenced_xid_tuple *tuple = referenced_xid_tuples[i];
            invariant(tuple->begin_id < tuple->end_id);
            invariant(tuple->references > 0);
113 114

            {
115
                //verify neither pair->begin_id nor end_id is in live_list
116
                r = txn_manager->live_txns.find_zero<TXNID, find_by_xid>(tuple->begin_id, nullptr, nullptr);
117
                invariant(r == DB_NOTFOUND);
118
                r = txn_manager->live_txns.find_zero<TXNID, find_by_xid>(tuple->end_id, nullptr, nullptr);
119 120 121 122
                invariant(r == DB_NOTFOUND);
            }
            {
                //verify neither pair->begin_id nor end_id is in snapshot_xids
123
                r = txn_manager->snapshot_txnids.find_zero<TXNID, toku_find_xid_by_xid>(tuple->begin_id, nullptr, nullptr);
124
                invariant(r == DB_NOTFOUND);
125
                r = txn_manager->snapshot_txnids.find_zero<TXNID, toku_find_xid_by_xid>(tuple->end_id, nullptr, nullptr);
126
                invariant(r == DB_NOTFOUND);
127
            }
128 129 130 131 132 133 134 135
            {
                // Verify number of references is correct
                uint32_t refs_found = 0;
                for (j = 0; j < num_snapshot_txnids; j++) {
                    TXNID snapshot_xid = snapshot_txnids[j];
                    TOKUTXN snapshot_txn;
                    toku_txn_manager_id2txn_unlocked(txn_manager, snapshot_xid, &snapshot_txn);

136
                    if (toku_is_txn_in_live_root_txn_list(*snapshot_txn->live_root_txn_list, tuple->begin_id)) {
137 138 139
                        refs_found++;
                    }
                    invariant(!toku_is_txn_in_live_root_txn_list(
140
                                *snapshot_txn->live_root_txn_list,
141
                                tuple->end_id));
142
                }
143 144 145 146 147 148 149 150 151 152 153 154 155
                invariant(refs_found == tuple->references);
            }
            {
                // Verify youngest makes sense.
                TXNID youngest = toku_get_youngest_live_list_txnid_for(
                    tuple->begin_id,
                    txn_manager->snapshot_txnids,
                    txn_manager->referenced_xids
                    );
                invariant(youngest != TXNID_NONE);
                invariant(youngest > tuple->begin_id);
                invariant(youngest < tuple->end_id);
                // Youngest must be found, and must be a snapshot txn
156
                r = txn_manager->snapshot_txnids.find_zero<TXNID, toku_find_xid_by_xid>(youngest, nullptr, nullptr);
157
                invariant_zero(r);
158 159 160 161 162 163 164 165
            }
        }
    }
    {
        //Verify live_txns
        for (i = 0; i < num_live_txns; i++) {
            TOKUTXN txn = live_txns[i];

Yoni Fogel's avatar
Yoni Fogel committed
166
            bool expect = txn->snapshot_txnid64 == txn->txnid64;
167 168
            {
                //verify pair->xid2 is in snapshot_xids
169
                r = txn_manager->snapshot_txnids.find_zero<TXNID, toku_find_xid_by_xid>(txn->txnid64, nullptr, nullptr);
170 171 172 173 174 175
                invariant(r==0 || r==DB_NOTFOUND);
                invariant((r==0) == (expect!=0));
            }

        }
    }
176
#endif
177 178 179
}

void toku_txn_manager_init(TXN_MANAGER* txn_managerp) {
180
    TXN_MANAGER XCALLOC(txn_manager);
181
    toku_mutex_init(&txn_manager->txn_manager_lock, NULL);
182
    txn_manager->live_root_txns.create();
183
    txn_manager->live_root_ids.create();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
184 185 186
    txn_manager->snapshot_head = NULL;
    txn_manager->snapshot_tail = NULL;
    txn_manager->num_snapshots = 0;
187
    txn_manager->referenced_xids.create();
188
    txn_manager->last_xid = 0;
189

190 191
    txn_manager->last_xid_seen_for_recover = TXNID_NONE;
    
192 193 194 195 196
    *txn_managerp = txn_manager;
}

void toku_txn_manager_destroy(TXN_MANAGER txn_manager) {
    toku_mutex_destroy(&txn_manager->txn_manager_lock);
197
    invariant(txn_manager->live_root_txns.size() == 0);
198
    txn_manager->live_root_txns.destroy();
199
    invariant(txn_manager->live_root_ids.size() == 0);
200
    txn_manager->live_root_ids.destroy();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
201
    invariant(txn_manager->snapshot_head == NULL);
202
    invariant(txn_manager->referenced_xids.size() == 0);
203
    txn_manager->referenced_xids.destroy();
204 205 206
    toku_free(txn_manager);
}

207 208
TXNID
toku_txn_manager_get_oldest_living_xid(TXN_MANAGER txn_manager) {
209
    TOKUTXN rtxn = NULL;
210
    TXNID rval = TXNID_NONE_LIVING;
211
    txn_manager_lock(txn_manager);
212

213
    if (txn_manager->live_root_txns.size() > 0) {
214
        int r = txn_manager->live_root_txns.fetch(0, &rtxn);
215 216
        invariant_zero(r);
    }
217 218 219
    if (rtxn) {
        rval = rtxn->txnid.parent_id64;
    }
220
    txn_manager_unlock(txn_manager);
221 222 223
    return rval;
}

224 225 226 227 228 229 230
int live_root_txn_list_iter(const TOKUTXN &live_xid, const uint32_t UU(index), TXNID **const referenced_xids);
int live_root_txn_list_iter(const TOKUTXN &live_xid, const uint32_t UU(index), TXNID **const referenced_xids){
    (*referenced_xids)[index] = live_xid->txnid.parent_id64;
    return 0;
}


231
// Create list of root transactions that were live when this txn began.
232 233 234
static inline void
setup_live_root_txn_list(xid_omt_t* live_root_txnid, xid_omt_t* live_root_txn_list) {
    live_root_txn_list->clone(*live_root_txnid);
235 236
}

237
//Heaviside function to search through an OMT by a TXNID
238 239
int
find_by_xid (const TOKUTXN &txn, const TXNID &txnidfind) {
240 241
    if (txn->txnid.parent_id64 < txnidfind) return -1;
    if (txn->txnid.parent_id64 > txnidfind) return +1;
242 243 244
    return 0;
}

245
#if 0
246 247 248 249 250 251 252 253 254
static void
omt_insert_at_end_unless_recovery(OMT omt, int (*h)(OMTVALUE, void*extra), TOKUTXN txn, OMTVALUE v, bool for_recovery)
// Effect: insert v into omt that is sorted by xid gotten from txn.
// Rationale:
//   During recovery, we get txns in the order that they did their first
//   write operation, which is not necessarily monotonically increasing.
//   During normal operation, txns are created with strictly increasing
//   txnids, so we can always insert at the end.
{
255
    int r;
256 257 258 259 260 261 262
    uint32_t idx = toku_omt_size(omt);
    if (for_recovery) {
        r = toku_omt_find_zero(omt, h, (void *) txn->txnid64, NULL, &idx);
        invariant(r==DB_NOTFOUND);
    }
    r = toku_omt_insert_at(omt, v, idx);
    lazy_assert_zero(r);
263
}
264
#endif
265

266 267 268 269 270
static TXNID
max_xid(TXNID a, TXNID b) {
    return a < b ? b : a;
}

Zardosht Kasheff's avatar
Zardosht Kasheff committed
271
static TXNID get_oldest_referenced_xid_unlocked(TXN_MANAGER txn_manager) {
272 273 274 275 276 277 278 279 280
    TXNID oldest_referenced_xid = TXNID_MAX;
    int r;
    if (txn_manager->live_root_ids.size() > 0) {
        r = txn_manager->live_root_ids.fetch(0, &oldest_referenced_xid);
        // this function should only be called when we know there is at least
        // one live transaction
        invariant_zero(r);
    }
    
Zardosht Kasheff's avatar
Zardosht Kasheff committed
281
    if (txn_manager->referenced_xids.size() > 0) {
282
        struct referenced_xid_tuple* tuple;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
283 284 285 286 287
        r = txn_manager->referenced_xids.fetch(0, &tuple);
        if (r == 0 && tuple->begin_id < oldest_referenced_xid) {
            oldest_referenced_xid = tuple->begin_id;
        }
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
288 289 290
    if (txn_manager->snapshot_head != NULL) {
        TXNID id = txn_manager->snapshot_head->snapshot_txnid64;
        if (id < oldest_referenced_xid) {
291 292 293 294 295 296 297
            oldest_referenced_xid = id;
        }
    }
    if (txn_manager->last_xid < oldest_referenced_xid) {
        oldest_referenced_xid = txn_manager->last_xid;
    }
    paranoid_invariant(oldest_referenced_xid != TXNID_MAX);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
298 299 300
    return oldest_referenced_xid;
}

301 302 303 304 305
//Heaviside function to find a TOKUTXN by TOKUTXN (used to find the index)
// template-only function, but must be extern
int find_xid (const TOKUTXN &txn, const TOKUTXN &txnfind);
int
find_xid (const TOKUTXN &txn, const TOKUTXN &txnfind)
306
{
307 308
    if (txn->txnid.parent_id64 < txnfind->txnid.parent_id64) return -1;
    if (txn->txnid.parent_id64 > txnfind->txnid.parent_id64) return +1;
309
    return 0;
310 311
}

312 313 314 315 316 317 318 319 320 321
static inline void txn_manager_create_snapshot_unlocked(
    TXN_MANAGER txn_manager,
    TOKUTXN txn
    ) 
{    
    txn->snapshot_txnid64 = ++txn_manager->last_xid;    
    setup_live_root_txn_list(&txn_manager->live_root_ids, txn->live_root_txn_list);  
    // Add this txn to the global list of txns that have their own snapshots.
    // (Note, if a txn is a child that creates its own snapshot, then that child xid
    // is the xid stored in the global list.) 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
322 323 324 325 326 327 328 329 330 331 332
    if (txn_manager->snapshot_head == NULL) {
        invariant(txn_manager->snapshot_tail == NULL);
        txn_manager->snapshot_head = txn;
        txn_manager->snapshot_tail = txn;
    }
    else {
        txn_manager->snapshot_tail->snapshot_next = txn;
        txn->snapshot_prev = txn_manager->snapshot_tail;
        txn_manager->snapshot_tail = txn;
    }
    txn_manager->num_snapshots++;
333 334
}

335 336 337 338 339 340 341
// template-only function, but must be extern
int find_tuple_by_xid (const struct referenced_xid_tuple &tuple, const TXNID &xidfind);
int
find_tuple_by_xid (const struct referenced_xid_tuple &tuple, const TXNID &xidfind)
{
    if (tuple.begin_id < xidfind) return -1;
    if (tuple.begin_id > xidfind) return +1;
342 343
    return 0;
}
344

345
// template-only function, but must be extern
346 347 348
int referenced_xids_note_snapshot_txn_end_iter(const TXNID &live_xid, const uint32_t UU(index), rx_omt_t *const referenced_xids)
    __attribute__((nonnull(3)));
int referenced_xids_note_snapshot_txn_end_iter(const TXNID &live_xid, const uint32_t UU(index), rx_omt_t *const referenced_xids)
349
{
350 351 352
    int r;
    uint32_t idx;
    struct referenced_xid_tuple *tuple;
353

354
    r = referenced_xids->find_zero<TXNID, find_tuple_by_xid>(live_xid, &tuple, &idx);
355 356 357 358 359 360
    if (r == DB_NOTFOUND) {
        goto done;
    }
    invariant_zero(r);
    invariant(tuple->references > 0);
    if (--tuple->references == 0) {
361
        r = referenced_xids->delete_at(idx);
362 363 364 365
        lazy_assert_zero(r);
    }
done:
    return 0;
366 367 368
}

// When txn ends, update reverse live list.  To do that, examine each txn in this (closing) txn's live list.
369 370
static inline int
note_snapshot_txn_end_by_ref_xids(TXN_MANAGER mgr, const xid_omt_t &live_root_txn_list) {
371
    int r;
372
    r = live_root_txn_list.iterate<rx_omt_t, referenced_xids_note_snapshot_txn_end_iter>(&mgr->referenced_xids);
373
    invariant_zero(r);
374 375 376
    return r;
}

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
typedef struct snapshot_iter_extra {
    uint32_t* indexes_to_delete;
    uint32_t num_indexes;
    xid_omt_t* live_root_txn_list;
} SNAPSHOT_ITER_EXTRA;

// template-only function, but must be extern
int note_snapshot_txn_end_by_txn_live_list_iter(referenced_xid_tuple* tuple, const uint32_t index, SNAPSHOT_ITER_EXTRA *const sie)
    __attribute__((nonnull(3)));
int note_snapshot_txn_end_by_txn_live_list_iter(
    referenced_xid_tuple* tuple, 
    const uint32_t index, 
    SNAPSHOT_ITER_EXTRA *const sie
    )
{
    int r;
    uint32_t idx;
    TXNID txnid;
    r = sie->live_root_txn_list->find_zero<TXNID, toku_find_xid_by_xid>(tuple->begin_id, &txnid, &idx);
    if (r == DB_NOTFOUND) {
        goto done;
    }
    invariant_zero(r);
    invariant(txnid == tuple->begin_id);
    invariant(tuple->references > 0);
    if (--tuple->references == 0) {
        sie->indexes_to_delete[sie->num_indexes] = index;
        sie->num_indexes++;
    }
done:
    return 0;
}

static inline int
note_snapshot_txn_end_by_txn_live_list(TXN_MANAGER mgr, xid_omt_t* live_root_txn_list) {
    uint32_t size = mgr->referenced_xids.size();
    uint32_t indexes_to_delete[size];
    SNAPSHOT_ITER_EXTRA sie = { .indexes_to_delete = indexes_to_delete, .num_indexes = 0, .live_root_txn_list = live_root_txn_list};
    mgr->referenced_xids.iterate_ptr<SNAPSHOT_ITER_EXTRA, note_snapshot_txn_end_by_txn_live_list_iter>(&sie);
    for (uint32_t i = 0; i < sie.num_indexes; i++) {
        uint32_t curr_index = sie.indexes_to_delete[sie.num_indexes-i-1];
        mgr->referenced_xids.delete_at(curr_index);
    }
    return 0;
}

423 424
static inline void txn_manager_remove_snapshot_unlocked(
    TOKUTXN txn, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
425
    TXN_MANAGER txn_manager
426
    ) 
427
{
Zardosht Kasheff's avatar
Zardosht Kasheff committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441
    // Remove from linked list of snapshot txns
    if (txn_manager->snapshot_head == txn) {
        txn_manager->snapshot_head = txn->snapshot_next;
    }
    if (txn_manager->snapshot_tail == txn) {
        txn_manager->snapshot_tail = txn->snapshot_prev;
    }
    if (txn->snapshot_next) {
        txn->snapshot_next->snapshot_prev = txn->snapshot_prev;
    }
    if (txn->snapshot_prev) {
        txn->snapshot_prev->snapshot_next = txn->snapshot_next;
    }    
    txn_manager->num_snapshots--;
442 443 444 445 446 447 448 449 450 451
    uint32_t ref_xids_size = txn_manager->referenced_xids.size();
    uint32_t live_list_size = txn->live_root_txn_list->size();
    if (ref_xids_size > 0 && live_list_size > 0) {
        if (live_list_size > ref_xids_size && ref_xids_size < 2000) {
            note_snapshot_txn_end_by_txn_live_list(txn_manager, txn->live_root_txn_list);
        }
        else {
            note_snapshot_txn_end_by_ref_xids(txn_manager, *txn->live_root_txn_list);
        }
    }
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
static inline void inherit_snapshot_from_parent(TOKUTXN child) {
    if (child->parent) {
        child->snapshot_txnid64 = child->parent->snapshot_txnid64;
        child->live_root_txn_list = child->parent->live_root_txn_list;
    }
}
void toku_txn_manager_handle_snapshot_create_for_child_txn(
    TOKUTXN txn,
    TXN_MANAGER txn_manager,
    TXN_SNAPSHOT_TYPE snapshot_type
    ) 
{
    // this is a function for child txns, so just doint a sanity check
    invariant(txn->parent != NULL);
    bool needs_snapshot = txn_needs_snapshot(snapshot_type, txn->parent);
    if (needs_snapshot) {
        invariant(txn->live_root_txn_list == nullptr);
        XMALLOC(txn->live_root_txn_list);
        txn_manager_lock(txn_manager);
        txn_manager_create_snapshot_unlocked(txn_manager, txn);
        txn_manager_unlock(txn_manager);
    }
    else {
        inherit_snapshot_from_parent(txn);
    }
}

void toku_txn_manager_handle_snapshot_destroy_for_child_txn(
    TOKUTXN txn,
    TXN_MANAGER txn_manager,
    TXN_SNAPSHOT_TYPE snapshot_type
    )
{
    // this is a function for child txns, so just doint a sanity check
    invariant(txn->parent != NULL);
    bool is_snapshot = txn_needs_snapshot(snapshot_type, txn->parent);
    if (is_snapshot) {
        txn_manager_lock(txn_manager);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
492
        txn_manager_remove_snapshot_unlocked(txn, txn_manager);
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
        txn_manager_unlock(txn_manager);
        invariant(txn->live_root_txn_list != nullptr);
        txn->live_root_txn_list->destroy();
        toku_free(txn->live_root_txn_list);
    }
}

void toku_txn_manager_start_txn_for_recovery(
    TOKUTXN txn,
    TXN_MANAGER txn_manager,
    TXNID xid
    )
{
    txn_manager_lock(txn_manager);
    // using xid that is passed in
    txn_manager->last_xid = max_xid(txn_manager->last_xid, xid);
    toku_txn_update_xids_in_txn(txn, xid);
    txn->oldest_referenced_xid = TXNID_NONE;

    uint32_t idx;
    int r = txn_manager->live_root_txns.find_zero<TOKUTXN, find_xid>(txn, nullptr, &idx);
    invariant(r == DB_NOTFOUND);
    r = txn_manager->live_root_txns.insert_at(txn, idx);
    invariant_zero(r);
    r = txn_manager->live_root_ids.insert_at(txn->txnid.parent_id64, idx);
    invariant_zero(r);

    txn_manager_unlock(txn_manager);
}

void toku_txn_manager_start_txn(
    TOKUTXN txn,
    TXN_MANAGER txn_manager,
526 527
    TXN_SNAPSHOT_TYPE snapshot_type,
    bool read_only
528 529
    )
{
530
    int r;
531 532 533 534 535 536 537 538 539 540 541 542 543
    TXNID xid = TXNID_NONE;
    // if we are running in recovery, we don't need to make snapshots
    bool needs_snapshot = txn_needs_snapshot(snapshot_type, NULL);

    // perform a malloc outside of the txn_manager lock
    // will be used in txn_manager_create_snapshot_unlocked below
    if (needs_snapshot) {
        invariant(txn->live_root_txn_list == nullptr);
        XMALLOC(txn->live_root_txn_list);
    }
    // the act of getting a transaction ID and adding the
    // txn to the proper OMTs must be atomic. MVCC depends
    // on this.
544
    txn_manager_lock(txn_manager);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    if (garbage_collection_debug) {
        verify_snapshot_system(txn_manager);
    }

    //
    // maintain the data structures necessary for MVCC:
    //  1. add txn to list of live_root_txns if this is a root transaction
    //  2. if the transaction is creating a snapshot:
    //    - create a live list for the transaction
    //    - add the id to the list of snapshot ids
    //
    // The order of operations is important here, and must be taken
    // into account when the transaction is closed. The txn is added
    // to the live_root_txns first (if it is a root txn). This has the implication
    // that a root level snapshot transaction is in its own live list. This fact
    // is taken into account when the transaction is closed.

    // add ancestor information, and maintain global live root txn list
563
    xid = ++txn_manager->last_xid; // we always need an ID, needed for lock tree
564
    toku_txn_update_xids_in_txn(txn, xid);
565 566 567 568 569 570 571
    if (!read_only) {
        uint32_t idx = txn_manager->live_root_txns.size();
        r = txn_manager->live_root_txns.insert_at(txn, idx);
        invariant_zero(r);
        r = txn_manager->live_root_ids.insert_at(txn->txnid.parent_id64, idx);
        invariant_zero(r);
    }
572
    txn->oldest_referenced_xid = get_oldest_referenced_xid_unlocked(txn_manager);
573 574 575 576 577 578 579
    
    if (needs_snapshot) {
        txn_manager_create_snapshot_unlocked(
            txn_manager,
            txn
            );
    }
580 581 582 583

    if (garbage_collection_debug) {
        verify_snapshot_system(txn_manager);
    }
584
    txn_manager_unlock(txn_manager);
585
    return;
586 587 588 589 590 591 592 593 594 595 596
}

TXNID
toku_get_youngest_live_list_txnid_for(TXNID xc, const xid_omt_t &snapshot_txnids, const rx_omt_t &referenced_xids) {
    struct referenced_xid_tuple *tuple;
    int r;
    TXNID rval = TXNID_NONE;

    r = referenced_xids.find_zero<TXNID, find_tuple_by_xid>(xc, &tuple, nullptr);
    if (r == DB_NOTFOUND) {
        goto done;
597
    }
598
    TXNID live;
599

600 601 602 603 604 605 606 607 608 609 610
    r = snapshot_txnids.find<TXNID, toku_find_xid_by_xid>(tuple->end_id, -1, &live, nullptr);
    if (r == DB_NOTFOUND) {
        goto done;
    }
    invariant(live < tuple->end_id);
    if (live > tuple->begin_id) {
        rval = live;
    }
done:
    return rval;
}
611

612 613 614 615 616
void toku_txn_manager_finish_txn(TXN_MANAGER txn_manager, TOKUTXN txn) {
    int r;
    invariant(txn->parent == NULL);
    bool is_snapshot = txn_needs_snapshot(txn->snapshot_type, NULL);
    txn_manager_lock(txn_manager);
617

618 619
    if (garbage_collection_debug) {
        verify_snapshot_system(txn_manager);
620
    }
621

622 623 624
    if (is_snapshot) {
        txn_manager_remove_snapshot_unlocked(
            txn, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
625
            txn_manager
626 627
            );
    }
628

629 630 631 632 633 634 635
    if (!txn_declared_read_only(txn)) {
        uint32_t idx;
        //Remove txn from list of live root txns
        TOKUTXN txnagain;
        r = txn_manager->live_root_txns.find_zero<TOKUTXN, find_xid>(txn, &txnagain, &idx);
        invariant_zero(r);
        invariant(txn==txnagain);
636

637 638 639 640
        r = txn_manager->live_root_txns.delete_at(idx);
        invariant_zero(r);
        r = txn_manager->live_root_ids.delete_at(idx);
        invariant_zero(r);
641

642
        if (!toku_txn_is_read_only(txn) || garbage_collection_debug) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
643 644 645 646 647 648 649 650 651 652
            uint32_t num_references = 0;
            TOKUTXN curr_txn = txn_manager->snapshot_tail;
            while(curr_txn != NULL) {
                if (curr_txn->snapshot_txnid64 > txn->txnid.parent_id64) {
                    num_references++;
                }
                else {
                    break;
                }
                curr_txn = curr_txn->snapshot_prev;
653
            }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
654
            
655 656 657 658 659 660 661 662 663 664
            if (num_references > 0) {
                // This transaction exists in a live list of another transaction.
                struct referenced_xid_tuple tuple = {
                    .begin_id = txn->txnid.parent_id64,
                    .end_id = ++txn_manager->last_xid,
                    .references = num_references
                };
                r = txn_manager->referenced_xids.insert<TXNID, find_tuple_by_xid>(tuple, txn->txnid.parent_id64, nullptr);
                lazy_assert_zero(r);
            }
665
        }
666 667 668 669 670
    }

    if (garbage_collection_debug) {
        verify_snapshot_system(txn_manager);
    }
671
    txn_manager_unlock(txn_manager);
672 673 674

    //Cleanup that does not require the txn_manager lock
    if (is_snapshot) {
675
        invariant(txn->live_root_txn_list != nullptr);
676 677
        txn->live_root_txn_list->destroy();
        toku_free(txn->live_root_txn_list);
678
    }
679
    return;
680 681 682
}

void toku_txn_manager_clone_state_for_gc(
683
    TXN_MANAGER txn_manager,
684 685 686
    xid_omt_t* snapshot_xids,
    rx_omt_t* referenced_xids,
    xid_omt_t* live_root_txns
687
    )
688
{
689
    txn_manager_lock(txn_manager);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    TXNID* snapshot_xids_array = NULL;
    XMALLOC_N(txn_manager->num_snapshots, snapshot_xids_array);
    TOKUTXN curr_txn = txn_manager->snapshot_head;
    uint32_t curr_index = 0;
    while (curr_txn != NULL) {
        snapshot_xids_array[curr_index] = curr_txn->snapshot_txnid64;
        curr_txn = curr_txn->snapshot_next;
        curr_index++;
    }
    snapshot_xids->create_steal_sorted_array(
        &snapshot_xids_array, 
        txn_manager->num_snapshots,
        txn_manager->num_snapshots
        );
    
705 706
    referenced_xids->clone(txn_manager->referenced_xids);
    setup_live_root_txn_list(&txn_manager->live_root_ids, live_root_txns);  
707
    txn_manager_unlock(txn_manager);
708 709
}

710
void toku_txn_manager_id2txn_unlocked(TXN_MANAGER txn_manager, TXNID_PAIR txnid, TOKUTXN *result) {
711
    TOKUTXN txn;
712
    int r = txn_manager->live_root_txns.find_zero<TXNID, find_by_xid>(txnid.parent_id64, &txn, nullptr);
713
    if (r==0) {
714
        assert(txn->txnid.parent_id64 == txnid.parent_id64);
715 716 717 718 719 720 721 722 723
        *result = txn;
    }
    else {
        assert(r==DB_NOTFOUND);
        // If there is no txn, then we treat it as the null txn.
        *result = NULL;
    }
}

724
int toku_txn_manager_get_root_txn_from_xid (TXN_MANAGER txn_manager, TOKU_XA_XID *xid, DB_TXN **txnp) {
725
    txn_manager_lock(txn_manager);
726
    int ret_val = 0;
727
    int num_live_txns = txn_manager->live_root_txns.size();
728
    for (int i = 0; i < num_live_txns; i++) {
729
        TOKUTXN txn;
730
        {
731
            int r = txn_manager->live_root_txns.fetch(i, &txn);
732 733 734 735 736 737 738 739 740 741 742 743 744
            assert_zero(r);
        }
        if (txn->xa_xid.formatID     == xid->formatID
            && txn->xa_xid.gtrid_length == xid->gtrid_length
            && txn->xa_xid.bqual_length == xid->bqual_length
            && 0==memcmp(txn->xa_xid.data, xid->data, xid->gtrid_length + xid->bqual_length)) {
            *txnp = txn->container_db_txn;
            ret_val = 0;
            goto exit;
        }
    }
    ret_val = DB_NOTFOUND;
exit:
745
    txn_manager_unlock(txn_manager);
746 747 748
    return ret_val;
}

749
uint32_t toku_txn_manager_num_live_root_txns(TXN_MANAGER txn_manager) {
750
    int ret_val = 0;
751
    txn_manager_lock(txn_manager);
752
    ret_val = txn_manager->live_root_txns.size();
753
    txn_manager_unlock(txn_manager);
754 755 756
    return ret_val;
}

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
static int txn_manager_iter(
    TXN_MANAGER txn_manager, 
    txn_mgr_iter_callback cb,
    void* extra,
    bool just_root_txns
    ) 
{
    int r = 0;
    toku_mutex_lock(&txn_manager->txn_manager_lock);
    uint32_t size = txn_manager->live_root_txns.size();
    for (uint32_t i = 0; i < size; i++) {
        TOKUTXN curr_txn = NULL;
        r = txn_manager->live_root_txns.fetch(i, &curr_txn);
        assert_zero(r);
        if (just_root_txns) {
            r = cb(curr_txn, extra);
        }
        else {
            r = curr_txn->child_manager->iterate(cb, extra);
        }
        if (r) {
            break;
        }
    }
    toku_mutex_unlock(&txn_manager->txn_manager_lock);
    return r;
783 784
}

785 786 787 788 789 790 791 792 793 794 795 796
int toku_txn_manager_iter_over_live_txns(
    TXN_MANAGER txn_manager, 
    txn_mgr_iter_callback cb,
    void* extra
    ) 
{
    return txn_manager_iter(
        txn_manager,
        cb,
        extra,
        false
        );
797 798
}

799 800 801 802 803 804 805 806 807 808 809 810
int toku_txn_manager_iter_over_live_root_txns(
    TXN_MANAGER txn_manager, 
    txn_mgr_iter_callback cb,
    void* extra
    )
{
    return txn_manager_iter(
        txn_manager,
        cb,
        extra,
        true
        );
811 812
}

813

814 815 816 817 818 819 820
//
// This function is called only via env_txn_xa_recover and env_txn_recover.
// See comments for those functions to understand assumptions that 
// can be made when calling this function. Namely, that the system is 
// quiescant, in that we are right after recovery and before user operations
// commence.
//
821 822 823 824 825
// Another key assumption made here is that only root transactions
// may be prepared and that child transactions cannot be prepared.
// This assumption is made by the fact that we iterate over the live root txns
// to find prepared transactions.
//
826 827 828 829
// I (Zardosht), don't think we take advantage of this fact, as we are holding
// the txn_manager_lock in this function, but in the future we might want
// to take these assumptions into account.
//
830
int toku_txn_manager_recover_root_txn (
831 832 833 834
    TXN_MANAGER txn_manager, 
    struct tokulogger_preplist preplist[/*count*/], 
    long count, 
    long *retp, /*out*/ 
Yoni Fogel's avatar
Yoni Fogel committed
835
    uint32_t flags
836 837 838
    )
{
    int ret_val = 0;
839
    txn_manager_lock(txn_manager);
840 841 842 843
    uint32_t num_txns_returned = 0;
    // scan through live root txns to find
    // prepared transactions and return them
    uint32_t size = txn_manager->live_root_txns.size();
844
    if (flags==DB_FIRST) {
845 846 847
        txn_manager->last_xid_seen_for_recover = TXNID_NONE;
    } 
    else if (flags!=DB_NEXT) { 
848 849 850
        ret_val = EINVAL;
        goto exit;
    }
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    for (uint32_t i = 0; i < size; i++) {
        TOKUTXN curr_txn = NULL;
        txn_manager->live_root_txns.fetch(i, &curr_txn);
        // skip over TOKUTXNs whose txnid64 is too small, meaning
        // we have already processed them.
        if (curr_txn->txnid.parent_id64 <= txn_manager->last_xid_seen_for_recover) {
            continue;
        }
        if (curr_txn->state == TOKUTXN_PREPARING) {
            assert(curr_txn->container_db_txn);
            preplist[num_txns_returned].txn = curr_txn->container_db_txn;
            preplist[num_txns_returned].xid = curr_txn->xa_xid;
            txn_manager->last_xid_seen_for_recover = curr_txn->txnid.parent_id64;
            num_txns_returned++;
        }
        txn_manager->last_xid_seen_for_recover = curr_txn->txnid.parent_id64;
        // if we found the maximum number of prepared transactions we are
        // allowed to find, then break
        if (num_txns_returned >= count) {
870 871 872
            break;
        }
    }
873 874
    invariant(num_txns_returned <= count);
    *retp = num_txns_returned;
875 876
    ret_val = 0;
exit:
877
    txn_manager_unlock(txn_manager);
878 879 880
    return ret_val;
}

881
static void txn_manager_lock(TXN_MANAGER txn_manager) {
882
    toku_mutex_lock(&txn_manager->txn_manager_lock);
883
}
884 885

static void txn_manager_unlock(TXN_MANAGER txn_manager) {
886 887 888
    toku_mutex_unlock(&txn_manager->txn_manager_lock);
}

889 890 891 892 893 894 895 896
void toku_txn_manager_suspend(TXN_MANAGER txn_manager) {
    txn_manager_lock(txn_manager);
}

void toku_txn_manager_resume(TXN_MANAGER txn_manager) {
    txn_manager_unlock(txn_manager);
}

897
void
898
toku_txn_manager_set_last_xid_from_logger(TXN_MANAGER txn_manager, TXNID last_xid) {
899
    invariant(txn_manager->last_xid == TXNID_NONE);
900
    txn_manager->last_xid = last_xid;
901 902 903 904 905 906 907 908 909
}

void
toku_txn_manager_set_last_xid_from_recovered_checkpoint(TXN_MANAGER txn_manager, TXNID last_xid) {
    txn_manager->last_xid = last_xid;
}

TXNID
toku_txn_manager_get_last_xid(TXN_MANAGER mgr) {
910
    txn_manager_lock(mgr);
911
    TXNID last_xid = mgr->last_xid;
912
    txn_manager_unlock(mgr);
913 914 915
    return last_xid;
}

916 917 918 919 920 921 922 923 924
bool 
toku_txn_manager_txns_exist(TXN_MANAGER mgr) {
    txn_manager_lock(mgr);
    bool retval = mgr->live_root_txns.size() > 0;
    txn_manager_unlock(mgr);
    return retval;
}


925 926 927
// Test-only function
void
toku_txn_manager_increase_last_xid(TXN_MANAGER mgr, uint64_t increment) {
928
    txn_manager_lock(mgr);
929
    mgr->last_xid += increment;
930
    txn_manager_unlock(mgr);
931
}
932