You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sm...@apache.org on 2015/09/23 07:33:04 UTC

ambari git commit: AMBARI-13196. Designate Active Ambari Server (Nahappan Somasundaram via smohanty)

Repository: ambari
Updated Branches:
  refs/heads/trunk 3df60b4fb -> e24e3c8b7


AMBARI-13196. Designate Active Ambari Server (Nahappan Somasundaram via smohanty)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/e24e3c8b
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/e24e3c8b
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/e24e3c8b

Branch: refs/heads/trunk
Commit: e24e3c8b75ad7b466d7ab82ebb4ffb6ac6ee6dcc
Parents: 3df60b4
Author: Sumit Mohanty <sm...@hortonworks.com>
Authored: Tue Sep 22 22:32:26 2015 -0700
Committer: Sumit Mohanty <sm...@hortonworks.com>
Committed: Tue Sep 22 22:32:26 2015 -0700

----------------------------------------------------------------------
 .../python/ambari_server/serverConfiguration.py | 22 ++++++++
 .../src/main/python/ambari_server_main.py       |  7 +++
 .../src/test/python/TestAmbariServer.py         | 57 +++++++++++++++++++-
 3 files changed, 85 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e24e3c8b/ambari-server/src/main/python/ambari_server/serverConfiguration.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/serverConfiguration.py b/ambari-server/src/main/python/ambari_server/serverConfiguration.py
index bc78d68..bc9823c 100644
--- a/ambari-server/src/main/python/ambari_server/serverConfiguration.py
+++ b/ambari-server/src/main/python/ambari_server/serverConfiguration.py
@@ -170,6 +170,8 @@ JDK_RELEASES="java.releases"
 
 VIEWS_DIR_PROPERTY = "views.dir"
 
+ACTIVE_INSTANCE_PROPERTY = "active.instance"
+
 #Common setup or upgrade message
 SETUP_OR_UPGRADE_MSG = "- If this is a new setup, then run the \"ambari-server setup\" command to create the user\n" \
                        "- If this is an upgrade of an existing setup, run the \"ambari-server upgrade\" command.\n" \
@@ -522,6 +524,26 @@ def read_ambari_user():
       return user
   return None
 
+def get_is_active_instance():
+  # active.instance, if missing, will be considered to be true;
+  # if present, it should be explicitly set to "true" to set this as the active instance;
+  # any other value will be taken as a "false"
+  properties = get_ambari_properties()
+  # Get the value of active.instance.
+  active_instance_value = None
+  if properties != -1:
+    if ACTIVE_INSTANCE_PROPERTY in properties.propertyNames():
+      active_instance_value = properties[ACTIVE_INSTANCE_PROPERTY]
+
+  if active_instance_value is None:  # property is missing
+    is_active_instance = True
+  elif (active_instance_value == 'true'): # property is explicitly set to true
+    is_active_instance = True
+  else:  # any other value
+    is_active_instance = False
+
+  return is_active_instance
+
 def get_value_from_properties(properties, key, default=""):
   try:
     value = properties.get_property(key)

http://git-wip-us.apache.org/repos/asf/ambari/blob/e24e3c8b/ambari-server/src/main/python/ambari_server_main.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server_main.py b/ambari-server/src/main/python/ambari_server_main.py
index 0634090..4f532f3 100644
--- a/ambari-server/src/main/python/ambari_server_main.py
+++ b/ambari-server/src/main/python/ambari_server_main.py
@@ -31,6 +31,7 @@ from ambari_server.dbConfiguration import ensure_dbms_is_running, ensure_jdbc_dr
   get_native_libs_path, get_jdbc_driver_path
 from ambari_server.serverConfiguration import configDefaults, find_jdk, get_ambari_classpath, get_ambari_properties, \
   get_conf_dir, get_is_persisted, get_is_secure, get_java_exe_path, get_original_master_key, read_ambari_user, \
+  get_is_active_instance, \
   PID_NAME, SECURITY_KEY_ENV_VAR_NAME, SECURITY_MASTER_KEY_LOCATION, \
   SETUP_OR_UPGRADE_MSG, check_database_name_property, parse_properties_file
 from ambari_server.serverUtils import refresh_stack_hash
@@ -219,6 +220,12 @@ def server_process_main(options, scmStatus=None):
   check_database_name_property()
   parse_properties_file(options)
 
