mirror of
https://github.com/openvswitch/ovs
synced 2025-08-31 06:15:47 +00:00
ovsdb: Add/use partial set updates.
This patchset mimics the changes introduced inf199df26
(ovsdb-idl: Add partial map updates functionality.)010fe7ae
(ovsdb-idlc.in: Autogenerate partial map updates functions.)7251075c
(tests: Add test for partial map updates.)b1048e6a
(ovsdb-idl: Fix issues detected in Partial Map Update feature) but for columns that store sets of values rather than key-value pairs. These columns will now be able to use the OVSDB mutate operation to transmit deltas on the wire rather than use verify/update and transmit wait/update operations on the wire. Side effect of modifying the comments in the partial map update tests. Signed-off-by: Ryan Moats <rmoats@us.ibm.com> Signed-off-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
380
lib/ovsdb-idl.c
380
lib/ovsdb-idl.c
@@ -185,6 +185,7 @@ static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
|
||||
static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
|
||||
static void ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *);
|
||||
static void ovsdb_idl_destroy_all_map_op_lists(struct ovsdb_idl_row *);
|
||||
static void ovsdb_idl_destroy_all_set_op_lists(struct ovsdb_idl_row *);
|
||||
|
||||
static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
|
||||
static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
|
||||
@@ -201,6 +202,10 @@ static void ovsdb_idl_txn_add_map_op(struct ovsdb_idl_row *,
|
||||
const struct ovsdb_idl_column *,
|
||||
struct ovsdb_datum *,
|
||||
enum map_op_type);
|
||||
static void ovsdb_idl_txn_add_set_op(struct ovsdb_idl_row *,
|
||||
const struct ovsdb_idl_column *,
|
||||
struct ovsdb_datum *,
|
||||
enum set_op_type);
|
||||
|
||||
static void ovsdb_idl_send_lock_request(struct ovsdb_idl *);
|
||||
static void ovsdb_idl_send_unlock_request(struct ovsdb_idl *);
|
||||
@@ -1814,6 +1819,8 @@ ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
|
||||
row->table = table;
|
||||
row->map_op_written = NULL;
|
||||
row->map_op_lists = NULL;
|
||||
row->set_op_written = NULL;
|
||||
row->set_op_lists = NULL;
|
||||
return row;
|
||||
}
|
||||
|
||||
@@ -1824,6 +1831,7 @@ ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
|
||||
ovsdb_idl_row_clear_old(row);
|
||||
hmap_remove(&row->table->rows, &row->hmap_node);
|
||||
ovsdb_idl_destroy_all_map_op_lists(row);
|
||||
ovsdb_idl_destroy_all_set_op_lists(row);
|
||||
if (ovsdb_idl_track_is_set(row->table)) {
|
||||
row->change_seqno[OVSDB_IDL_CHANGE_DELETE]
|
||||
= row->table->change_seqno[OVSDB_IDL_CHANGE_DELETE]
|
||||
@@ -1857,6 +1865,27 @@ ovsdb_idl_destroy_all_map_op_lists(struct ovsdb_idl_row *row)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ovsdb_idl_destroy_all_set_op_lists(struct ovsdb_idl_row *row)
|
||||
{
|
||||
if (row->set_op_written) {
|
||||
/* Clear Set Operation Lists */
|
||||
size_t idx, n_columns;
|
||||
const struct ovsdb_idl_column *columns;
|
||||
const struct ovsdb_type *type;
|
||||
n_columns = row->table->class->n_columns;
|
||||
columns = row->table->class->columns;
|
||||
BITMAP_FOR_EACH_1 (idx, n_columns, row->set_op_written) {
|
||||
type = &columns[idx].type;
|
||||
set_op_list_destroy(row->set_op_lists[idx], type);
|
||||
}
|
||||
free(row->set_op_lists);
|
||||
bitmap_free(row->set_op_written);
|
||||
row->set_op_lists = NULL;
|
||||
row->set_op_written = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *idl)
|
||||
{
|
||||
@@ -2393,6 +2422,7 @@ ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
|
||||
|
||||
HMAP_FOR_EACH_SAFE (row, next, txn_node, &txn->txn_rows) {
|
||||
ovsdb_idl_destroy_all_map_op_lists(row);
|
||||
ovsdb_idl_destroy_all_set_op_lists(row);
|
||||
if (row->old) {
|
||||
if (row->written) {
|
||||
ovsdb_idl_row_unparse(row);
|
||||
@@ -2429,111 +2459,188 @@ ovsdb_idl_txn_extract_mutations(struct ovsdb_idl_row *row,
|
||||
size_t idx;
|
||||
bool any_mutations = false;
|
||||
|
||||
BITMAP_FOR_EACH_1(idx, class->n_columns, row->map_op_written) {
|
||||
struct map_op_list *map_op_list;
|
||||
const struct ovsdb_idl_column *column;
|
||||
const struct ovsdb_datum *old_datum;
|
||||
enum ovsdb_atomic_type key_type, value_type;
|
||||
struct json *mutation, *map, *col_name, *mutator;
|
||||
struct json *del_set, *ins_map;
|
||||
bool any_del, any_ins;
|
||||
if (row->map_op_written) {
|
||||
BITMAP_FOR_EACH_1(idx, class->n_columns, row->map_op_written) {
|
||||
struct map_op_list *map_op_list;
|
||||
const struct ovsdb_idl_column *column;
|
||||
const struct ovsdb_datum *old_datum;
|
||||
enum ovsdb_atomic_type key_type, value_type;
|
||||
struct json *mutation, *map, *col_name, *mutator;
|
||||
struct json *del_set, *ins_map;
|
||||
bool any_del, any_ins;
|
||||
|
||||
map_op_list = row->map_op_lists[idx];
|
||||
column = &class->columns[idx];
|
||||
key_type = column->type.key.type;
|
||||
value_type = column->type.value.type;
|
||||
map_op_list = row->map_op_lists[idx];
|
||||
column = &class->columns[idx];
|
||||
key_type = column->type.key.type;
|
||||
value_type = column->type.value.type;
|
||||
|
||||
/* Get the value to be changed */
|
||||
if (row->new && row->written && bitmap_is_set(row->written,idx)) {
|
||||
old_datum = &row->new[idx];
|
||||
} else if (row->old != NULL) {
|
||||
old_datum = &row->old[idx];
|
||||
} else {
|
||||
old_datum = ovsdb_datum_default(&column->type);
|
||||
}
|
||||
/* Get the value to be changed */
|
||||
if (row->new && row->written && bitmap_is_set(row->written,idx)) {
|
||||
old_datum = &row->new[idx];
|
||||
} else if (row->old != NULL) {
|
||||
old_datum = &row->old[idx];
|
||||
} else {
|
||||
old_datum = ovsdb_datum_default(&column->type);
|
||||
}
|
||||
|
||||
del_set = json_array_create_empty();
|
||||
ins_map = json_array_create_empty();
|
||||
any_del = false;
|
||||
any_ins = false;
|
||||
del_set = json_array_create_empty();
|
||||
ins_map = json_array_create_empty();
|
||||
any_del = false;
|
||||
any_ins = false;
|
||||
|
||||
for (struct map_op *map_op = map_op_list_first(map_op_list); map_op;
|
||||
map_op = map_op_list_next(map_op_list, map_op)) {
|
||||
for (struct map_op *map_op = map_op_list_first(map_op_list); map_op;
|
||||
map_op = map_op_list_next(map_op_list, map_op)) {
|
||||
|
||||
if (map_op_type(map_op) == MAP_OP_UPDATE) {
|
||||
/* Find out if value really changed. */
|
||||
struct ovsdb_datum *new_datum;
|
||||
unsigned int pos;
|
||||
new_datum = map_op_datum(map_op);
|
||||
pos = ovsdb_datum_find_key(old_datum,
|
||||
&new_datum->keys[0],
|
||||
key_type);
|
||||
if (ovsdb_atom_equals(&new_datum->values[0],
|
||||
&old_datum->values[pos],
|
||||
value_type)) {
|
||||
/* No change in value. Move on to next update. */
|
||||
continue;
|
||||
if (map_op_type(map_op) == MAP_OP_UPDATE) {
|
||||
/* Find out if value really changed. */
|
||||
struct ovsdb_datum *new_datum;
|
||||
unsigned int pos;
|
||||
new_datum = map_op_datum(map_op);
|
||||
pos = ovsdb_datum_find_key(old_datum,
|
||||
&new_datum->keys[0],
|
||||
key_type);
|
||||
if (ovsdb_atom_equals(&new_datum->values[0],
|
||||
&old_datum->values[pos],
|
||||
value_type)) {
|
||||
/* No change in value. Move on to next update. */
|
||||
continue;
|
||||
}
|
||||
} else if (map_op_type(map_op) == MAP_OP_DELETE){
|
||||
/* Verify that there is a key to delete. */
|
||||
unsigned int pos;
|
||||
pos = ovsdb_datum_find_key(old_datum,
|
||||
&map_op_datum(map_op)->keys[0],
|
||||
key_type);
|
||||
if (pos == UINT_MAX) {
|
||||
/* No key to delete. Move on to next update. */
|
||||
VLOG_WARN("Trying to delete a key that doesn't "
|
||||
"exist in the map.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else if (map_op_type(map_op) == MAP_OP_DELETE){
|
||||
/* Verify that there is a key to delete. */
|
||||
unsigned int pos;
|
||||
pos = ovsdb_datum_find_key(old_datum,
|
||||
&map_op_datum(map_op)->keys[0],
|
||||
key_type);
|
||||
if (pos == UINT_MAX) {
|
||||
/* No key to delete. Move on to next update. */
|
||||
VLOG_WARN("Trying to delete a key that doesn't "
|
||||
"exist in the map.");
|
||||
continue;
|
||||
|
||||
if (map_op_type(map_op) == MAP_OP_INSERT) {
|
||||
map = json_array_create_2(
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type),
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->values[0],
|
||||
value_type));
|
||||
json_array_add(ins_map, map);
|
||||
any_ins = true;
|
||||
} else { /* MAP_OP_UPDATE or MAP_OP_DELETE */
|
||||
map = ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type);
|
||||
json_array_add(del_set, map);
|
||||
any_del = true;
|
||||
}
|
||||
|
||||
/* Generate an additional insert mutate for updates. */
|
||||
if (map_op_type(map_op) == MAP_OP_UPDATE) {
|
||||
map = json_array_create_2(
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type),
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->values[0],
|
||||
value_type));
|
||||
json_array_add(ins_map, map);
|
||||
any_ins = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (map_op_type(map_op) == MAP_OP_INSERT) {
|
||||
map = json_array_create_2(
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type),
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->values[0],
|
||||
value_type));
|
||||
json_array_add(ins_map, map);
|
||||
any_ins = true;
|
||||
} else { /* MAP_OP_UPDATE or MAP_OP_DELETE */
|
||||
map = ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type);
|
||||
json_array_add(del_set, map);
|
||||
any_del = true;
|
||||
if (any_del) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("delete");
|
||||
map = json_array_create_2(json_string_create("set"), del_set);
|
||||
mutation = json_array_create_3(col_name, mutator, map);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(del_set);
|
||||
}
|
||||
|
||||
/* Generate an additional insert mutate for updates. */
|
||||
if (map_op_type(map_op) == MAP_OP_UPDATE) {
|
||||
map = json_array_create_2(
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->keys[0],
|
||||
key_type),
|
||||
ovsdb_atom_to_json(&map_op_datum(map_op)->values[0],
|
||||
value_type));
|
||||
json_array_add(ins_map, map);
|
||||
any_ins = true;
|
||||
if (any_ins) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("insert");
|
||||
map = json_array_create_2(json_string_create("map"), ins_map);
|
||||
mutation = json_array_create_3(col_name, mutator, map);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(ins_map);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (row->set_op_written) {
|
||||
BITMAP_FOR_EACH_1(idx, class->n_columns, row->set_op_written) {
|
||||
struct set_op_list *set_op_list;
|
||||
const struct ovsdb_idl_column *column;
|
||||
const struct ovsdb_datum *old_datum;
|
||||
enum ovsdb_atomic_type key_type;
|
||||
struct json *mutation, *set, *col_name, *mutator;
|
||||
struct json *del_set, *ins_set;
|
||||
bool any_del, any_ins;
|
||||
|
||||
if (any_del) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("delete");
|
||||
map = json_array_create_2(json_string_create("set"), del_set);
|
||||
mutation = json_array_create_3(col_name, mutator, map);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(del_set);
|
||||
}
|
||||
if (any_ins) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("insert");
|
||||
map = json_array_create_2(json_string_create("map"), ins_map);
|
||||
mutation = json_array_create_3(col_name, mutator, map);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(ins_map);
|
||||
set_op_list = row->set_op_lists[idx];
|
||||
column = &class->columns[idx];
|
||||
key_type = column->type.key.type;
|
||||
|
||||
/* Get the value to be changed */
|
||||
if (row->new && row->written && bitmap_is_set(row->written,idx)) {
|
||||
old_datum = &row->new[idx];
|
||||
} else if (row->old != NULL) {
|
||||
old_datum = &row->old[idx];
|
||||
} else {
|
||||
old_datum = ovsdb_datum_default(&column->type);
|
||||
}
|
||||
|
||||
del_set = json_array_create_empty();
|
||||
ins_set = json_array_create_empty();
|
||||
any_del = false;
|
||||
any_ins = false;
|
||||
|
||||
for (struct set_op *set_op = set_op_list_first(set_op_list); set_op;
|
||||
set_op = set_op_list_next(set_op_list, set_op)) {
|
||||
if (set_op_type(set_op) == SET_OP_INSERT) {
|
||||
set = ovsdb_atom_to_json(&set_op_datum(set_op)->keys[0],
|
||||
key_type);
|
||||
json_array_add(ins_set, set);
|
||||
any_ins = true;
|
||||
} else { /* SETP_OP_DELETE */
|
||||
/* Verify that there is a key to delete. */
|
||||
unsigned int pos;
|
||||
pos = ovsdb_datum_find_key(old_datum,
|
||||
&set_op_datum(set_op)->keys[0],
|
||||
key_type);
|
||||
if (pos == UINT_MAX) {
|
||||
/* No key to delete. Move on to next update. */
|
||||
VLOG_WARN("Trying to delete a key that doesn't "
|
||||
"exist in the set.");
|
||||
continue;
|
||||
}
|
||||
set = ovsdb_atom_to_json(&set_op_datum(set_op)->keys[0],
|
||||
key_type);
|
||||
json_array_add(del_set, set);
|
||||
any_del = true;
|
||||
}
|
||||
}
|
||||
if (any_del) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("delete");
|
||||
set = json_array_create_2(json_string_create("set"), del_set);
|
||||
mutation = json_array_create_3(col_name, mutator, set);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(del_set);
|
||||
}
|
||||
if (any_ins) {
|
||||
col_name = json_string_create(column->name);
|
||||
mutator = json_string_create("insert");
|
||||
set = json_array_create_2(json_string_create("set"), ins_set);
|
||||
mutation = json_array_create_3(col_name, mutator, set);
|
||||
json_array_add(mutations, mutation);
|
||||
any_mutations = true;
|
||||
} else {
|
||||
json_destroy(ins_set);
|
||||
}
|
||||
}
|
||||
}
|
||||
return any_mutations;
|
||||
@@ -2726,8 +2833,8 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
|
||||
}
|
||||
}
|
||||
|
||||
/* Add mutate operation, for partial map updates. */
|
||||
if (row->map_op_written) {
|
||||
/* Add mutate operation, for partial map or partial set updates. */
|
||||
if (row->map_op_written || row->set_op_written) {
|
||||
struct json *op, *mutations;
|
||||
bool any_mutations;
|
||||
|
||||
@@ -3590,6 +3697,42 @@ ovsdb_idl_txn_add_map_op(struct ovsdb_idl_row *row,
|
||||
}
|
||||
}
|
||||
|
||||
/* Inserts a new Set Operation into current transaction. */
|
||||
static void
|
||||
ovsdb_idl_txn_add_set_op(struct ovsdb_idl_row *row,
|
||||
const struct ovsdb_idl_column *column,
|
||||
struct ovsdb_datum *datum,
|
||||
enum set_op_type op_type)
|
||||
{
|
||||
const struct ovsdb_idl_table_class *class;
|
||||
size_t column_idx;
|
||||
struct set_op *set_op;
|
||||
|
||||
class = row->table->class;
|
||||
column_idx = column - class->columns;
|
||||
|
||||
/* Check if a set operation list exists for this column. */
|
||||
if (!row->set_op_written) {
|
||||
row->set_op_written = bitmap_allocate(class->n_columns);
|
||||
row->set_op_lists = xzalloc(class->n_columns *
|
||||
sizeof *row->set_op_lists);
|
||||
}
|
||||
if (!row->set_op_lists[column_idx]) {
|
||||
row->set_op_lists[column_idx] = set_op_list_create();
|
||||
}
|
||||
|
||||
/* Add a set operation to the corresponding list. */
|
||||
set_op = set_op_create(datum, op_type);
|
||||
bitmap_set1(row->set_op_written, column_idx);
|
||||
set_op_list_add(row->set_op_lists[column_idx], set_op, &column->type);
|
||||
|
||||
/* Add this row to the transactions's list of rows. */
|
||||
if (hmap_node_is_null(&row->txn_node)) {
|
||||
hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
|
||||
uuid_hash(&row->uuid));
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
is_valid_partial_update(const struct ovsdb_idl_row *row,
|
||||
const struct ovsdb_idl_column *column,
|
||||
@@ -3611,6 +3754,55 @@ is_valid_partial_update(const struct ovsdb_idl_row *row,
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Inserts the value described in 'datum' into the map in 'column' in
|
||||
* 'row_'. If the value doesn't already exist in 'column' then it's value
|
||||
* is added. The value in 'datum' must be of the same type as the values
|
||||
* in 'column'. This function takes ownership of 'datum'.
|
||||
*
|
||||
* Usually this function is used indirectly through one of the "update"
|
||||
* functions generated by vswitch-idl. */
|
||||
void
|
||||
ovsdb_idl_txn_write_partial_set(const struct ovsdb_idl_row *row_,
|
||||
const struct ovsdb_idl_column *column,
|
||||
struct ovsdb_datum *datum)
|
||||
{
|
||||
struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
|
||||
enum set_op_type op_type;
|
||||
|
||||
if (!is_valid_partial_update(row, column, datum)) {
|
||||
ovsdb_datum_destroy(datum, &column->type);
|
||||
free(datum);
|
||||
return;
|
||||
}
|
||||
|
||||
op_type = SET_OP_INSERT;
|
||||
|
||||
ovsdb_idl_txn_add_set_op(row, column, datum, op_type);
|
||||
}
|
||||
|
||||
/* Deletes the value specified in 'datum' from the set in 'column' in 'row_'.
|
||||
* The value in 'datum' must be of the same type as the keys in 'column'.
|
||||
* This function takes ownership of 'datum'.
|
||||
*
|
||||
* Usually this function is used indirectly through one of the "update"
|
||||
* functions generated by vswitch-idl. */
|
||||
void
|
||||
ovsdb_idl_txn_delete_partial_set(const struct ovsdb_idl_row *row_,
|
||||
const struct ovsdb_idl_column *column,
|
||||
struct ovsdb_datum *datum)
|
||||
{
|
||||
struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
|
||||
|
||||
if (!is_valid_partial_update(row, column, datum)) {
|
||||
struct ovsdb_type type_ = column->type;
|
||||
type_.value.type = OVSDB_TYPE_VOID;
|
||||
ovsdb_datum_destroy(datum, &type_);
|
||||
free(datum);
|
||||
return;
|
||||
}
|
||||
ovsdb_idl_txn_add_set_op(row, column, datum, SET_OP_DELETE);
|
||||
}
|
||||
|
||||
/* Inserts the key-value specified in 'datum' into the map in 'column' in
|
||||
* 'row_'. If the key already exist in 'column', then it's value is updated
|
||||
* with the value in 'datum'. The key-value in 'datum' must be of the same type
|
||||
|
Reference in New Issue
Block a user