浏览代码

AMBARI-3530. Backend support to perform database connectivity testing for Hive/Oozie DB Config/Reconfig.(Vitaly Brodetskyi via odiachenko)

Oleksandr Diachenko 11 年之前
父节点
当前提交
859ba4b499

+ 8 - 1
ambari-agent/src/main/puppet/modules/hdp-hive/manifests/init.pp

@@ -54,7 +54,14 @@ class hdp-hive(
   }
 
   anchor { 'hdp-hive::begin': }
-  anchor { 'hdp-hive::end': } 
+  anchor { 'hdp-hive::end': }
+
+  if ($service_state == 'installed_and_configured' and ($hive_jdbc_driver == "com.mysql.jdbc.Driver" or $hive_jdbc_driver == "oracle.jdbc.driver.OracleDriver")) {
+    hdp::exec { "download DBConnectorVerification.jar" :
+      command => "/bin/sh -c 'cd /usr/lib/ambari-agent/ && curl -s -o ${hdp::params::check_db_connection_jar_name} ${hdp::params::jdk_location}${hdp::params::check_db_connection_jar_name}'",
+      unless  => "[ -f ${check_db_connection_jar} ]"
+    }
+  }
 
   if ($service_state == 'uninstalled') {
     hdp::package { 'hive' : 

+ 1 - 0
ambari-agent/src/main/puppet/modules/hdp-hive/manifests/params.pp

@@ -25,6 +25,7 @@ class hdp-hive::params() inherits hdp::params
   $hive_metastore_user_name = hdp_default("hive-site/javax.jdo.option.ConnectionUserName","dbusername")
   $hive_metastore_user_passwd = hdp_default("hive-site/javax.jdo.option.ConnectionPassword","dbpassword")
   $hive_server_conf_dir = hdp_default("hive_server_conf_dir", "/etc/hive/conf.server")
+  $hive_jdbc_connection_url = hdp_default("hive_jdbc_connection_url", "")
 
   ### users
   $hive_user = $hdp::params::hive_user

+ 18 - 4
ambari-agent/src/main/puppet/modules/hdp-hive/manifests/service.pp

@@ -94,12 +94,26 @@ class hdp-hive::service(
       }
     }
 
+    file { $pid_file:
+      ensure => $pid_file_state
+    }
 
-  file { $pid_file:
-    ensure => $pid_file_state
-  }
+    if ($ensure == 'running' and ($hive_jdbc_driver == "com.mysql.jdbc.Driver" or $hive_jdbc_driver == "oracle.jdbc.driver.OracleDriver")) {
+      $db_connection_check_command = "java -cp ${hdp::params::check_db_connection_jar}:/usr/share/java/${hdp-hive::params::jdbc_jar_name} org.apache.ambari.server.DBConnectionVerification ${hdp-hive::params::hive_jdbc_connection_url} ${hdp-hive::params::hive_metastore_user_name} ${hdp-hive::params::hive_metastore_user_passwd} ${hdp-hive::params::hive_jdbc_driver}"
+    } else {
+      $db_connection_check_command = undef
+    }
 
-    Hdp-hive::Service::Directory<||> -> File[ $start_metastore_path]-> File[ $start_hiveserver2_path]-> Hdp::Exec[$daemon_cmd] -> File[$pid_file] -> Anchor['hdp-hive::service::end']
+    if ($db_connection_check_command != undef) {
+      hdp::exec { "DB connection check $db_connection_check_command" :
+        command => $db_connection_check_command,
+        path    => '/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin'
+      }
+
+      Hdp-hive::Service::Directory<||> -> Hdp::Exec["DB connection check $db_connection_check_command"] -> File[ $start_metastore_path]-> File[ $start_hiveserver2_path]-> Hdp::Exec[$daemon_cmd] -> File[$pid_file] -> Anchor['hdp-hive::service::end']
+    } else {
+      Hdp-hive::Service::Directory<||> -> File[ $start_metastore_path]-> File[ $start_hiveserver2_path]-> Hdp::Exec[$daemon_cmd] -> File[$pid_file] -> Anchor['hdp-hive::service::end']
+    }
   }
 }
 

