ovsdb-idl: Simplify usage of ovsdb_idl_run().
authorBen Pfaff <blp@nicira.com>
Wed, 9 Jun 2010 22:18:17 +0000 (15:18 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 23 Jun 2010 19:43:02 +0000 (12:43 -0700)
It makes client code simpler if ovsdb_idl_run() simply lets the caller
know whether anything changed.

lib/ovsdb-idl.c
lib/ovsdb-idl.h
tests/test-ovsdb.c
utilities/ovs-vsctl.c
vswitchd/ovs-vswitchd.c

index ca77cc2..da97579 100644 (file)
@@ -231,9 +231,10 @@ ovsdb_idl_clear(struct ovsdb_idl *idl)
     }
 }
 
-void
+bool
 ovsdb_idl_run(struct ovsdb_idl *idl)
 {
+    unsigned int initial_change_seqno = idl->change_seqno;
     int i;
 
     assert(!idl->txn);
@@ -290,6 +291,8 @@ ovsdb_idl_run(struct ovsdb_idl *idl)
         }
         jsonrpc_msg_destroy(msg);
     }
+
+    return initial_change_seqno != idl->change_seqno;
 }
 
 void
index 31fae98..88bd7b3 100644 (file)
@@ -31,7 +31,7 @@ struct ovsdb_idl *ovsdb_idl_create(const char *remote,
                                    const struct ovsdb_idl_class *);
 void ovsdb_idl_destroy(struct ovsdb_idl *);
 
-void ovsdb_idl_run(struct ovsdb_idl *);
+bool ovsdb_idl_run(struct ovsdb_idl *);
 void ovsdb_idl_wait(struct ovsdb_idl *);
 
 unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
index 21825d7..2c8470e 100644 (file)
@@ -1565,31 +1565,6 @@ print_idl(struct ovsdb_idl *idl, int step)
     }
 }
 
-static unsigned int
-print_updated_idl(struct ovsdb_idl *idl, struct jsonrpc *rpc,
-                  int step, unsigned int seqno)
-{
-    for (;;) {
-        unsigned int new_seqno;
-
-        if (rpc) {
-            jsonrpc_run(rpc);
-        }
-        ovsdb_idl_run(idl);
-        new_seqno = ovsdb_idl_get_seqno(idl);
-        if (new_seqno != seqno) {
-            print_idl(idl, step);
-            return new_seqno;
-        }
-
-        if (rpc) {
-            jsonrpc_wait(rpc);
-        }
-        ovsdb_idl_wait(idl);
-        poll_block();
-    }
-}
-
 static void
 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
             size_t *n)
@@ -1786,8 +1761,19 @@ do_idl(int argc, char *argv[])
             /* The previous transaction didn't change anything. */
             arg++;
         } else {
-            seqno = print_updated_idl(idl, rpc, step++, seqno);
+            /* Wait for update. */
+            while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
+                jsonrpc_run(rpc);
+
+                ovsdb_idl_wait(idl);
+                jsonrpc_wait(rpc);
+                poll_block();
+            }
+
+            /* Print update. */
+            print_idl(idl, step++);
         }
+        seqno = ovsdb_idl_get_seqno(idl);
 
         if (!strcmp(arg, "reconnect")) {
             printf("%03d: reconnect\n", step++);
@@ -1816,7 +1802,11 @@ do_idl(int argc, char *argv[])
     if (rpc) {
         jsonrpc_close(rpc);
     }
-    print_updated_idl(idl, NULL, step++, seqno);
+    while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
+        ovsdb_idl_wait(idl);
+        poll_block();
+    }
+    print_idl(idl, step++);
     ovsdb_idl_destroy(idl);
     printf("%03d: done\n", step);
 }
index c16767d..9d34267 100644 (file)
@@ -119,7 +119,6 @@ int
 main(int argc, char *argv[])
 {
     struct ovsdb_idl *idl;
-    unsigned int seqno;
     struct vsctl_command *commands;
     size_t n_commands;
     char *args;
@@ -147,19 +146,13 @@ main(int argc, char *argv[])
 
     /* Now execute the commands. */
     idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
-    seqno = ovsdb_idl_get_seqno(idl);
     trials = 0;
     for (;;) {
-        unsigned int new_seqno;
-
-        ovsdb_idl_run(idl);
-        new_seqno = ovsdb_idl_get_seqno(idl);
-        if (new_seqno != seqno) {
+        if (ovsdb_idl_run(idl)) {
             if (++trials > 5) {
                 vsctl_fatal("too many database inconsistency failures");
             }
             do_vsctl(args, commands, n_commands, idl);
-            seqno = new_seqno;
         }
 
         ovsdb_idl_wait(idl);
index 6478156..5c8c80a 100644 (file)
@@ -62,9 +62,7 @@ main(int argc, char *argv[])
     struct signal *sighup;
     struct ovsdb_idl *idl;
     const char *remote;
-    bool need_reconfigure;
     bool inited, exiting;
-    unsigned int idl_seqno;
     int retval;
 
     proctitle_init(argc, argv);
@@ -89,27 +87,27 @@ main(int argc, char *argv[])
     daemonize_complete();
 
     idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
-    idl_seqno = ovsdb_idl_get_seqno(idl);
 
-    need_reconfigure = false;
     inited = false;
     exiting = false;
     while (!exiting) {
+        bool need_reconfigure;
+
         if (signal_poll(sighup)) {
             vlog_reopen_log_file();
         }
+
+        need_reconfigure = false;
         if (inited && bridge_run()) {
             need_reconfigure = true;
         }
-        ovsdb_idl_run(idl);
-        if (idl_seqno != ovsdb_idl_get_seqno(idl)) {
-            idl_seqno = ovsdb_idl_get_seqno(idl);
+        if (ovsdb_idl_run(idl)) {
             need_reconfigure = true;
         }
+
         if (need_reconfigure) {
             const struct ovsrec_open_vswitch *cfg;
 
-            need_reconfigure = false;
             cfg = ovsrec_open_vswitch_first(idl);
             if (cfg) {
                 if (inited) {