jsonrpc-server: refactor ovsdb_jsonrpc_parse_monitor_request
[cascardo/ovs.git] / ovsdb / jsonrpc-server.c
index 9f99d64..5375357 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
+/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
 #include "ovsdb-error.h"
 #include "ovsdb-parser.h"
 #include "ovsdb.h"
+#include "poll-loop.h"
 #include "reconnect.h"
 #include "row.h"
 #include "server.h"
@@ -36,7 +37,7 @@
 #include "timeval.h"
 #include "transaction.h"
 #include "trigger.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
 
@@ -62,6 +63,8 @@ static bool ovsdb_jsonrpc_session_get_status(
     struct ovsdb_jsonrpc_remote_status *);
 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
+static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
+                                       struct jsonrpc_msg *);
 
 /* Triggers. */
 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
@@ -75,15 +78,16 @@ static void ovsdb_jsonrpc_trigger_complete_done(
     struct ovsdb_jsonrpc_session *);
 
 /* Monitors. */
-static struct json *ovsdb_jsonrpc_monitor_create(
-    struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params);
+static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
+    struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
+    const struct json *request_id);
 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
     struct ovsdb_jsonrpc_session *,
     struct json_array *params,
     const struct json *request_id);
 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
-static size_t ovsdb_jsonrpc_monitor_json_length_all(
-    struct ovsdb_jsonrpc_session *);
+static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
+static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
 \f
 /* JSON-RPC database server. */
 
