ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject [1/3] ambari git commit: AMBARI-20997. Ambari upgrade failed (dgrinenko via dlysnichenko)
Date Fri, 12 May 2017 13:54:59 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 57a6d15f6 -> c4691de8e
  refs/heads/trunk 08dd15bd3 -> 48c9b3402


AMBARI-20997. Ambari upgrade failed (dgrinenko via dlysnichenko)


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

Branch: refs/heads/branch-2.5
Commit: e18e41229d79a86bdcbb1d6555e8a4356816fcef
Parents: 57a6d15
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Fri May 12 16:50:47 2017 +0300
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Fri May 12 16:50:47 2017 +0300

----------------------------------------------------------------------
 .../apache/ambari/server/orm/DBAccessor.java    | 24 +++++++++++
 .../ambari/server/orm/DBAccessorImpl.java       | 44 ++++++++++++++++++++
 .../server/orm/helpers/dbms/DbmsHelper.java     | 21 ++++++++++
 .../orm/helpers/dbms/GenericDbmsHelper.java     |  8 ++++
 .../server/orm/helpers/dbms/MySqlHelper.java    | 12 ++++++
 .../server/orm/helpers/dbms/OracleHelper.java   | 12 ++++++
 .../server/orm/helpers/dbms/PostgresHelper.java | 12 ++++++
 .../server/upgrade/UpgradeCatalog251.java       | 15 ++-----
 8 files changed, 137 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
