Skip to content

Commit

Permalink
Migrate chain mask list config to full shard id list (#831)
Browse files Browse the repository at this point in the history
* Migrate chain mask list config to full shard id list

* Update config and fix redundant code

* Backward compatable

* Address comments

Co-authored-by: ninjaahhh <[email protected]>
  • Loading branch information
lyhe18 and ninjaahhh authored Mar 24, 2020
1 parent f7afb7f commit 68539b5
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 183 deletions.
16 changes: 8 additions & 8 deletions devnet/singularity/cluster_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,35 +380,35 @@
"HOST": "127.0.0.1",
"PORT": 38000,
"ID": "S0",
"CHAIN_MASK_LIST": [
4
"FULL_SHARD_ID_LIST": [
"0x1", "0x00040001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38001,
"WEBSOCKET_JSON_RPC_PORT": 38591,
"ID": "S1",
"CHAIN_MASK_LIST": [
5
"FULL_SHARD_ID_LIST": [
"0x00010001", "0x00050001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38002,
"WEBSOCKET_JSON_RPC_PORT": 38592,
"ID": "S2",
"CHAIN_MASK_LIST": [
6
"FULL_SHARD_ID_LIST": [
"0x00020001", "0x00060001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38003,
"WEBSOCKET_JSON_RPC_PORT": 38593,
"ID": "S3",
"CHAIN_MASK_LIST": [
7
"FULL_SHARD_ID_LIST": [
"0x00030001", "0x00070001"
]
}
],
Expand Down
16 changes: 8 additions & 8 deletions mainnet/singularity/cluster_config_bootnodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,32 +456,32 @@
"HOST": "127.0.0.1",
"PORT": 38000,
"ID": "S0",
"CHAIN_MASK_LIST": [
4
"FULL_SHARD_ID_LIST": [
"0x1", "0x00040001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38001,
"ID": "S1",
"CHAIN_MASK_LIST": [
5
"FULL_SHARD_ID_LIST": [
"0x00010001", "0x00050001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38002,
"ID": "S2",
"CHAIN_MASK_LIST": [
6
"FULL_SHARD_ID_LIST": [
"0x00020001", "0x00060001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38003,
"ID": "S3",
"CHAIN_MASK_LIST": [
7
"FULL_SHARD_ID_LIST": [
"0x00030001", "0x00070001"
]
}
],
Expand Down
16 changes: 8 additions & 8 deletions mainnet/singularity/cluster_config_p2p_crawling.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,32 +456,32 @@
"HOST": "127.0.0.1",
"PORT": 38000,
"ID": "S0",
"CHAIN_MASK_LIST": [
4
"FULL_SHARD_ID_LIST": [
"0x1", "0x00040001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38001,
"ID": "S1",
"CHAIN_MASK_LIST": [
5
"FULL_SHARD_ID_LIST": [
"0x00010001", "0x00050001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38002,
"ID": "S2",
"CHAIN_MASK_LIST": [
6
"FULL_SHARD_ID_LIST": [
"0x00020001", "0x00060001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38003,
"ID": "S3",
"CHAIN_MASK_LIST": [
7
"FULL_SHARD_ID_LIST": [
"0x00030001", "0x00070001"
]
}
],
Expand Down
16 changes: 8 additions & 8 deletions mainnet/singularity/cluster_config_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,32 +461,32 @@
"HOST": "127.0.0.1",
"PORT": 38000,
"ID": "S0",
"CHAIN_MASK_LIST": [
4
"FULL_SHARD_ID_LIST": [
"0x1", "0x00040001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38001,
"ID": "S1",
"CHAIN_MASK_LIST": [
5
"FULL_SHARD_ID_LIST": [
"0x00010001", "0x00050001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38002,
"ID": "S2",
"CHAIN_MASK_LIST": [
6
"FULL_SHARD_ID_LIST": [
"0x00020001", "0x00060001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38003,
"ID": "S3",
"CHAIN_MASK_LIST": [
7
"FULL_SHARD_ID_LIST": [
"0x00030001", "0x00070001"
]
}
],
Expand Down
32 changes: 16 additions & 16 deletions mainnet/singularity/cluster_config_template_8nodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,64 +461,64 @@
"HOST": "127.0.0.1",
"PORT": 38000,
"ID": "S0",
"CHAIN_MASK_LIST": [
8
"FULL_SHARD_ID_LIST": [
"0x1"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38001,
"ID": "S1",
"CHAIN_MASK_LIST": [
9
"FULL_SHARD_ID_LIST": [
"0x00010001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38002,
"ID": "S2",
"CHAIN_MASK_LIST": [
10
"FULL_SHARD_ID_LIST": [
"0x00020001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38003,
"ID": "S3",
"CHAIN_MASK_LIST": [
11
"FULL_SHARD_ID_LIST": [
"0x00030001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38004,
"ID": "S4",
"CHAIN_MASK_LIST": [
12
"FULL_SHARD_ID_LIST": [
"0x00040001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38005,
"ID": "S5",
"CHAIN_MASK_LIST": [
13
"FULL_SHARD_ID_LIST": [
"0x00050001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38006,
"ID": "S6",
"CHAIN_MASK_LIST": [
14
"FULL_SHARD_ID_LIST": [
"0x00060001"
]
},
{
"HOST": "127.0.0.1",
"PORT": 38007,
"ID": "S7",
"CHAIN_MASK_LIST": [
15
"FULL_SHARD_ID_LIST": [
"0x00070001"
]
}
],
Expand Down
71 changes: 58 additions & 13 deletions quarkchain/cluster/cluster_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import os
import socket
import tempfile
from typing import List
from typing import List, Optional

from quarkchain.cluster.monitoring import KafkaSampleLogger
from quarkchain.cluster.rpc import SlaveInfo
from quarkchain.config import BaseConfig, ChainConfig, QuarkChainConfig
from quarkchain.core import Address, ChainMask
from quarkchain.utils import Logger, check, is_p2
from quarkchain.core import Address
from quarkchain.utils import Logger, check, is_p2, int_left_most_bit

DEFAULT_HOST = socket.gethostbyname(socket.gethostname())

Expand Down Expand Up @@ -91,17 +91,50 @@ class SlaveConfig(BaseConfig):
PORT = 38392
WEBSOCKET_JSON_RPC_PORT = None
ID = ""
CHAIN_MASK_LIST = None
FULL_SHARD_ID_LIST = []

def to_dict(self):
ret = super().to_dict()
ret["CHAIN_MASK_LIST"] = [m.value for m in self.CHAIN_MASK_LIST]
# format to hex
ret["FULL_SHARD_ID_LIST"] = [
"0x{:08x}".format(i) for i in self.FULL_SHARD_ID_LIST
]
return ret

@classmethod
def from_dict(cls, d):
def from_dict(cls, d, chains: Optional[List[ChainConfig]] = None):
config = super().from_dict(d)
config.CHAIN_MASK_LIST = [ChainMask(v) for v in config.CHAIN_MASK_LIST]
# bail if both full shard ID list and chain mask list exist
shard_ids = getattr(config, "FULL_SHARD_ID_LIST", None)
chain_mask = getattr(config, "CHAIN_MASK_LIST", None)
if shard_ids and chain_mask:
raise ValueError(
"Can only have either FULL_SHARD_ID_LIST or CHAIN_MASK_LIST"
)
elif shard_ids:
# parse from hex to int
config.FULL_SHARD_ID_LIST = [int(h, 16) for h in config.FULL_SHARD_ID_LIST]
elif chain_mask:
if chains is None:
raise ValueError(
"Can't handle legacy CHAIN_MASK_LIST without chain configs"
)
# a simple way to be backward compatible with hard-coded shard ID
# e.g. chain mask 4 => 0x00000001, 0x00040001
# note that this only works if every chain has 1 shard only
check(all(chain.SHARD_SIZE == 1 for chain in chains))
for m in chain_mask:
bit_mask = (1 << (int_left_most_bit(m) - 1)) - 1
config.FULL_SHARD_ID_LIST = [
int("0x{:04x}0001".format(chain_id), 16)
for chain_id in range(len(chains))
if chain_id & bit_mask == m & bit_mask
]
delattr(config, "CHAIN_MASK_LIST")
else:
raise ValueError(
"Missing FULL_SHARD_ID_LIST (or CHAIN_MASK_LIST as legacy config)"
)
return config


Expand All @@ -112,7 +145,7 @@ class SimpleNetworkConfig(BaseConfig):

class P2PConfig(BaseConfig):
# *new p2p module*
BOOT_NODES = "" # comma seperated enodes format: enode://PUBKEY@IP:PORT
BOOT_NODES = "" # comma separated enodes format: enode://PUBKEY@IP:PORT
PRIV_KEY = ""
MAX_PEERS = 25
UPNP = False
Expand Down Expand Up @@ -171,7 +204,7 @@ def __init__(self):
slave_config = SlaveConfig()
slave_config.PORT = 38000
slave_config.ID = "S0"
slave_config.CHAIN_MASK_LIST = [ChainMask(1)]
slave_config.FULL_SHARD_ID_LIST = [1]
self.SLAVE_LIST.append(slave_config)

fd, self.json_filepath = tempfile.mkstemp()
Expand All @@ -182,7 +215,7 @@ def get_slave_info_list(self):
results = []
for slave in self.SLAVE_LIST:
results.append(
SlaveInfo(slave.ID, slave.HOST, slave.PORT, slave.CHAIN_MASK_LIST)
SlaveInfo(slave.ID, slave.HOST, slave.PORT, slave.FULL_SHARD_ID_LIST)
)
return results

Expand Down Expand Up @@ -414,10 +447,19 @@ def __create_from_args_internal():
slave_config = SlaveConfig()
slave_config.PORT = args.port_start + i
slave_config.ID = "S{}".format(i)
slave_config.CHAIN_MASK_LIST = [ChainMask(i | args.num_slaves)]

slave_config.FULL_SHARD_ID_LIST = []
config.SLAVE_LIST.append(slave_config)

# assign full shard IDs to each slave, using hex strings to write into JSON
full_shard_ids = [
(i << 16) + args.num_shards_per_chain + j
for i in range(args.num_chains)
for j in range(args.num_shards_per_chain)
]
for i, full_shard_id in enumerate(full_shard_ids):
slave = config.SLAVE_LIST[i % args.num_slaves]
slave.FULL_SHARD_ID_LIST.append(full_shard_id)

fd, config.json_filepath = tempfile.mkstemp()
with os.fdopen(fd, "w") as tmp:
tmp.write(config.to_json())
Expand Down Expand Up @@ -455,7 +497,10 @@ def from_dict(cls, d):
config.QUARKCHAIN = QuarkChainConfig.from_dict(config.QUARKCHAIN)
config.MONITORING = MonitoringConfig.from_dict(config.MONITORING)
config.MASTER = MasterConfig.from_dict(config.MASTER)
config.SLAVE_LIST = [SlaveConfig.from_dict(s) for s in config.SLAVE_LIST]
config.SLAVE_LIST = [
SlaveConfig.from_dict(s, config.QUARKCHAIN.CHAINS)
for s in config.SLAVE_LIST
]

if "P2P" in d:
config.P2P = P2PConfig.from_dict(d["P2P"])
Expand Down
Loading

0 comments on commit 68539b5

Please sign in to comment.