Merge pull request #258 from bigchaindb/automatic-sharding

Add command to configure number of shards.
This commit is contained in:
Rodolphe Marques 2016-05-09 17:35:41 +02:00
commit c2e8a5712a
5 changed files with 49 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import builtins
import logstats import logstats
import rethinkdb as r
import bigchaindb import bigchaindb
import bigchaindb.config_utils import bigchaindb.config_utils
@ -203,6 +204,12 @@ def run_load(args):
workers.start() workers.start()
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)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Control your BigchainDB node.', description='Control your BigchainDB node.',
@ -243,6 +250,13 @@ def main():
subparsers.add_parser('start', subparsers.add_parser('start',
help='Start BigchainDB') help='Start BigchainDB')
# parser for configuring the number of shards
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,
help='Number of shards')
load_parser = subparsers.add_parser('load', load_parser = subparsers.add_parser('load',
help='Write transactions to the backlog') help='Write transactions to the backlog')

View File

@ -131,6 +131,7 @@ if [ "$WHAT_TO_DEPLOY" == "servers" ]; then
# this will only be sent to one of the nodes, see the # this will only be sent to one of the nodes, see the
# definition of init_bigchaindb() in fabfile.py to see why. # definition of init_bigchaindb() in fabfile.py to see why.
fab init_bigchaindb fab init_bigchaindb
fab configure_sharding:$NUM_NODES
else else
# Deploying clients # Deploying clients
# The only thing to configure on clients is the api_endpoint # The only thing to configure on clients is the api_endpoint

View File

@ -166,6 +166,12 @@ def init_bigchaindb():
run('bigchaindb init', pty=False) 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 # Start BigchainDB using screen
@task @task
@parallel @parallel

View File

@ -43,3 +43,10 @@ This command is used to run benchmarking tests. You can learn more about it usin
```text ```text
$ bigchaindb load -h $ 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
```

View File

@ -1,11 +1,12 @@
import json import json
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from argparse import Namespace from argparse import Namespace
from pprint import pprint
import copy import copy
import pytest import pytest
from tests.db.conftest import setup_database
@pytest.fixture @pytest.fixture
def mock_run_configure(monkeypatch): def mock_run_configure(monkeypatch):
@ -225,3 +226,22 @@ def test_start_rethinkdb_exits_when_cannot_start(mock_popen):
with pytest.raises(exceptions.StartupError): with pytest.raises(exceptions.StartupError):
utils.start_rethinkdb() utils.start_rethinkdb()
def test_configure_sharding(b):
import rethinkdb as r
from bigchaindb.commands.bigchain import run_set_shards
# change number of shards
args = Namespace(num_shards=3)
run_set_shards(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