Skip to content

Commit

Permalink
feat(forknet): allow url update when using update-binaries cmd (#11415)
Browse files Browse the repository at this point in the history
Add two parameters to update-binaries to allow url update for an epoch.

Usage:
`mirror update-binaries ` # will redownload all binaries in
`config.json`
`mirror amend-binaries --neard-binary-url <some url> --epoch-height 0` #
will update the url for epoch height 0 in `config.json` and then
download all binaries.
`mirror amend-binaries --neard-binary-url <some url> --binary-idx 0` #
will update the url for the first binary which is at epoch height 0 in
`config.json` and then download all binaries.

`mirror amend-binaries --neard-binary-url <some url> --epoch-height 10`
# will insert the url for epoch height 10 in `config.json` and then
download all binaries.

`mirror amend-binaries --neard-binary-url <some url> --binary-idx 1` #
will update the url for the second binary which is at epoch height 10 in
`config.json` and then download all binaries.
  • Loading branch information
VanBarbascu authored Jun 3, 2024
1 parent 936d9fd commit 33f9ca3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
57 changes: 55 additions & 2 deletions pytest/tests/mocknet/helpers/neard_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ def is_traffic_generator(self):

def save_data(self):
with open(self.home_path('data.json'), 'w') as f:
json.dump(self.data, f)
json.dump(self.data, f, indent=2)

def save_config(self):
with open(self.home_path('config.json'), 'w') as f:
json.dump(self.config, f, indent=2)

def parse_binaries_config(self):
if 'binaries' not in self.config:
Expand Down Expand Up @@ -665,9 +669,58 @@ def do_ls_backups(self):
with self.lock:
return self.data.get('backups', {})

def do_update_binaries(self):
# Updates the URL for the given epoch height or binary idx. adds a new one if the epoch height does not exit
def update_binaries_url(self, neard_binary_url, epoch_height, binary_idx):
if neard_binary_url is not None and ((epoch_height is None)
!= (binary_idx is None)):
logging.info(
f'Updating binary list for height:{epoch_height} or idx:{binary_idx} with '
f'url: {neard_binary_url}')
else:
logging.error(
f'Update binaries failed. Wrong params: url: {neard_binary_url}, height:{epoch_height}, idx:{binary_idx}'
)
raise jsonrpc.exceptions.JSONRPCInvalidParams()

if 'binaries' not in self.config:
self.config['binaries'] = []

if not isinstance(self.config['binaries'], list):
self.config['binaries'] = []

if epoch_height is not None:
binary = next((b for b in self.config['binaries']
if b['epoch_height'] == epoch_height), None)
if binary:
binary['url'] = neard_binary_url
else:
self.config['binaries'].append({
'url': neard_binary_url,
'epoch_height': epoch_height
})
self.config['binaries'].sort(
key=lambda binary: binary['epoch_height'])
if binary_idx is not None:
binaries_number = len(self.config['binaries'])
if binary_idx >= binaries_number:
logging.error(
f'idx {binary_idx} is out of bounds for the binary list of length {binaries_number}'
)
raise jsonrpc.exceptions.JSONRPCInvalidParams(
message=
f'Invalid binary idx. Out of bounds for list of length {binaries_number}'
)
self.config['binaries'][binary_idx]['url'] = neard_binary_url

def do_update_binaries(self, neard_binary_url, epoch_height, binary_idx):
with self.lock:
logging.info('update binaries')
if any(arg is not None
for arg in [neard_binary_url, epoch_height, binary_idx]):
self.update_binaries_url(neard_binary_url, epoch_height,
binary_idx)
self.save_config()

try:
self.download_binaries(force=True)
except ValueError as e:
Expand Down
47 changes: 42 additions & 5 deletions pytest/tests/mocknet/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ def update_binaries_cmd(args, traffic_generator, nodes):
nodes + to_list(traffic_generator))


def amend_binaries_cmd(args, traffic_generator, nodes):
pmap(
lambda node: node.neard_runner_update_binaries(
args.neard_binary_url, args.epoch_height, args.binary_idx),
nodes + to_list(traffic_generator))


def run_remote_cmd(args, traffic_generator, nodes):
targeted = nodes + to_list(traffic_generator)
logger.info(f'Running cmd on {"".join([h.name() for h in targeted ])}')
Expand Down Expand Up @@ -604,13 +611,43 @@ def filter_hosts(args, traffic_generator, nodes):
# nearcore-release buildkite and urls in the following format without commit
# but only with the branch name:
# https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore/Linux/<branch-name>/neard"
update_binaries_parser = subparsers.add_parser(
'update-binaries',
help=
'Update the neard binaries by re-downloading them. The same urls are used.'
)
update_binaries_parser = subparsers.add_parser('update-binaries',
help='''
Update the neard binaries by re-downloading them. The same urls are used.
If you plan to restart the network multiple times, it is recommended to use
URLs that only depend on the branch name. This way, every time you build,
you will not need to amend the URL but just run update-binaries.''')
update_binaries_parser.set_defaults(func=update_binaries_cmd)

amend_binaries_parsers = subparsers.add_parser('amend-binaries',
help='''
Add or override the neard URLs by specifying the epoch height or index if you have multiple binaries.
If the network was started with 2 binaries, the epoch height for the second binary can be randomly assigned
on each host. Use caution when updating --epoch-height so that it will not add a binary in between the upgrade
window for another binary.''')

amend_binaries_parsers.add_argument('--neard-binary-url',
type=str,
required=True,
help='URL to the neard binary.')
group = amend_binaries_parsers.add_mutually_exclusive_group(required=True)
group.add_argument('--epoch-height',
type=int,
help='''
The epoch height where this binary will begin to run.
If a binary already exists on the host for this epoch height, the old one will be replaced.
Otherwise a new binary will be added with this epoch height.
''')
group.add_argument('--binary-idx',
type=int,
help='''
0 based indexing.
The index in the binary list that you want to replace.
If the index does not exist on the host this operation will not do anything.
''')
amend_binaries_parsers.set_defaults(func=amend_binaries_cmd)

run_cmd_parser = subparsers.add_parser('run-cmd',
help='''Run the cmd on the hosts.''')
run_cmd_parser.add_argument('--cmd', type=str)
Expand Down
13 changes: 11 additions & 2 deletions pytest/tests/mocknet/node_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@ def neard_runner_reset(self, backup_id=None):
return self.neard_runner_jsonrpc('reset',
params={'backup_id': backup_id})

def neard_runner_update_binaries(self):
return self.neard_runner_jsonrpc('update_binaries')
def neard_runner_update_binaries(self,
neard_binary_url=None,
epoch_height=None,
binary_idx=None):
return self.neard_runner_jsonrpc(
'update_binaries',
params={
'neard_binary_url': neard_binary_url,
'epoch_height': epoch_height,
'binary_idx': binary_idx,
})

def neard_update_config(self, key_value):
return self.neard_runner_jsonrpc(
Expand Down

0 comments on commit 33f9ca3

Please sign in to comment.