Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit ec44c53

Browse files
authored
Use click instead of argparse in py instance (#3598)
* Use click instead of argparse in py instance * Improve heron-python-instance option name consistency * Rename instance/st_heron_instance.py to instance.py to avoid [this kind of runtime warning](https://stackoverflow.com/questions/43393764/python-3-6-project-structure-leads-to-runtimewarning).
1 parent 4847584 commit ec44c53

File tree

5 files changed

+66
-86
lines changed

5 files changed

+66
-86
lines changed

heron/executor/src/python/heron_executor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,8 @@ def _get_python_instance_cmd(self, instance_info):
709709
'--stmgr_id=%s' % self.stmgr_ids[self.shard],
710710
'--stmgr_port=%s' % self.tmaster_controller_port,
711711
'--metricsmgr_port=%s' % self.metrics_manager_port,
712-
'--sys_config=%s' % self.heron_internals_config_file,
713-
'--override_config=%s' % self.override_config_file,
712+
'--config_file=%s' % self.heron_internals_config_file,
713+
'--override_config_file=%s' % self.override_config_file,
714714
'--topology_pex=%s' % self.topology_binary_file,
715715
'--max_ram=%s' % str(self.component_ram_map[component_name])]
716716

heron/instance/src/python/BUILD

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ pex_library(
1515
# build binary for single thread python heron instance
1616
pex_binary(
1717
name = "heron-python-instance",
18-
srcs = ["instance/st_heron_instance.py"],
18+
srcs = ["instance.py"],
1919
reqs = [
20-
"colorlog==2.6.1",
2120
"PyYAML==3.13",
21+
"click==7.1.2",
22+
"colorlog==2.6.1",
2223
],
2324
deps = [":instance-py"],
2425
)

heron/instance/src/python/__init__.py

-16
This file was deleted.

heron/instance/src/python/instance/st_heron_instance.py heron/instance/src/python/instance.py

+61-46
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# under the License.
2020

2121
'''module for single-thread Heron Instance in python'''
22-
import argparse
2322
import collections
2423
import logging
2524
import os
@@ -38,12 +37,15 @@
3837
from heron.instance.src.python.network import create_socket_options
3938
from heron.instance.src.python.network import GatewayLooper
4039
from heron.instance.src.python.basics import SpoutInstance, BoltInstance
41-
import heron.instance.src.python.utils.system_constants as constants
40+
from heron.instance.src.python.utils import system_constants as constants
4241
from heron.instance.src.python.utils import system_config
4342

44-
import heronpy.api.api_constants as api_constants
43+
from heronpy.api import api_constants
4544
from heronpy.api.state.state import HashMapState
4645

46+
import click
47+
48+
4749
Log = log.Log
4850
AssignedInstance = collections.namedtuple('AssignedInstance', 'is_spout, protobuf, py_class')
4951

@@ -324,70 +326,83 @@ def yaml_config_reader(config_path):
324326

325327
return config
326328

327-
# pylint: disable=missing-docstring
328-
def main():
329-
parser = argparse.ArgumentParser(description='Heron Python Instance')
330-
parser.add_argument('--topology_name', required=True, help='Topology Name')
331-
parser.add_argument('--topology_id', required=True, help='Topology Id')
332-
parser.add_argument('--instance_id', required=True, help='Instance Id')
333-
parser.add_argument('--component_name', required=True, help='Component Name')
334-
parser.add_argument('--task_id', required=True, help='Task Id', type=int)
335-
parser.add_argument('--component_index', required=True, help='Component Index', type=int)
336-
parser.add_argument('--stmgr_id', required=True, help='StMgr Id')
337-
parser.add_argument('--stmgr_port', required=True, help='StMgr Port', type=int)
338-
parser.add_argument('--metricsmgr_port', required=True, help='MetricsMgr Port', type=int)
339-
parser.add_argument('--sys_config', required=True, help='System Config File')
340-
parser.add_argument('--override_config', required=True, help='Override Config File')
341-
parser.add_argument('--topology_pex', required=True, help='Topology Pex File')
342-
parser.add_argument('--max_ram', required=True, help='Maximum RAM to limit', type=int)
343-
344-
args = parser.parse_args()
345-
346-
sys_config = yaml_config_reader(args.sys_config)
347-
override_config = yaml_config_reader(args.override_config)
329+
330+
# pylint: disable=too-many-arguments,too-many-locals
331+
@click.command()
332+
@click.option('--topology_name', required=True, help='Topology Name')
333+
@click.option('--topology_id', required=True, help='Topology Id')
334+
@click.option('--instance_id', required=True, help='Instance Id')
335+
@click.option('--component_name', required=True, help='Component Name')
336+
@click.option('--task_id', required=True, help='Task Id', type=int)
337+
@click.option('--component_index', required=True, help='Component Index', type=int)
338+
@click.option('--stmgr_id', required=True, help='StMgr Id')
339+
@click.option('--stmgr_port', required=True, help='StMgr Port', type=int)
340+
@click.option('--metricsmgr_port', required=True, help='MetricsMgr Port', type=int)
341+
@click.option('--config_file', required=True, help='System Config File')
342+
@click.option('--override_config_file', required=True, help='Override Config File')
343+
@click.option('--topology_pex', required=True, help='Topology Pex File')
344+
@click.option('--max_ram', required=True, help='Maximum RAM to limit', type=int)
345+
def cli(
346+
topology_name: str,
347+
topology_id: str,
348+
instance_id: str,
349+
component_name: str,
350+
task_id: int,
351+
component_index: int,
352+
stmgr_id: str,
353+
stmgr_port: int,
354+
metricsmgr_port: int,
355+
config_file: str,
356+
override_config_file: str,
357+
topology_pex: str,
358+
max_ram: int,
359+
) -> None:
360+
"""Heron Python Instance."""
361+
362+
sys_config = yaml_config_reader(config_file)
363+
override_config = yaml_config_reader(override_config_file)
348364
system_config.set_sys_config(sys_config, override_config)
349365

350366
# get combined configuration
351367
sys_config = system_config.get_sys_config()
352368

353369
# set resource limits
354-
set_resource_limit(args.max_ram)
370+
set_resource_limit(max_ram)
355371

356372
# create the protobuf instance
357373
instance_info = physical_plan_pb2.InstanceInfo()
358-
instance_info.task_id = args.task_id
359-
instance_info.component_index = args.component_index
360-
instance_info.component_name = args.component_name
374+
instance_info.task_id = task_id
375+
instance_info.component_index = component_index
376+
instance_info.component_name = component_name
361377

362378
instance = physical_plan_pb2.Instance()
363-
instance.instance_id = args.instance_id
364-
instance.stmgr_id = args.stmgr_id
379+
instance.instance_id = instance_id
380+
instance.stmgr_id = stmgr_id
365381
instance.info.MergeFrom(instance_info)
366382

367383
# Logging init
368384
log_dir = os.path.abspath(sys_config[constants.HERON_LOGGING_DIRECTORY])
369385
max_log_files = sys_config[constants.HERON_LOGGING_MAXIMUM_FILES]
370386
max_log_bytes = sys_config[constants.HERON_LOGGING_MAXIMUM_SIZE_MB] * constants.MB
371387

372-
log_file = os.path.join(log_dir, args.instance_id + ".log.0")
388+
log_file = os.path.join(log_dir, instance_id + ".log.0")
373389
log.init_rotating_logger(level=logging.INFO, logfile=log_file,
374390
max_files=max_log_files, max_bytes=max_log_bytes)
375391

376-
Log.info("\nStarting instance: " + args.instance_id + " for topology: " + args.topology_name +
377-
" and topologyId: " + args.topology_id + " for component: " + args.component_name +
378-
" with taskId: " + str(args.task_id) + " and componentIndex: " +
379-
str(args.component_index) +
380-
" and stmgrId: " + args.stmgr_id + " and stmgrPort: " + str(args.stmgr_port) +
381-
" and metricsManagerPort: " + str(args.metricsmgr_port) +
382-
"\n **Topology Pex file located at: " + args.topology_pex)
383-
Log.debug("System config: " + str(sys_config))
384-
Log.debug("Override config: " + str(override_config))
385-
Log.debug("Maximum RAM: " + str(args.max_ram))
386-
387-
heron_instance = SingleThreadHeronInstance(args.topology_name, args.topology_id, instance,
388-
args.stmgr_port, args.metricsmgr_port,
389-
args.topology_pex)
392+
Log.info(f"\nStarting instance: {instance_id} for topology: {topology_name}"
393+
f" and topologyId: {topology_id} for component: {component_name}"
394+
f" with taskId: {task_id} and componentIndex: {component_index}"
395+
f" and stmgrId: {stmgr_id} and stmgrPort: {stmgr_port}"
396+
f" and metricsManagerPort: {metricsmgr_port}"
397+
f"\n **Topology Pex file located at: {topology_pex}")
398+
Log.debug(f"System config: {sys_config}")
399+
Log.debug(f"Override config: {override_config}")
400+
Log.debug(f"Maximum RAM: {max_ram}")
401+
402+
heron_instance = SingleThreadHeronInstance(topology_name, topology_id, instance,
403+
stmgr_port, metricsmgr_port,
404+
topology_pex)
390405
heron_instance.start()
391406

392407
if __name__ == '__main__':
393-
main()
408+
cli() # pylint: disable=no-value-for-parameter

heron/instance/src/python/instance/__init__.py

-20
This file was deleted.

0 commit comments

Comments
 (0)