@@ -99,7 +103,7 @@ struct ovsdb_jsonrpc_server {
 struct ovsdb_jsonrpc_remote {
     struct ovsdb_jsonrpc_server *server;
     struct pstream *listener;   /* Listener, if passive. */
-    struct list sessions;       /* List of "struct ovsdb_jsonrpc_session"s. */
+    struct ovs_list sessions;   /* List of "struct ovsdb_jsonrpc_session"s. */
     uint8_t dscp;
 };
 
@@ -118,7 +122,7 @@ ovsdb_jsonrpc_server_create(void)
 {
     struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
     ovsdb_server_init(&server->up);
-    server->max_sessions = 64;
+    server->max_sessions = 330;   /* Random limit. */
     shash_init(&server->remotes);
     return server;
 }
@@ -129,9 +133,36 @@ ovsdb_jsonrpc_server_create(void)
 bool
 ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
 {
+    /* The OVSDB protocol doesn't have a way to notify a client that a
+     * database has been added.  If some client tried to use the database
+     * that we're adding and failed, then forcing it to reconnect seems like
+     * a reasonable way to make it try again.
+     *
+     * If this is too big of a hammer in practice, we could be more selective,
+     * e.g. disconnect only connections that actually tried to use a database
+     * with 'db''s name. */
+    ovsdb_jsonrpc_server_reconnect(svr);
+
     return ovsdb_server_add_db(&svr->up, db);
 }
 
+/* Removes 'db' from the set of databases served out by 'svr'.  Returns
+ * true if successful, false if there is no database associated with 'db'. */
+bool
+ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
+                               struct ovsdb *db)
+{
+    /* There might be pointers to 'db' from 'svr', such as monitors or
+     * outstanding transactions.  Disconnect all JSON-RPC connections to avoid
+     * accesses to freed memory.
+     *
+     * If this is too big of a hammer in practice, we could be more selective,
+     * e.g. disconnect only connections that actually reference 'db'. */
+    ovsdb_jsonrpc_server_reconnect(svr);
+
+    return ovsdb_server_remove_db(&svr->up, db);
+}
+
 void
 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
 {
@@ -168,10 +199,16 @@ ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
     struct shash_node *node, *next;
 
     SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
-        if (!shash_find(new_remotes, node->name)) {
+        struct ovsdb_jsonrpc_remote *remote = node->data;
+        struct ovsdb_jsonrpc_options *options
+            = shash_find_data(new_remotes, node->name);
+
+        if (!options) {
             VLOG_INFO("%s: remote deconfigured", node->name);
             ovsdb_jsonrpc_server_del_remote(node);
-        }
+        } else if (options->dscp != remote->dscp) {
+            ovsdb_jsonrpc_server_del_remote(node);
+         }
     }
     SHASH_FOR_EACH (node, new_remotes) {
         const struct ovsdb_jsonrpc_options *options = node->data;
@@ -200,7 +237,7 @@ ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
 
     error = jsonrpc_pstream_open(name, &listener, options->dscp);
     if (error && error != EAFNOSUPPORT) {
-        VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
+        VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
         return NULL;
     }
 
@@ -278,20 +315,26 @@ ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
     SHASH_FOR_EACH (node, &svr->remotes) {
         struct ovsdb_jsonrpc_remote *remote = node->data;
 
-        if (remote->listener && svr->n_sessions < svr->max_sessions) {
-            struct stream *stream;
-            int error;
-
-            error = pstream_accept(remote->listener, &stream);
-            if (!error) {
-                struct jsonrpc_session *js;
-                js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
-                                                     remote->dscp);
-                ovsdb_jsonrpc_session_create(remote, js);
-            } else if (error != EAGAIN) {
-                VLOG_WARN_RL(&rl, "%s: accept failed: %s",
+        if (remote->listener) {
+            if (svr->n_sessions < svr->max_sessions) {
+                struct stream *stream;
+                int error;
+
+                error = pstream_accept(remote->listener, &stream);
+                if (!error) {
+                    struct jsonrpc_session *js;
+                    js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
+                                                         remote->dscp);
+                    ovsdb_jsonrpc_session_create(remote, js);
+                } else if (error != EAGAIN) {
+                    VLOG_WARN_RL(&rl, "%s: accept failed: %s",
+                                 pstream_get_name(remote->listener),
+                                 ovs_strerror(error));
+                }
+            } else {
+                VLOG_WARN_RL(&rl, "%s: connection exceeded maximum (%d)",
                              pstream_get_name(remote->listener),
-                             strerror(error));
+                             svr->max_sessions);
             }
         }
 
@@ -334,11 +377,9 @@ ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
 /* JSON-RPC database server session. */
 
 struct ovsdb_jsonrpc_session {
-    struct list node;           /* Element in remote's sessions list. */
+    struct ovs_list node;       /* Element in remote's sessions list. */
     struct ovsdb_session up;
     struct ovsdb_jsonrpc_remote *remote;
-    size_t backlog_threshold;   /* See ovsdb_jsonrpc_session_run(). */
-    size_t reply_backlog;
 
     /* Triggers. */
     struct hmap triggers;       /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
@@ -356,8 +397,6 @@ static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
 static void ovsdb_jsonrpc_session_get_memory_usage(
     const struct ovsdb_jsonrpc_session *, struct simap *usage);
-static void ovsdb_jsonrpc_session_set_options(
-    struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
                                              struct jsonrpc_msg *);
 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
@@ -375,8 +414,6 @@ ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
     list_push_back(&remote->sessions, &s->node);
     hmap_init(&s->triggers);
     hmap_init(&s->monitors);
-    s->reply_backlog = 0;
-    s->backlog_threshold = 1024 * 1024;
     s->js = js;
     s->js_seqno = jsonrpc_session_get_seqno(js);
 
@@ -405,8 +442,6 @@ ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
 static int
 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
 {
-    size_t backlog;
-
     jsonrpc_session_run(s->js);
     if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
         s->js_seqno = jsonrpc_session_get_seqno(s->js);
@@ -417,9 +452,12 @@ ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
 
     ovsdb_jsonrpc_trigger_complete_done(s);
 
-    backlog = jsonrpc_session_get_backlog(s->js);
-    if (!backlog) {
-        struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
+    if (!jsonrpc_session_get_backlog(s->js)) {
+        struct jsonrpc_msg *msg;
+
+        ovsdb_jsonrpc_monitor_flush_all(s);
+
+        msg = jsonrpc_session_recv(s->js);
         if (msg) {
             if (msg->type == JSONRPC_REQUEST) {
                 ovsdb_jsonrpc_session_got_request(s, msg);
@@ -433,39 +471,6 @@ ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
                 jsonrpc_msg_destroy(msg);
             }
         }
-        s->reply_backlog = jsonrpc_session_get_backlog(s->js);
-    } else if (backlog > s->reply_backlog + s->backlog_threshold) {
-        /* We have a lot of data queued to send to the client.  The data is
-         * likely to be mostly monitor updates.  It is unlikely that the
-         * monitor updates are due to transactions by 's', because we will not
-         * let 's' make any more transactions until it drains its backlog to 0
-         * (see previous 'if' case).  So the monitor updates are probably due
-         * to transactions made by database clients other than 's'.  We can't
-         * fix that by preventing 's' from executing more transactions.  We
-         * could fix it by preventing every client from executing transactions,
-         * but then one slow or hung client could prevent other clients from
-         * doing useful work.
-         *
-         * Our solution is to cap the maximum backlog to O(1) in the amount of
-         * data in the database.  If the backlog exceeds that amount, then we
-         * disconnect the client.  When it reconnects, it can fetch the entire
-         * contents of the database using less data than was previously
-         * backlogged. */
-        size_t monitor_length;
-
-        monitor_length = ovsdb_jsonrpc_monitor_json_length_all(s);
-        if (backlog > s->reply_backlog + monitor_length * 2) {
-            VLOG_INFO("%s: %zu bytes backlogged but a complete replica "
-                      "would only take %zu bytes, disconnecting",
-                      jsonrpc_session_get_name(s->js),
-                      backlog - s->reply_backlog, monitor_length);
-            jsonrpc_session_force_reconnect(s->js);
-        } else {
-            /* The backlog is not unreasonably big.  Only check again after it
-             * becomes much bigger. */
-            s->backlog_threshold = 2 * MAX(s->backlog_threshold * 2,
-                                           monitor_length);
-        }
     }
     return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
 }
@@ -497,7 +502,11 @@ ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
 {
     jsonrpc_session_wait(s->js);
     if (!jsonrpc_session_get_backlog(s->js)) {
-        jsonrpc_session_recv_wait(s->js);
+        if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
+            poll_immediate_wake();
+        } else {
+            jsonrpc_session_recv_wait(s->js);
+        }
     }
 }
 
@@ -558,7 +567,10 @@ ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
 }
 
 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
- * 'options'. */
+ * 'options'.
+ *
+ * (The dscp value can't be changed directly; the caller must instead close and
+ * re-open the session.) */
 static void
 ovsdb_jsonrpc_session_set_all_options(
     struct ovsdb_jsonrpc_remote *remote,
@@ -566,22 +578,6 @@ ovsdb_jsonrpc_session_set_all_options(
 {
     struct ovsdb_jsonrpc_session *s;
 
-    if (remote->listener) {
-        int error;
-
-        error = pstream_set_dscp(remote->listener, options->dscp);
-        if (error) {
-            VLOG_ERR("%s: set_dscp failed %s",
-                     pstream_get_name(remote->listener), strerror(error));
-        } else {
-            remote->dscp = options->dscp;
-        }
-        /*
-         * XXX race window between setting dscp to listening socket
-         * and accepting socket. Accepted socket may have old dscp value.
-         * Ignore this race window for now.
-         */
-    }
     LIST_FOR_EACH (s, node, &remote->sessions) {
         ovsdb_jsonrpc_session_set_options(s, options);
     }
@@ -679,7 +675,7 @@ ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
     return db;
 
 error:
-    *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+    *replyp = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
     ovsdb_error_destroy(error);
     return NULL;
 }
@@ -713,7 +709,7 @@ ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
 
     s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
     params = json_array_create_1(json_string_create(lock_name));
-    jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
+    ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
 }
 
 static struct jsonrpc_msg *
@@ -757,7 +753,7 @@ ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
     return jsonrpc_create_reply(result, request->id);
 
 error:
-    reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+    reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
     ovsdb_error_destroy(error);
     return reply;
 }
@@ -817,7 +813,7 @@ ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
     return jsonrpc_create_reply(json_object_create(), request->id);
 
 error:
-    reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+    reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
     ovsdb_error_destroy(error);
     return reply;
 }
@@ -847,9 +843,8 @@ ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
     } else if (!strcmp(request->method, "monitor")) {
         struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
         if (!reply) {
-            reply = jsonrpc_create_reply(
-                ovsdb_jsonrpc_monitor_create(s, db, request->params),
-                request->id);
+            reply = ovsdb_jsonrpc_monitor_create(s, db, request->params,
+                                                 request->id);
         }
     } else if (!strcmp(request->method, "monitor_cancel")) {
         reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
@@ -888,7 +883,7 @@ ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
 
     if (reply) {
         jsonrpc_msg_destroy(request);
-        jsonrpc_session_send(s->js, reply);
+        ovsdb_jsonrpc_session_send(s, reply);
     }
 }
 