+  is_active_instance = get_is_active_instance()
+  if not is_active_instance:
+      print_warning_msg("This instance of ambari server is not designated as active. Cannot start ambari server.")
+      err = "This is not an active instance. Shutting down..."
+      raise FatalException(1, err)
+
   ambari_user = read_ambari_user()
   current_user = ensure_can_start_under_current_user(ambari_user)
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/e24e3c8b/ambari-server/src/test/python/TestAmbariServer.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestAmbariServer.py b/ambari-server/src/test/python/TestAmbariServer.py
index f2ac75e..3dcf02a 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/ambari-server/src/test/python/TestAmbariServer.py
@@ -63,6 +63,7 @@ with patch("platform.linux_distribution", return_value = os_distro_value):
           check_database_name_property, OS_FAMILY_PROPERTY, \
           find_properties_file, get_ambari_classpath, get_ambari_jars, get_ambari_properties, get_JAVA_HOME, \
           parse_properties_file, read_ambari_user, update_ambari_properties, update_properties_2, write_property, find_jdk, \
+          get_is_active_instance, \
           AMBARI_CONF_VAR, AMBARI_SERVER_LIB, JDBC_DATABASE_PROPERTY, JDBC_RCA_PASSWORD_FILE_PROPERTY, \
           PERSISTENCE_TYPE_PROPERTY, JDBC_URL_PROPERTY, get_conf_dir, JDBC_USER_NAME_PROPERTY, JDBC_PASSWORD_PROPERTY, \
           JDBC_DATABASE_NAME_PROPERTY, OS_TYPE_PROPERTY, validate_jdk, JDBC_POSTGRES_SCHEMA_PROPERTY, \
@@ -1110,6 +1111,45 @@ class TestAmbariServer(TestCase):
     self.assertEquals(user, None)
     pass
 
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch("ambari_server.serverConfiguration.Properties")
+  def test_read_active_instance(self, properties_mock, get_ambari_properties_mock):
+    # Set up the mock
+    properties_mock.propertyNames = MagicMock(return_value=['active.instance'])
+    get_ambari_properties_mock.return_value = properties_mock
+
+    # Test with explicitly set value of "false" (should return False)
+    properties_mock.__getitem__.return_value = "false"
+    is_active_instance = get_is_active_instance()
+    self.assertFalse(is_active_instance)
+
+    # Test with empty string  (should return False)
+    properties_mock.__getitem__.return_value = ""
+    is_active_instance = get_is_active_instance()
+    self.assertFalse(is_active_instance)
+
+    # Test with a random string (should return False)
+    properties_mock.__getitem__.return_value = "xyz"
+    is_active_instance = get_is_active_instance()
+    self.assertFalse(is_active_instance)
+
+    # Test with a explicit false string (should return False)
+    properties_mock.__getitem__.return_value = "false"
+    is_active_instance = get_is_active_instance()
+    self.assertFalse(is_active_instance)
+
+    # Test with explicitly set value of "true"  (should return True)
+    properties_mock.__getitem__.return_value = "true"
+    is_active_instance = get_is_active_instance()
+    self.assertTrue(is_active_instance)
+
+    # Test with missing active.instance entry (should return True)
+    properties_mock.propertyNames = MagicMock(return_value=[])
+    is_active_instance = get_is_active_instance()
+    self.assertTrue(is_active_instance)
+
+    pass
+
   @patch("ambari_server.setupSecurity.get_file_owner")
   @patch("ambari_server.setupSecurity.get_ambari_repo_file_full_name")
   @patch("os.path.exists")
@@ -4115,6 +4155,7 @@ class TestAmbariServer(TestCase):
 
 
   @not_for_platform(PLATFORM_WINDOWS)
+  @patch("ambari_server_main.get_is_active_instance")
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch("sys.stdout.flush")
   @patch("sys.stdout.write")
@@ -4168,10 +4209,13 @@ class TestAmbariServer(TestCase):
                  save_master_key_method, get_master_key_location_method,
                  os_chown_mock, is_server_running_mock, locate_file_mock,
                  os_makedirs_mock, check_exitcode_mock, save_main_pid_ex_mock,
-                 wait_for_pid_mock, looking_for_pid_mock, stdout_write_mock, stdout_flush_mock):
+                 wait_for_pid_mock, looking_for_pid_mock, stdout_write_mock, stdout_flush_mock,
+                 get_is_active_instance_mock):
 
     def reset_mocks():
       pexistsMock.reset_mock()
+      get_is_active_instance_mock.reset_mock()
+      get_is_active_instance_mock.return_value = True
 
       args = MagicMock()
       del args.dbms
@@ -4261,6 +4305,17 @@ class TestAmbariServer(TestCase):
       self.assertTrue('Unable to start Ambari Server as user' in e.reason)
       #self.assertFalse(parse_properties_file_mock.called)
 
+    # If not active instance, exception should be thrown
+    args = reset_mocks()
+    get_is_active_instance_mock.return_value = False
+    try:
+      _ambari_server_.start(args)
+      self.fail("Should fail with 'This is not an active instance. Shutting down...'")
+    except FatalException as e:
+      # Expected
+      self.assertTrue('This is not an active instance' in e.reason)
+      pass
+
     # Checking "jdk not found"
     args = reset_mocks()
     is_root_3_mock.return_value = \