jsonrpc: Suppress duplicate logging.
[sliver-openvswitch.git] / lib / ovsdb-idl.c
index 53be4ec..ca77cc2 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "bitmap.h"
 #include "dynamic-string.h"
+#include "fatal-signal.h"
 #include "json.h"
 #include "jsonrpc.h"
 #include "ovsdb-data.h"
@@ -342,7 +343,9 @@ ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
 
     json_destroy(idl->monitor_request_id);
     msg = jsonrpc_create_request(
-        "monitor", json_array_create_2(json_null_create(), monitor_requests),
+        "monitor",
+        json_array_create_3(json_string_create(idl->class->database),
+                            json_null_create(), monitor_requests),
         &idl->monitor_request_id);
     jsonrpc_session_send(idl->session, msg);
 }
@@ -539,10 +542,41 @@ ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
     }
 }
 
+/* When a row A refers to row B through a column with a "refTable" constraint,
+ * but row B does not exist, row B is called an "orphan row".  Orphan rows
+ * should not persist, because the database enforces referential integrity, but
+ * they can appear transiently as changes from the database are received (the
+ * database doesn't try to topologically sort them and circular references mean
+ * it isn't always possible anyhow).
+ *
+ * This function returns true if 'row' is an orphan row, otherwise false.
+ */
 static bool
 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
 {
-    return !row->old;
+    return !row->old && !row->new;
+}
+
+/* Returns true if 'row' is conceptually part of the database as modified by
+ * the current transaction (if any), false otherwise.
+ *
+ * This function will return true if 'row' is not an orphan (see the comment on
+ * ovsdb_idl_row_is_orphan()) and:
+ *
+ *   - 'row' exists in the database and has not been deleted within the
+ *     current transaction (if any).
+ *
+ *   - 'row' was inserted within the current transaction and has not been
+ *     deleted.  (In the latter case you should not have passed 'row' in at
+ *     all, because ovsdb_idl_txn_delete() freed it.)
+ *
+ * This function will return false if 'row' is an orphan or if 'row' was
+ * deleted within the current transaction.
+ */
+static bool
+ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
+{
+    return row->new != NULL;
 }
 
 static void
@@ -808,7 +842,7 @@ next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
         struct ovsdb_idl_row *row;
 
         row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
-        if (row->new || !ovsdb_idl_row_is_orphan(row)) {
+        if (ovsdb_idl_row_exists(row)) {
             return row;
         }
     }
@@ -881,13 +915,22 @@ ovsdb_idl_txn_create(struct ovsdb_idl *idl)
     return txn;
 }
 
+/* Appends 's', which is treated as a printf()-type format string, to the
+ * comments that will be passed to the OVSDB server when 'txn' is committed.
+ * (The comment will be committed to the OVSDB log, which "ovsdb-tool
+ * show-log" can print in a relatively human-readable form.) */
 void
-ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
+ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
 {
+    va_list args;
+
     if (txn->comment.length) {
         ds_put_char(&txn->comment, '\n');
     }
-    ds_put_cstr(&txn->comment, s);
+
+    va_start(args, s);
+    ds_put_format_valist(&txn->comment, s, args);
+    va_end(args);
 }
 
 void
@@ -1072,7 +1115,8 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
         return txn->status;
     }
 
-    operations = json_array_create_empty();
+    operations = json_array_create_1(
+        json_string_create(txn->idl->class->database));
 
     /* Add prerequisites and declarations of new rows. */
     HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
@@ -1103,16 +1147,6 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
                                                     &column->type));
             }
         }
-        if (row->new && !row->old) {
-            struct json *op;
-
-            op = json_object_create();
-            json_array_add(operations, op);
-            json_object_put_string(op, "op", "declare");
-            json_object_put(op, "uuid-name",
-                            json_string_create_nocopy(
-                                uuid_name_from_uuid(&row->uuid)));
-        }
     }
 
     /* Add updates. */
@@ -1148,7 +1182,7 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
 
                 insert = xmalloc(sizeof *insert);
                 insert->dummy = row->uuid;
-                insert->op_index = operations->u.array.n;
+                insert->op_index = operations->u.array.n - 1;
                 uuid_zero(&insert->real);
                 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
                             uuid_hash(&insert->dummy));
@@ -1184,7 +1218,7 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
     if (txn->inc_table && any_updates) {
         struct json *op;
 
-        txn->inc_index = operations->u.array.n;
+        txn->inc_index = operations->u.array.n - 1;
 
         op = json_object_create();
         json_object_put_string(op, "op", "mutate");
@@ -1233,13 +1267,31 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
         hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
                     json_hash(txn->request_id, 0));
     } else {
-        txn->status = TXN_INCOMPLETE;
+        txn->status = TXN_TRY_AGAIN;
     }
 
     ovsdb_idl_txn_disassemble(txn);
     return txn->status;
 }
 
+/* Attempts to commit 'txn', blocking until the commit either succeeds or
+ * fails.  Returns the final commit status, which may be any TXN_* value other
+ * than TXN_INCOMPLETE. */
+enum ovsdb_idl_txn_status
+ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
+{
+    enum ovsdb_idl_txn_status status;
+
+    fatal_signal_run();
+    while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
+        ovsdb_idl_run(txn->idl);
+        ovsdb_idl_wait(txn->idl);
+        ovsdb_idl_txn_wait(txn);
+        poll_block();
+    }
+    return status;
+}
+
 int64_t
 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
 {
@@ -1408,10 +1460,18 @@ ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
 
 const struct ovsdb_idl_row *
 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
-                     const struct ovsdb_idl_table_class *class)
+                     const struct ovsdb_idl_table_class *class,
+                     const struct uuid *uuid)
 {
     struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
-    uuid_generate(&row->uuid);
+
+    if (uuid) {
+        assert(!ovsdb_idl_txn_get_row(txn, uuid));
+        row->uuid = *uuid;
+    } else {
+        uuid_generate(&row->uuid);
+    }
+
     row->table = ovsdb_idl_table_from_class(txn->idl, class);
     row->new = xmalloc(class->n_columns * sizeof *row->new);
     row->written = bitmap_allocate(class->n_columns);
@@ -1470,7 +1530,7 @@ ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
 
     if (txn->inc_index + 2 > results->n) {
         VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
-                     "for increment (has %u, needs %u)",
+                     "for increment (has %zu, needs %u)",
                      results->n, txn->inc_index + 2);
         return false;
     }
@@ -1484,7 +1544,7 @@ ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
     }
     if (count->u.integer != 1) {
         VLOG_WARN_RL(&syntax_rl,
-                     "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
+                     "\"mutate\" reply \"count\" is %lld instead of 1",
                      count->u.integer);
         return false;
     }
@@ -1495,7 +1555,7 @@ ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
         return false;
     }
     if (rows->u.array.n != 1) {
-        VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
+        VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %zu elements "
                      "instead of 1",
                      rows->u.array.n);
         return false;
@@ -1525,7 +1585,7 @@ ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
 
     if (insert->op_index >= results->n) {
         VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
-                     "for insert (has %u, needs %u)",
+                     "for insert (has %zu, needs %u)",
                      results->n, insert->op_index);
         return false;
     }
@@ -1640,3 +1700,10 @@ ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
     assert(txn != NULL);
     return txn;
 }
+
+struct ovsdb_idl *
+ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)
+{
+    return txn->idl;
+}
+