From: jdybnis Date: Sun, 16 Nov 2008 04:07:14 +0000 (+0000) Subject: add beginings of transactional ht X-Git-Url: https://pd.if.org/git/?p=nbds;a=commitdiff_plain;h=d61d7ffdfa8fab555a5086e61d2e9dfe699a14b9 add beginings of transactional ht fix regression in ht causing a test to fail optimize lwt improve tracing in ht --- diff --git a/include/common.h b/include/common.h index 4774628..5bc2a6f 100644 --- a/include/common.h +++ b/include/common.h @@ -35,7 +35,7 @@ #define MASK(n) ((1LL << (n)) - 1) #define TAG (1LL << 63) -#define IS_TAGGED(v) ((uint64_t)(v) < 0) +#define IS_TAGGED(v) ((int64_t)(v) < 0) #define TAG_VALUE(v) ((uint64_t)(v) | TAG) #define STRIP_TAG(v) ((uint64_t)(v) & ~TAG) diff --git a/include/lwt.h b/include/lwt.h index db86292..9f07e7a 100644 --- a/include/lwt.h +++ b/include/lwt.h @@ -28,8 +28,8 @@ void lwt_set_trace_level (const char *flags); // the dump. It is only included when its specified category is enabled at a trace level greater than or equal to // the one in . Categories are case sensitive. static inline void lwt_trace (const char *flag, const char *format, size_t value1, size_t value2) { - extern uint64_t flag_mask_; - if (EXPECT_FALSE(flag_mask_ & (1 << (flag[0] - 'A')))) { + extern char flag_state_[256]; + if (EXPECT_FALSE(flag_state_[(unsigned)flag[0]] >= flag[1])) { // embed in so we don't have to make the lwt_record_t any bigger than it already is format = (const char *)((size_t)format | ((uint64_t)flag[0] << 56) | ((uint64_t)flag[1] << 48)); extern void lwt_trace_i (const char *format, size_t value1, size_t value2); diff --git a/runtime/lwt.c b/runtime/lwt.c index be83e5f..8212c5c 100644 --- a/runtime/lwt.c +++ b/runtime/lwt.c @@ -29,7 +29,7 @@ typedef struct lwt_buffer { } lwt_buffer_t; lwt_buffer_t *lwt_buf_[MAX_NUM_THREADS] = {}; -uint64_t flag_mask_ = 0; +char flag_state_[256] = {}; static const char *flags_ = ""; void lwt_thread_init (int thread_id) @@ -45,9 +45,9 @@ void lwt_set_trace_level (const char *flags) { assert(strlen(flags) % 2 == 0); // a well formed should be an even number of characters long flags_ = flags; - int i; - for (i = 0; flags[i]; i+=2) { - flag_mask_ |= 1 << (flags[i] - 'A'); + memset(flag_state_, 0, sizeof(flag_state_)); + for (int i = 0; flags[i]; i+=2) { + flag_state_[(unsigned)flags[i]] = flags[i+1]; } } @@ -56,8 +56,7 @@ static inline void dump_record (FILE *file, int thread_id, lwt_record_t *r, uint // print the record if its trace category is enabled at a high enough level int flag = (size_t)r->format >> 56; int level = ((size_t)r->format >> 48) & 0xFF; - const char *f = strchr(flags_, flag); - if (f != NULL && level <= f[1]) { + if (flag_state_[(unsigned)flag] >= level) { char s[3] = {flag, level, '\0'}; fprintf(file, "%09llu %d %s ", ((uint64_t)r->timestamp - offset) >> 6, thread_id, s); const char *format = (const char *)((size_t)r->format & MASK(48)); // strip out the embedded flags diff --git a/struct/ht.c b/struct/ht.c index 7044435..f444af0 100644 --- a/struct/ht.c +++ b/struct/ht.c @@ -17,14 +17,6 @@ #include "murmur.h" #include "mem.h" -#define COPIED_VALUE (-1) -#define TOMBSTONE STRIP_TAG(COPIED_VALUE) - -#define ENTRIES_PER_BUCKET (CACHE_LINE_SIZE/sizeof(entry_t)) -#define ENTRIES_PER_COPY_CHUNK (ENTRIES_PER_BUCKET * 2) -#define MIN_SCALE (__builtin_ctz(ENTRIES_PER_BUCKET) + 2) // min 4 buckets -#define MAX_BUCKETS_TO_PROBE 250 - #define GET_PTR(x) ((string_t *)((x) & MASK(48))) // low-order 48 bits is a pointer to a string_t typedef struct ht_entry { @@ -49,6 +41,14 @@ typedef struct hash_table_i { int scan; } hash_table_i_t; +static const uint64_t COPIED_VALUE = -1; +static const uint64_t TOMBSTONE = STRIP_TAG(-1); + +static const unsigned ENTRIES_PER_BUCKET = CACHE_LINE_SIZE/sizeof(entry_t); +static const unsigned ENTRIES_PER_COPY_CHUNK = CACHE_LINE_SIZE/sizeof(entry_t)*2; +static const unsigned MIN_SCALE = 4; // min 16 entries (4 buckets) +static const unsigned MAX_BUCKETS_TO_PROBE = 250; + static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *e, uint32_t e_key_hash, hash_table_i_t *ht2); @@ -81,7 +81,7 @@ static inline int ht_key_equals (uint64_t a, uint32_t b_hash, const char *b_valu // Record if the entry being returned is empty. Otherwise the caller will have to waste time with // ht_key_equals() to confirm that it did not lose a race to fill an empty entry. static volatile entry_t *hti_lookup (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, uint32_t key_len, int *is_empty) { - TRACE("h0", "hti_lookup(key %p in hti %p)", key_val, hti); + TRACE("h2", "hti_lookup(key %p in hti %p)", key_val, hti); *is_empty = 0; // Probe one cache line at a time @@ -97,15 +97,14 @@ static volatile entry_t *hti_lookup (hash_table_i_t *hti, uint32_t key_hash, con uint64_t e_key = e->key; if (e_key == DOES_NOT_EXIST) { - TRACE("h0", "hti_lookup: empty entry %p found", e, 0); + TRACE("h1", "hti_lookup: entry %p for key \"%s\" is empty", e, GET_PTR(e_key)->val); *is_empty = 1; // indicate an empty so the caller avoids an expensive ht_key_equals return e; } if (ht_key_equals(e_key, key_hash, key_val, key_len)) { - TRACE("h0", "hti_lookup: entry %p found value %p", e, e->value); - TRACE("h0", "hti_lookup: entry key %p len %llu", GET_PTR(e_key)->val, - GET_PTR(e_key)->len); + TRACE("h1", "hti_lookup: entry %p key \"%s\"", e, GET_PTR(e_key)->val); + TRACE("h2", "hti_lookup: entry key len %llu, value %p", GET_PTR(e_key)->len, e->value); return e; } } @@ -114,7 +113,7 @@ static volatile entry_t *hti_lookup (hash_table_i_t *hti, uint32_t key_hash, con } // maximum number of probes exceeded - TRACE("h0", "hti_lookup: maximum number of probes exceeded returning 0x0", 0, 0); + TRACE("h1", "hti_lookup: maximum number of probes exceeded returning 0x0", 0, 0); return NULL; } @@ -161,15 +160,14 @@ static void hti_start_copy (hash_table_i_t *hti) { // Allocate the new table and attempt to install it. hash_table_i_t *next = hti_alloc(hti->ht, new_scale); - TRACE("h0", "hti_start_copy: new hti %p scale %llu", next->scale, next->scale); hash_table_i_t *old_next = SYNC_CAS(&hti->next, NULL, next); if (old_next != NULL) { - TRACE("h0", "hti_start_copy: lost race to install new hti; found %p", old_next, 0); // Another thread beat us to it. + TRACE("h0", "hti_start_copy: lost race to install new hti; found %p", old_next, 0); nbd_free(next); return; } - TRACE("h0", "hti_start_copy: new hti is %p", next, 0); + TRACE("h0", "hti_start_copy: new hti %p scale %llu", next, next->scale); } // Copy the key and value stored in (which must be an entry in ) to . @@ -178,7 +176,7 @@ static void hti_start_copy (hash_table_i_t *hti) { // number of entries left to copy. static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_t key_hash, hash_table_i_t *ht2) { - TRACE("h0", "hti_copy_entry(copy entry from %p to %p)", ht1, ht2); + TRACE("h2", "hti_copy_entry: entry %p to table %p", ht1_e, ht2); assert(ht1); assert(ht1->next); assert(ht2); @@ -186,35 +184,40 @@ static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_ assert(key_hash == 0 || (key_hash >> 16) == (ht1_e->key >> 48)); uint64_t ht1_e_value = ht1_e->value; - TRACE("h0", "hti_copy_entry: entry %p current value %p", ht1_e, ht1_e_value); - if (EXPECT_FALSE(ht1_e_value == COPIED_VALUE)) + if (EXPECT_FALSE(ht1_e_value == COPIED_VALUE)) { + TRACE("h1", "hti_copy_entry: entry %p already copied to table %p", ht1_e, ht2); return FALSE; // already copied + } // Kill empty entries. if (EXPECT_FALSE(ht1_e_value == DOES_NOT_EXIST)) { uint64_t ht1_e_value = SYNC_CAS(&ht1_e->value, DOES_NOT_EXIST, COPIED_VALUE); if (ht1_e_value == DOES_NOT_EXIST) { - TRACE("h0", "hti_copy_entry: old entry killed", 0, 0); + TRACE("h1", "hti_copy_entry: empty entry %p killed", ht1_e, 0); return TRUE; } if (ht1_e_value == COPIED_VALUE) { - TRACE("h0", "hti_copy_entry: lost race to kill empty entry in old hti", 0, 0); + TRACE("h0", "hti_copy_entry: lost race to kill empty entry %p", ht1_e, 0); return FALSE; // another thread beat us to it } - TRACE("h0", "hti_copy_entry: lost race to kill empty entry in old hti; " - "the entry is now being used", 0, 0); + TRACE("h0", "hti_copy_entry: lost race to kill empty entry %p; the entry is now" + "in use and should be copied", ht1_e, 0); } // Tag the value in the old entry to indicate a copy is in progress. ht1_e_value = SYNC_FETCH_AND_OR(&ht1_e->value, TAG_VALUE(0)); - TRACE("h0", "hti_copy_entry: tagged the value %p in old entry %p", ht1_e_value, ht1_e); - if (ht1_e_value == COPIED_VALUE) + TRACE("h2", "hti_copy_entry: tagged the value %p in old entry %p", ht1_e_value, ht1_e); + if (ht1_e_value == COPIED_VALUE) { + TRACE("h1", "hti_copy_entry: entry %p already copied to table %p", ht1_e, ht2); return FALSE; // was already copied by another thread. + } - // Deleted entries don't need to be installed into to the new table, but their keys do need to - // be freed. + // The old table's deleted entries don't need to be copied to the new table, but their keys need + // to be freed. assert(COPIED_VALUE == TAG_VALUE(TOMBSTONE)); if (ht1_e_value == TOMBSTONE) { + TRACE("h1", "hti_copy_entry: entry %p old value was deleted, now freeing key %p", ht1_e, + GET_PTR(ht1_e->key)); nbd_defer_free(GET_PTR(ht1_e->key)); return TRUE; } @@ -223,7 +226,6 @@ static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_ uint64_t key = ht1_e->key; string_t *key_string = GET_PTR(key); uint64_t value = STRIP_TAG(ht1_e_value); - TRACE("h0", "hti_copy_entry: key %p value %p", key, value); // We use 0 to indicate that isn't initiallized. Occasionally the will // really be 0 and we will waste time recomputing it. That is rare enough that it is OK. @@ -233,9 +235,11 @@ static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_ int is_empty; volatile entry_t *ht2_e = hti_lookup(ht2, key_hash, key_string->val, key_string->len, &is_empty); + TRACE("h0", "hti_copy_entry: copy entry %p to entry %p", ht1_e, ht2_e); // it is possible that there is not any room in the new table either if (EXPECT_FALSE(ht2_e == NULL)) { + TRACE("h0", "hti_copy_entry: no room in table %p copy to next table %p", ht2, ht2->next); if (ht2->next == NULL) { hti_start_copy(ht2); // initiate nested copy, if not already started } @@ -251,28 +255,28 @@ static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_ return hti_copy_entry(ht1, ht1_e, key_hash, ht2); // recursive tail-call } } - assert(ht_key_equals(ht2_e->key, key_hash, key_string->val, key_string->len)); - TRACE("h0", "hti_copy_entry: key %p installed in new hti %p", key_string->val, ht2); // Copy the value to the entry in the new table. uint64_t old_ht2_e_value = SYNC_CAS(&ht2_e->value, DOES_NOT_EXIST, value); // If there is a nested copy in progress, we might have installed the key into a dead entry. - if (old_ht2_e_value == COPIED_VALUE) + if (old_ht2_e_value == COPIED_VALUE) { + TRACE("h0", "hti_copy_entry: nested copy in progress; copy %p to next table %p", ht2_e, ht2->next); return hti_copy_entry(ht1, ht1_e, key_hash, ht2->next); // recursive tail-call + } // Mark the old entry as dead. ht1_e->value = COPIED_VALUE; // Update the count if we were the one that completed the copy. if (old_ht2_e_value == DOES_NOT_EXIST) { - TRACE("h0", "hti_copy_entry: value %p installed in new hti %p", value, ht2); + TRACE("h0", "hti_copy_entry: key \"%s\" value %p copied to new entry", key_string->val, value); SYNC_ADD(&ht1->count, -1); SYNC_ADD(&ht2->count, 1); return TRUE; } - TRACE("h0", "hti_copy_entry: lost race to CAS value %p in new hti; found %p", + TRACE("h0", "hti_copy_entry: lost race to install value %p in new entry; found value %p", value, old_ht2_e_value); return FALSE; // another thread completed the copy } @@ -293,8 +297,8 @@ static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_ // static uint64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, uint32_t key_len, uint64_t expected, uint64_t new) { - TRACE("h0", "hti_compare_and_set: hti %p key %p", hti, key_val); - TRACE("h0", "hti_compare_and_set: new value %p expected old value %p", new, expected); + TRACE("h1", "hti_compare_and_set: hti %p key %p", hti, key_val); + TRACE("h1", "hti_compare_and_set: value %p expect %p", new, expected); assert(hti); assert(new != DOES_NOT_EXIST && !IS_TAGGED(new)); assert(key_val); @@ -336,17 +340,23 @@ static uint64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, con nbd_free(key); return hti_compare_and_set(hti, key_hash, key_val, key_len, expected, new); // tail-call } - TRACE("h0", "hti_compare_and_set: installed key %p in entry %p", key, e); + TRACE("h2", "hti_compare_and_set: installed key %p in entry %p", key, e); } + TRACE("h0", "hti_compare_and_set: entry for key \"%s\" is %p", GET_PTR(e->key)->val, e); + // If the entry is in the middle of a copy, the copy must be completed first. uint64_t e_value = e->value; - TRACE("h0", "hti_compare_and_set: value in entry %p is %p", e, e_value); if (EXPECT_FALSE(IS_TAGGED(e_value))) { - int did_copy = hti_copy_entry(hti, e, key_hash, ((volatile hash_table_i_t *)hti)->next); - if (did_copy) { - SYNC_ADD(&hti->num_entries_copied, 1); + if (e_value != COPIED_VALUE) { + int did_copy = hti_copy_entry(hti, e, key_hash, ((volatile hash_table_i_t *)hti)->next); + if (did_copy) { + SYNC_ADD(&hti->num_entries_copied, 1); + } + TRACE("h0", "hti_compare_and_set: value in the middle of a copy, copy completed by %s", + (did_copy ? "self" : "other"), 0); } + TRACE("h0", "hti_compare_and_set: value copied to next table, retry on next table", 0, 0); return COPIED_VALUE; } @@ -354,12 +364,18 @@ static uint64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, con int old_existed = (e_value != TOMBSTONE && e_value != DOES_NOT_EXIST); if (EXPECT_FALSE(expected != HT_EXPECT_WHATEVER && expected != e_value)) { if (EXPECT_FALSE(expected != (old_existed ? HT_EXPECT_EXISTS : HT_EXPECT_NOT_EXISTS))) { - TRACE("h0", "hti_compare_and_set: value %p expected by caller not found; found value %p", + TRACE("h1", "hti_compare_and_set: value %p expected by caller not found; found value %p", expected, e_value); return e_value; } } + // No need to update if value is unchanged. + if ((new == TOMBSTONE && !old_existed) || e_value == new) { + TRACE("h1", "hti_compare_and_set: old value and new value were the same", 0, 0); + return e_value; + } + // CAS the value into the entry. Retry if it fails. uint64_t v = SYNC_CAS(&e->value, e_value, new); if (EXPECT_FALSE(v != e_value)) { @@ -422,8 +438,8 @@ uint64_t ht_get (hash_table_t *ht, const char *key_val, uint32_t key_len) { uint64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key_len, uint64_t expected_val, uint64_t new_val) { - TRACE("h0", "ht_compare_and_set: key %p len %u", key_val, key_len); - TRACE("h0", "ht_compare_and_set: expected val %p new val %p", expected_val, new_val); + TRACE("h2", "ht_compare_and_set: key %p len %u", key_val, key_len); + TRACE("h2", "ht_compare_and_set: expected val %p new val %p", expected_val, new_val); assert(key_val); assert(!IS_TAGGED(new_val) && new_val != DOES_NOT_EXIST); @@ -436,7 +452,7 @@ uint64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key int num_copied = 0; int x = hti->scan; - TRACE("h0", "ht_compare_and_set: help copy. scan is %llu, size is %llu", x, 1<scale); + TRACE("h1", "ht_compare_and_set: help copy. scan is %llu, size is %llu", x, 1<scale); // Panic if we've been around the array twice and still haven't finished the copy. int panic = (x >= (1 << (hti->scale + 1))); if (!panic) { @@ -451,7 +467,7 @@ uint64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key // the table. e = hti->table + (x & MASK(hti->scale)); } else { - TRACE("h0", "ht_compare_and_set: help copy panic", 0, 0); + TRACE("h1", "ht_compare_and_set: help copy panic", 0, 0); // scan the whole table limit = (1 << hti->scale); e = hti->table; diff --git a/test/ht_test.c b/test/ht_test.c index e1afc34..eda9c51 100644 --- a/test/ht_test.c +++ b/test/ht_test.c @@ -161,7 +161,7 @@ void concurrent_insert (CuTest* tc) { int main (void) { nbd_init(); - lwt_set_trace_level("h4t9"); + //lwt_set_trace_level("h0"); // Create and run test suite CuString *output = CuStringNew(); diff --git a/todo b/todo index ab8c0db..e8684e1 100644 --- a/todo +++ b/todo @@ -2,4 +2,4 @@ - fix makefile to compute dependency info as a side-effect of compilation (-MF) - investigate 16 byte CAS; ht can store GUIDs inline instead of pointers to actual keys - test ht -- optimize tracing code, still too much overhead ++ optimize tracing code, still too much overhead diff --git a/txn/txn.c b/txn/txn.c new file mode 100644 index 0000000..013388e --- /dev/null +++ b/txn/txn.c @@ -0,0 +1,250 @@ +/* + * Written by Josh Dybnis and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + */ +#include "common.h" +#include "txn.h" +#include "mem.h" + +#define UNDETERMINED_VERSION 0 +#define INITIAL_WRITES_SIZE 4 + +typedef enum { TXN_RUNNING, TXN_VALIDATING, TXN_VALIDATED, TXN_ABORTED } txn_state_t; +typedef enum { UPDATE_TYPE_PUT, UPDATE_TYPE_DELETE } update_type_t; + +typedef struct update_rec update_rec_t; + +struct update_rec { + update_type_t type; + uint64_t value; + uint64_t version; + update_rec_t *prev; // a previous update +}; + +typedef struct write_rec { + const char *key; + update_rec_t *rec; +} write_rec_t; + +struct txn { + uint64_t rv; + uint64_t wv; + hash_table_t *ht; + write_rec_t *writes; + uint32_t writes_size; + uint32_t writes_count; + uint32_t writes_scan; + txn_access_t access; + txn_isolation_t isolation; + txn_state_t state; +}; + +uint64_t GlobalVersion = 1; +uint64_t MinActiveTxnVersion = 0; + +static txn_state_t txn_validate (txn_t *txn); + +update_rec_t *alloc_update_rec (void) { + update_rec_t *u = (update_rec_t *)nbd_malloc(sizeof(update_rec_t)); + memset(u, 0, sizeof(update_rec_t)); + return u; +} + +txn_t *txn_begin (txn_access_t access, txn_isolation_t isolation, hash_table_t *ht) { + txn_t *txn = (txn_t *)nbd_malloc(sizeof(txn_t)); + memset(txn, 0, sizeof(txn_t)); + txn->access = access; + txn->isolation = isolation; + txn->rv = GlobalVersion; + txn->wv = UNDETERMINED_VERSION; + txn->state = TXN_RUNNING; + txn->ht = ht; + if (isolation != TXN_READ_ONLY) { + txn->writes = nbd_malloc(sizeof(*txn->writes) * INITIAL_WRITES_SIZE); + txn->writes_size = INITIAL_WRITES_SIZE; + } + return txn; +} + +// Get most recent committed version prior to our read version. +int64_t txn_ht_get (txn_t *txn, const char *key, uint32_t key_len) { + + // Iterate through update records associated with to find the latest committed version. + // We can use the first matching version. Older updates always come later in the list. + update_rec_t *update = (update_rec_t *) ht_get(txn->ht, key, key_len); + for (; update != NULL; update = update->prev) { + uint64_t writer_version = update->version; + if (writer_version < txn->rv) + return update->value; + + // If the version is tagged, it means that it is not a version number, but a pointer to an + // in progress transaction. + if (IS_TAGGED(update->version)) { + txn_t *writer = (txn_t *)STRIP_TAG(writer_version); + + if (writer == txn) + return update->type == UPDATE_TYPE_DELETE ? DOES_NOT_EXIST : update->value; + + // Skip updates from aborted transactions. + txn_state_t writer_state = writer->state; + if (EXPECT_FALSE(writer_state == TXN_ABORTED)) + continue; + + if (writer_state == TXN_VALIDATING) { + writer_state = txn_validate(writer); + } + + if (writer_state == TXN_VALIDATED && writer->wv <= txn->rv && writer->wv != UNDETERMINED_VERSION) + return update->type == UPDATE_TYPE_DELETE ? DOES_NOT_EXIST : update->value; + } + } + return DOES_NOT_EXIST; +} + +// Validate the updates for . Validation fails for a key we have written to if there is a +// write committed newer than our read version. +static txn_state_t txn_ht_validate_key (txn_t *txn, const char *key, uint32_t key_len) { + + update_rec_t *update = (update_rec_t *) ht_get(txn->ht, key, key_len); + for (; update != NULL; update = update->prev) { + uint64_t writer_version = update->version; + if (writer_version <= txn->rv) + return TXN_VALIDATED; + + // If the version is tagged, it means it is a pointer to a transaction in progress. + if (IS_TAGGED(writer_version)) { + + // Skip aborted transactions. + if (EXPECT_FALSE(writer_version == TAG_VALUE(0))) + continue; + + // Skip our own updates. + txn_t *writer = (txn_t *)STRIP_TAG(writer_version); + if (writer == txn) + continue; + + writer_version = writer->wv; + if (writer_version <= txn->rv && writer_version != UNDETERMINED_VERSION) + return TXN_VALIDATED; + + txn_state_t writer_state = writer->state; + if (EXPECT_FALSE(writer_state == TXN_ABORTED)) + continue; + + // Help validate if it is a committing transaction that might cause us to + // abort. However, if the has a later version than us we can safely ignore its + // updates. This protocol ensures a deterministic resolution to every conflict, and + // avoids infinite ping-ponging between validating two conflicting transactions. + if (writer_state == TXN_VALIDATING && (writer_version < txn->wv || + writer_version == UNDETERMINED_VERSION)) { + writer_state = txn_validate(writer); + } + + if (writer_state == TXN_VALIDATED) + return TXN_ABORTED; + } + + return TXN_ABORTED; + } + + return TXN_VALIDATED; +} + +static txn_state_t txn_validate (txn_t *txn) { + int i; + switch (txn->state) { + + case TXN_VALIDATING: + if (txn->wv == UNDETERMINED_VERSION) { + uint64_t wv = SYNC_ADD(&GlobalVersion, 1); + SYNC_CAS(&txn->wv, UNDETERMINED_VERSION, wv); + } + + for (i = 0; i < txn->writes_count; ++i) { + txn_state_t s = txn_ht_validate_key(txn, txn->writes[i].key, strlen(txn->writes[i].key)); + if (s == TXN_ABORTED) { + txn->state = TXN_ABORTED; + break; + } + } + if (txn->state == TXN_VALIDATING) { + txn->state = TXN_VALIDATED; + } + break; + + case TXN_VALIDATED: + case TXN_ABORTED: + break; + + default: + assert(FALSE); + } + + return txn->state; +} + +void txn_abort (txn_t *txn) { + + int i; + for (i = 0; i < txn->writes_count; ++i) { + update_rec_t *update = (update_rec_t *)txn->writes[i].rec; + update->version = TAG_VALUE(0); + } + + nbd_defer_free(txn->writes); + nbd_defer_free(txn); +} + +txn_state_t txn_commit (txn_t *txn) { + + assert(txn->state == TXN_RUNNING); + txn->state = TXN_VALIDATING; + txn_state_t state = txn_validate(txn); + + // Detach from its updates. + uint64_t wv = (txn->state == TXN_ABORTED) ? TAG_VALUE(0) : txn->wv; + int i; + for (i = 0; i < txn->writes_count; ++i) { + update_rec_t *update = (update_rec_t *)txn->writes[i].rec; + update->version = wv; + } + + nbd_defer_free(txn->writes); + nbd_defer_free(txn); + + return state; +} + +void txn_ht_put (txn_t *txn, const char *key, uint32_t key_len, int64_t value) { + + // create a new update record + update_rec_t *update = alloc_update_rec(); + update->type = UPDATE_TYPE_PUT; + update->value = value; + update->version = TAG_VALUE((uint64_t)txn); + + // push the new update record onto 's update list + int64_t update_prev; + do { + update->prev = (update_rec_t *) ht_get(txn->ht, key, key_len); + update_prev = (int64_t)update->prev; + } while (ht_compare_and_set(txn->ht, key, key_len, update_prev, (int64_t)update) != update_prev); + + // add to the write set for commit-time validation + if (txn->writes_count == txn->writes_size) { + write_rec_t *w = nbd_malloc(sizeof(write_rec_t) * txn->writes_size * 2); + memcpy(w, txn->writes, txn->writes_size * sizeof(write_rec_t)); + txn->writes_size *= 2; + } + int i = txn->writes_count++; + txn->writes[i].key = key; + txn->writes[i].rec = update; +} + +#ifdef MAKE_txn_test +#include "runtime.h" +int main (void) { + nbd_init(); + return 0; +} +#endif//txn_test