index c132a3d..fac524c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
@@ -623,6 +623,30 @@ public interface DBAccessor {
    */
   void addDefaultConstraint(String tableName, DBColumnInfo column) throws SQLException;
 
+  /**
+   * Move column data from {@code sourceTableName} to {@code targetTableName} using {@code
sourceIDFieldName} and
+   * {@code targetIDFieldName} keys to match right rows
+   *
+   * @param sourceTableName
+   *          the source table name
+   * @param sourceColumn
+   *          the source column name
+   * @param sourceIDFieldName
+   *          the source id key filed name matched with {@code targetIDFieldName}
+   * @param targetTableName
+   *          the target table name
+   * @param targetColumn
+   *          the target column name
+   * @param targetIDFieldName
+   *          the target id key name matched with {@code sourceIDFieldName}
+   * @param isColumnNullable
+   *          should be target column nullable or not
+   *
+   * @throws SQLException
+   */
+  void moveColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn, String
sourceIDFieldName,
+       String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName, boolean
isColumnNullable) throws SQLException;
+
   enum DbType {
     ORACLE,
     MYSQL,

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
index a399558..c5b116c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java
@@ -1298,4 +1298,48 @@ public class DBAccessorImpl implements DBAccessor {
 
     return valueString;
   }
+
+  /**
+   * Move column data from {@code sourceTableName} to {@code targetTableName} using {@code
sourceIDFieldName} and
+   * {@code targetIDFieldName} keys to match right rows
+   *
+   * @param sourceTableName
+   *          the source table name
+   * @param sourceColumn
+   *          the source column name
+   * @param sourceIDFieldName
+   *          the source id key filed name matched with {@code targetIDFieldName}
+   * @param targetTableName
+   *          the target table name
+   * @param targetColumn
+   *          the target column name
+   * @param targetIDFieldName
+   *          the target id key name matched with {@code sourceIDFieldName}
+   * @param isColumnNullable
+   *          should be target column nullable or not
+   *
+   * @throws SQLException
+   */
+  @Override
+  public void moveColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn,
String sourceIDFieldName,
+              String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName,
 boolean isColumnNullable) throws SQLException {
+
+    if (this.tableHasColumn(sourceTableName, sourceIDFieldName)) {
+
+      final String moveSQL = dbmsHelper.getCopyColumnToAnotherTableStatement(sourceTableName,
sourceColumn.getName(),
+        sourceIDFieldName, targetTableName, targetColumn.getName(),targetIDFieldName);
+
+      targetColumn.setNullable(true);  // setting column nullable by default
+
+      this.addColumn(targetTableName, targetColumn);
+      this.executeUpdate(moveSQL, false);
+
+      if (!isColumnNullable) {
+        // this can will trigger exception if some record is null
+        // ToDo: add default option
+        this.setColumnNullable(targetTableName, targetColumn.getName(), false);
+      }
+      this.dropColumn(sourceTableName, sourceColumn.getName());
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
index c2778d3..fa9814a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java
@@ -128,6 +128,27 @@ public interface DbmsHelper {
   String getSetNullableStatement(String tableName, DBAccessor.DBColumnInfo columnInfo, boolean
nullable);
 
   /**
+   * Get's the {@code UPDATE} statement for {@code sourceTable} for copy column from {@code
targetTable} by matching
+   * table keys {@code sourceIDColumnName} and {@code targetIDColumnName}
+   *
+   * @param sourceTable
+   *          the source table name
+   * @param sourceColumnName
+   *          the source column name
+   * @param sourceIDColumnName
+   *          source key id column which would be used to math right rows for {@code targetTable}
+   * @param targetTable
+   *          the destination table name
+   * @param targetColumnName
+   *          the destination column name
+   * @param targetIDColumnName
+   *          destination key id column name which should math {@code sourceIDColumnName}
+   * @return
+   */
+  String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName,
String sourceIDColumnName,
+                                              String targetTable, String targetColumnName,
String targetIDColumnName);
+
+  /**
    * Gets whether the database platform supports adding contraints after the
    * {@code NULL} constraint. Some database, such as Oracle, don't allow this.
    * Unfortunately, EclipsLink hard codes the order of constraints.

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
index 36fab83..2a0bb93 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java
@@ -78,6 +78,14 @@ public class GenericDbmsHelper implements DbmsHelper {
     return stringBuilder.toString();
   }
 
+  /**
+   {@inheritDoc}
+   */
+  @Override
+  public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName,
String sourceIDColumnName, String targetTable, String targetColumnName, String targetIDColumnName)
{
+    throw new UnsupportedOperationException("Column copy is not supported for generic DB");
+  }
+
   public StringBuilder writeAlterTableClause(StringBuilder builder, String tableName) {
     builder.append("ALTER TABLE ").append(tableName).append(" ");
     return builder;

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java
index 8a83c90..ad26520 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java
@@ -93,4 +93,16 @@ public class MySqlHelper extends GenericDbmsHelper {
     }
     return defaultWriter;
   }
+
+  /**
+   {@inheritDoc}
+   */
+  @Override
+  public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName,
+         String sourceIDColumnName, String targetTable, String targetColumnName, String targetIDColumnName)
{
+
+    return String.format("UPDATE %1$s AS a INNER JOIN %2$s AS b ON a.%5$s = b.%6$s SET a.%3$s
= b.%4$s",
+      targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName);
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java
index b5955b4..73356d1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java
@@ -79,4 +79,16 @@ public class OracleHelper extends GenericDbmsHelper {
   public boolean isConstraintSupportedAfterNullability() {
     return false;
   }
+
+  /**
+   {@inheritDoc}
+   */
+  @Override
+  public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName,
+         String sourceIDColumnName, String targetTable, String targetColumnName, String targetIDColumnName)
{
+
+    // sub-query should return only one value, ROWNUM is safe-guard for this
+    return String.format("UPDATE %1$s a SET (a.%3$s) = (SELECT b.%4$s FROM %2$s b WHERE b.%6$s
= a.%5$s and ROWNUM < 2)",
+      targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName);
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java
index ad18428..46d7315 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java
@@ -44,6 +44,18 @@ public class PostgresHelper extends GenericDbmsHelper {
     return builder;
   }
 
+  /**
+   {@inheritDoc}
+   */
+  @Override
+  public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName,
+         String sourceIDColumnName, String targetTable, String targetColumnName, String targetIDColumnName)
{
+
+    return String.format("UPDATE %1$s AS a SET %3$s = b.%4$s FROM %2$s AS b WHERE a.%5$s
= b.%6$s",
+      targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName);
+  }
+
+
   @Override
   public StringBuilder writeSetNullableString(StringBuilder builder,
       String tableName, DBAccessor.DBColumnInfo columnInfo, boolean nullable) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/e18e4122/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog251.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog251.java
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog251.java
index c4f198f..146520f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog251.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog251.java
@@ -114,17 +114,10 @@ public class UpgradeCatalog251 extends AbstractUpgradeCatalog {
    * @throws SQLException
    */
   private void moveClusterHostColumnFromStageToRequest() throws SQLException {
-    if (dbAccessor.tableHasColumn(STAGE_TABLE, CLUSTER_HOST_INFO_COLUMN)){
+    DBColumnInfo sourceColumn = new DBColumnInfo(CLUSTER_HOST_INFO_COLUMN, byte[].class,
null, null, false);
+    DBColumnInfo targetColumn = new DBColumnInfo(CLUSTER_HOST_INFO_COLUMN, byte[].class,
null, null, false);
 
-      dbAccessor.addColumn(REQUEST_TABLE, new DBColumnInfo(CLUSTER_HOST_INFO_COLUMN, byte[].class,
null, null, true));
-
-      // Native query currently is best way to move data, in future move this functionality
to dbAccessor
-      final String moveSQL = String.format("UPDATE %1$s AS a SET %3$s = b.%3$s FROM %2$s
AS b WHERE a.%4$s = b.%4$s;",
-        REQUEST_TABLE, STAGE_TABLE, CLUSTER_HOST_INFO_COLUMN, REQUEST_ID_COLUMN);
-
-      dbAccessor.executeUpdate(moveSQL, false);
-      dbAccessor.alterColumn(REQUEST_TABLE, new DBColumnInfo(CLUSTER_HOST_INFO_COLUMN, byte[].class,
null, null, false));
-      dbAccessor.dropColumn(STAGE_TABLE, CLUSTER_HOST_INFO_COLUMN);
-    }
+    dbAccessor.moveColumnToAnotherTable(STAGE_TABLE, sourceColumn, REQUEST_ID_COLUMN, REQUEST_TABLE,
targetColumn,
+      REQUEST_ID_COLUMN, false);
   }
 }


Mime
View raw message