From patchwork Mon Aug 26 21:01:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: aginwala aginwala X-Patchwork-Id: 1153456 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Ggokz+++"; dkim-atps=neutral Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46HPZy2ftgz9sN4 for ; Tue, 27 Aug 2019 07:02:18 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id EFC651532; Mon, 26 Aug 2019 21:02:16 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id BA1EAC9A for ; Mon, 26 Aug 2019 21:02:15 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id EAFD888E for ; Mon, 26 Aug 2019 21:02:14 +0000 (UTC) Received: by mail-wm1-f41.google.com with SMTP id p74so826835wme.4 for ; Mon, 26 Aug 2019 14:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/7ziDjsQIgE4Wu8rIvI0zUhMc49UDzaimxHSRDgmV20=; b=Ggokz+++EPiMl6BhurtIvX0Oz603EamA+kb6V2k6CynHnlgdsIGn2TbWXQQcWpnPvP SFbLVfWkwyy5UGB3wWx8eqiiEARXlND7WWeCrqNTVay+HY90jif8zOeBabyccfEZfgDV 1/GQrXTCv4VHQZD2SCbEbVadK3Xkzsb9uOoLjXLgVQ4hlDGubgCdIMEpNZW1XVQTcts+ xnN58eySm7+vYEUVM7aPHUv9g8c7r51cZn2VCos0Z310Los4P3ak2A0/QEwgpl6c0S7f Xm2jqgNs8lE0OcID49BsfimliZBOlOGJlzwItr9ya+qO5WE7quV9CCFghYQCozpLZvNB OkRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/7ziDjsQIgE4Wu8rIvI0zUhMc49UDzaimxHSRDgmV20=; b=hgzTnc4tDuATpe/yDKVktpw/gzF5wEP++bwc4SovPOTNStrQqJrAFSxnfFhUTbEJya gT1Ay7HBpn448PpwLwJh2U65k1RAw9klnnyvwtT65VFAZJKhQ49d0HlTnopGsQA34yNu FVTHRbpmwbihXQ6yT0Vck8njGxd3PRwLI0AmaWAc5t/JT6ungKFnEjJ0gRiVF+LLKjCa 5cEICPnBrqaECTOqvaBbbDYbcllou33ViyxlGHx9kVyfOmKpk1FWew4tYq1imyRjyvRJ DThuYbYqoCHItR5V9TroZqfL131PzV9uhILTcteD1AiOE0OZJ7QJQTeWigqTNRJtfVw5 KdKw== X-Gm-Message-State: APjAAAWplk3ce8gJKn8HYOff58q3IAqXcaF5DmMWIIo0uD28deLcki6Y z1V6gZDFjPpj8ZJE7edVxb6v+FEQ X-Google-Smtp-Source: APXvYqycKtG2pCDU5ApU3Hso8jFNLt22/eutiLrnNpWiYPjEvC3ijJ9/idqWGn3y+Y3J01KcsQCZNQ== X-Received: by 2002:a1c:2dcf:: with SMTP id t198mr22708873wmt.147.1566853333283; Mon, 26 Aug 2019 14:02:13 -0700 (PDT) Received: from LM-SJC-11015761.corp.ebay.com ([216.113.160.77]) by smtp.gmail.com with ESMTPSA id f7sm16397683wrf.8.2019.08.26.14.02.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Aug 2019 14:02:12 -0700 (PDT) From: amginwal@gmail.com To: dev@openvswitch.org Date: Mon, 26 Aug 2019 14:01:52 -0700 Message-Id: <20190826210152.11417-1-amginwal@gmail.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Aliasgar Ginwala Subject: [ovs-dev] [PATCH v3] ovsdb-tool: Convert clustered db to standalone db. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org From: Aliasgar Ginwala Add support in ovsdb-tool for migrating clustered dbs to standalone dbs. E.g. usage to migrate nb/sb db to standalone db from raft: ovsdb-tool cluster-to-standalone ovnnb_db.db ovnnb_db_cluster.db Signed-off-by: Aliasgar Ginwala --- Documentation/ref/ovsdb.7.rst | 3 + ovsdb/ovsdb-tool.c | 112 +++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/Documentation/ref/ovsdb.7.rst b/Documentation/ref/ovsdb.7.rst index cd1c63d64..b12d8066c 100644 --- a/Documentation/ref/ovsdb.7.rst +++ b/Documentation/ref/ovsdb.7.rst @@ -514,6 +514,9 @@ standalone database from the contents of a running clustered database. When the cluster is down and cannot be revived, ``ovsdb-client backup`` will not work. +Use ``ovsdb-tool cluster-to-standalone`` to convert clustered database to +standalone database when the cluster is down and cannot be revived. + Upgrading or Downgrading a Database ----------------------------------- diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c index 438f97590..1956e5492 100644 --- a/ovsdb/ovsdb-tool.c +++ b/ovsdb/ovsdb-tool.c @@ -173,6 +173,9 @@ usage(void) " compare-versions A OP B compare OVSDB schema version numbers\n" " query [DB] TRNS execute read-only transaction on DB\n" " transact [DB] TRNS execute read/write transaction on DB\n" + " cluster-to-standalone [DB [DB]] Convert clustered DB to\n" + "standalone DB when cluster is down and cannot be\n" + "revived\n" " [-m]... show-log [DB] print DB's log entries\n" "The default DB is %s.\n" "The default SCHEMA is %s.\n", @@ -942,6 +945,63 @@ print_raft_record(const struct raft_record *r, } } +static void +write_raft_header_to_file(const struct json *data, + struct ovsdb_log *db_log_data) +{ + if (!data || json_array(data)->n != 2) { + ovs_fatal(0, "***Invalid data***\n"); + } + + struct json *schema_json = json_array(data)->elems[0]; + if (schema_json->type != JSON_NULL) { + struct ovsdb_schema *schema; + check_ovsdb_error(ovsdb_schema_from_json(schema_json, &schema)); + check_ovsdb_error(ovsdb_log_write_and_free(db_log_data, schema_json)); + } + + struct json *data_json = json_array(data)->elems[1]; + if (!data_json || data_json->type != JSON_OBJECT) { + ovs_fatal(0, "***invalid data***\n"); + } + if (data_json->type != JSON_NULL) { + check_ovsdb_error(ovsdb_log_write_and_free(db_log_data, data_json)); + } +} + +static void +write_raft_header(const struct raft_header *h, struct ovsdb_log *db_log_data) +{ + if (h->snap_index) { + write_raft_header_to_file(h->snap.data, db_log_data); + } +} + +static void +write_raft_record_to_file(const struct json *data, + struct ovsdb_log *db_log_data) +{ + if (json_array(data)->n != 2) { + ovs_fatal(0, "***invalid data***\n"); + } + + struct json *data_json = json_array(data)->elems[1]; + if (data_json->type != JSON_NULL) { + check_ovsdb_error(ovsdb_log_write_and_free(db_log_data, data_json)); + } +} + +static void +write_raft_record(const struct raft_record *r, struct ovsdb_log *db_log_data) +{ + if (r->type == RAFT_REC_ENTRY) { + if (!r->entry.data) { + return; + } + write_raft_record_to_file(r->entry.data, db_log_data); + } +} + static void do_show_log_cluster(struct ovsdb_log *log) { @@ -1511,6 +1571,54 @@ do_compare_versions(struct ovs_cmdl_context *ctx) exit(result ? 0 : 2); } +static void +do_convert_to_standalone(struct ovsdb_log *log, struct ovsdb_log *db_log_data) +{ + for (unsigned int i = 0; ; i++) { + struct json *json; + check_ovsdb_error(ovsdb_log_read(log, &json)); + if (!json) { + break; + } + + struct ovsdb_error *error; + if (i == 0) { + struct raft_header h; + check_ovsdb_error(raft_header_from_json(&h, json)); + write_raft_header(&h, db_log_data); + check_ovsdb_error(ovsdb_log_commit_block(db_log_data)); + raft_header_uninit(&h); + + } else { + struct raft_record r; + check_ovsdb_error(raft_record_from_json(&r, json)); + write_raft_record(&r, db_log_data); + check_ovsdb_error(ovsdb_log_commit_block(db_log_data)); + raft_record_uninit(&r); + } + } +} + +static void +do_cluster_standalone(struct ovs_cmdl_context *ctx) +{ + const char *db_file_name = ctx->argv[1]; + const char *cluster_db_file_name = ctx->argv[2]; + struct ovsdb_log *log; + struct ovsdb_log *db_log_data; + + check_ovsdb_error(ovsdb_log_open(cluster_db_file_name, + OVSDB_MAGIC"|"RAFT_MAGIC, + OVSDB_LOG_READ_ONLY, -1, &log)); + check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_MAGIC, + OVSDB_LOG_CREATE_EXCL, -1, &db_log_data)); + if (strcmp(ovsdb_log_get_magic(log), RAFT_MAGIC) != 0) { + ovs_fatal(0, "Database is not clustered db.\n"); + } + do_convert_to_standalone(log, db_log_data); + ovsdb_log_close(db_log_data); + ovsdb_log_close(log); +} static void do_help(struct ovs_cmdl_context *ctx OVS_UNUSED) @@ -1550,7 +1658,9 @@ static const struct ovs_cmdl_command all_commands[] = { { "compare-versions", "a op b", 3, 3, do_compare_versions, OVS_RO }, { "help", NULL, 0, INT_MAX, do_help, OVS_RO }, { "list-commands", NULL, 0, INT_MAX, do_list_commands, OVS_RO }, - { NULL, NULL, 0, 0, NULL, OVS_RO }, + { "cluster-to-standalone", "[db [clusterdb]]", 2, 2, + do_cluster_standalone, OVS_RW }, + { NULL, NULL, 2, 2, NULL, OVS_RO }, }; static const struct ovs_cmdl_command *get_all_commands(void)