diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index 8a9c6888..bbdffedb 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -207,8 +207,20 @@ def run_load(args): def run_set_shards(args): b = bigchaindb.Bigchain() - r.table('bigchain').reconfigure(shards=args.num_shards, replicas=1).run(b.conn) - r.table('backlog').reconfigure(shards=args.num_shards, replicas=1).run(b.conn) + for table in ['bigchain', 'backlog']: + # See https://www.rethinkdb.com/api/python/config/ + table_config = r.table(table).config().run(b.conn) + num_replicas = len(table_config['shards'][0]['replicas']) + r.table(table).reconfigure(shards=args.num_shards, replicas=num_replicas).run(b.conn) + + +def run_set_replicas(args): + b = bigchaindb.Bigchain() + for table in ['bigchain', 'backlog']: + # See https://www.rethinkdb.com/api/python/config/ + table_config = r.table(table).config().run(b.conn) + num_shards = len(table_config['shards']) + r.table(table).reconfigure(shards=num_shards, replicas=args.num_replicas).run(b.conn) def main(): @@ -255,9 +267,18 @@ def main(): sharding_parser = subparsers.add_parser('set-shards', help='Configure number of shards') - sharding_parser.add_argument('num_shards', metavar='num_shards', type=int, default=1, + sharding_parser.add_argument('num_shards', metavar='num_shards', + type=int, default=1, help='Number of shards') + # parser for configuring the number of replicas + replicas_parser = subparsers.add_parser('set-replicas', + help='Configure number of replicas') + + replicas_parser.add_argument('num_replicas', metavar='num_replicas', + type=int, default=1, + help='Number of replicas (i.e. the replication factor)') + load_parser = subparsers.add_parser('load', help='Write transactions to the backlog') diff --git a/docs/source/servers/bigchaindb-cli.md b/docs/source/servers/bigchaindb-cli.md index ad9fd5a0..6ca22328 100644 --- a/docs/source/servers/bigchaindb-cli.md +++ b/docs/source/servers/bigchaindb-cli.md @@ -49,4 +49,11 @@ $ bigchaindb load -h This command is used to set the number of shards in the underlying datastore. For example, the following command will set the number of shards to four: ```text $ bigchaindb set-shards 4 -``` \ No newline at end of file +``` + +### bigchaindb set-replicas + +This command is used to set the number of replicas (of each shard) in the underlying datastore. For example, the following command will set the number of replicas to three (i.e. it will set the replication factor to three): +```text +$ bigchaindb set-replicas 3 +``` diff --git a/tests/test_commands.py b/tests/test_commands.py index 12c1350e..37af2b0e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -245,3 +245,19 @@ def test_set_shards(b): for table in table_config: if table['name'] in ['backlog', 'bigchain']: assert len(table['shards']) == 3 + + +def test_set_replicas(b): + import rethindb as r + from bigchaindb.commands.bigchain import run_set_replicas + + # set the number of replicas + args = Namespace(num_replicas=2) + run_set_replicas(args) + + # check that the replication factor got set to 2 in all tables + for table in ['backlog', 'bigchain']: + # See https://www.rethinkdb.com/api/python/config/ + table_config = r.table(table).config().run(b.conn) + num_replicas = len(table_config['shards'][0]['replicas']) + assert num_replicas == 2