AMBARI-21581 - Replace Hard Coded conf-select Structures (jonathanhurley)
[ambari.git] / ambari-server / src / main / resources / common-services / HBASE / 2.0.0.3.0 / package / scripts / upgrade.py
1
2 #!/usr/bin/env python
3 """
4 Licensed to the Apache Software Foundation (ASF) under one
5 or more contributor license agreements. See the NOTICE file
6 distributed with this work for additional information
7 regarding copyright ownership. The ASF licenses this file
8 to you under the Apache License, Version 2.0 (the
9 "License"); you may not use this file except in compliance
10 with the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 """
21 import re
22 import socket
23
24 from resource_management.core import shell
25 from resource_management.core.exceptions import ComponentIsNotRunning
26 from resource_management.core.exceptions import Fail
27 from resource_management.core.logger import Logger
28 from resource_management.libraries.functions import stack_select
29 from resource_management.libraries.functions.constants import StackFeature
30 from resource_management.libraries.functions.stack_features import check_stack_feature
31 from resource_management.libraries.functions.decorator import retry
32 from resource_management.libraries.functions.format import format
33 from resource_management.libraries.functions import check_process_status
34
35
36 def prestart(env):
37 import params
38
39 if params.version and check_stack_feature(StackFeature.ROLLING_UPGRADE, params.version):
40 stack_select.select_packages(params.version)
41
42 def post_regionserver(env):
43 import params
44 env.set_params(params)
45
46 check_cmd = "echo 'status \"simple\"' | {0} shell".format(params.hbase_cmd)
47
48 exec_cmd = "{0} {1}".format(params.kinit_cmd, check_cmd)
49 is_regionserver_registered(exec_cmd, params.hbase_user, params.hostname, re.IGNORECASE)
50
51
52 def is_region_server_process_running():
53 try:
54 pid_file = format("{pid_dir}/hbase-{hbase_user}-regionserver.pid")
55 check_process_status(pid_file)
56 return True
57 except ComponentIsNotRunning:
58 return False
59
60
61 @retry(times=30, sleep_time=30, err_class=Fail)
62 def is_regionserver_registered(cmd, user, hostname, regex_search_flags):
63 """
64 Queries HBase through the HBase shell to see which servers have successfully registered. This is
65 useful in cases, such as upgrades, where we must ensure that a RegionServer has not only started,
66 but also completed it's registration handshake before moving into upgrading the next RegionServer.
67
68 The hbase shell is used along with the "show 'simple'" command in order to determine if the
69 specified host has registered.
70 :param cmd:
71 :param user:
72 :param hostname:
73 :param regex_search_flags:
74 :return:
75 """
76 if not is_region_server_process_running():
77 Logger.info("RegionServer process is not running")
78 raise Fail("RegionServer process is not running")
79
80 # use hbase shell with "status 'simple'" command
81 code, out = shell.call(cmd, user=user)
82
83 # if we don't have ouput, then we can't check
84 if not out:
85 raise Fail("Unable to retrieve status information from the HBase shell")
86
87 # try matching the hostname with a colon (which indicates a bound port)
88 bound_hostname_to_match = hostname + ":"
89 match = re.search(bound_hostname_to_match, out, regex_search_flags)
90
91 # if there's no match, try again with the IP address
92 if not match:
93 try:
94 ip_address = socket.gethostbyname(hostname)
95 bound_ip_address_to_match = ip_address + ":"
96 match = re.search(bound_ip_address_to_match, out, regex_search_flags)
97 except socket.error:
98 # this is merely a backup, so just log that it failed
99 Logger.warning("Unable to lookup the IP address of {0}, reverse DNS lookup may not be working.".format(hostname))
100 pass
101
102 # failed with both a hostname and an IP address, so raise the Fail and let the function auto retry
103 if not match:
104 raise Fail(
105 "The RegionServer named {0} has not yet registered with the HBase Master".format(hostname))