+ 7 - 0
ambari-agent/src/main/puppet/modules/hdp-oozie/manifests/init.pp

@@ -89,6 +89,13 @@ class hdp-oozie(
 
      if ($service_state == 'installed_and_configured') {
        hdp-oozie::configfile { 'oozie-log4j.properties': }
+
+       if ($hdp::params::oozie_jdbc_driver == "com.mysql.jdbc.Driver" or $hdp::params::oozie_jdbc_driver == "oracle.jdbc.driver.OracleDriver") {
+         hdp::exec { "download DBConnectorVerification.jar" :
+           command => "/bin/sh -c 'cd /usr/lib/ambari-agent/ && curl -s -o ${hdp::params::check_db_connection_jar_name} ${hdp::params::jdk_location}${hdp::params::check_db_connection_jar_name}'",
+           unless  => "[ -f ${check_db_connection_jar} ]"
+         }
+       }
      }
 
      hdp-oozie::ownership { 'ownership': }

+ 5 - 0
ambari-agent/src/main/puppet/modules/hdp-oozie/manifests/params.pp

@@ -27,6 +27,11 @@ class hdp-oozie::params() inherits hdp::params
   $ext_zip_url = "${download_url}/ext-2.2.zip"
   $ext_zip_name = hdp_default("ext_zip_name","ext-2.2.zip")
 
+  ###oozie jdbc
+  $oozie_metastore_user_name = hdp_default("oozie_metastore_user_name","oozie")
+  $oozie_metastore_user_passwd = hdp_default("oozie_metastore_user_passwd","")
+  $oozie_jdbc_connection_url = hdp_default("oozie_jdbc_connection_url", "")
+
   ### oozie-env
   $conf_dir = $hdp::params::oozie_conf_dir
   $hadoop_prefix = hdp_default("hadoop_prefix","/usr")

+ 17 - 2
ambari-agent/src/main/puppet/modules/hdp-oozie/manifests/service.pp

@@ -54,7 +54,7 @@ class hdp-oozie::service(
   $driver_curl_target = "${java_share_dir}/${oracle_driver_jar_name}"
   $curl_cmd = "curl -kf --retry 10 ${driver_location}${oracle_driver_jar_name} -o ${driver_curl_target}"
   
-  $jdbc_driver_name = $configuration['oozie-site']['oozie.service.JPAService.jdbc.driver']
+  $jdbc_driver_name = $hdp::params::oozie_jdbc_driver
   if ($jdbc_driver_name == "com.mysql.jdbc.Driver"){
     $jdbc_driver_jar = "/usr/share/java/mysql-connector-java.jar"
   } elsif($jdbc_driver_name == "oracle.jdbc.driver.OracleDriver") {
@@ -128,6 +128,11 @@ class hdp-oozie::service(
     $user_cmds_on_run = [$cmd5]   
     $start_cmd = "su - ${user} -c  'cd ${oozie_tmp} && /usr/lib/oozie/bin/oozie-start.sh'"
     $no_op_test = "ls ${pid_file} >/dev/null 2>&1 && ps `cat ${pid_file}` >/dev/null 2>&1"
+    if ($jdbc_driver_name == "com.mysql.jdbc.Driver" or $jdbc_driver_name == "oracle.jdbc.driver.OracleDriver") {
+      $db_connection_check_command = "java -cp ${hdp::params::check_db_connection_jar}:${jdbc_driver_jar} org.apache.ambari.server.DBConnectionVerification ${hdp-oozie::params::oozie_jdbc_connection_url} ${hdp-oozie::params::oozie_metastore_user_name} ${hdp-oozie::params::oozie_metastore_user_passwd} ${jdbc_driver_name}"
+    } else {
+      $db_connection_check_command = undef
+    }
   } elsif ($ensure == 'stopped') {
     $stop_cmd  = "su - ${user} -c  'cd ${oozie_tmp} && /usr/lib/oozie/bin/oozie-stop.sh' && rm -f ${pid_file}"
     $no_op_test = "ls ${pid_file} >/dev/null 2>&1 && ps `cat ${pid_file}` >/dev/null 2>&1"
@@ -160,7 +165,17 @@ class hdp-oozie::service(
       initial_wait => $initial_wait,
       require => Exec["exec $cmd6"]
     }
-    Hdp-oozie::Service::Directory<||> -> Hdp-oozie::Service::Exec_user[$cmd5] -> Hdp::Exec["exec $cmd6"] -> Hdp::Exec["exec $start_cmd"] -> Anchor['hdp-oozie::service::end']
+
+    if ($db_connection_check_command != undef) {
+      hdp::exec { "DB connection check $db_connection_check_command" :
+        command => $db_connection_check_command,
+        path    => '/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin'
+      }
+
+      Hdp-oozie::Service::Directory<||> -> Hdp::Exec["DB connection check $db_connection_check_command"] -> Hdp-oozie::Service::Exec_user[$cmd5] -> Hdp::Exec["exec $cmd6"] -> Hdp::Exec["exec $start_cmd"] -> Anchor['hdp-oozie::service::end']
+    } else {
+      Hdp-oozie::Service::Directory<||> -> Hdp-oozie::Service::Exec_user[$cmd5] -> Hdp::Exec["exec $cmd6"] -> Hdp::Exec["exec $start_cmd"] -> Anchor['hdp-oozie::service::end']
+    }
   } elsif ($ensure == 'stopped') {
     hdp::exec { "exec $stop_cmd":
       command => $stop_cmd,

+ 3 - 0
ambari-agent/src/main/puppet/modules/hdp/manifests/params.pp

@@ -279,6 +279,9 @@ class hdp::params()
   $oracle_jdbc_url = hdp_default("oracle_jdbc_url", "")
   $mysql_jdbc_url = hdp_default("mysql_jdbc_url", "")
   $db_driver_file = hdp_default("db_driver_filename", "")
+  $check_db_connection_jar_name = "DBConnectionVerification.jar"
+  $check_db_connection_jar = "/usr/lib/ambari-agent/${check_db_connection_jar_name}"
+  $oozie_jdbc_driver = hdp_default("oozie-site/oozie.service.JPAService.jdbc.driver", "")
 
   #####
   $hadoop_home = hdp_default("hadoop_home","/usr")

+ 19 - 0
ambari-server/pom.xml

@@ -314,6 +314,9 @@
                  <source>
                   <location>src/main/resources/hive-schema-0.10.0.oracle.sql</location>
                 </source>
+                <source>
+                  <location>${project.build.directory}/DBConnectionVerification.jar</location>
+                </source> 
                 <source>
                   <!-- This file is also included into agent rpm-->
                   <location>src/main/resources/role_command_order.json</location>
@@ -524,6 +527,22 @@
               </target>
             </configuration>
           </execution>
+          <execution>
+            <phase>package</phase>
+            <configuration>
+              <tasks>
+                <jar destfile="target/DBConnectionVerification.jar">
+                     <fileset dir="${basedir}/target/classes/" includes="**/DBConnectionVerification.class" />
+		     <manifest>
+		      <attribute name="Main-Class" value="org.apache.ambari.server.DBConnectionVerification"/>
+		     </manifest>
+		</jar>
+              </tasks>
+            </configuration>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
         </executions>
       </plugin>
     </plugins>

+ 43 - 0
ambari-server/src/main/java/org/apache/ambari/server/DBConnectionVerification.java

@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.ambari.server;
+
+import java.sql.*;
+
+public class DBConnectionVerification {
+  public static void main(String[] args) throws Exception {
+    String url = args[0];
+    String username = args[1];
+    String password = args[2];
+    String driver = args[3];
+ 
+    Connection conn = null;
+    try {
+       Class.forName(driver);
+       conn = DriverManager.getConnection(url, username, password);
+       System.out.println("Connected to DB Successfully!");
+    } catch (Exception e) {
+       System.out.println("ERROR: Unable to connect to the DB. Please check DB connection properties.");
+       System.out.println(e);
+       System.exit(1);
+    } finally {
+       conn.close();
+    }
+  }
+}