@@ -916,6 +911,14 @@ ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
     }
     jsonrpc_msg_destroy(request);
 }
+
+static void
+ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
+                           struct jsonrpc_msg *msg)
+{
+    ovsdb_jsonrpc_monitor_flush_all(s);
+    jsonrpc_session_send(s->js, msg);
+}
 \f
 /* JSON-RPC database server triggers.
  *
@@ -943,7 +946,7 @@ ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
 
         msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
                                    id);
-        jsonrpc_session_send(s->js, msg);
+        ovsdb_jsonrpc_session_send(s, msg);
         json_destroy(id);
         json_destroy(params);
         return;
@@ -994,7 +997,7 @@ ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
             reply = jsonrpc_create_error(json_string_create("canceled"),
                                          t->id);
         }
-        jsonrpc_session_send(s->js, reply);
+        ovsdb_jsonrpc_session_send(s, reply);
     }
 
     json_destroy(t->id);
@@ -1023,9 +1026,9 @@ ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
     }
 }
 \f
-/* JSON-RPC database table monitors. */
+/* database table monitors. */
 
-enum ovsdb_jsonrpc_monitor_selection {
+enum ovsdb_monitor_selection {
     OJMS_INITIAL = 1 << 0,      /* All rows when monitor is created. */
     OJMS_INSERT = 1 << 1,       /* New rows. */
     OJMS_DELETE = 1 << 2,       /* Deleted rows. */
@@ -1033,44 +1036,65 @@ enum ovsdb_jsonrpc_monitor_selection {
 };
 
 /* A particular column being monitored. */
-struct ovsdb_jsonrpc_monitor_column {
+struct ovsdb_monitor_column {
     const struct ovsdb_column *column;
-    enum ovsdb_jsonrpc_monitor_selection select;
+    enum ovsdb_monitor_selection select;
+};
+
+/* A row that has changed in a monitored table. */
+struct ovsdb_monitor_row {
+    struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
+    struct uuid uuid;           /* UUID of row that changed. */
+    struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
+    struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
 };
 
 /* A particular table being monitored. */
-struct ovsdb_jsonrpc_monitor_table {
+struct ovsdb_monitor_table {
     const struct ovsdb_table *table;
 
     /* This is the union (bitwise-OR) of the 'select' values in all of the
      * members of 'columns' below. */
-    enum ovsdb_jsonrpc_monitor_selection select;
+    enum ovsdb_monitor_selection select;
 
     /* Columns being monitored. */
-    struct ovsdb_jsonrpc_monitor_column *columns;
+    struct ovsdb_monitor_column *columns;
     size_t n_columns;
+
+    /* Contains 'struct ovsdb_monitor_row's for rows that have been
+     * updated but not yet flushed to the jsonrpc connection. */
+    struct hmap changes;
 };
 
+struct ovsdb_jsonrpc_monitor;
+/*  Backend monitor.
+ *
+ *  ovsdb_monitor keep track of the ovsdb changes.
+ */
 /* A collection of tables being monitored. */
-struct ovsdb_jsonrpc_monitor {
+struct ovsdb_monitor {
     struct ovsdb_replica replica;
+    struct shash tables;     /* Holds "struct ovsdb_monitor_table"s. */
+    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
+};
+
+/* Jsonrpc front end monitor. */
+struct ovsdb_jsonrpc_monitor {
     struct ovsdb_jsonrpc_session *session;
     struct ovsdb *db;
     struct hmap_node node;      /* In ovsdb_jsonrpc_session's "monitors". */
 
     struct json *monitor_id;
-    struct shash tables;     /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
+    struct ovsdb_monitor *dbmon;
 };
 
 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
 
 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
     struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
-static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
+static void ovsdb_monitor_destroy(struct ovsdb_replica *);
 static struct json *ovsdb_jsonrpc_monitor_get_initial(
     const struct ovsdb_jsonrpc_monitor *);
-static size_t ovsdb_jsonrpc_monitor_json_length(
-    const struct ovsdb_jsonrpc_monitor *);
 
 static bool
 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
@@ -1097,12 +1121,12 @@ ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
 }
 
 static void
-ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
-                                 const struct ovsdb_column *column,
-                                 enum ovsdb_jsonrpc_monitor_selection select,
-                                 size_t *allocated_columns)
+ovsdb_add_monitor_column(struct ovsdb_monitor_table *mt,
+                         const struct ovsdb_column *column,
+                         enum ovsdb_monitor_selection select,
+                         size_t *allocated_columns)
 {
-    struct ovsdb_jsonrpc_monitor_column *c;
+    struct ovsdb_monitor_column *c;
 
     if (mt->n_columns >= *allocated_columns) {
         mt->columns = x2nrealloc(mt->columns, allocated_columns,
@@ -1115,24 +1139,33 @@ ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
 }
 
 static int
-compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
+compare_ovsdb_monitor_column(const void *a_, const void *b_)
 {
-    const struct ovsdb_jsonrpc_monitor_column *a = a_;
-    const struct ovsdb_jsonrpc_monitor_column *b = b_;
+    const struct ovsdb_monitor_column *a = a_;
+    const struct ovsdb_monitor_column *b = b_;
 
     return a->column < b->column ? -1 : a->column > b->column;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
-ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
+static void
+ovsdb_monitor_add_select(struct ovsdb_monitor_table *mt,
+                         enum ovsdb_monitor_selection select)
+{
+    mt->select |= select;
+}
+
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_monitor *dbmon,
+                                    const struct ovsdb_table *table,
                                     const struct json *monitor_request,
                                     size_t *allocated_columns)
 {
-    const struct ovsdb_table_schema *ts = mt->table->schema;
-    enum ovsdb_jsonrpc_monitor_selection select;
+    const struct ovsdb_table_schema *ts = table->schema;
+    enum ovsdb_monitor_selection select;
     const struct json *columns, *select_json;
     struct ovsdb_parser parser;
     struct ovsdb_error *error;
+    struct ovsdb_monitor_table *mt;
 
     ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
     columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
@@ -1165,8 +1198,9 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
     } else {
         select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
     }
-    mt->select |= select;
 
+    mt = shash_find_data(&dbmon->tables, table->schema->name);
+    ovsdb_monitor_add_select(mt, select);
     if (columns) {
         size_t i;
 
@@ -1190,8 +1224,7 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
                 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
                                           "column name", s);
             }
-            ovsdb_jsonrpc_add_monitor_column(mt, column, select,
-                                             allocated_columns);
+            ovsdb_add_monitor_column(mt, column, select, allocated_columns);
         }
     } else {
         struct shash_node *node;
@@ -1199,8 +1232,8 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
         SHASH_FOR_EACH (node, &ts->columns) {
             const struct ovsdb_column *column = node->data;
             if (column->index != OVSDB_COL_UUID) {
-                ovsdb_jsonrpc_add_monitor_column(mt, column, select,
-                                                 allocated_columns);
+                ovsdb_add_monitor_column(mt, column, select,
+                                         allocated_columns);
             }
         }
     }
@@ -1208,9 +1241,65 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
     return NULL;
 }
 
-static struct json *
+static struct ovsdb_monitor *
+ovsdb_monitor_create(struct ovsdb *db,
+                     struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
+                     const struct ovsdb_replica_class *replica_class)
+{
+    struct ovsdb_monitor *m;
+
+    m = xzalloc(sizeof *m);
+
+    ovsdb_replica_init(&m->replica, replica_class);
+    ovsdb_add_replica(db, &m->replica);
+    m->jsonrpc_monitor = jsonrpc_monitor;
+    shash_init(&m->tables);
+
+    return m;
+}
+
+static void
+ovsdb_monitor_add_table(struct ovsdb_monitor *m,
+                        const struct ovsdb_table *table)
+{
+    struct ovsdb_monitor_table *mt;
+
+    mt = xzalloc(sizeof *mt);
+    mt->table = table;
+    hmap_init(&mt->changes);
+    shash_add(&m->tables, table->schema->name, mt);
+}
+
+/* Check for duplicated column names. Return the first
+ * duplicated column's name if found. Otherwise return
+ * NULL.  */
+static const char * OVS_WARN_UNUSED_RESULT
+ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
+                          const struct ovsdb_table *table)
+{
+    struct ovsdb_monitor_table *mt;
+    int i;
+
+    mt = shash_find_data(&m->tables, table->schema->name);
+
+    if (mt) {
+        /* Check for duplicate columns. */
+        qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
+              compare_ovsdb_monitor_column);
+        for (i = 1; i < mt->n_columns; i++) {
+            if (mt->columns[i].column == mt->columns[i - 1].column) {
+                return mt->columns[i].column->name;
+            }
+        }
+    }
+
+    return NULL;
+}
+
+static struct jsonrpc_msg *
 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
