Skip to content

Commit

Permalink
PY: added configuration parameters correctness check
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Tereshchenko committed Sep 12, 2017
1 parent 6d13995 commit 4d0ba12
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
73 changes: 58 additions & 15 deletions source/python/urb/service/mesos_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from urb.messaging.messaging_utility import MessagingUtility
from urb.adapters.adapter_manager import AdapterManager
from urb.utility.naming_utility import NamingUtility
from urb.utility.utils import isfloat
from urb.messaging.mesos.resource_offers_message import ResourceOffersMessage
from urb.messaging.mesos.register_framework_message import RegisterFrameworkMessage
from urb.messaging.mesos.reregister_framework_message import ReregisterFrameworkMessage
Expand Down Expand Up @@ -2481,6 +2482,43 @@ def scrub_framework_name(self, framework_name):
framework_name)
return scrubbed_framework_name

def __check_type(self, key, value, t):
ret = False
for tt in t.split(','):
self.logger.trace("k=%s, v=%s, t=%s, tt=%s" % (key, value, t, tt))
if tt == 'num':
if value.isdigit():
return True
elif tt == 'float':
if isfloat(value):
return True
elif tt == 'str':
if isinstance(value, str):
return True
elif tt == 'list_tuple_num':
lst = []
try:
lst = eval(value)
self.logger.trace("lst=%s" % lst)
except Exception, ex:
self.logger.trace("lst exception: %s" % ex)
return False
for l in lst:
if isinstance(l, tuple):
if not isinstance(l[0], int) or not isinstance(l[1], int):
return False
else:
return False
return True
elif tt == 'bool':
if value in ['True', 'False']:
return True
else:
self.logger.error("Invalid configuration option type: %s" % tt)
return False
self.logger.trace("ret=%s" % ret)
return ret

def configure_framework(self, framework):
cm = ConfigManager.get_instance()
framework_name = framework['name']
Expand Down Expand Up @@ -2509,25 +2547,30 @@ def configure_framework(self, framework):

default_config_section = 'DefaultFrameworkConfig'
framework_config = {}
for key in ['mem',
'cpus',
'disk',
'ports',
'max_rejected_offers',
'max_tasks',
'send_task_lost',
'scale_count',
'concurrent_tasks',
'job_submit_options',
'initial_tasks',
'job_class',
'resource_mapping',
'executor_runner']:
for key, t in {
'mem' : 'num',
'cpus' : 'num,float',
'disk' : 'num',
'ports' : 'list_tuple_num',
'max_rejected_offers' : 'num',
'max_tasks' : 'num',
'send_task_lost' : 'bool',
'scale_count' : 'num',
'concurrent_tasks' : 'num',
'job_submit_options' : 'str',
'initial_tasks' : 'num',
'job_class' : 'str',
'resource_mapping' : 'str,bool',
'executor_runner' : 'str'
}.items():
value = cm.get_config_option(framework_config_section, key)
if not value:
value = cm.get_config_option(default_config_section, key)
if value is not None:
framework_config[key] = value
if self.__check_type(key, value, t):
framework_config[key] = value
else:
self.logger.error("Configuration option: %s=%s invalid type, expected: %s" % (key, value, t))
else:
self.logger.debug('config parameter missing: %s' % key)

Expand Down
8 changes: 8 additions & 0 deletions source/python/urb/utility/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ def replace_vars(init_str, env):

def join_args(*args):
return " ".join(map(str, args))

def isfloat(value):
try:
float(value)
return True
except:
return False

0 comments on commit 4d0ba12

Please sign in to comment.