[ovs-dev] [blp/ovs-reviews/raft9][PATCH] ovn-ctl: Support starting clustered OVN dbs

nusiddiq at redhat.com nusiddiq at redhat.com
Fri Nov 3 15:51:34 UTC 2017


From: Numan Siddique <nusiddiq at redhat.com>

This patch adds the options to start clustered OVN db servers.
To support this following options are added - '--db-nb-cluster-local-addr',
'--db-nb-cluster-remote-addr', '--db-sb-cluster-local-addr' and
'--db-sb-cluster-remote-addr'. If only '--db-(nb/sb)-cluster-local-addr' is defined
then clustered db is created (using ovsdb-tool create-cluster). If both are defined,
then the db is added to the cluster (using ovsdb-tool join-cluster)

Signed-off-by: Numan Siddique <nusiddiq at redhat.com>
---

This patch is on top of Ben's ovs-revews git repo branch raft9.

 ovn/utilities/ovn-ctl | 129 +++++++++++++++++++++++++++++++++++++-------------
 utilities/ovs-lib.in  |  47 ++++++++++++++++++
 2 files changed, 143 insertions(+), 33 deletions(-)

diff --git a/ovn/utilities/ovn-ctl b/ovn/utilities/ovn-ctl
index 3e247a1c6..45203219f 100755
--- a/ovn/utilities/ovn-ctl
+++ b/ovn/utilities/ovn-ctl
@@ -95,70 +95,120 @@ promote_ovnsb() {
 
 start_nb_ovsdb() {
     # Check and eventually start ovsdb-server for Northbound DB
-    if ! pidfile_is_running $DB_NB_PID; then
+    if pidfile_is_running $DB_NB_PID; then
+        return
+    fi
+
+    mode="standalone"
+
+    if test ! -z "$DB_NB_CLUSTER_LOCAL_ADDR"; then
+        mode="cluster"
+    elif test ! -z "$DB_NB_SYNC_FROM_ADDR"; then
+        mode="active_passive"
+        echo "$DB_NB_SYNC_FROM_PROTO:$DB_NB_SYNC_FROM_ADDR:\
+$DB_NB_SYNC_FROM_PORT" > $ovnnb_active_conf_file
+    fi
+
+    if test X$mode != "Xcluster"; then
         upgrade_db "$DB_NB_FILE" "$DB_NB_SCHEMA" 1>/dev/null 2>/dev/null
+    else
+        if test -z "$DB_NB_CLUSTER_REMOTE_ADDR"; then
+            create_cluster "$DB_NB_FILE" "$DB_NB_SCHEMA" \
+"$DB_NB_CLUSTER_LOCAL_ADDR"
+        else
+            join_cluster "$DB_NB_FILE" "OVN_Northbound" \
+"$DB_NB_CLUSTER_LOCAL_ADDR" "$DB_NB_CLUSTER_REMOTE_ADDR"
+        fi
+    fi
 
-        set ovsdb-server
+    set ovsdb-server
+    set "$@" --detach --monitor
+    set "$@" $OVN_NB_LOG --log-file=$OVN_NB_LOGFILE
+    set "$@" --remote=punix:$DB_NB_SOCK --pidfile=$DB_NB_PID
+    set "$@" --unixctl=ovnnb_db.ctl
 
-        set "$@" --detach --monitor
-        set "$@" $OVN_NB_LOG --log-file=$OVN_NB_LOGFILE
-        set "$@" --remote=punix:$DB_NB_SOCK --pidfile=$DB_NB_PID
+    # TODO (numans): Remove this 'if' once we have the fix to
+    # start ovsdb-server with the below options for the cluster db.
+    if test X$mode != "Xcluster"; then
         set "$@" --remote=db:OVN_Northbound,NB_Global,connections
-        set "$@" --unixctl=ovnnb_db.ctl
         set "$@" --private-key=db:OVN_Northbound,SSL,private_key
         set "$@" --certificate=db:OVN_Northbound,SSL,certificate
         set "$@" --ca-cert=db:OVN_Northbound,SSL,ca_cert
         set "$@" --ssl-protocols=db:OVN_Northbound,SSL,ssl_protocols
         set "$@" --ssl-ciphers=db:OVN_Northbound,SSL,ssl_ciphers
+    fi
 
-        if test X"$DB_NB_CREATE_INSECURE_REMOTE" = Xyes; then
-            set "$@" --remote=ptcp:$DB_NB_PORT:$DB_NB_ADDR
-        fi
+    if test X"$DB_NB_CREATE_INSECURE_REMOTE" = Xyes; then
+        set "$@" --remote=ptcp:$DB_NB_PORT:$DB_NB_ADDR
+    fi
 
-        if test ! -z "$DB_NB_SYNC_FROM_ADDR"; then
-            echo "$DB_NB_SYNC_FROM_PROTO:$DB_NB_SYNC_FROM_ADDR:$DB_NB_SYNC_FROM_PORT" > $ovnnb_active_conf_file
-        fi
+    if test -e $ovnnb_active_conf_file; then
+        set "$@" --sync-from=`cat $ovnnb_active_conf_file`
+    fi
 
-        if test -e $ovnnb_active_conf_file; then
-            set "$@" --sync-from=`cat $ovnnb_active_conf_file`
-        fi
+    $@ $DB_NB_FILE
 
-        $@ $DB_NB_FILE
+    if test -z "$DB_NB_CLUSTER_REMOTE_ADDR"; then
         ovn-nbctl init
     fi
 }
 
 start_sb_ovsdb() {
-    # Check and eventually start ovsdb-server for Southbound DB
-    if ! pidfile_is_running $DB_SB_PID; then
+    # Check and eventually start ovsdb-server for Northbound DB
+    if pidfile_is_running $DB_SB_PID; then
+        return
+    fi
+
+    mode="standalone"
+
+    if test ! -z "$DB_SB_CLUSTER_LOCAL_ADDR"; then
+        mode="cluster"
+    elif test ! -z "$DB_SB_SYNC_FROM_ADDR"; then
+        mode="active_passive"
+        echo "$DB_SB_SYNC_FROM_PROTO:$DB_SB_SYNC_FROM_ADDR:\
+$DB_SB_SYNC_FROM_PORT" > $ovnsb_active_conf_file
+    fi
+
+    if test X$mode != "Xcluster"; then
         upgrade_db "$DB_SB_FILE" "$DB_SB_SCHEMA" 1>/dev/null 2>/dev/null
+    else
+        if test -z "$DB_SB_CLUSTER_REMOTE_ADDR"; then
+            create_cluster "$DB_SB_FILE" "$DB_SB_SCHEMA" \
+"$DB_SB_CLUSTER_LOCAL_ADDR"
+        else
+            join_cluster "$DB_SB_FILE" "OVN_Southbound" \
+"$DB_SB_CLUSTER_LOCAL_ADDR" "$DB_SB_CLUSTER_REMOTE_ADDR"
+        fi
+    fi
 
-        set ovsdb-server
+    set ovsdb-server
+    set "$@" --detach --monitor
+    set "$@" $OVN_SB_LOG --log-file=$OVN_SB_LOGFILE
+    set "$@" --remote=punix:$DB_SB_SOCK --pidfile=$DB_SB_PID
+    set "$@" --unixctl=ovnsb_db.ctl
 
-        set "$@" --detach --monitor
-        set "$@" $OVN_SB_LOG --log-file=$OVN_SB_LOGFILE
-        set "$@" --remote=punix:$DB_SB_SOCK --pidfile=$DB_SB_PID
+    # TODO (numans): Remove this 'if' once we have the fix to
+    # start ovsdb-server with the below options for the cluster db.
+    if test X$mode != "Xcluster"; then
         set "$@" --remote=db:OVN_Southbound,SB_Global,connections
-        set "$@" --unixctl=ovnsb_db.ctl
         set "$@" --private-key=db:OVN_Southbound,SSL,private_key
         set "$@" --certificate=db:OVN_Southbound,SSL,certificate
         set "$@" --ca-cert=db:OVN_Southbound,SSL,ca_cert
         set "$@" --ssl-protocols=db:OVN_Southbound,SSL,ssl_protocols
         set "$@" --ssl-ciphers=db:OVN_Southbound,SSL,ssl_ciphers
+    fi
 
-        if test X"$DB_SB_CREATE_INSECURE_REMOTE" = Xyes; then
-            set "$@" --remote=ptcp:$DB_SB_PORT:$DB_SB_ADDR
-        fi
+    if test X"$DB_SB_CREATE_INSECURE_REMOTE" = Xyes; then
+        set "$@" --remote=ptcp:$DB_SB_PORT:$DB_SB_ADDR
+    fi
 
-        if test ! -z "$DB_SB_SYNC_FROM_ADDR"; then
-            echo "$DB_SB_SYNC_FROM_PROTO:$DB_SB_SYNC_FROM_ADDR:$DB_SB_SYNC_FROM_PORT" > $ovnsb_active_conf_file
-        fi
+    if test -e $ovnsb_active_conf_file; then
+        set "$@" --sync-from=`cat $ovnsb_active_conf_file`
+    fi
 
-        if test -e $ovnsb_active_conf_file; then
-            set "$@" --sync-from=`cat $ovnsb_active_conf_file`
-        fi
+    $@ $DB_SB_FILE
 
-        $@ $DB_SB_FILE
+    if test -z "$DB_SB_CLUSTER_REMOTE_ADDR"; then
         ovn-sbctl init
     fi
 }
@@ -383,6 +433,11 @@ set_defaults () {
     DB_NB_CREATE_INSECURE_REMOTE="no"
 
     MONITOR="yes"
+
+    DB_NB_CLUSTER_LOCAL_ADDR=""
+    DB_NB_CLUSTER_REMOTE_ADDR=""
+    DB_SB_CLUSTER_LOCAL_ADDR=""
+    DB_SB_CLUSTER_REMOTE_ADDR=""
 }
 
 set_option () {
@@ -469,6 +524,14 @@ File location options:
   --db-sb-sync-from-port=ADDR OVN Southbound active db tcp port (default: $DB_SB_SYNC_FROM_PORT)
   --db-sb-sync-from-proto=PROTO OVN Southbound active db transport (default: $DB_SB_SYNC_FROM_PROTO)
   --db-sb-create-insecure-remote=yes|no Create ptcp OVN Southbound remote (default: $DB_SB_CREATE_INSECURE_REMOTE)
+  --db-nb-cluster-local-addr=ADDR OVN_Northbound cluster local address \
+  (default: $DB_NB_CLUSTER_LOCAL_ADDR)
+  --db-nb-cluster-remote-addr=ADDR OVN_Northbound cluster remote address \
+  (default: $DB_NB_CLUSTER_REMOTE_ADDR)
+  --db-sb-cluster-local-addr=ADDR OVN_Northbound cluster local address \
+  (default: $DB_SB_CLUSTER_LOCAL_ADDR)
+  --db-sb-cluster-remote-addr=ADDR OVN_Northbound cluster remote address \
+  (default: $DB_SB_CLUSTER_REMOTE_ADDR)
 
 Default directories with "configure" option and environment variable override:
   logs: /usr/local/var/log/openvswitch (--with-logdir, OVS_LOGDIR)
diff --git a/utilities/ovs-lib.in b/utilities/ovs-lib.in
index ac2da85b7..f99748ae0 100644
--- a/utilities/ovs-lib.in
+++ b/utilities/ovs-lib.in
@@ -440,3 +440,50 @@ upgrade_db () {
         fi
     fi
 }
+
+create_cluster () {
+    DB_FILE="$1"
+    DB_SCHEMA="$2"
+    LOCAL_ADDR="$3"
+
+    if test ! -e "$DB_FILE"; then
+        action "Creating cluster database $DB_FILE" ovsdb_tool create-cluster "$DB_FILE" "$DB_SCHEMA" "$LOCAL_ADDR"
+    else
+        # DB file exists. Check if it is a clustered db or not. If it is a
+        # clustered db, nothing to be done. Else create a clustered db from that.
+        ovsdb_tool db-local-address $DB_FILE
+        if [ "$?" = "1" ]; then
+            version=`ovsdb_tool db-version "$DB_FILE"`
+            cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
+            backup=$DB_FILE.backup$version-$cksum
+            action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
+
+            action "Creating cluster database $DB_FILE from existing one" \
+            ovsdb_tool create-cluster "$DB_FILE" "$backup" "$LOCAL_ADDR"
+        fi
+    fi
+}
+
+join_cluster() {
+    DB_FILE="$1"
+    SCHEMA_NAME="$2"
+    LOCAL_ADDR="$3"
+    REMOTE_ADDR="$4"
+
+    if test ! -e "$DB_FILE"; then
+        ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR" "$REMOTE_ADDR"
+    else
+        # DB file exists. Check if it is a clustered db or not. If it is a
+        # clustered db, nothing to be done. Else backup the db and join the cluster.
+        ovsdb_tool db-local-address $DB_FILE
+        if [ "$?" = "1" ]; then
+            version=`ovsdb_tool db-version "$DB_FILE"`
+            cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
+            backup=$DB_FILE.backup$version-$cksum
+            action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
+
+            action "Creating cluster database $DB_FILE from existing one" \
+            ovsdb_tool join-cluster "$DB_FILE" "$SCHEMA_NAME" "$LOCAL_ADDR"
+        fi
+    fi
+}
-- 
2.13.5



More information about the dev mailing list