-                             struct json *params)
+                             struct json *params,
+                             const struct json *request_id)
 {
     struct ovsdb_jsonrpc_monitor *m = NULL;
     struct json *monitor_id, *monitor_requests;
@@ -1236,17 +1325,15 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
     }
 
     m = xzalloc(sizeof *m);
-    ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
-    ovsdb_add_replica(db, &m->replica);
     m->session = s;
     m->db = db;
+    m->dbmon = ovsdb_monitor_create(db, m, &ovsdb_jsonrpc_replica_class);
     hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
     m->monitor_id = json_clone(monitor_id);
-    shash_init(&m->tables);
 
     SHASH_FOR_EACH (node, json_object(monitor_requests)) {
         const struct ovsdb_table *table;
-        struct ovsdb_jsonrpc_monitor_table *mt;
+        const char *column_name;
         size_t allocated_columns;
         const struct json *mr_value;
         size_t i;
@@ -1258,9 +1345,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
             goto error;
         }
 
-        mt = xzalloc(sizeof *mt);
-        mt->table = table;
-        shash_add(&m->tables, table->schema->name, mt);
+        ovsdb_monitor_add_table(m->dbmon, table);
 
         /* Parse columns. */
         mr_value = node->data;
@@ -1270,42 +1355,40 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
 
             for (i = 0; i < array->n; i++) {
                 error = ovsdb_jsonrpc_parse_monitor_request(
-                    mt, array->elems[i], &allocated_columns);
+                    m->dbmon, table, array->elems[i], &allocated_columns);
                 if (error) {
                     goto error;
                 }
             }
         } else {
             error = ovsdb_jsonrpc_parse_monitor_request(
-                mt, mr_value, &allocated_columns);
+                m->dbmon, table, mr_value, &allocated_columns);
             if (error) {
                 goto error;
             }
         }
 
