Skip to content

Commit 4c0612d

Browse files
kevin-bateslresende
authored andcommitted
Add support for handling multiple RMs sans Hadoop config
Also added method `get_active_host_port()` to return the address,port tuple currently in use for the ResourceManager instance.
1 parent 075e4a8 commit 4c0612d

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

tests/test_hadoop_conf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_get_resource_host_port(self):
7070

7171
@mock.patch('yarn_api_client.hadoop_conf._get_rm_ids')
7272
@mock.patch('yarn_api_client.hadoop_conf.parse')
73-
@mock.patch('yarn_api_client.hadoop_conf._check_is_active_rm')
73+
@mock.patch('yarn_api_client.hadoop_conf.check_is_active_rm')
7474
def test_get_resource_host_port_with_ha(self, check_is_active_rm_mock, parse_mock, get_rm_ids_mock):
7575
get_rm_ids_mock.return_value = ['rm1', 'rm2']
7676
parse_mock.return_value = 'example.com:8022'
@@ -116,17 +116,17 @@ def getheader(self, header_key, default_return):
116116

117117
http_conn_request_mock.return_value = None
118118
http_getresponse_mock.return_value = ResponseMock(OK, {})
119-
self.assertTrue(hadoop_conf._check_is_active_rm('example2', '8022'))
119+
self.assertTrue(hadoop_conf.check_is_active_rm('example2', '8022'))
120120
http_getresponse_mock.reset_mock()
121121
http_getresponse_mock.return_value = ResponseMock(OK, {'Refresh': "testing"})
122-
self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022'))
122+
self.assertFalse(hadoop_conf.check_is_active_rm('example2', '8022'))
123123
http_getresponse_mock.reset_mock()
124124
http_getresponse_mock.return_value = ResponseMock(NOT_FOUND, {'Refresh': "testing"})
125-
self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022'))
125+
self.assertFalse(hadoop_conf.check_is_active_rm('example2', '8022'))
126126
http_conn_request_mock.side_effect = Exception('error')
127127
http_conn_request_mock.reset_mock()
128128
http_conn_request_mock.return_value = None
129-
self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022'))
129+
self.assertFalse(hadoop_conf.check_is_active_rm('example2', '8022'))
130130
pass
131131

132132
def test_get_resource_manager(self):

yarn_api_client/hadoop_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _get_resource_manager(hadoop_conf_path, rm_id=None):
2929
return None
3030

3131

32-
def _check_is_active_rm(rm_web_host, rm_web_port):
32+
def check_is_active_rm(rm_web_host, rm_web_port):
3333
conn = HTTPConnection(rm_web_host, rm_web_port)
3434
try:
3535
conn.request('GET', '/cluster')
@@ -52,7 +52,7 @@ def get_resource_manager_host_port():
5252
ret = _get_resource_manager(hadoop_conf_path, rm_id)
5353
if ret is not None:
5454
(host, port) = ret
55-
if _check_is_active_rm(host, port):
55+
if check_is_active_rm(host, port):
5656
return host, port
5757
return None
5858
else:

yarn_api_client/resource_manager.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .base import BaseYarnAPI
44
from .constants import YarnApplicationState, FinalApplicationStatus
55
from .errors import IllegalArgumentError
6-
from .hadoop_conf import get_resource_manager_host_port
6+
from .hadoop_conf import get_resource_manager_host_port, check_is_active_rm, CONF_DIR
77

88

99
class ResourceManager(BaseYarnAPI):
@@ -14,20 +14,38 @@ class ResourceManager(BaseYarnAPI):
1414
and information about applications on the cluster.
1515
1616
If `address` argument is `None` client will try to extract `address` and
17-
`port` from Hadoop configuration files.
17+
`port` from Hadoop configuration files. If both `address` and `alt_address`
18+
are provided, the address corresponding to the ACTIVE HA Resource Manager will
19+
be used.
1820
1921
:param str address: ResourceManager HTTP address
2022
:param int port: ResourceManager HTTP port
23+
:param str alt_address: Alternate ResourceManager HTTP address for HA configurations
24+
:param int alt_port: Alternate ResourceManager HTTP port for HA configurations
2125
:param int timeout: API connection timeout in seconds
2226
:param boolean kerberos_enabled: Flag identifying is Kerberos Security has been enabled for YARN
2327
"""
24-
def __init__(self, address=None, port=8088, timeout=30, kerberos_enabled=False):
28+
def __init__(self, address=None, port=8088, alt_address=None, alt_port=8088, timeout=30, kerberos_enabled=False):
2529
if address is None:
26-
self.logger.debug('Get configuration from hadoop conf dir')
30+
self.logger.debug('Get configuration from hadoop conf dir: {conf_dir}'.format(conf_dir=CONF_DIR))
2731
address, port = get_resource_manager_host_port()
32+
else:
33+
if alt_address: # Determine active RM
34+
if not check_is_active_rm(address, port):
35+
# Default is not active, check alternate
36+
if check_is_active_rm(alt_address, alt_port):
37+
address, port = alt_address, alt_port
2838

2939
super(ResourceManager, self).__init__(address, port, timeout, kerberos_enabled)
3040

41+
def get_active_host_port(self):
42+
"""
43+
The active address, port tuple to which this instance is associated.
44+
45+
:return: Tuple (str, int) corresponding to the active address and port
46+
"""
47+
return self.address, self.port
48+
3149
def cluster_information(self):
3250
"""
3351
The cluster information resource provides overall information about

0 commit comments

Comments
 (0)