2 * Copyright (c) 2015, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/json.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
30 #include "condition.h"
36 #include "transaction.h"
37 #include "jsonrpc-server.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(ovsdb_monitor
);
44 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
;
45 static struct hmap ovsdb_monitors
= HMAP_INITIALIZER(&ovsdb_monitors
);
47 /* Keep state of session's conditions */
48 struct ovsdb_monitor_session_condition
{
49 bool conditional
; /* True iff every table's condition is true. */
50 struct shash tables
; /* Contains
51 * "struct ovsdb_monitor_table_condition *"s. */
54 /* Monitored table session's conditions */
55 struct ovsdb_monitor_table_condition
{
56 const struct ovsdb_table
*table
;
57 struct ovsdb_monitor_table
*mt
;
58 struct ovsdb_condition old_condition
;
59 struct ovsdb_condition new_condition
;
64 * ovsdb_monitor keep track of the ovsdb changes.
67 /* A collection of tables being monitored. */
68 struct ovsdb_monitor
{
69 struct ovsdb_replica replica
;
70 struct shash tables
; /* Holds "struct ovsdb_monitor_table"s. */
71 struct ovs_list jsonrpc_monitors
; /* Contains "jsonrpc_monitor_node"s. */
73 uint64_t n_transactions
; /* Count number of committed transactions. */
74 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
75 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
78 /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
80 struct ovsdb_monitor_json_cache_node
{
81 struct hmap_node hmap_node
; /* Elements in json cache. */
82 enum ovsdb_monitor_version version
;
84 struct json
*json
; /* Null, or a cloned of json */
87 struct jsonrpc_monitor_node
{
89 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
92 /* A particular column being monitored. */
93 struct ovsdb_monitor_column
{
94 const struct ovsdb_column
*column
;
95 enum ovsdb_monitor_selection select
;
99 /* A row that has changed in a monitored table. */
100 struct ovsdb_monitor_row
{
101 struct hmap_node hmap_node
; /* In ovsdb_jsonrpc_monitor_table.changes. */
102 struct uuid uuid
; /* UUID of row that changed. */
103 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
104 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
107 /* Contains 'struct ovsdb_monitor_row's for rows that have been
108 * updated but not yet flushed to all the jsonrpc connection.
110 * 'n_refs' represent the number of jsonrpc connections that have
111 * not received updates. Generate the update for the last jsonprc
112 * connection will also destroy the whole "struct ovsdb_monitor_changes"
115 * 'transaction' stores the first update's transaction id.
117 struct ovsdb_monitor_changes
{
118 struct hmap_node hmap_node
; /* Element in ovsdb_monitor_tables' changes
120 struct ovsdb_monitor_table
*mt
;
123 uint64_t transaction
;
126 /* A particular table being monitored. */
127 struct ovsdb_monitor_table
{
128 const struct ovsdb_table
*table
;
130 /* This is the union (bitwise-OR) of the 'select' values in all of the
131 * members of 'columns' below. */
132 enum ovsdb_monitor_selection select
;
134 /* Columns being monitored. */
135 struct ovsdb_monitor_column
*columns
;
137 size_t n_monitored_columns
;
138 size_t allocated_columns
;
140 /* Columns in ovsdb_monitor_row have different indexes then in
141 * ovsdb_row. This field maps between column->index to the index in the
142 * ovsdb_monitor_row. It is used for condition evaluation. */
143 unsigned int *columns_index_map
;
145 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
149 enum ovsdb_monitor_row_type
{
154 typedef struct json
*
155 (*compose_row_update_cb_func
)
156 (const struct ovsdb_monitor_table
*mt
,
157 const struct ovsdb_monitor_session_condition
* condition
,
158 enum ovsdb_monitor_row_type row_type
,
160 bool initial
, unsigned long int *changed
);
162 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
);
163 static struct ovsdb_monitor_changes
* ovsdb_monitor_table_add_changes(
164 struct ovsdb_monitor_table
*mt
, uint64_t next_txn
);
165 static struct ovsdb_monitor_changes
*ovsdb_monitor_table_find_changes(
166 struct ovsdb_monitor_table
*mt
, uint64_t unflushed
);
167 static void ovsdb_monitor_changes_destroy(
168 struct ovsdb_monitor_changes
*changes
);
169 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
173 json_cache_hash(enum ovsdb_monitor_version version
, uint64_t from_txn
)
177 hash
= hash_uint64(version
);
178 hash
= hash_uint64_basis(from_txn
, hash
);
183 static struct ovsdb_monitor_json_cache_node
*
184 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
185 enum ovsdb_monitor_version version
,
188 struct ovsdb_monitor_json_cache_node
*node
;
189 uint32_t hash
= json_cache_hash(version
, from_txn
);
191 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
192 if (node
->from_txn
== from_txn
&& node
->version
== version
) {
201 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
202 enum ovsdb_monitor_version version
,
203 uint64_t from_txn
, struct json
*json
)
205 struct ovsdb_monitor_json_cache_node
*node
;
206 uint32_t hash
= json_cache_hash(version
, from_txn
);
208 node
= xmalloc(sizeof *node
);
210 node
->version
= version
;
211 node
->from_txn
= from_txn
;
212 node
->json
= json
? json_clone(json
) : NULL
;
214 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
218 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
220 struct ovsdb_monitor_json_cache_node
*node
;
222 HMAP_FOR_EACH_POP(node
, hmap_node
, &dbmon
->json_cache
) {
223 json_destroy(node
->json
);
229 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
231 const struct ovsdb_monitor_column
*a
= a_
;
232 const struct ovsdb_monitor_column
*b
= b_
;
234 /* put all monitored columns at the begining */
235 if (a
->monitored
!= b
->monitored
) {
236 return a
->monitored
? -1 : 1;
239 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
242 static struct ovsdb_monitor
*
243 ovsdb_monitor_cast(struct ovsdb_replica
*replica
)
245 ovs_assert(replica
->class == &ovsdb_jsonrpc_replica_class
);
246 return CONTAINER_OF(replica
, struct ovsdb_monitor
, replica
);
249 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
250 * given 'uuid', or NULL if there is no such row. */
251 static struct ovsdb_monitor_row
*
252 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes
*changes
,
253 const struct uuid
*uuid
)
255 struct ovsdb_monitor_row
*row
;
257 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
259 if (uuid_equals(uuid
, &row
->uuid
)) {
266 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
267 * copies of the data in 'row' drawn from the columns represented by
268 * mt->columns[]. Returns the array.
270 * If 'row' is NULL, returns NULL. */
271 static struct ovsdb_datum
*
272 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
273 const struct ovsdb_row
*row
)
275 struct ovsdb_datum
*data
;
282 data
= xmalloc(mt
->n_columns
* sizeof *data
);
283 for (i
= 0; i
< mt
->n_columns
; i
++) {
284 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
285 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
286 struct ovsdb_datum
*dst
= &data
[i
];
287 const struct ovsdb_type
*type
= &c
->type
;
289 ovsdb_datum_clone(dst
, src
, type
);
294 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
295 * in 'row' drawn from the columns represented by mt->columns[]. */
297 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
298 const struct ovsdb_row
*row
,
299 struct ovsdb_datum
*data
)
303 for (i
= 0; i
< mt
->n_columns
; i
++) {
304 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
305 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
306 struct ovsdb_datum
*dst
= &data
[i
];
307 const struct ovsdb_type
*type
= &c
->type
;
309 if (!ovsdb_datum_equals(src
, dst
, type
)) {
310 ovsdb_datum_destroy(dst
, type
);
311 ovsdb_datum_clone(dst
, src
, type
);
316 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
317 * from mt->columns[], plus 'data' itself. */
319 free_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
320 struct ovsdb_datum
*data
)
325 for (i
= 0; i
< mt
->n_columns
; i
++) {
326 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
328 ovsdb_datum_destroy(&data
[i
], &c
->type
);
334 /* Frees 'row', which must have been created from 'mt'. */
336 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
337 struct ovsdb_monitor_row
*row
)
340 free_monitor_row_data(mt
, row
->old
);
341 free_monitor_row_data(mt
, row
->new);
347 ovsdb_monitor_columns_sort(struct ovsdb_monitor
*dbmon
)
350 struct shash_node
*node
;
352 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
353 struct ovsdb_monitor_table
*mt
= node
->data
;
355 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
356 compare_ovsdb_monitor_column
);
357 for (i
= 0; i
< mt
->n_columns
; i
++) {
358 /* re-set index map due to sort */
359 mt
->columns_index_map
[mt
->columns
[i
].column
->index
] = i
;
365 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
366 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
368 struct jsonrpc_monitor_node
*jm
;
370 jm
= xzalloc(sizeof *jm
);
371 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
372 ovs_list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
375 struct ovsdb_monitor
*
376 ovsdb_monitor_create(struct ovsdb
*db
,
377 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
379 struct ovsdb_monitor
*dbmon
;
381 dbmon
= xzalloc(sizeof *dbmon
);
383 ovsdb_replica_init(&dbmon
->replica
, &ovsdb_jsonrpc_replica_class
);
384 ovsdb_add_replica(db
, &dbmon
->replica
);
385 ovs_list_init(&dbmon
->jsonrpc_monitors
);
387 dbmon
->n_transactions
= 0;
388 shash_init(&dbmon
->tables
);
389 hmap_node_nullify(&dbmon
->hmap_node
);
390 hmap_init(&dbmon
->json_cache
);
392 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
397 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
398 const struct ovsdb_table
*table
)
400 struct ovsdb_monitor_table
*mt
;
402 size_t n_columns
= shash_count(&table
->schema
->columns
);
404 mt
= xzalloc(sizeof *mt
);
406 shash_add(&m
->tables
, table
->schema
->name
, mt
);
407 hmap_init(&mt
->changes
);
408 mt
->columns_index_map
=
409 xmalloc(sizeof *mt
->columns_index_map
* n_columns
);
410 for (i
= 0; i
< n_columns
; i
++) {
411 mt
->columns_index_map
[i
] = -1;
416 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
417 const struct ovsdb_table
*table
,
418 const struct ovsdb_column
*column
,
419 enum ovsdb_monitor_selection select
,
422 struct ovsdb_monitor_table
*mt
;
423 struct ovsdb_monitor_column
*c
;
425 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
427 /* Check for column duplication. Return duplicated column name. */
428 if (mt
->columns_index_map
[column
->index
] != -1) {
432 if (mt
->n_columns
>= mt
->allocated_columns
) {
433 mt
->columns
= x2nrealloc(mt
->columns
, &mt
->allocated_columns
,
434 sizeof *mt
->columns
);
437 mt
->select
|= select
;
438 mt
->columns_index_map
[column
->index
] = mt
->n_columns
;
439 c
= &mt
->columns
[mt
->n_columns
++];
442 c
->monitored
= monitored
;
444 mt
->n_monitored_columns
++;
451 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor
*dbmon
,
452 const struct ovsdb_table
*table
,
453 struct ovsdb_condition
*condition
)
457 const struct ovsdb_column
**columns
=
458 ovsdb_condition_get_columns(condition
, &n_columns
);
460 for (i
= 0; i
< n_columns
; i
++) {
461 ovsdb_monitor_add_column(dbmon
, table
, columns
[i
],
468 /* Bind this session's condition to ovsdb_monitor */
470 ovsdb_monitor_condition_bind(struct ovsdb_monitor
*dbmon
,
471 struct ovsdb_monitor_session_condition
*cond
)
473 struct shash_node
*node
;
475 SHASH_FOR_EACH(node
, &cond
->tables
) {
476 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
477 struct ovsdb_monitor_table
*mt
=
478 shash_find_data(&dbmon
->tables
, mtc
->table
->schema
->name
);
481 ovsdb_monitor_condition_add_columns(dbmon
, mtc
->table
,
482 &mtc
->new_condition
);
487 ovsdb_monitor_table_exists(struct ovsdb_monitor
*m
,
488 const struct ovsdb_table
*table
)
490 return shash_find_data(&m
->tables
, table
->schema
->name
);
493 static struct ovsdb_monitor_changes
*
494 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table
*mt
,
497 struct ovsdb_monitor_changes
*changes
;
499 changes
= xzalloc(sizeof *changes
);
501 changes
->transaction
= next_txn
;
504 hmap_init(&changes
->rows
);
505 hmap_insert(&mt
->changes
, &changes
->hmap_node
, hash_uint64(next_txn
));
510 static struct ovsdb_monitor_changes
*
511 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table
*mt
,
512 uint64_t transaction
)
514 struct ovsdb_monitor_changes
*changes
;
515 size_t hash
= hash_uint64(transaction
);
517 HMAP_FOR_EACH_WITH_HASH(changes
, hmap_node
, hash
, &mt
->changes
) {
518 if (changes
->transaction
== transaction
) {
526 /* Stop currently tracking changes to table 'mt' since 'transaction'. */
528 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table
*mt
,
529 uint64_t transaction
)
531 struct ovsdb_monitor_changes
*changes
=
532 ovsdb_monitor_table_find_changes(mt
, transaction
);
534 if (--changes
->n_refs
== 0) {
535 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
536 ovsdb_monitor_changes_destroy(changes
);
541 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
544 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
545 uint64_t transaction
)
547 struct ovsdb_monitor_changes
*changes
;
549 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
553 ovsdb_monitor_table_add_changes(mt
, transaction
);
558 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes
*changes
)
560 struct ovsdb_monitor_row
*row
, *next
;
562 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
563 hmap_remove(&changes
->rows
, &row
->hmap_node
);
564 ovsdb_monitor_row_destroy(changes
->mt
, row
);
566 hmap_destroy(&changes
->rows
);
570 static enum ovsdb_monitor_selection
571 ovsdb_monitor_row_update_type(bool initial
, const bool old
, const bool new)
573 return initial
? OJMS_INITIAL
579 /* Set conditional monitoring mode only if we have non-empty condition in one
580 * of the tables at least */
582 ovsdb_monitor_session_condition_set_mode(
583 struct ovsdb_monitor_session_condition
*cond
)
585 struct shash_node
*node
;
587 SHASH_FOR_EACH (node
, &cond
->tables
) {
588 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
590 if (!ovsdb_condition_is_true(&mtc
->new_condition
)) {
591 cond
->conditional
= true;
595 cond
->conditional
= false;
598 /* Returnes an empty allocated session's condition state holder */
599 struct ovsdb_monitor_session_condition
*
600 ovsdb_monitor_session_condition_create(void)
602 struct ovsdb_monitor_session_condition
*condition
=
603 xzalloc(sizeof *condition
);
605 condition
->conditional
= false;
606 shash_init(&condition
->tables
);
611 ovsdb_monitor_session_condition_destroy(
612 struct ovsdb_monitor_session_condition
*condition
)
614 struct shash_node
*node
, *next
;
620 SHASH_FOR_EACH_SAFE (node
, next
, &condition
->tables
) {
621 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
623 ovsdb_condition_destroy(&mtc
->new_condition
);
624 ovsdb_condition_destroy(&mtc
->old_condition
);
625 shash_delete(&condition
->tables
, node
);
628 shash_destroy(&condition
->tables
);
633 ovsdb_monitor_table_condition_create(
634 struct ovsdb_monitor_session_condition
*condition
,
635 const struct ovsdb_table
*table
,
636 const struct json
*json_cnd
)
638 struct ovsdb_monitor_table_condition
*mtc
;
639 struct ovsdb_error
*error
;
641 mtc
= xzalloc(sizeof *mtc
);
643 ovsdb_condition_init(&mtc
->old_condition
);
644 ovsdb_condition_init(&mtc
->new_condition
);
647 error
= ovsdb_condition_from_json(table
->schema
,
650 &mtc
->old_condition
);
657 shash_add(&condition
->tables
, table
->schema
->name
, mtc
);
658 /* On session startup old == new condition */
659 ovsdb_condition_clone(&mtc
->new_condition
, &mtc
->old_condition
);
660 ovsdb_monitor_session_condition_set_mode(condition
);
666 ovsdb_monitor_get_table_conditions(
667 const struct ovsdb_monitor_table
*mt
,
668 const struct ovsdb_monitor_session_condition
*condition
,
669 struct ovsdb_condition
**old_condition
,
670 struct ovsdb_condition
**new_condition
)
676 struct ovsdb_monitor_table_condition
*mtc
=
677 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
682 *old_condition
= &mtc
->old_condition
;
683 *new_condition
= &mtc
->new_condition
;
689 ovsdb_monitor_table_condition_update(
690 struct ovsdb_monitor
*dbmon
,
691 struct ovsdb_monitor_session_condition
*condition
,
692 const struct ovsdb_table
*table
,
693 const struct json
*cond_json
)
699 struct ovsdb_monitor_table_condition
*mtc
=
700 shash_find_data(&condition
->tables
, table
->schema
->name
);
701 struct ovsdb_error
*error
;
702 struct ovsdb_condition cond
= OVSDB_CONDITION_INITIALIZER(&cond
);
704 error
= ovsdb_condition_from_json(table
->schema
, cond_json
,
709 ovsdb_condition_destroy(&mtc
->new_condition
);
710 ovsdb_condition_clone(&mtc
->new_condition
, &cond
);
711 ovsdb_condition_destroy(&cond
);
712 ovsdb_monitor_condition_add_columns(dbmon
,
714 &mtc
->new_condition
);
720 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table
*mt
,
721 struct ovsdb_monitor_session_condition
*condition
)
723 struct ovsdb_monitor_table_condition
*mtc
=
724 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
727 /* If conditional monitoring - set old condition to new condition */
728 if (ovsdb_condition_cmp_3way(&mtc
->old_condition
,
729 &mtc
->new_condition
)) {
730 ovsdb_condition_destroy(&mtc
->old_condition
);
731 ovsdb_condition_clone(&mtc
->old_condition
, &mtc
->new_condition
);
732 ovsdb_monitor_session_condition_set_mode(condition
);
737 static enum ovsdb_monitor_selection
738 ovsdb_monitor_row_update_type_condition(
739 const struct ovsdb_monitor_table
*mt
,
740 const struct ovsdb_monitor_session_condition
*condition
,
742 enum ovsdb_monitor_row_type row_type
,
743 const struct ovsdb_datum
*old
,
744 const struct ovsdb_datum
*new)
746 struct ovsdb_condition
*old_condition
, *new_condition
;
747 enum ovsdb_monitor_selection type
=
748 ovsdb_monitor_row_update_type(initial
, old
, new);
750 if (ovsdb_monitor_get_table_conditions(mt
,
754 bool old_cond
= !old
? false
755 : ovsdb_condition_empty_or_match_any(old
,
757 row_type
== OVSDB_MONITOR_ROW
?
758 mt
->columns_index_map
:
760 bool new_cond
= !new ? false
761 : ovsdb_condition_empty_or_match_any(new,
763 row_type
== OVSDB_MONITOR_ROW
?
764 mt
->columns_index_map
:
767 if (!old_cond
&& !new_cond
) {
779 type
= !old_cond
? OJMS_INSERT
: !new_cond
780 ? OJMS_DELETE
: OJMS_MODIFY
;
795 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table
*mt
,
796 enum ovsdb_monitor_row_type row_type
,
797 const struct ovsdb_datum
*old
,
798 const struct ovsdb_datum
*new,
799 enum ovsdb_monitor_selection type
,
800 unsigned long int *changed
)
802 if (!(mt
->select
& type
)) {
806 if (type
== OJMS_MODIFY
) {
810 memset(changed
, 0, bitmap_n_bytes(mt
->n_columns
));
811 for (i
= 0; i
< mt
->n_columns
; i
++) {
812 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
813 size_t index
= row_type
== OVSDB_ROW
? c
->index
: i
;
814 if (!ovsdb_datum_equals(&old
[index
], &new[index
], &c
->type
)) {
815 bitmap_set1(changed
, i
);
820 /* No actual changes: presumably a row changed and then
821 * changed back later. */
829 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
830 * 'mt', or NULL if no row update should be sent.
832 * The caller should specify 'initial' as true if the returned JSON is going to
833 * be used as part of the initial reply to a "monitor" request, false if it is
834 * going to be used as part of an "update" notification.
836 * 'changed' must be a scratch buffer for internal use that is at least
837 * bitmap_n_bytes(mt->n_columns) bytes long. */
839 ovsdb_monitor_compose_row_update(
840 const struct ovsdb_monitor_table
*mt
,
841 const struct ovsdb_monitor_session_condition
*condition OVS_UNUSED
,
842 enum ovsdb_monitor_row_type row_type OVS_UNUSED
,
844 bool initial
, unsigned long int *changed
)
846 const struct ovsdb_monitor_row
*row
= _row
;
847 enum ovsdb_monitor_selection type
;
848 struct json
*old_json
, *new_json
;
849 struct json
*row_json
;
852 ovs_assert(row_type
== OVSDB_MONITOR_ROW
);
853 type
= ovsdb_monitor_row_update_type(initial
, row
->old
, row
->new);
854 if (ovsdb_monitor_row_skip_update(mt
, row_type
, row
->old
,
855 row
->new, type
, changed
)) {
859 row_json
= json_object_create();
860 old_json
= new_json
= NULL
;
861 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
862 old_json
= json_object_create();
863 json_object_put(row_json
, "old", old_json
);
865 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
866 new_json
= json_object_create();
867 json_object_put(row_json
, "new", new_json
);
869 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
870 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
872 if (!c
->monitored
|| !(type
& c
->select
)) {
873 /* We don't care about this type of change for this
874 * particular column (but we will care about it for some
879 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
880 || type
== OJMS_DELETE
) {
881 json_object_put(old_json
, c
->column
->name
,
882 ovsdb_datum_to_json(&row
->old
[i
],
885 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
886 json_object_put(new_json
, c
->column
->name
,
887 ovsdb_datum_to_json(&row
->new[i
],
895 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
896 * for 'row' within * 'mt', or NULL if no row update should be sent.
898 * The caller should specify 'initial' as true if the returned JSON is
899 * going to be used as part of the initial reply to a "monitor_cond" request,
900 * false if it is going to be used as part of an "update2" notification.
902 * 'changed' must be a scratch buffer for internal use that is at least
903 * bitmap_n_bytes(mt->n_columns) bytes long. */
905 ovsdb_monitor_compose_row_update2(
906 const struct ovsdb_monitor_table
*mt
,
907 const struct ovsdb_monitor_session_condition
*condition
,
908 enum ovsdb_monitor_row_type row_type
,
910 bool initial
, unsigned long int *changed
)
912 enum ovsdb_monitor_selection type
;
913 struct json
*row_update2
, *diff_json
;
914 const struct ovsdb_datum
*old
, *new;
917 if (row_type
== OVSDB_MONITOR_ROW
) {
918 old
= ((const struct ovsdb_monitor_row
*)_row
)->old
;;
919 new = ((const struct ovsdb_monitor_row
*)_row
)->new;
921 old
= new = ((const struct ovsdb_row
*)_row
)->fields
;
924 type
= ovsdb_monitor_row_update_type_condition(mt
, condition
, initial
,
926 if (ovsdb_monitor_row_skip_update(mt
, row_type
, old
, new, type
, changed
)) {
930 row_update2
= json_object_create();
931 if (type
== OJMS_DELETE
) {
932 json_object_put(row_update2
, "delete", json_null_create());
934 diff_json
= json_object_create();
937 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
938 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
939 size_t index
= row_type
== OVSDB_ROW
? c
->column
->index
: i
;
940 if (!c
->monitored
|| !(type
& c
->select
)) {
941 /* We don't care about this type of change for this
942 * particular column (but we will care about it for some
947 if (type
== OJMS_MODIFY
) {
948 struct ovsdb_datum diff
;
950 if (!bitmap_is_set(changed
, i
)) {
954 ovsdb_datum_diff(&diff
,&old
[index
], &new[index
],
956 json_object_put(diff_json
, c
->column
->name
,
957 ovsdb_datum_to_json(&diff
, &c
->column
->type
));
958 ovsdb_datum_destroy(&diff
, &c
->column
->type
);
960 if (!ovsdb_datum_is_default(&new[index
], &c
->column
->type
)) {
961 json_object_put(diff_json
, c
->column
->name
,
962 ovsdb_datum_to_json(&new[index
],
968 op
= type
== OJMS_INITIAL
? "initial"
969 : type
== OJMS_MODIFY
? "modify" : "insert";
970 json_object_put(row_update2
, op
, diff_json
);
977 ovsdb_monitor_max_columns(struct ovsdb_monitor
*dbmon
)
979 struct shash_node
*node
;
980 size_t max_columns
= 0;
982 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
983 struct ovsdb_monitor_table
*mt
= node
->data
;
985 max_columns
= MAX(max_columns
, mt
->n_columns
);
992 ovsdb_monitor_add_json_row(struct json
**json
, const char *table_name
,
993 struct json
**table_json
, struct json
*row_json
,
994 const struct uuid
*row_uuid
)
996 char uuid
[UUID_LEN
+ 1];
998 /* Create JSON object for transaction overall. */
1000 *json
= json_object_create();
1003 /* Create JSON object for transaction on this table. */
1005 *table_json
= json_object_create();
1006 json_object_put(*json
, table_name
, *table_json
);
1009 /* Add JSON row to JSON table. */
1010 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(row_uuid
));
1011 json_object_put(*table_json
, uuid
, row_json
);
1014 /* Constructs and returns JSON for a <table-updates> object (as described in
1015 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1018 ovsdb_monitor_compose_update(
1019 struct ovsdb_monitor
*dbmon
,
1020 bool initial
, uint64_t transaction
,
1021 const struct ovsdb_monitor_session_condition
*condition
,
1022 compose_row_update_cb_func row_update
)
1024 struct shash_node
*node
;
1026 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1027 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1030 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1031 struct ovsdb_monitor_table
*mt
= node
->data
;
1032 struct ovsdb_monitor_row
*row
, *next
;
1033 struct ovsdb_monitor_changes
*changes
;
1034 struct json
*table_json
= NULL
;
1036 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
1041 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
1042 struct json
*row_json
;
1043 row_json
= (*row_update
)(mt
, condition
, OVSDB_MONITOR_ROW
, row
,
1046 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1047 &table_json
, row_json
,
1058 ovsdb_monitor_compose_cond_change_update(
1059 struct ovsdb_monitor
*dbmon
,
1060 struct ovsdb_monitor_session_condition
*condition
)
1062 struct shash_node
*node
;
1063 struct json
*json
= NULL
;
1064 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1065 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1067 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1068 struct ovsdb_monitor_table
*mt
= node
->data
;
1069 struct ovsdb_row
*row
;
1070 struct json
*table_json
= NULL
;
1071 struct ovsdb_condition
*old_condition
, *new_condition
;
1073 if (!ovsdb_monitor_get_table_conditions(mt
,
1077 !ovsdb_condition_cmp_3way(old_condition
, new_condition
)) {
1078 /* Nothing to update on this table */
1082 /* Iterate over all rows in table */
1083 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1084 struct json
*row_json
;
1086 row_json
= ovsdb_monitor_compose_row_update2(mt
, condition
,
1090 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1091 &table_json
, row_json
,
1092 ovsdb_row_get_uuid(row
));
1095 ovsdb_monitor_table_condition_updated(mt
, condition
);
1102 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1103 * for all the outstanding changes within 'monitor' that starts from
1105 * If cond_updated is true all rows in the db that match conditions will be
1108 * The caller should specify 'initial' as true if the returned JSON is going to
1109 * be used as part of the initial reply to a "monitor" request, false if it is
1110 * going to be used as part of an "update" notification. */
1112 ovsdb_monitor_get_update(
1113 struct ovsdb_monitor
*dbmon
,
1114 bool initial
, bool cond_updated
,
1115 uint64_t *unflushed_
,
1116 struct ovsdb_monitor_session_condition
*condition
,
1117 enum ovsdb_monitor_version version
)
1119 struct ovsdb_monitor_json_cache_node
*cache_node
= NULL
;
1120 struct shash_node
*node
;
1122 const uint64_t unflushed
= *unflushed_
;
1123 const uint64_t next_unflushed
= dbmon
->n_transactions
+ 1;
1125 ovs_assert(cond_updated
? unflushed
== next_unflushed
: true);
1127 /* Return a clone of cached json if one exists. Otherwise,
1128 * generate a new one and add it to the cache. */
1129 if (!condition
|| (!condition
->conditional
&& !cond_updated
)) {
1130 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, version
,
1134 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
1136 if (version
== OVSDB_MONITOR_V1
) {
1138 ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1140 ovsdb_monitor_compose_row_update
);
1142 ovs_assert(version
== OVSDB_MONITOR_V2
);
1143 if (!cond_updated
) {
1144 json
= ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1146 ovsdb_monitor_compose_row_update2
);
1148 if (!condition
|| !condition
->conditional
) {
1149 ovsdb_monitor_json_cache_insert(dbmon
, version
, unflushed
,
1153 /* Compose update on whole db due to condition update.
1154 Session must be flushed (change list is empty)*/
1156 ovsdb_monitor_compose_cond_change_update(dbmon
, condition
);
1161 /* Maintain transaction id of 'changes'. */
1162 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1163 struct ovsdb_monitor_table
*mt
= node
->data
;
1165 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1166 ovsdb_monitor_table_track_changes(mt
, next_unflushed
);
1168 *unflushed_
= next_unflushed
;
1174 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
1175 uint64_t next_transaction
)
1177 ovs_assert(next_transaction
<= dbmon
->n_transactions
+ 1);
1178 return (next_transaction
<= dbmon
->n_transactions
);
1182 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
1183 const struct ovsdb_table
*table
,
1184 enum ovsdb_monitor_selection select
)
1186 struct ovsdb_monitor_table
* mt
;
1188 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
1189 mt
->select
|= select
;
1193 * If a row's change type (insert, delete or modify) matches that of
1194 * the monitor, they should be sent to the monitor's clients as updates.
1195 * Of cause, the monitor should also internally update with this change.
1197 * When a change type does not require client side update, the monitor
1198 * may still need to keep track of certain changes in order to generate
1199 * correct future updates. For example, the monitor internal state should
1200 * be updated whenever a new row is inserted, in order to generate the
1201 * correct initial state, regardless if a insert change type is being
1204 * On the other hand, if a transaction only contains changes to columns
1205 * that are not monitored, this transaction can be safely ignored by the
1208 * Thus, the order of the declaration is important:
1209 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1210 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1211 enum ovsdb_monitor_changes_efficacy
{
1212 OVSDB_CHANGES_NO_EFFECT
, /* Monitor does not care about this
1214 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
, /* Monitor internal updates. */
1215 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
, /* Client needs to be updated. */
1218 struct ovsdb_monitor_aux
{
1219 const struct ovsdb_monitor
*monitor
;
1220 struct ovsdb_monitor_table
*mt
;
1221 enum ovsdb_monitor_changes_efficacy efficacy
;
1225 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
1226 const struct ovsdb_monitor
*m
)
1230 aux
->efficacy
= OVSDB_CHANGES_NO_EFFECT
;
1234 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
1235 const struct ovsdb_row
*new,
1236 const struct ovsdb_monitor_table
*mt
,
1237 struct ovsdb_monitor_changes
*changes
)
1239 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
1240 struct ovsdb_monitor_row
*change
;
1242 change
= ovsdb_monitor_changes_row_find(changes
, uuid
);
1244 change
= xzalloc(sizeof *change
);
1245 hmap_insert(&changes
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
1246 change
->uuid
= *uuid
;
1247 change
->old
= clone_monitor_row_data(mt
, old
);
1248 change
->new = clone_monitor_row_data(mt
, new);
1252 /* Reinsert the row that was just deleted.
1254 * This path won't be hit without replication. Whenever OVSDB
1255 * server inserts a new row, It always generates a new UUID
1256 * that is different from the row just deleted.
1258 * With replication, this path can be hit in a corner
1259 * case when two OVSDB servers are set up to replicate
1260 * each other. Not that is a useful set up, but can
1261 * happen in practice.
1263 * An example of how this path can be hit is documented below.
1264 * The details is not as important to the correctness of the
1265 * logic, but added here to convince ourselves that this path
1268 * Imagine two OVSDB servers that replicates from each
1269 * other. For each replication session, there is a
1270 * corresponding monitor at the other end of the replication
1271 * JSONRPC connection.
1273 * The events can lead to a back to back deletion and
1274 * insertion operation of the same row for the monitor of
1275 * the first server are:
1277 * 1. A row is inserted in the first OVSDB server.
1278 * 2. The row is then replicated to the remote OVSDB server.
1279 * 3. The row is now deleted by the local OVSDB server. This
1280 * deletion operation is replicated to the local monitor
1281 * of the OVSDB server.
1282 * 4. The monitor now receives the same row, as an insertion,
1283 * from the replication server. Because of
1284 * replication, the row carries the same UUID as the row
1287 change
->new = clone_monitor_row_data(mt
, new);
1289 update_monitor_row_data(mt
, new, change
->new);
1292 free_monitor_row_data(mt
, change
->new);
1296 /* This row was added then deleted. Forget about it. */
1297 hmap_remove(&changes
->rows
, &change
->hmap_node
);
1305 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table
*mt
,
1306 const unsigned long int *changed
)
1310 for (i
= 0; i
< mt
->n_columns
; i
++) {
1311 size_t column_index
= mt
->columns
[i
].column
->index
;
1313 if (bitmap_is_set(changed
, column_index
)) {
1321 /* Return the efficacy of a row's change to a monitor table.
1323 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1324 * definition form more information. */
1325 static enum ovsdb_monitor_changes_efficacy
1326 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type
,
1327 const struct ovsdb_monitor_table
*mt
,
1328 const unsigned long int *changed
)
1330 if (type
== OJMS_MODIFY
&&
1331 !ovsdb_monitor_columns_changed(mt
, changed
)) {
1332 return OVSDB_CHANGES_NO_EFFECT
;
1335 if (type
== OJMS_MODIFY
) {
1336 /* Condition might turn a modify operation to insert or delete */
1337 type
|= OJMS_INSERT
| OJMS_DELETE
;
1340 return (mt
->select
& type
)
1341 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1342 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
;
1346 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
1347 const struct ovsdb_row
*new,
1348 const unsigned long int *changed
,
1351 struct ovsdb_monitor_aux
*aux
= aux_
;
1352 const struct ovsdb_monitor
*m
= aux
->monitor
;
1353 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1354 struct ovsdb_monitor_table
*mt
;
1355 struct ovsdb_monitor_changes
*changes
;
1357 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
1358 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
1360 /* We don't care about rows in this table at all. Tell the caller
1367 enum ovsdb_monitor_selection type
=
1368 ovsdb_monitor_row_update_type(false, old
, new);
1369 enum ovsdb_monitor_changes_efficacy efficacy
=
1370 ovsdb_monitor_changes_classify(type
, mt
, changed
);
1372 HMAP_FOR_EACH(changes
, hmap_node
, &mt
->changes
) {
1373 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1374 ovsdb_monitor_changes_update(old
, new, mt
, changes
);
1377 if (aux
->efficacy
< efficacy
) {
1378 aux
->efficacy
= efficacy
;
1385 ovsdb_monitor_get_initial(const struct ovsdb_monitor
*dbmon
)
1387 struct shash_node
*node
;
1389 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1390 struct ovsdb_monitor_table
*mt
= node
->data
;
1392 if (mt
->select
& OJMS_INITIAL
) {
1393 struct ovsdb_row
*row
;
1394 struct ovsdb_monitor_changes
*changes
;
1396 changes
= ovsdb_monitor_table_find_changes(mt
, 0);
1398 changes
= ovsdb_monitor_table_add_changes(mt
, 0);
1399 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1400 ovsdb_monitor_changes_update(NULL
, row
, mt
, changes
);
1410 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
1411 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
,
1414 struct jsonrpc_monitor_node
*jm
;
1416 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1417 ovsdb_monitor_destroy(dbmon
);
1421 /* Find and remove the jsonrpc monitor from the list. */
1422 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
1423 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
1424 /* Release the tracked changes. */
1425 struct shash_node
*node
;
1426 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1427 struct ovsdb_monitor_table
*mt
= node
->data
;
1428 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1430 ovs_list_remove(&jm
->node
);
1433 /* Destroy ovsdb monitor if this is the last user. */
1434 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1435 ovsdb_monitor_destroy(dbmon
);
1442 /* Should never reach here. jsonrpc_monitor should be on the list. */
1447 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
1448 const struct ovsdb_monitor_table
*b
)
1452 ovs_assert(b
->n_columns
== b
->n_monitored_columns
);
1454 if ((a
->table
!= b
->table
) ||
1455 (a
->select
!= b
->select
) ||
1456 (a
->n_monitored_columns
!= b
->n_monitored_columns
)) {
1460 /* Compare only monitored columns that must be sorted already */
1461 for (i
= 0; i
< a
->n_monitored_columns
; i
++) {
1462 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
1463 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
1471 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
1472 const struct ovsdb_monitor
*b
)
1474 struct shash_node
*node
;
1476 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
1480 SHASH_FOR_EACH(node
, &a
->tables
) {
1481 const struct ovsdb_monitor_table
*mta
= node
->data
;
1482 const struct ovsdb_monitor_table
*mtb
;
1484 mtb
= shash_find_data(&b
->tables
, node
->name
);
1489 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
1498 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
1500 const struct shash_node
**nodes
;
1503 nodes
= shash_sort(&dbmon
->tables
);
1504 n
= shash_count(&dbmon
->tables
);
1506 for (i
= 0; i
< n
; i
++) {
1507 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
1509 basis
= hash_pointer(mt
->table
, basis
);
1510 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
1512 for (j
= 0; j
< mt
->n_columns
; j
++) {
1513 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
1514 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
1522 struct ovsdb_monitor
*
1523 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
1525 struct ovsdb_monitor
*dbmon
;
1528 /* New_dbmon should be associated with only one jsonrpc
1530 ovs_assert(ovs_list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
1532 ovsdb_monitor_columns_sort(new_dbmon
);
1534 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
1535 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
1536 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
1541 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
1546 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
1548 struct shash_node
*node
;
1550 ovs_list_remove(&dbmon
->replica
.node
);
1552 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
1553 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
1556 ovsdb_monitor_json_cache_flush(dbmon
);
1557 hmap_destroy(&dbmon
->json_cache
);
1559 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1560 struct ovsdb_monitor_table
*mt
= node
->data
;
1561 struct ovsdb_monitor_changes
*changes
, *next
;
1563 HMAP_FOR_EACH_SAFE (changes
, next
, hmap_node
, &mt
->changes
) {
1564 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
1565 ovsdb_monitor_changes_destroy(changes
);
1567 hmap_destroy(&mt
->changes
);
1569 free(mt
->columns_index_map
);
1572 shash_destroy(&dbmon
->tables
);
1576 static struct ovsdb_error
*
1577 ovsdb_monitor_commit(struct ovsdb_replica
*replica
,
1578 const struct ovsdb_txn
*txn
,
1579 bool durable OVS_UNUSED
)
1581 struct ovsdb_monitor
*m
= ovsdb_monitor_cast(replica
);
1582 struct ovsdb_monitor_aux aux
;
1584 ovsdb_monitor_init_aux(&aux
, m
);
1585 /* Update ovsdb_monitor's transaction number for
1586 * each transaction, before calling ovsdb_monitor_change_cb(). */
1587 m
->n_transactions
++;
1588 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
1590 switch(aux
.efficacy
) {
1591 case OVSDB_CHANGES_NO_EFFECT
:
1592 /* The transaction is ignored by the monitor.
1593 * Roll back the 'n_transactions' as if the transaction
1594 * has never happened. */
1595 m
->n_transactions
--;
1597 case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
:
1600 case OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
:
1601 ovsdb_monitor_json_cache_flush(m
);
1609 ovsdb_monitor_destroy_callback(struct ovsdb_replica
*replica
)
1611 struct ovsdb_monitor
*dbmon
= ovsdb_monitor_cast(replica
);
1612 struct jsonrpc_monitor_node
*jm
, *next
;
1614 /* Delete all front end monitors. Removing the last front
1615 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
1616 * ovsdb monitor will also be destroied. */
1617 LIST_FOR_EACH_SAFE(jm
, next
, node
, &dbmon
->jsonrpc_monitors
) {
1618 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
);
1622 /* Add some memory usage statics for monitors into 'usage', for use with
1623 * memory_report(). */
1625 ovsdb_monitor_get_memory_usage(struct simap
*usage
)
1627 struct ovsdb_monitor
*dbmon
;
1628 simap_put(usage
, "monitors", hmap_count(&ovsdb_monitors
));
1630 HMAP_FOR_EACH(dbmon
, hmap_node
, &ovsdb_monitors
) {
1631 simap_increase(usage
, "json-caches", hmap_count(&dbmon
->json_cache
));
1635 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
= {
1636 ovsdb_monitor_commit
,
1637 ovsdb_monitor_destroy_callback
,