]> git.proxmox.com Git - mirror_ovs.git/blobdiff - ovsdb/transaction.c
ovsdb-idlc: Return expected sequence number while setting conditions.
[mirror_ovs.git] / ovsdb / transaction.c
index 646163a99824c164b1c30685e612617db1b35f18..8ffefcf7c9d0aa44baa90eb91460555e5ed695df 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017, 2019 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "transaction.h"
 
 #include "bitmap.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
+#include "file.h"
 #include "hash.h"
-#include "hmap.h"
-#include "json.h"
-#include "list.h"
+#include "monitor.h"
+#include "openvswitch/hmap.h"
+#include "openvswitch/json.h"
+#include "openvswitch/list.h"
+#include "openvswitch/poll-loop.h"
+#include "openvswitch/vlog.h"
 #include "ovsdb-error.h"
 #include "ovsdb.h"
 #include "row.h"
+#include "storage.h"
 #include "table.h"
 #include "uuid.h"
 
+VLOG_DEFINE_THIS_MODULE(transaction);
+
 struct ovsdb_txn {
     struct ovsdb *db;
-    struct list txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
+    struct ovs_list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
     struct ds comment;
+    struct uuid txnid; /* For clustered mode only. It is the eid. */
 };
 
 /* A table modified by a transaction. */
 struct ovsdb_txn_table {
-    struct list node;           /* Element in ovsdb_txn's txn_tables list. */
+    struct ovs_list node;       /* Element in ovsdb_txn's txn_tables list. */
     struct ovsdb_table *table;
     struct hmap txn_rows;       /* Contains "struct ovsdb_txn_row"s. */
 
@@ -84,10 +92,10 @@ struct ovsdb_txn_row {
     unsigned long changed[];    /* Bits set to 1 for columns that changed. */
 };
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *r);
 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 for_each_txn_row(struct ovsdb_txn *txn,
                       struct ovsdb_error *(*)(struct ovsdb_txn *,
                                               struct ovsdb_txn_row *));
@@ -99,17 +107,29 @@ static unsigned int serial;
 struct ovsdb_txn *
 ovsdb_txn_create(struct ovsdb *db)
 {
-    struct ovsdb_txn *txn = xmalloc(sizeof *txn);
+    struct ovsdb_txn *txn = xzalloc(sizeof *txn);
     txn->db = db;
-    list_init(&txn->txn_tables);
+    ovs_list_init(&txn->txn_tables);
     ds_init(&txn->comment);
     return txn;
 }
 
+void
+ovsdb_txn_set_txnid(const struct uuid *txnid, struct ovsdb_txn *txn)
+{
+    txn->txnid = *txnid;
+}
+
+const struct uuid *
+ovsdb_txn_get_txnid(const struct ovsdb_txn *txn)
+{
+    return &txn->txnid;
+}
+
 static void
 ovsdb_txn_free(struct ovsdb_txn *txn)
 {
-    ovs_assert(list_is_empty(&txn->txn_tables));
+    ovs_assert(ovs_list_is_empty(&txn->txn_tables));
     ds_destroy(&txn->comment);
     free(txn);
 }
@@ -204,7 +224,7 @@ find_or_make_txn_row(struct ovsdb_txn *txn, const struct ovsdb_table *table,
     return txn_row;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
                            const struct ovsdb_column *c,
                            const struct ovsdb_base_type *base,
@@ -218,7 +238,7 @@ ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
         return NULL;
     }
 