-        /* Check for duplicate columns. */
-        qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
-              compare_ovsdb_jsonrpc_monitor_column);
-        for (i = 1; i < mt->n_columns; i++) {
-            if (mt->columns[i].column == mt->columns[i - 1].column) {
-                error = ovsdb_syntax_error(mr_value, NULL, "column %s "
-                                           "mentioned more than once",
-                                           mt->columns[i].column->name);
-                goto error;
-            }
+        column_name = ovsdb_monitor_table_check_duplicates(m->dbmon, table);
+
+        if (column_name) {
+            error = ovsdb_syntax_error(mr_value, NULL, "column %s "
+                                       "mentioned more than once",
+                                        column_name);
+            goto error;
         }
     }
 
-    return ovsdb_jsonrpc_monitor_get_initial(m);
+    return jsonrpc_create_reply(ovsdb_jsonrpc_monitor_get_initial(m),
+                                request_id);
 
 error:
     if (m) {
-        ovsdb_remove_replica(m->db, &m->replica);
+        ovsdb_remove_replica(m->db, &m->dbmon->replica);
     }
 
     json = ovsdb_error_to_json(error);
     ovsdb_error_destroy(error);
-    return json;
+    return jsonrpc_create_error(json, request_id);
 }
 
 static struct jsonrpc_msg *
@@ -1324,7 +1407,7 @@ ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
             return jsonrpc_create_error(json_string_create("unknown monitor"),
                                         request_id);
         } else {
-            ovsdb_remove_replica(m->db, &m->replica);
+            ovsdb_remove_replica(m->db, &m->dbmon->replica);
             return jsonrpc_create_reply(json_object_create(), request_id);
         }
     }
@@ -1336,238 +1419,371 @@ ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
     struct ovsdb_jsonrpc_monitor *m, *next;
 
     HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
-        ovsdb_remove_replica(m->db, &m->replica);
+        ovsdb_remove_replica(m->db, &m->dbmon->replica);
     }
 }
 
-/* Returns an overestimate of the number of bytes of JSON data required to
- * report the current contents of the database over all the monitors currently
- * configured in 's'.  */
-static size_t
-ovsdb_jsonrpc_monitor_json_length_all(struct ovsdb_jsonrpc_session *s)
+static struct ovsdb_monitor *
+ovsdb_monitor_cast(struct ovsdb_replica *replica)
 {
-    struct ovsdb_jsonrpc_monitor *m;
-    size_t length;
+    ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
+    return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
+}
 
-    length = 0;
-    HMAP_FOR_EACH (m, node, &s->monitors) {
-        length += ovsdb_jsonrpc_monitor_json_length(m);
+struct ovsdb_monitor_aux {
+    const struct ovsdb_monitor *monitor;
+    struct ovsdb_monitor_table *mt;
+};
+
+/* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
+ * given 'uuid', or NULL if there is no such row. */
+static struct ovsdb_monitor_row *
+ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
+                       const struct uuid *uuid)
+{
+    struct ovsdb_monitor_row *row;
+
+    HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
+        if (uuid_equals(uuid, &row->uuid)) {
+            return row;
+        }
     }
-    return length;
+    return NULL;
 }
 
-static struct ovsdb_jsonrpc_monitor *
-ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
+/* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
+ * copies of the data in 'row' drawn from the columns represented by
+ * mt->columns[].  Returns the array.
+ *
+ * If 'row' is NULL, returns NULL. */
+static struct ovsdb_datum *
+clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
+                       const struct ovsdb_row *row)
 {
-    ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
-    return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
-}
+    struct ovsdb_datum *data;
+    size_t i;
 
