Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support range format in worker #325

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/installation/presto-admin-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ username and port, a sample ``config.json`` would be:
"workers": ["slave1","slave2","slave3","slave4","slave5"]
}

You can specify a range of workers by including the number range in brackets in the worker name. For example:

::

"workers": ["worker[01-03]"]

is the same as

::

"workers": ["worker01", "worker02", "worker03"]


.. _sudo-password-spec:

Expand Down
23 changes: 23 additions & 0 deletions prestoadmin/standalone/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""
Module for setting and validating the presto-admin config
"""
import re

from fabric.api import env
from overrides import overrides

Expand Down Expand Up @@ -116,6 +118,7 @@ def validate(conf):
except KeyError:
pass
else:
workers = [h for host in workers for h in _expand_host(host)]
conf['workers'] = validate_workers(workers)

try:
Expand All @@ -140,6 +143,26 @@ def validate_workers(workers):
return workers


def _expand_host(host):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should raise an error if the bottom of the range is greater than the top (e.g. worker[5-2]. And also add a unit test for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I add a unittest: test_except_expand_host.

match = re.match("(.*)\[(\d{1,})-(\d{1,})\](.*)", host)
if match is not None and len(match.groups()) == 4:
prefix, start, end, suffix = match.groups()
if int(start) > int(end):
raise ValueError("the range format must be ascending order")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rschlussel Is the error message is OK?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "the range must be in ascending order"

if len(start) == len(end) and len(start) > 1:
number_format = "{0:0" + str(len(start)) + "d}"
host_list = [number_format.format(i) for i in range(int(start), int(end) + 1)]
return [_format_hostname(prefix, i, suffix) for i in host_list]
else:
return [_format_hostname(prefix, i, suffix) for i in range(int(start), int(end) + 1)]
else:
return [host]


def _format_hostname(prefix, number, suffix):
return "{prefix}{num}{suffix}".format(prefix=prefix, num=number, suffix=suffix)


class StandaloneConfig(BaseConfig):
def __init__(self):
super(StandaloneConfig, self).__init__(get_topology_path(), _TOPOLOGY_CONFIG)
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_expand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from prestoadmin.standalone.config import _expand_host
from tests.unit.base_unit_case import BaseUnitCase


class TestExpandHost(BaseUnitCase):

def test_basic_expand_host_01(self):
input_host = "worker0[1-2].example.com"
expected = ["worker01.example.com", "worker02.example.com"]
self.assertEqual(expected, _expand_host(input_host))

def test_basic_expand_host_02(self):
input_host = "worker[01-02].example.com"
expected = ["worker01.example.com", "worker02.example.com"]
self.assertEqual(expected, _expand_host(input_host))

def test_expand_host_include_hyphen(self):
input_host = "cdh5-[1-2].example.com"
expected = ["cdh5-1.example.com", "cdh5-2.example.com"]
self.assertEqual(expected, _expand_host(input_host))

def test_not_expand_host(self):
input_host = "worker1.example.com"
expected = ["worker1.example.com"]
self.assertEqual(expected, _expand_host(input_host))

def test_except_expand_host(self):
input_host = "worker0[3-2].example.com"
self.assertRaises(ValueError, _expand_host, input_host)