From patchwork Fri Aug 26 23:15:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Zhou X-Patchwork-Id: 663265 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (archives.nicira.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 3sLcPB08sXz9sBR for ; Sat, 27 Aug 2016 09:16:29 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 25FBE10A6A; Fri, 26 Aug 2016 16:16:10 -0700 (PDT) X-Original-To: dev@openvswitch.com Delivered-To: dev@openvswitch.com Received: from mail-pf0-f177.google.com (mail-pf0-f177.google.com [209.85.192.177]) by archives.nicira.com (Postfix) with ESMTPS id 2479A10A50 for ; Fri, 26 Aug 2016 16:16:04 -0700 (PDT) Received: by mail-pf0-f177.google.com with SMTP id x72so32786392pfd.2 for ; Fri, 26 Aug 2016 16:16:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gNuPFwiVUrpr9OLWUltmZwAfuhLS5vSPetCnZZrl4+w=; b=FfcJkRlxWI1KcTsLNazrVOP6DTE7ZjohGqq1XSpDzI6zjnWh+dh5HWHEYMkB6k1NO6 j9eRUAvaO/b820S7E6zyFxP00r7PFxOlqZROt2PPcwNvX4jfD1jyoCKg7i5MYna45XZM EE8DWX/OgxfHXd6VQ+XMfjpMMf7G5kRl/h86Jatm6WGqS4TfWcy7YQxmd9QrJi53M1K3 fEpBv+zmYrY4EV26oBYwyS0SPvo67yqrKhMn+dFTl7YUi9SzqEh0m+6q9YiQVoeU1zox o7tVM57CB18owc+9ot3GdFeTWi1oFXG8V1H769Ev39kKOWMEiw9tuyORLdFc6isbwDfX s0Zw== X-Gm-Message-State: AE9vXwNj8ArF9mfWXSMsFl3ikNk+/hyNg/fgyPaytKxJpyAyoEbcMk7pw9qGic/7BKDtVA== X-Received: by 10.98.155.149 with SMTP id e21mr10245999pfk.65.1472253363334; Fri, 26 Aug 2016 16:16:03 -0700 (PDT) Received: from ubuntu.localdomain ([208.91.1.34]) by smtp.gmail.com with ESMTPSA id yu7sm10195832pab.45.2016.08.26.16.16.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Aug 2016 16:16:02 -0700 (PDT) From: Andy Zhou To: dev@openvswitch.com Date: Fri, 26 Aug 2016 16:15:51 -0700 Message-Id: <1472253354-69501-5-git-send-email-azhou@ovn.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1472253354-69501-1-git-send-email-azhou@ovn.org> References: <1472253354-69501-1-git-send-email-azhou@ovn.org> Subject: [ovs-dev] [replication SMv2 4/7] ovsdb: (Re)hide struct db in ovsdb-server.c X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" It seems odd that the currently replication implementation moves the struct db from ovsdb-server.c (file private) to replication.h (global). This patch moves the 'struct db' defintion back into ovsdb-server.c, Signed-off-by: Andy Zhou --- ovsdb/ovsdb-server.c | 65 +++++++++++++++++++++++++-------- ovsdb/replication.c | 100 ++++++++++++++++++++++++++++----------------------- ovsdb/replication.h | 19 ++-------- 3 files changed, 110 insertions(+), 74 deletions(-) diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c index b1d7fc5..d4bc80b 100644 --- a/ovsdb/ovsdb-server.c +++ b/ovsdb/ovsdb-server.c @@ -60,7 +60,15 @@ VLOG_DEFINE_THIS_MODULE(ovsdb_server); -struct db; +struct db { + /* Initialized in main(). */ + char *filename; + struct ovsdb_file *file; + struct ovsdb *db; + + /* Only used by update_remote_status(). */ + struct ovsdb_txn *txn; +}; /* SSL configuration. */ static char *private_key_file; @@ -68,7 +76,7 @@ static char *certificate_file; static char *ca_cert_file; static bool bootstrap_ca_cert; -/* Replication configuration. */ +/* Replication current state. */ static bool is_backup_server; static unixctl_cb_func ovsdb_server_exit; @@ -123,6 +131,17 @@ static void load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames); static void +ovsdb_replication_init(struct shash *all_dbs) +{ + replication_init(); + struct shash_node *node; + SHASH_FOR_EACH (node, all_dbs) { + struct db *db = node->data; + replication_add_db(db->db->schema->name, db->db); + } +} + +static void main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs, struct unixctl_server *unixctl, struct sset *remotes, struct process *run_process, bool *exiting) @@ -169,7 +188,7 @@ main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs, ovsdb_jsonrpc_server_run(jsonrpc); if (is_backup_server) { - replication_run(all_dbs); + replication_run(); } SHASH_FOR_EACH(node, all_dbs) { @@ -240,7 +259,6 @@ main(int argc, char *argv[]) service_start(&argc, &argv); fatal_ignore_sigpipe(); process_init(); - replication_init(); parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command); daemon_become_new_user(false); @@ -358,13 +376,13 @@ main(int argc, char *argv[]) ovsdb_server_get_active_ovsdb_server, NULL); unixctl_command_register("ovsdb-server/connect-active-ovsdb-server", "", 0, 0, ovsdb_server_connect_active_ovsdb_server, - NULL); + &all_dbs); unixctl_command_register("ovsdb-server/disconnect-active-ovsdb-server", "", 0, 0, ovsdb_server_disconnect_active_ovsdb_server, NULL); unixctl_command_register("ovsdb-server/set-sync-excluded-tables", "", 0, 1, ovsdb_server_set_sync_excluded_tables, - NULL); + &all_dbs); unixctl_command_register("ovsdb-server/get-sync-excluded-tables", "", 0, 0, ovsdb_server_get_sync_excluded_tables, NULL); @@ -374,6 +392,10 @@ main(int argc, char *argv[]) unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0, ovsdb_server_disable_monitor_cond, jsonrpc); + if (is_backup_server) { + ovsdb_replication_init(&all_dbs); + } + main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting); ovsdb_jsonrpc_server_destroy(jsonrpc); @@ -470,6 +492,21 @@ open_db(struct server_config *config, const char *filename) return error; } +static const struct db * +find_db(const struct shash *all_dbs, const char *db_name) +{ + struct shash_node *node; + + SHASH_FOR_EACH(node, all_dbs) { + struct db *db = node->data; + if (!strcmp(db->db->schema->name, db_name)) { + return db; + } + } + + return NULL; +} + static char * OVS_WARN_UNUSED_RESULT parse_db_column__(const struct shash *all_dbs, const char *name_, char *name, @@ -1059,8 +1096,6 @@ ovsdb_server_set_active_ovsdb_server(struct unixctl_conn *conn, void *arg_ OVS_UNUSED) { set_active_ovsdb_server(argv[1]); - is_backup_server = true; - VLOG_INFO("become a backup server"); unixctl_command_reply(conn, NULL); } @@ -1077,10 +1112,12 @@ static void ovsdb_server_connect_active_ovsdb_server(struct unixctl_conn *conn, int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, - void *arg_ OVS_UNUSED) + void *all_dbs_) { - if (is_backup_server) { - replication_init(); + struct shash *all_dbs = all_dbs_; + + if (!is_backup_server) { + ovsdb_replication_init(all_dbs); } is_backup_server = true; unixctl_command_reply(conn, NULL); @@ -1094,7 +1131,6 @@ ovsdb_server_disconnect_active_ovsdb_server(struct unixctl_conn *conn, { disconnect_active_server(); is_backup_server = false; - VLOG_INFO("become an active server"); unixctl_command_reply(conn, NULL); } @@ -1102,13 +1138,14 @@ static void ovsdb_server_set_sync_excluded_tables(struct unixctl_conn *conn, int argc OVS_UNUSED, const char *argv[], - void *arg_ OVS_UNUSED) + void *all_dbs_) { + struct shash *all_dbs = all_dbs_; char *err = set_blacklist_tables(argv[1], true); if (!err) { if (is_backup_server) { - replication_init(); + ovsdb_replication_init(all_dbs); } set_blacklist_tables(argv[1], false); } diff --git a/ovsdb/replication.c b/ovsdb/replication.c index 815730d..9658145 100644 --- a/ovsdb/replication.c +++ b/ovsdb/replication.c @@ -49,17 +49,16 @@ static void fetch_dbs(struct jsonrpc *rpc, struct svec *dbs); static struct ovsdb_schema *fetch_schema(struct jsonrpc *rpc, const char *database); -static void send_monitor_requests(struct shash *all_dbs); +static void send_monitor_requests(void); static void add_monitored_table(struct ovsdb_table_schema *table, struct json *monitor_requests); -static void get_initial_db_state(const struct db *database); +static void get_initial_db_state(struct ovsdb *db); static void reset_database(struct ovsdb *db, struct ovsdb_txn *txn); -static struct ovsdb_error *reset_databases(struct shash *all_dbs); +static struct ovsdb_error *reset_databases(void); -static void check_for_notifications(struct shash *all_dbs); -static void process_notification(struct json *table_updates, - struct ovsdb *database); +static void check_for_notifications(void); +static void process_notification(struct json *table_updates, struct ovsdb *db); static struct ovsdb_error *process_table_update(struct json *table_update, const char *table_name, struct ovsdb *database, @@ -99,9 +98,18 @@ static void request_ids_destroy(void); void request_ids_clear(void); +/* Currently replicating DBs. + * replication_dbs is an shash of 'struct ovsdb *'s that stores the + * replicating dbs. */ +static struct shash replication_dbs = SHASH_INITIALIZER(&replication_dbs); +/* Find 'struct ovsdb' by name within 'replication_dbs' */ +static struct ovsdb* find_db(const char *db_name); + + void replication_init(void) { + shash_clear(&replication_dbs); if (rpc) { disconnect_active_server(); } @@ -109,12 +117,19 @@ replication_init(void) } void -replication_run(struct shash *all_dbs) +replication_add_db(const char *database, struct ovsdb *db) +{ + struct shash_node *node = xmalloc(sizeof *node); + shash_add_assert(&replication_dbs, database, db); +} + +void +replication_run(void) { if (sset_is_empty(&monitored_tables) && active_ovsdb_server) { /* Reset local databases. */ if (reset_dbs) { - struct ovsdb_error *error = reset_databases(all_dbs); + struct ovsdb_error *error = reset_databases(); if (error) { /* In case reset DB fails, log the error before exiting. */ char *msg = ovsdb_error_to_string(error); @@ -133,10 +148,10 @@ replication_run(struct shash *all_dbs) } /* Send monitor requests. */ - send_monitor_requests(all_dbs); + send_monitor_requests(); } if (!sset_is_empty(&monitored_tables)) { - check_for_notifications(all_dbs); + check_for_notifications(); } } @@ -160,9 +175,12 @@ get_active_ovsdb_server(void) return active_ovsdb_server; } -/* Parse 'blacklist' to rebuild 'blacklist_tables'. The current - * black list tables will be wiped out, regardless if 'blacklist' - * can be parsed or not. +/* Parse 'blacklist' to rebuild 'blacklist_tables'. + * + * Set 'blacklist_tables' according to 'blacklist'. + * + * If 'dryrun' is true, the function only attempts to parse + * 'blacklist' without actually changing the 'blacklist table'. * * On error, Returns the error string which the caller is * responsible for freeing. Return NULL otherwise. */ @@ -176,6 +194,7 @@ set_blacklist_tables(const char *blacklist, bool dryrun) const char *longname; if (!dryrun) { + /* Can only add to an empty shash. */ blacklist_tables_clear(); } @@ -280,6 +299,7 @@ disconnect_active_server(void) jsonrpc_close(rpc); rpc = NULL; sset_clear(&monitored_tables); + shash_clear(&replication_dbs); } void @@ -296,33 +316,25 @@ replication_destroy(void) } request_ids_destroy(); + shash_destroy(&replication_dbs); } -const struct db * -find_db(const struct shash *all_dbs, const char *db_name) +static struct ovsdb * +find_db(const char *db_name) { - struct shash_node *node; - - SHASH_FOR_EACH (node, all_dbs) { - struct db *db = node->data; - if (!strcmp(db->db->schema->name, db_name)) { - return db; - } - } - - return NULL; + return shash_find_data(&replication_dbs, db_name); } static struct ovsdb_error * -reset_databases(struct shash *all_dbs) +reset_databases() { struct shash_node *db_node; struct ovsdb_error *error = NULL; - SHASH_FOR_EACH (db_node, all_dbs) { - struct db *db = db_node->data; - struct ovsdb_txn *txn = ovsdb_txn_create(db->db); - reset_database(db->db, txn); + SHASH_FOR_EACH (db_node, &replication_dbs) { + struct ovsdb *db = db_node->data; + struct ovsdb_txn *txn = ovsdb_txn_create(db); + reset_database(db, txn); error = ovsdb_txn_commit(txn, false); } @@ -445,7 +457,7 @@ fetch_schema(struct jsonrpc *rpc, const char *database) } static void -send_monitor_requests(struct shash *all_dbs) +send_monitor_requests(void) { const char *db_name; struct svec dbs; @@ -454,12 +466,12 @@ send_monitor_requests(struct shash *all_dbs) svec_init(&dbs); fetch_dbs(rpc, &dbs); SVEC_FOR_EACH (i, db_name, &dbs) { - const struct db *database = find_db(all_dbs, db_name); + struct ovsdb *db = find_db(db_name); - if (database) { + if (db) { struct ovsdb_schema *local_schema, *remote_schema; - local_schema = database->db->schema; + local_schema = db->schema; remote_schema = fetch_schema(rpc, db_name); if (ovsdb_schema_equal(local_schema, remote_schema)) { struct jsonrpc_msg *request; @@ -487,7 +499,7 @@ send_monitor_requests(struct shash *all_dbs) monitor_request); request = jsonrpc_create_request("monitor", monitor, NULL); jsonrpc_send(rpc, request); - get_initial_db_state(database); + get_initial_db_state(db); } ovsdb_schema_destroy(remote_schema); } @@ -496,14 +508,14 @@ send_monitor_requests(struct shash *all_dbs) } static void -get_initial_db_state(const struct db *database) +get_initial_db_state(struct ovsdb *db) { struct jsonrpc_msg *msg; jsonrpc_recv_block(rpc, &msg); if (msg->type == JSONRPC_REPLY) { - process_notification(msg->result, database->db); + process_notification(msg->result, db); } jsonrpc_msg_destroy(msg); @@ -524,7 +536,7 @@ add_monitored_table(struct ovsdb_table_schema *table, } static void -check_for_notifications(struct shash *all_dbs) +check_for_notifications() { struct jsonrpc_msg *msg; int error; @@ -551,9 +563,9 @@ check_for_notifications(struct shash *all_dbs) if (params->type == JSON_ARRAY && params->u.array.n == 2) { char *db_name = params->u.array.elems[0]->u.string; - const struct db *database = find_db(all_dbs, db_name); - if (database) { - process_notification(params->u.array.elems[1], database->db); + struct ovsdb *db = find_db(db_name); + if (db) { + process_notification(params->u.array.elems[1], db); } } } @@ -562,7 +574,7 @@ check_for_notifications(struct shash *all_dbs) } static void -process_notification(struct json *table_updates, struct ovsdb *database) +process_notification(struct json *table_updates, struct ovsdb *db) { struct ovsdb_error *error; struct ovsdb_txn *txn; @@ -572,7 +584,7 @@ process_notification(struct json *table_updates, struct ovsdb *database) return; } - txn = ovsdb_txn_create(database); + txn = ovsdb_txn_create(db); error = NULL; /* Process each table update. */ @@ -580,7 +592,7 @@ process_notification(struct json *table_updates, struct ovsdb *database) SHASH_FOR_EACH (node, json_object(table_updates)) { struct json *table_update = node->data; if (table_update) { - error = process_table_update(table_update, node->name, database, txn); + error = process_table_update(table_update, node->name, db, txn); if (error) { break; } diff --git a/ovsdb/replication.h b/ovsdb/replication.h index 1de9c8b..7745b47 100644 --- a/ovsdb/replication.h +++ b/ovsdb/replication.h @@ -19,32 +19,19 @@ #define REPLICATION_H 1 #include -#include "openvswitch/shash.h" +struct ovsdb; - -struct db { - /* Initialized in main(). */ - char *filename; - struct ovsdb_file *file; - struct ovsdb *db; - - /* Only used by update_remote_status(). */ - struct ovsdb_txn *txn; -}; - -/* Functions to be called from the main loop. */ void replication_init(void); -void replication_run(struct shash *dbs); +void replication_run(void); void replication_wait(void); void replication_destroy(void); void replication_usage(void); +void replication_add_db(const char *databse, struct ovsdb *db); -/* Unixctl APIs */ void set_active_ovsdb_server(const char *remote_server); const char *get_active_ovsdb_server(void); char *set_blacklist_tables(const char *blacklist, bool dryrun); char *get_blacklist_tables(void); void disconnect_active_server(void); -const struct db *find_db(const struct shash *all_dbs, const char *db_name); #endif /* ovsdb/replication.h */