-struct ovsdb_jsonrpc_monitor_aux {
-    bool initial;               /* Sending initial contents of table? */
-    const struct ovsdb_jsonrpc_monitor *monitor;
-    struct json *json;          /* JSON for the whole transaction. */
+    if (!row) {
+        return NULL;
+    }
 
-    /* Current table.  */
-    struct ovsdb_jsonrpc_monitor_table *mt;
-    struct json *table_json;    /* JSON for table's transaction. */
-};
+    data = xmalloc(mt->n_columns * sizeof *data);
+    for (i = 0; i < mt->n_columns; i++) {
+        const struct ovsdb_column *c = mt->columns[i].column;
+        const struct ovsdb_datum *src = &row->fields[c->index];
+        struct ovsdb_datum *dst = &data[i];
+        const struct ovsdb_type *type = &c->type;
 
-static bool
-any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
-                      const unsigned long int *changed)
+        ovsdb_datum_clone(dst, src, type);
+    }
+    return data;
+}
+
+/* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
+ * in 'row' drawn from the columns represented by mt->columns[]. */
+static void
+update_monitor_row_data(const struct ovsdb_monitor_table *mt,
+                        const struct ovsdb_row *row,
+                        struct ovsdb_datum *data)
 {
     size_t i;
 
     for (i = 0; i < mt->n_columns; i++) {
-        const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
-        unsigned int idx = c->column->index;
+        const struct ovsdb_column *c = mt->columns[i].column;
+        const struct ovsdb_datum *src = &row->fields[c->index];
+        struct ovsdb_datum *dst = &data[i];
+        const struct ovsdb_type *type = &c->type;
+
+        if (!ovsdb_datum_equals(src, dst, type)) {
+            ovsdb_datum_destroy(dst, type);
+            ovsdb_datum_clone(dst, src, type);
+        }
+    }
+}
 
-        if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
-            return true;
+/* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
+ * from mt->columns[], plus 'data' itself. */
+static void
+free_monitor_row_data(const struct ovsdb_monitor_table *mt,
+                      struct ovsdb_datum *data)
+{
+    if (data) {
+        size_t i;
+
+        for (i = 0; i < mt->n_columns; i++) {
+            const struct ovsdb_column *c = mt->columns[i].column;
+
+            ovsdb_datum_destroy(&data[i], &c->type);
         }
+        free(data);
     }
+}
 
-    return false;
+/* Frees 'row', which must have been created from 'mt'. */
+static void
+ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
+                          struct ovsdb_monitor_row *row)
+{
+    if (row) {
+        free_monitor_row_data(mt, row->old);
+        free_monitor_row_data(mt, row->new);
+        free(row);
+    }
 }
 
 static bool
-ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
-                                const struct ovsdb_row *new,
-                                const unsigned long int *changed,
-                                void *aux_)
+ovsdb_monitor_change_cb(const struct ovsdb_row *old,
+                        const struct ovsdb_row *new,
+                        const unsigned long int *changed OVS_UNUSED,
+                        void *aux_)
 {
-    struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
-    const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
+    struct ovsdb_monitor_aux *aux = aux_;
+    const struct ovsdb_monitor *m = aux->monitor;
     struct ovsdb_table *table = new ? new->table : old->table;
-    enum ovsdb_jsonrpc_monitor_selection type;
-    struct json *old_json, *new_json;
-    struct json *row_json;
-    char uuid[UUID_LEN + 1];
-    size_t i;
+    const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
+    struct ovsdb_monitor_row *change;
+    struct ovsdb_monitor_table *mt;
 
     if (!aux->mt || table != aux->mt->table) {
         aux->mt = shash_find_data(&m->tables, table->schema->name);
-        aux->table_json = NULL;
         if (!aux->mt) {
             /* We don't care about rows in this table at all.  Tell the caller
              * to skip it.  */
             return false;
         }
     }
+    mt = aux->mt;
 
-    type = (aux->initial ? OJMS_INITIAL
-            : !old ? OJMS_INSERT
-            : !new ? OJMS_DELETE
+    change = ovsdb_monitor_row_find(mt, uuid);
+    if (!change) {
+        change = xmalloc(sizeof *change);
+        hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
+        change->uuid = *uuid;
+        change->old = clone_monitor_row_data(mt, old);
+        change->new = clone_monitor_row_data(mt, new);
+    } else {
+        if (new) {
+            update_monitor_row_data(mt, new, change->new);
+        } else {
+            free_monitor_row_data(mt, change->new);
+            change->new = NULL;
+
+            if (!change->old) {
+                /* This row was added then deleted.  Forget about it. */
+                hmap_remove(&mt->changes, &change->hmap_node);
+                free(change);
+            }
+        }
+    }
+    return true;
+}
+
+/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
+ * 'mt', or NULL if no row update should be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification.
+ *
+ * 'changed' must be a scratch buffer for internal use that is at least
+ * bitmap_n_bytes(mt->n_columns) bytes long. */
+static struct json *
+ovsdb_monitor_compose_row_update(
+    const struct ovsdb_monitor_table *mt,
+    const struct ovsdb_monitor_row *row,
+    bool initial, unsigned long int *changed)
+{
+    enum ovsdb_monitor_selection type;
+    struct json *old_json, *new_json;
+    struct json *row_json;
+    size_t i;
+
+    type = (initial ? OJMS_INITIAL
+            : !row->old ? OJMS_INSERT
+            : !row->new ? OJMS_DELETE
             : OJMS_MODIFY);
-    if (!(aux->mt->select & type)) {
-        /* We don't care about this type of change (but do want to be called
-         * back for changes to other rows in the same table). */
-        return true;
+    if (!(mt->select & type)) {
+        return NULL;
     }
 
-    if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
-        /* Nothing of interest changed. */
-        return true;
+    if (type == OJMS_MODIFY) {
+        size_t n_changes;
+
+        n_changes = 0;
+        memset(changed, 0, bitmap_n_bytes(mt->n_columns));
+        for (i = 0; i < mt->n_columns; i++) {
+            const struct ovsdb_column *c = mt->columns[i].column;
+            if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
+                bitmap_set1(changed, i);
+                n_changes++;
+            }
+        }
+        if (!n_changes) {
+            /* No actual changes: presumably a row changed and then
+             * changed back later. */
+            return NULL;
+        }
     }
 
+    row_json = json_object_create();
     old_json = new_json = NULL;
     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
         old_json = json_object_create();
+        json_object_put(row_json, "old", old_json);
     }
     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
         new_json = json_object_create();
+        json_object_put(row_json, "new", new_json);
     }
