diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index b37a3812..e6b4314d 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -13,6 +13,7 @@ import builtins import logstats +import rethinkdb as r import bigchaindb import bigchaindb.config_utils @@ -203,6 +204,12 @@ def run_load(args): workers.start() +def run_sharding(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) + + def main(): parser = argparse.ArgumentParser( description='Control your BigchainDB node.', @@ -243,6 +250,13 @@ def main(): subparsers.add_parser('start', help='Start BigchainDB') + # parser for configuring the number of shards + sharding_parser = subparsers.add_parser('sharding', + help='Configure number of shards') + + sharding_parser.add_argument('num_shards', metavar='num_shards', type=int, default=1, + help='Number of shards') + load_parser = subparsers.add_parser('load', help='Write transactions to the backlog') diff --git a/deploy-cluster-aws/awsdeploy.sh b/deploy-cluster-aws/awsdeploy.sh index 3ce621d9..b68b0a5e 100755 --- a/deploy-cluster-aws/awsdeploy.sh +++ b/deploy-cluster-aws/awsdeploy.sh @@ -131,6 +131,7 @@ if [ "$WHAT_TO_DEPLOY" == "servers" ]; then # this will only be sent to one of the nodes, see the # definition of init_bigchaindb() in fabfile.py to see why. fab init_bigchaindb + fab configure_sharding:$NUM_NODES else # Deploying clients # The only thing to configure on clients is the api_endpoint diff --git a/deploy-cluster-aws/fabfile.py b/deploy-cluster-aws/fabfile.py index 1d481111..581a1c1b 100644 --- a/deploy-cluster-aws/fabfile.py +++ b/deploy-cluster-aws/fabfile.py @@ -166,6 +166,12 @@ def init_bigchaindb(): run('bigchaindb init', pty=False) +@task +@hosts(public_dns_names[0]) +def configure_sharding(num_shards): + run('bigchaindb sharding {}'.format(num_shards)) + + # Start BigchainDB using screen @task @parallel diff --git a/docs/source/bigchaindb-cli.md b/docs/source/bigchaindb-cli.md index 229d5b4c..43f8dbb0 100644 --- a/docs/source/bigchaindb-cli.md +++ b/docs/source/bigchaindb-cli.md @@ -43,3 +43,10 @@ This command is used to run benchmarking tests. You can learn more about it usin ```text $ bigchaindb load -h ``` + +### bigchaindb sharding + +This command is used to configure the number of shards in the underlying datastore, for example: +```text +$ bigchaindb sharding 3 +``` \ No newline at end of file diff --git a/tests/test_commands.py b/tests/test_commands.py index 99a4f466..f90bc503 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,11 +1,12 @@ import json from unittest.mock import Mock, patch from argparse import Namespace -from pprint import pprint import copy import pytest +from tests.db.conftest import setup_database + @pytest.fixture def mock_run_configure(monkeypatch): @@ -225,3 +226,22 @@ def test_start_rethinkdb_exits_when_cannot_start(mock_popen): with pytest.raises(exceptions.StartupError): utils.start_rethinkdb() + +def test_configure_sharding(b): + import rethinkdb as r + from bigchaindb.commands.bigchain import run_sharding + + # change number of shards + args = Namespace(num_shards=3) + run_sharding(args) + + # retrieve table configuration + table_config = list(r.db('rethinkdb') + .table('table_config') + .filter(r.row['db'] == b.dbname) + .run(b.conn)) + + # check shard configuration + for table in table_config: + if table['name'] in ['backlog', 'bigchain']: + assert len(table['shards']) == 3