-    table = base->u.uuid.refTable;
+    table = base->uuid.refTable;
     for (i = 0; i < n; i++) {
         const struct uuid *uuid = &atoms[i].uuid;
         struct ovsdb_txn_row *txn_row;
@@ -244,7 +264,7 @@ ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
                           const struct ovsdb_column *column, int delta)
 {
@@ -260,7 +280,7 @@ ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
     return error;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
 {
     struct ovsdb_table *table = r->table;
@@ -270,16 +290,18 @@ update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
         const struct ovsdb_column *column = node->data;
         struct ovsdb_error *error;
 
-        if (r->old) {
-            error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
-            if (error) {
-                return OVSDB_WRAP_BUG("error decreasing refcount", error);
+        if (bitmap_is_set(r->changed, column->index)) {
+            if (r->old) {
+                error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
+                if (error) {
+                    return OVSDB_WRAP_BUG("error decreasing refcount", error);
+                }
             }
-        }
-        if (r->new) {
-            error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
-            if (error) {
-                return error;
+            if (r->new) {
+                error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
+                if (error) {
+                    return error;
+                }
             }
         }
     }
@@ -287,7 +309,7 @@ update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
 {
     if (r->new || !r->n_refs) {
@@ -295,13 +317,13 @@ check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
     } else {
         return ovsdb_error("referential integrity violation",
                            "cannot delete %s row "UUID_FMT" because "
-                           "of %zu remaining reference(s)",
+                           "of %"PRIuSIZE" remaining reference(s)",
                            r->table->schema->name, UUID_ARGS(&r->uuid),
                            r->n_refs);
     }
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
                 const struct ovsdb_base_type *base,
                 const union ovsdb_atom *atoms, unsigned int n)
@@ -313,7 +335,7 @@ delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
         return NULL;
     }
 
-    table = base->u.uuid.refTable;
+    table = base->uuid.refTable;
     for (i = 0; i < n; i++) {
         const struct uuid *uuid = &atoms[i].uuid;
         struct ovsdb_txn_row *txn_row;
@@ -343,7 +365,7 @@ delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct shash_node *node;
@@ -378,7 +400,7 @@ delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     if (txn_row->new && !txn_row->n_refs) {
@@ -387,7 +409,7 @@ collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 update_ref_counts(struct ovsdb_txn *txn)
 {
     struct ovsdb_error *error;
@@ -433,9 +455,48 @@ ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
     return NULL;
 }
 
+static struct ovsdb_error *
+ovsdb_txn_update_weak_refs(struct ovsdb_txn *txn OVS_UNUSED,
+                           struct ovsdb_txn_row *txn_row)
+{
+    struct ovsdb_weak_ref *weak, *next;
+
+    /* Remove the weak references originating in the old version of the row. */
+    if (txn_row->old) {
+        LIST_FOR_EACH_SAFE (weak, next, src_node, &txn_row->old->src_refs) {
+            ovs_list_remove(&weak->src_node);
+            ovs_list_remove(&weak->dst_node);
+            free(weak);
+        }
+    }
+
+    /* Although the originating rows have the responsibility of updating the
+     * weak references in the dst, it is possible that some source rows aren't
+     * part of the transaction.  In that situation this row needs to move the
+     * list of incoming weak references from the old row into the new one.
+     */
+    if (txn_row->old && txn_row->new) {
+        /* Move the incoming weak references from old to new. */
+        ovs_list_push_back_all(&txn_row->new->dst_refs,
+                               &txn_row->old->dst_refs);
+    }
+
+    /* Insert the weak references originating in the new version of the row. */
+    struct ovsdb_row *dst_row;
+    if (txn_row->new) {
+        LIST_FOR_EACH (weak, src_node, &txn_row->new->src_refs) {
+            /* dst_row MUST exist. */
+            dst_row = CONST_CAST(struct ovsdb_row *,
+                    ovsdb_table_get_row(weak->dst_table, &weak->dst));
+            ovs_list_insert(&dst_row->dst_refs, &weak->dst_node);
+        }
+    }
+
+    return NULL;
+}
+
 static void
-add_weak_ref(struct ovsdb_txn *txn,
-             const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
+add_weak_ref(const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
 {
     struct ovsdb_row *src = CONST_CAST(struct ovsdb_row *, src_);
     struct ovsdb_row *dst = CONST_CAST(struct ovsdb_row *, dst_);
@@ -445,11 +506,9 @@ add_weak_ref(struct ovsdb_txn *txn,
         return;
     }
 
-    dst = ovsdb_txn_row_modify(txn, dst);
-
-    if (!list_is_empty(&dst->dst_refs)) {
+    if (!ovs_list_is_empty(&dst->dst_refs)) {
         /* Omit duplicates. */
-        weak = CONTAINER_OF(list_back(&dst->dst_refs),
+        weak = CONTAINER_OF(ovs_list_back(&dst->dst_refs),
                             struct ovsdb_weak_ref, dst_node);
         if (weak->src == src) {
             return;
@@ -458,17 +517,20 @@ add_weak_ref(struct ovsdb_txn *txn,
 
     weak = xmalloc(sizeof *weak);
     weak->src = src;
-    list_push_back(&dst->dst_refs, &weak->dst_node);
-    list_push_back(&src->src_refs, &weak->src_node);
+    weak->dst_table = dst->table;
+    weak->dst = *ovsdb_row_get_uuid(dst);
+    /* The dst_refs list is updated at commit time. */
+    ovs_list_init(&weak->dst_node);
+    ovs_list_push_back(&src->src_refs, &weak->src_node);
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct ovsdb_table *table;
     struct shash_node *node;
 
-    if (txn_row->old) {
+    if (txn_row->old && !txn_row->new) {
         /* Mark rows that have weak references to 'txn_row' as modified, so
          * that their weak references will get reassessed. */
         struct ovsdb_weak_ref *weak, *next;
@@ -500,10 +562,10 @@ assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
             for (i = 0; i < datum->n; ) {
                 const struct ovsdb_row *row;
 
-                row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
+                row = ovsdb_table_get_row(column->type.key.uuid.refTable,
                                           &datum->keys[i].uuid);
                 if (row) {
-                    add_weak_ref(txn, txn_row->new, row);
+                    add_weak_ref(txn_row->new, row);
                     i++;
                 } else {
                     if (uuid_is_zero(&datum->keys[i].uuid)) {
@@ -518,10 +580,10 @@ assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
             for (i = 0; i < datum->n; ) {
                 const struct ovsdb_row *row;
 
-                row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
+                row = ovsdb_table_get_row(column->type.value.uuid.refTable,
                                           &datum->values[i].uuid);
                 if (row) {
-                    add_weak_ref(txn, txn_row->new, row);
+                    add_weak_ref(txn_row->new, row);
                     i++;
                 } else {
                     if (uuid_is_zero(&datum->values[i].uuid)) {
@@ -568,7 +630,7 @@ assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct ovsdb_table *table = txn_row->table;
@@ -602,7 +664,7 @@ determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_max_rows(struct ovsdb_txn *txn)
 {
     struct ovsdb_txn_table *t;
@@ -614,7 +676,7 @@ check_max_rows(struct ovsdb_txn *txn)
         if (n_rows > max_rows) {
             return ovsdb_error("constraint violation",
                                "transaction causes \"%s\" table to contain "
-                               "%zu rows, greater than the schema-defined "
+                               "%"PRIuSIZE" rows, greater than the schema-defined "
                                "limit of %u row(s)",
                                t->table->schema->name, n_rows, max_rows);
         }
@@ -653,7 +715,7 @@ duplicate_index_row__(const struct ovsdb_column_set *index,
     ds_put_format(out, "%s row, with UUID "UUID_FMT", ",
                   title, UUID_ARGS(ovsdb_row_get_uuid(row)));
     if (!row->txn_row
-        || bitmap_scan(row->txn_row->changed, 0, n_columns) == n_columns) {
+        || bitmap_scan(row->txn_row->changed, 1, 0, n_columns) == n_columns) {
         ds_put_cstr(out, "existed in the database before this "
                     "transaction and was not modified by the transaction.");
     } else if (!row->txn_row->old) {
@@ -671,7 +733,7 @@ duplicate_index_row__(const struct ovsdb_column_set *index,
     }
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 duplicate_index_row(const struct ovsdb_column_set *index,
                     const struct ovsdb_row *a,
                     const struct ovsdb_row *b)
@@ -710,35 +772,44 @@ duplicate_index_row(const struct ovsdb_column_set *index,
     return error;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
                        struct ovsdb_txn_row *txn_row)
 {
-    struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
-    struct ovsdb_table *table = txn_row->table;
+    /* Skip rows that are being deleted since they can't violate uniqueness. */
     struct ovsdb_row *row = txn_row->new;
-    size_t i;
-
     if (!row) {
         return NULL;
     }
 
-    for (i = 0; i < table->schema->n_indexes; i++) {
+    struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
+    struct ovsdb_table *table = txn_row->table;
+    for (size_t i = 0; i < table->schema->n_indexes; i++) {
         const struct ovsdb_column_set *index = &table->schema->indexes[i];
-        struct ovsdb_row *irow;
-        uint32_t hash;
-
-        hash = ovsdb_row_hash_columns(row, index, 0);
-        irow = ovsdb_index_search(&txn_table->txn_indexes[i], row, i, hash);
+        uint32_t hash = ovsdb_row_hash_columns(row, index, 0);
+
+        /* Check whether the row has a match in the temporary hash table that
+         * we're building.  If we add two rows with the same index data, then
+         * there's a duplicate within the rows added or modified in this
+         * transaction.*/
+        struct ovsdb_row *irow
+            = ovsdb_index_search(&txn_table->txn_indexes[i], row, i, hash);
         if (irow) {
             return duplicate_index_row(index, irow, row);
         }
 
+        /* Also check whether the row has a match in the table's real index
+         * (which won't be updated until transaction commit is certain).  If
+         * there's a match, and it's for a row that wasn't pulled into the
+         * transaction, then it's a duplicate.  (If it is for a row that is
+         * part of the transaction, then the first check has already handled
+         * it.) */
         irow = ovsdb_index_search(&table->indexes[i], row, i, hash);
         if (irow && !irow->txn_row) {
             return duplicate_index_row(index, irow, row);
         }
 
+        /* Add row to temporary hash table. */
         hmap_insert(&txn_table->txn_indexes[i],
                     ovsdb_row_get_index_node(row, i), hash);
     }
@@ -746,41 +817,58 @@ check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
     return NULL;
 }
 
-struct ovsdb_error *
-ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+update_version(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *txn_row)
+{
+    struct ovsdb_table *table = txn_row->table;
+    size_t n_columns = shash_count(&table->schema->columns);
+
+    if (txn_row->old && txn_row->new
+        && !bitmap_is_all_zeros(txn_row->changed, n_columns)) {
+        bitmap_set1(txn_row->changed, OVSDB_COL_VERSION);
+        uuid_generate(ovsdb_row_get_version_rw(txn_row->new));
+    }
+
+    return NULL;
+}
+
+static bool
+ovsdb_txn_is_empty(const struct ovsdb_txn *txn)
+{
+    return ovs_list_is_empty(&txn->txn_tables);
+}
+
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_txn_precommit(struct ovsdb_txn *txn)
 {
-    struct ovsdb_replica *replica;
     struct ovsdb_error *error;
 
     /* Figure out what actually changed, and abort early if the transaction
      * was really a no-op. */
     error = for_each_txn_row(txn, determine_changes);
     if (error) {
+        ovsdb_txn_abort(txn);
         return OVSDB_WRAP_BUG("can't happen", error);
     }
-    if (list_is_empty(&txn->txn_tables)) {
-        ovsdb_txn_abort(txn);
+    if (ovs_list_is_empty(&txn->txn_tables)) {
         return NULL;
     }
 
     /* Update reference counts and check referential integrity. */
     error = update_ref_counts(txn);
     if (error) {
-        ovsdb_txn_abort(txn);
         return error;
     }
 
     /* Delete unreferenced, non-root rows. */
     error = for_each_txn_row(txn, collect_garbage);
     if (error) {
-        ovsdb_txn_abort(txn);
         return OVSDB_WRAP_BUG("can't happen", error);
     }
 
     /* Check maximum rows table constraints. */
     error = check_max_rows(txn);
     if (error) {
-        ovsdb_txn_abort(txn);
         return error;
     }
 
@@ -788,36 +876,287 @@ ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
      * integrity. */
     error = for_each_txn_row(txn, assess_weak_refs);
     if (error) {
-        ovsdb_txn_abort(txn);
         return error;
     }
 
     /* Verify that the indexes will still be unique post-transaction. */
     error = for_each_txn_row(txn, check_index_uniqueness);
     if (error) {
-        ovsdb_txn_abort(txn);
         return error;
     }
 
-    /* Send the commit to each replica. */
-    LIST_FOR_EACH (replica, node, &txn->db->replicas) {
-        error = (replica->class->commit)(replica, txn, durable);
-        if (error) {
-            /* We don't support two-phase commit so only the first replica is
-             * allowed to report an error. */
-            ovs_assert(&replica->node == txn->db->replicas.next);
+    /* Update _version for rows that changed.  */
+    error = for_each_txn_row(txn, update_version);
+    if (error) {
+        return OVSDB_WRAP_BUG("can't happen", error);
+    }
 
-            ovsdb_txn_abort(txn);
-            return error;
+    return error;
+}
+
+static struct ovsdb_txn*
+ovsdb_txn_clone(const struct ovsdb_txn *txn)
+{
+    struct ovsdb_txn *txn_cloned = xzalloc(sizeof *txn_cloned);
+    ovs_list_init(&txn_cloned->txn_tables);
+    txn_cloned->txnid = txn->txnid;
+
+    struct ovsdb_txn_table *t;
+    LIST_FOR_EACH (t, node, &txn->txn_tables) {
+        struct ovsdb_txn_table *t_cloned = xmalloc(sizeof *t_cloned);
+        ovs_list_push_back(&txn_cloned->txn_tables, &t_cloned->node);
+        hmap_init(&t_cloned->txn_rows);
+
+        struct ovsdb_txn_row *r;
+        HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) {
+            size_t n_columns = shash_count(&t->table->schema->columns);
+            struct ovsdb_txn_row *r_cloned =
+                xzalloc(offsetof(struct ovsdb_txn_row, changed)
+                        + bitmap_n_bytes(n_columns));
+
+            r_cloned->uuid = r->uuid;
+            r_cloned->table = r->table;
+            r_cloned->old = r->old ? ovsdb_row_clone(r->old) : NULL;
+            r_cloned->new = r->new ? ovsdb_row_clone(r->new) : NULL;
+            memcpy(&r_cloned->changed, &r->changed, bitmap_n_bytes(n_columns));
+            hmap_insert(&t_cloned->txn_rows, &r_cloned->hmap_node,
+                        uuid_hash(&r_cloned->uuid));
+        }
+    }
+    return txn_cloned;
+}
+
+static void
+ovsdb_txn_destroy_cloned(struct ovsdb_txn *txn)
+{
+    ovs_assert(!txn->db);
+    struct ovsdb_txn_table *t, *next_txn_table;
+    LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) {
+        struct ovsdb_txn_row *r, *next_txn_row;
+        HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) {
+            if (r->old) {
+                ovsdb_row_destroy(r->old);
+            }
+            if (r->new) {
+                ovsdb_row_destroy(r->new);
+            }
+            hmap_remove(&t->txn_rows, &r->hmap_node);
+            free(r);
         }
+        hmap_destroy(&t->txn_rows);
+        ovs_list_remove(&t->node);
+        free(t);
+    }
+    free(txn);
+}
+
+static void
+ovsdb_txn_add_to_history(struct ovsdb_txn *txn)
+{
+    if (txn->db->need_txn_history) {
+        struct ovsdb_txn_history_node *node = xzalloc(sizeof *node);
+        node->txn = ovsdb_txn_clone(txn);
+        ovs_list_push_back(&txn->db->txn_history, &node->node);
+        txn->db->n_txn_history++;
     }
+}
+
+/* Finalize commit. */
+void
+ovsdb_txn_complete(struct ovsdb_txn *txn)
+{
+    if (!ovsdb_txn_is_empty(txn)) {
 
-    /* Finalize commit. */
-    txn->db->run_triggers = true;
-    ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
+        txn->db->run_triggers_now = txn->db->run_triggers = true;
+        ovsdb_monitors_commit(txn->db, txn);
+        ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_update_weak_refs));
+        ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
+    }
     ovsdb_txn_free(txn);
+}
 
-    return NULL;
+/* Applies 'txn' to the internal representation of the database.  This is for
+ * transactions that don't need to be written to storage; probably, they came
+ * from storage.  These transactions shouldn't ordinarily fail because storage
+ * should contain only consistent transactions.  (One exception is for database
+ * conversion in ovsdb_convert().) */
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_txn_replay_commit(struct ovsdb_txn *txn)
+{
+    struct ovsdb_error *error = ovsdb_txn_precommit(txn);
+    if (error) {
+        ovsdb_txn_abort(txn);
+    } else {
+        ovsdb_txn_add_to_history(txn);
+        ovsdb_txn_complete(txn);
+    }
+    return error;
+}
+
+/* If 'error' is nonnull, the transaction is complete, with the given error as
+ * the result.
+ *
+ * Otherwise, if 'write' is nonnull, then the transaction is waiting for
+ * 'write' to complete.
+ *
+ * Otherwise, if 'commit_index' is nonzero, then the transaction is waiting for
+ * 'commit_index' to be applied to the storage.
+ *
+ * Otherwise, the transaction is complete and successful. */
+struct ovsdb_txn_progress {
+    struct ovsdb_error *error;
+    struct ovsdb_write *write;
+    uint64_t commit_index;
+
+    struct ovsdb_storage *storage;
+};
+
+bool
+ovsdb_txn_precheck_prereq(const struct ovsdb *db)
+{
+    const struct uuid *eid = ovsdb_storage_peek_last_eid(db->storage);
+    if (!eid) {
+        return true;
+    }
+    return uuid_equals(&db->prereq, eid);
+}
+
+struct ovsdb_txn_progress *
+ovsdb_txn_propose_schema_change(struct ovsdb *db,
+                                const struct json *schema,
+                                const struct json *data)
+{
+    struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress);
+    progress->storage = db->storage;
+
+    struct uuid next;
+    struct ovsdb_write *write = ovsdb_storage_write_schema_change(
+        db->storage, schema, data, &db->prereq, &next);
+    if (!ovsdb_write_is_complete(write)) {
+        progress->write = write;
+    } else {
+        progress->error = ovsdb_error_clone(ovsdb_write_get_error(write));
+        ovsdb_write_destroy(write);
+    }
+    return progress;
+}
+
+struct ovsdb_txn_progress *
+ovsdb_txn_propose_commit(struct ovsdb_txn *txn, bool durable)
+{
+    struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress);
+    progress->storage = txn->db->storage;
+    progress->error = ovsdb_txn_precommit(txn);
+    if (progress->error) {
+        return progress;
+    }
+
+    /* Turn the commit into the format used for the storage logs.. */
+    struct json *txn_json = ovsdb_file_txn_to_json(txn);
+    if (!txn_json) {
+        /* Nothing to do, so success. */
+        return progress;
+    }
+    txn_json = ovsdb_file_txn_annotate(txn_json, ovsdb_txn_get_comment(txn));
+
+    struct uuid next;
+    struct ovsdb_write *write = ovsdb_storage_write(
+        txn->db->storage, txn_json, &txn->db->prereq, &next, durable);
+    json_destroy(txn_json);
+    if (!ovsdb_write_is_complete(write)) {
+        progress->write = write;
+    } else {
+        progress->error = ovsdb_error_clone(ovsdb_write_get_error(write));
+        ovsdb_write_destroy(write);
+    }
+    return progress;
+}
+
+/* Proposes 'txn' for commitment and then waits for the commit to succeed or
+ * fail.  Returns null if successful, otherwise the error.
+ *
+ * **In addition**, this function also completes or aborts the transaction if
+ * the transaction succeeded or failed, respectively. */
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_txn_propose_commit_block(struct ovsdb_txn *txn, bool durable)
+{
+    struct ovsdb_txn_progress *p = ovsdb_txn_propose_commit(txn, durable);
+    for (;;) {
+        ovsdb_storage_run(p->storage);
+        if (ovsdb_txn_progress_is_complete(p)) {
+            struct ovsdb_error *error
+                = ovsdb_error_clone(ovsdb_txn_progress_get_error(p));
+            ovsdb_txn_progress_destroy(p);
+
+            if (error) {
+                ovsdb_txn_abort(txn);
+            } else {
+                ovsdb_txn_complete(txn);
+            }
+
+            return error;
+        }
+        ovsdb_storage_wait(p->storage);
+        poll_block();
+    }
+}
+
+static void
+ovsdb_txn_progress_run(struct ovsdb_txn_progress *p)
+{
+    if (p->error) {
+        return;
+    }
+
+    if (p->write) {
+        if (!ovsdb_write_is_complete(p->write)) {
+            return;
+        }
+        p->error = ovsdb_error_clone(ovsdb_write_get_error(p->write));
+        p->commit_index = ovsdb_write_get_commit_index(p->write);
+        ovsdb_write_destroy(p->write);
+        p->write = NULL;
+
+        if (p->error) {
+            return;
+        }
+    }
+
+    if (p->commit_index) {
+        if (ovsdb_storage_get_applied_index(p->storage) >= p->commit_index) {
+            p->commit_index = 0;
+        }
+    }
+}
+
+static bool
+ovsdb_txn_progress_is_complete__(const struct ovsdb_txn_progress *p)
+{
+    return p->error || (!p->write && !p->commit_index);
+}
+
+bool
+ovsdb_txn_progress_is_complete(const struct ovsdb_txn_progress *p)
+{
+    ovsdb_txn_progress_run(CONST_CAST(struct ovsdb_txn_progress *, p));
+    return ovsdb_txn_progress_is_complete__(p);
+}
+
+const struct ovsdb_error *
+ovsdb_txn_progress_get_error(const struct ovsdb_txn_progress *p)
+{
+    ovs_assert(ovsdb_txn_progress_is_complete__(p));
+    return p->error;
+}
+
+void
+ovsdb_txn_progress_destroy(struct ovsdb_txn_progress *p)
+{
+    if (p) {
+        ovsdb_error_destroy(p->error);
+        ovsdb_write_destroy(p->write);
+        free(p);
+    }
 }
 
 void
@@ -852,7 +1191,7 @@ ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
         for (i = 0; i < table->schema->n_indexes; i++) {
             hmap_init(&txn_table->txn_indexes[i]);
         }
-        list_push_back(&txn->txn_tables, &txn_table->node);
+        ovs_list_push_back(&txn->txn_tables, &txn_table->node);
     }
     return table->txn_table;
 }
@@ -904,7 +1243,6 @@ ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
 
         rw_row = ovsdb_row_clone(ro_row);
         rw_row->n_refs = ro_row->n_refs;
-        uuid_generate(ovsdb_row_get_version_rw(rw_row));
         ovsdb_txn_row_create(txn, table, ro_row, rw_row);
         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
 
@@ -949,6 +1287,26 @@ ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
     }
 }
 
+/* Returns true if 'row_uuid' may be used as the UUID for a newly created row
+ * in 'table' (that is, that it is unique within 'table'), false otherwise. */
+bool
+ovsdb_txn_may_create_row(const struct ovsdb_table *table,
+                         const struct uuid *row_uuid)
+{
+    /* If a row 'row_uuid' currently exists, disallow creating a duplicate. */
+    if (ovsdb_table_get_row(table, row_uuid)) {
+        return false;
+    }
+
+    /* If a row 'row_uuid' previously existed in this transaction, disallow
+     * creating a new row with the same UUID. */
+    if (find_txn_row(table, row_uuid)) {
+        return false;
+    }
+
+    return true;
+}
+
 void
 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
 {
@@ -994,7 +1352,7 @@ ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
 
     txn_table->table->txn_table = NULL;
     hmap_destroy(&txn_table->txn_rows);
-    list_remove(&txn_table->node);
+    ovs_list_remove(&txn_table->node);
     free(txn_table);
 }
 
@@ -1012,7 +1370,7 @@ ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
  * (Even though 'cb' is not allowed to delete some txn_rows, it can still
  * delete any actual row by clearing a txn_row's 'new' member.)
  */
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 for_each_txn_row(struct ovsdb_txn *txn,
                  struct ovsdb_error *(*cb)(struct ovsdb_txn *,
                                            struct ovsdb_txn_row *))
@@ -1058,3 +1416,46 @@ for_each_txn_row(struct ovsdb_txn *txn,
 
     return NULL;
 }
+
+void
+ovsdb_txn_history_run(struct ovsdb *db)
+{
+    if (!db->need_txn_history) {
+        return;
+    }
+    /* Remove old histories to limit the size of the history */
+    while (db->n_txn_history > 100) {
+        struct ovsdb_txn_history_node *txn_h_node = CONTAINER_OF(
+                ovs_list_pop_front(&db->txn_history),
+                struct ovsdb_txn_history_node, node);
+
+        ovsdb_txn_destroy_cloned(txn_h_node->txn);
+        free(txn_h_node);
+        db->n_txn_history--;
+    }
+}
+
+void
+ovsdb_txn_history_init(struct ovsdb *db, bool need_txn_history)
+{
+    db->need_txn_history = need_txn_history;
+    db->n_txn_history = 0;
+    ovs_list_init(&db->txn_history);
+}
+
+void
+ovsdb_txn_history_destroy(struct ovsdb *db)
+{
+
+    if (!db->need_txn_history) {
+        return;
+    }
+
+    struct ovsdb_txn_history_node *txn_h_node, *next;
+    LIST_FOR_EACH_SAFE (txn_h_node, next, node, &db->txn_history) {
+        ovs_list_remove(&txn_h_node->node);
+        ovsdb_txn_destroy_cloned(txn_h_node->txn);
+        free(txn_h_node);
+    }
+    db->n_txn_history = 0;
+}