-    for (i = 0; i < aux->mt->n_columns; i++) {
-        const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
-        const struct ovsdb_column *column = c->column;
-        unsigned int idx = c->column->index;
+    for (i = 0; i < mt->n_columns; i++) {
+        const struct ovsdb_monitor_column *c = &mt->columns[i];
 
         if (!(type & c->select)) {
-            /* We don't care about this type of change for this particular
-             * column (but we will care about it for some other column). */
+            /* We don't care about this type of change for this
+             * particular column (but we will care about it for some
+             * other column). */
             continue;
         }
 
-        if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
+        if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
             || type == OJMS_DELETE) {
-            json_object_put(old_json, column->name,
-                            ovsdb_datum_to_json(&old->fields[idx],
-                                                &column->type));
+            json_object_put(old_json, c->column->name,
+                            ovsdb_datum_to_json(&row->old[i],
+                                                &c->column->type));
         }
         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
-            json_object_put(new_json, column->name,
-                            ovsdb_datum_to_json(&new->fields[idx],
-                                                &column->type));
+            json_object_put(new_json, c->column->name,
+                            ovsdb_datum_to_json(&row->new[i],
+                                                &c->column->type));
         }
     }
 
-    /* Create JSON object for transaction overall. */
-    if (!aux->json) {
-        aux->json = json_object_create();
-    }
+    return row_json;
+}
 
-    /* Create JSON object for transaction on this table. */
-    if (!aux->table_json) {
-        aux->table_json = json_object_create();
-        json_object_put(aux->json, aux->mt->table->schema->name,
-                        aux->table_json);
-    }
+/* Constructs and returns JSON for a <table-updates> object (as described in
+ * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
+ * the outstanding changes from 'monitor'.  Returns NULL if no update needs to
+ * be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification. */
+static struct json *
+ovsdb_jsonrpc_monitor_compose_table_update(
+    const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
+{
+    struct shash_node *node;
+    unsigned long int *changed;
+    struct json *json;
+    size_t max_columns;
 
-    /* Create JSON object for transaction on this row. */
-    row_json = json_object_create();
-    if (old_json) {
-        json_object_put(row_json, "old", old_json);
+    max_columns = 0;
+    SHASH_FOR_EACH (node, &monitor->dbmon->tables) {
+        struct ovsdb_monitor_table *mt = node->data;
+
+        max_columns = MAX(max_columns, mt->n_columns);
     }
-    if (new_json) {
-        json_object_put(row_json, "new", new_json);
+    changed = xmalloc(bitmap_n_bytes(max_columns));
+
+    json = NULL;
+    SHASH_FOR_EACH (node, &monitor->dbmon->tables) {
+        struct ovsdb_monitor_table *mt = node->data;
+        struct ovsdb_monitor_row *row, *next;
+        struct json *table_json = NULL;
+
+        HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+            struct json *row_json;
+
+            row_json = ovsdb_monitor_compose_row_update(
+                mt, row, initial, changed);
+            if (row_json) {
+                char uuid[UUID_LEN + 1];
+
+                /* Create JSON object for transaction overall. */
+                if (!json) {
+                    json = json_object_create();
+                }
+
+                /* Create JSON object for transaction on this table. */
+                if (!table_json) {
+                    table_json = json_object_create();
+                    json_object_put(json, mt->table->schema->name, table_json);
+                }
+
+                /* Add JSON row to JSON table. */
+                snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
+                json_object_put(table_json, uuid, row_json);
+            }
+
+            hmap_remove(&mt->changes, &row->hmap_node);
+            ovsdb_monitor_row_destroy(mt, row);
+        }
     }
 
-    /* Add JSON row to JSON table. */
-    snprintf(uuid, sizeof uuid,
-             UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
-    json_object_put(aux->table_json, uuid, row_json);
+    free(changed);
 
-    return true;
+    return json;
 }
 
-/* Returns an overestimate of the number of bytes of JSON data required to
- * report the current contents of the database over monitor 'm'. */
-static size_t
-ovsdb_jsonrpc_monitor_json_length(const struct ovsdb_jsonrpc_monitor *m)
+static bool
+ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
 {
-    const struct shash_node *node;
-    size_t length;
+    struct ovsdb_jsonrpc_monitor *m;
 
-    /* Top-level overhead of monitor JSON. */
-    length = 256;
+    HMAP_FOR_EACH (m, node, &s->monitors) {
+        struct shash_node *node;
 
-    SHASH_FOR_EACH (node, &m->tables) {
-        const struct ovsdb_jsonrpc_monitor_table *mt = node->data;
-        const struct ovsdb_table *table = mt->table;
-        const struct ovsdb_row *row;
-        size_t i;
+        SHASH_FOR_EACH (node, &m->dbmon->tables) {
+            struct ovsdb_monitor_table *mt = node->data;
 
-        /* Per-table JSON overhead: "<table>":{...}. */
-        length += strlen(table->schema->name) + 32;
+            if (!hmap_is_empty(&mt->changes)) {
+                return true;
+            }
+        }
+    }
 
-        /* Per-row JSON overhead: ,"<uuid>":{"old":{...},"new":{...}} */
-        length += hmap_count(&table->rows) * (UUID_LEN + 32);
+    return false;
+}
 
-        /* Per-row, per-column JSON overhead: ,"<column>": */
-        for (i = 0; i < mt->n_columns; i++) {
-            const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
-            const struct ovsdb_column *column = c->column;
+static void
+ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
+{
+    struct ovsdb_jsonrpc_monitor *m;
 
-            length += hmap_count(&table->rows) * (8 + strlen(column->name));
-        }
+    HMAP_FOR_EACH (m, node, &s->monitors) {
+        struct json *json;
 
-        /* Data. */
-        HMAP_FOR_EACH (row, hmap_node, &table->rows) {
-            for (i = 0; i < mt->n_columns; i++) {
-                const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
-                const struct ovsdb_column *column = c->column;
+        json = ovsdb_jsonrpc_monitor_compose_table_update(m, false);
+        if (json) {
+            struct jsonrpc_msg *msg;
+            struct json *params;
 
-                length += ovsdb_datum_json_length(&row->fields[column->index],
-                                                  &column->type);
-            }
+            params = json_array_create_2(json_clone(m->monitor_id), json);
+            msg = jsonrpc_create_notify("update", params);
+            jsonrpc_session_send(s->js, msg);
         }
     }
-
-    return length;
 }
 
 static void
-ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
-                               const struct ovsdb_jsonrpc_monitor *m,
-                               bool initial)
+ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
+                       const struct ovsdb_monitor *m)
 {
-    aux->initial = initial;
     aux->monitor = m;
-    aux->json = NULL;
     aux->mt = NULL;
-    aux->table_json = NULL;
 }
 
 static struct ovsdb_error *
-ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
-                             const struct ovsdb_txn *txn,
-                             bool durable OVS_UNUSED)
+ovsdb_monitor_commit(struct ovsdb_replica *replica,
+                     const struct ovsdb_txn *txn,
+                     bool durable OVS_UNUSED)
 {
-    struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
-    struct ovsdb_jsonrpc_monitor_aux aux;
+    struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
+    struct ovsdb_monitor_aux aux;
 
-    ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
-    ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
-    if (aux.json) {
-        struct jsonrpc_msg *msg;
-        struct json *params;
-
-        params = json_array_create_2(json_clone(aux.monitor->monitor_id),
-                                     aux.json);
-        msg = jsonrpc_create_notify("update", params);
-        jsonrpc_session_send(aux.monitor->session->js, msg);
-    }
+    ovsdb_monitor_init_aux(&aux, m);
+    ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
 
     return NULL;
 }
@@ -1575,42 +1791,54 @@ ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
 static struct json *
 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
 {
-    struct ovsdb_jsonrpc_monitor_aux aux;
+    struct ovsdb_monitor_aux aux;
     struct shash_node *node;
+    struct json *json;
 
-    ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
-    SHASH_FOR_EACH (node, &m->tables) {
-        struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+    ovsdb_monitor_init_aux(&aux, m->dbmon);
+    SHASH_FOR_EACH (node, &m->dbmon->tables) {
+        struct ovsdb_monitor_table *mt = node->data;
 
         if (mt->select & OJMS_INITIAL) {
             struct ovsdb_row *row;
 
             HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
-                ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
+                ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
             }
         }
     }
-    return aux.json ? aux.json : json_object_create();
+    json = ovsdb_jsonrpc_monitor_compose_table_update(m, true);
+    return json ? json : json_object_create();
 }
 
 static void
-ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
+ovsdb_monitor_destroy(struct ovsdb_replica *replica)
 {
-    struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
+    struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
+    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor = m->jsonrpc_monitor;
     struct shash_node *node;
 
-    json_destroy(m->monitor_id);
+    json_destroy(jsonrpc_monitor->monitor_id);
     SHASH_FOR_EACH (node, &m->tables) {
-        struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+        struct ovsdb_monitor_table *mt = node->data;
+        struct ovsdb_monitor_row *row, *next;
+
+        HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+            hmap_remove(&mt->changes, &row->hmap_node);
+            ovsdb_monitor_row_destroy(mt, row);
+        }
+        hmap_destroy(&mt->changes);
+
         free(mt->columns);
         free(mt);
     }
     shash_destroy(&m->tables);
-    hmap_remove(&m->session->monitors, &m->node);
+    hmap_remove(&jsonrpc_monitor->session->monitors, &jsonrpc_monitor->node);
+    free(jsonrpc_monitor);
     free(m);
 }
 
 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
-    ovsdb_jsonrpc_monitor_commit,
-    ovsdb_jsonrpc_monitor_destroy
+    ovsdb_monitor_commit,
+    ovsdb_monitor_destroy
 };