You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by mp...@apache.org on 2015/09/17 16:10:26 UTC

ambari git commit: AMBARI-13125. AlertCurrent and AlertHistory cannot persist into Oracle DB. (mpapirkovskyy)

Repository: ambari
Updated Branches:
  refs/heads/branch-2.0.maint cb9586542 -> 744ac1054


AMBARI-13125. AlertCurrent and AlertHistory cannot persist into Oracle DB. (mpapirkovskyy)


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

Branch: refs/heads/branch-2.0.maint
Commit: 744ac1054e70a19ecdad7043b6c9ebad88ee84e0
Parents: cb95865
Author: Myroslav Papirkovskyy <mp...@hortonworks.com>
Authored: Thu Sep 17 17:08:19 2015 +0300
Committer: Myroslav Papirkovskyy <mp...@hortonworks.com>
Committed: Thu Sep 17 17:08:19 2015 +0300

----------------------------------------------------------------------
 .../apache/ambari/server/orm/DBAccessor.java    |  30 +++--
 .../server/orm/entities/AlertCurrentEntity.java |   6 +-
 .../server/orm/entities/AlertHistoryEntity.java |  17 +--
 .../orm/helpers/dbms/GenericDbmsHelper.java     |   6 +-
 .../server/upgrade/SchemaUpgradeHelper.java     |   1 +
 .../server/upgrade/UpgradeCatalog203.java       |  62 ++++++++++
 .../main/resources/Ambari-DDL-Oracle-CREATE.sql |   4 +-
 .../server/upgrade/UpgradeCatalog203Test.java   | 114 +++++++++++++++++++
 8 files changed, 211 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/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 c39a806..404c9ca 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
@@ -23,6 +23,7 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
 
+import org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition;
 import org.eclipse.persistence.sessions.DatabaseSession;
 
 /**
@@ -411,11 +412,12 @@ public interface DBAccessor {
   public class DBColumnInfo {
     private String name;
     private Class type;
-//    private DBColumnType type;
     private Integer length;
     private Object defaultValue;
     private boolean isNullable;
 
+    private FieldTypeDefinition dbType = null;
+
     public DBColumnInfo(String name, Class type, Integer length) {
       this(name, type, length, null, true);
     }
@@ -429,6 +431,18 @@ public interface DBAccessor {
       isNullable = nullable;
     }
 
+    public DBColumnInfo(String name, FieldTypeDefinition dbType, Integer length, Object defaultValue, boolean isNullable) {
+      this.name = name;
+      this.length = length;
+      this.isNullable = isNullable;
+      this.defaultValue = defaultValue;
+      this.dbType = dbType;
+    }
+
+    public DBColumnInfo(String name, FieldTypeDefinition dbType, Integer length) {
+      this(name, dbType, length, null, true);
+    }
+
     public String getName() {
       return name;
     }
@@ -469,14 +483,12 @@ public interface DBAccessor {
       isNullable = nullable;
     }
 
-    public enum DBColumnType {
-      VARCHAR,
-      CHAR,
-      INT,
-      LONG,
-      BOOL,
-      TIME,
-      BLOB
+    public FieldTypeDefinition getDbType() {
+      return dbType;
+    }
+
+    public void setDbType(FieldTypeDefinition dbType) {
+      this.dbType = dbType;
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
index 66aa119..47549c7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
@@ -26,6 +26,7 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.Lob;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
@@ -75,7 +76,8 @@ public class AlertCurrentEntity {
   @Column(name = "original_timestamp", nullable = false)
   private Long originalTimestamp;
 
-  @Column(name = "latest_text", length = 4000)
+  @Lob
+  @Column(name = "latest_text")
   private String latestText = null;
 
   /**
@@ -250,4 +252,4 @@ public class AlertCurrentEntity {
     int result = null != alertId ? alertId.hashCode() : 0;
     return result;
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
index 8e96aca..46216b1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
@@ -17,19 +17,7 @@
  */
 package org.apache.ambari.server.orm.entities;
 
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.EnumType;
-import javax.persistence.Enumerated;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.JoinColumn;
-import javax.persistence.ManyToOne;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.Table;
-import javax.persistence.TableGenerator;
+import javax.persistence.*;
 
 import org.apache.ambari.server.state.AlertState;
 
@@ -68,7 +56,8 @@ public class AlertHistoryEntity {
   @Column(name = "alert_state", nullable = false, length = 255)
   private AlertState alertState;
 
-  @Column(name = "alert_text", length = 32672)
+  @Lob
+  @Column(name = "alert_text")
   private String alertText;
 
   @Column(name = "alert_timestamp", nullable = false)

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/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 9816133..5783f44 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
@@ -90,9 +90,11 @@ public class GenericDbmsHelper implements DbmsHelper {
   }
 
   public StringBuilder writeColumnType(StringBuilder builder, DBAccessor.DBColumnInfo columnInfo) {
-    FieldTypeDefinition fieldType;
+    FieldTypeDefinition fieldType = columnInfo.getDbType();
 
-    fieldType = databasePlatform.getFieldTypeDefinition(columnInfo.getType());
+    if (fieldType == null) {
+      fieldType = databasePlatform.getFieldTypeDefinition(columnInfo.getType());
+    }
 
     if (fieldType == null) {
       throw new IllegalArgumentException("Unable to convert data type");

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
index 5968b2f..bb0fb38 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
@@ -173,6 +173,7 @@ public class SchemaUpgradeHelper {
       catalogBinder.addBinding().to(UpgradeCatalog161.class);
       catalogBinder.addBinding().to(UpgradeCatalog170.class);
       catalogBinder.addBinding().to(UpgradeCatalog200.class);
+      catalogBinder.addBinding().to(UpgradeCatalog203.class);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog203.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog203.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog203.java
new file mode 100644
index 0000000..738fb4d
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog203.java
@@ -0,0 +1,62 @@
+/*
+ * 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.upgrade;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.DBAccessor;
+import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
+import org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition;
+
+import java.sql.SQLException;
+
+/**
+ * Upgrade catalog for version 2.0.3
+ */
+public class UpgradeCatalog203 extends AbstractUpgradeCatalog {
+
+  @Inject
+  public UpgradeCatalog203(Injector injector) {
+    super(injector);
+  }
+
+  @Override
+  protected void executeDDLUpdates() throws AmbariException, SQLException {
+    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
+    if (Configuration.DatabaseType.MYSQL == databaseType) {
+        dbAccessor.alterColumn("alert_current", new DBColumnInfo("latest_text", new FieldTypeDefinition("TEXT"), null));
+        dbAccessor.alterColumn("alert_history", new DBColumnInfo("alert_text", new FieldTypeDefinition("TEXT"), null));
+      } else {
+        dbAccessor.alterColumn("alert_current", new DBColumnInfo("latest_text", Character[].class, null));
+        dbAccessor.alterColumn("alert_history", new DBColumnInfo("alert_text", Character[].class, null));
+      }
+  }
+
+  @Override
+  protected void executeDMLUpdates() throws AmbariException, SQLException {
+
+  }
+
+  @Override
+  public String getTargetVersion() {
+    return "2.0.3";
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
index 7d62aee..e9b0802 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -643,7 +643,7 @@ CREATE TABLE alert_history (
   alert_timestamp NUMBER(19) NOT NULL,
   alert_label VARCHAR2(1024),
   alert_state VARCHAR2(255) NOT NULL,
-  alert_text VARCHAR2(4000),
+  alert_text CLOB,
   PRIMARY KEY (alert_id),
   FOREIGN KEY (alert_definition_id) REFERENCES alert_definition(definition_id),
   FOREIGN KEY (cluster_id) REFERENCES clusters(cluster_id)
@@ -656,7 +656,7 @@ CREATE TABLE alert_current (
   maintenance_state VARCHAR2(255) NOT NULL,
   original_timestamp NUMBER(19) NOT NULL,
   latest_timestamp NUMBER(19) NOT NULL,
-  latest_text VARCHAR2(4000),
+  latest_text CLOB,
   PRIMARY KEY (alert_id),
   FOREIGN KEY (definition_id) REFERENCES alert_definition(definition_id),
   FOREIGN KEY (history_id) REFERENCES alert_history(alert_id)

http://git-wip-us.apache.org/repos/asf/ambari/blob/744ac105/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog203Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog203Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog203Test.java
new file mode 100644
index 0000000..5d67ee6
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog203Test.java
@@ -0,0 +1,114 @@
+/*
+ * 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.upgrade;
+
+import com.google.inject.*;
+import com.google.inject.persist.PersistService;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.DBAccessor;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.easymock.Capture;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+
+import java.lang.reflect.Field;
+import java.sql.ResultSet;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.*;
+
+public class UpgradeCatalog203Test {
+  private final String CLUSTER_NAME = "c1";
+  private final String HOST_NAME = "h1";
+
+  private Injector injector;
+  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
+  private EntityManager entityManager = createNiceMock(EntityManager.class);
+  private UpgradeCatalogHelper upgradeCatalogHelper;
+
+  @Before
+  public void init() {
+    reset(entityManagerProvider);
+    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
+    replay(entityManagerProvider);
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+
+    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
+  }
+
+  @After
+  public void tearDown() {
+    injector.getInstance(PersistService.class).stop();
+  }
+
+  @Test
+  public void testExecuteDDLUpdates() throws Exception {
+    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+    Configuration configuration = createNiceMock(Configuration.class);
+    ResultSet resultSet = createNiceMock(ResultSet.class);
+
+    Capture<DBAccessor.DBColumnInfo> alertCurrentLatestTextCapture = new Capture<DBAccessor.DBColumnInfo>();
+    Capture<DBAccessor.DBColumnInfo> alertHistoryAlertTextCapture = new Capture<DBAccessor.DBColumnInfo>();
+
+    dbAccessor.alterColumn(eq("alert_current"), capture(alertCurrentLatestTextCapture));
+    dbAccessor.alterColumn(eq("alert_history"), capture(alertHistoryAlertTextCapture));
+
+    replay(dbAccessor, configuration, resultSet);
+
+    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
+    Class<?> c = AbstractUpgradeCatalog.class;
+    Field f = c.getDeclaredField("configuration");
+    f.setAccessible(true);
+    f.set(upgradeCatalog, configuration);
+
+    upgradeCatalog.executeDDLUpdates();
+
+    verify(dbAccessor, configuration, resultSet);
+
+    DBAccessor.DBColumnInfo column = alertCurrentLatestTextCapture.getValue();
+    Assert.assertNull(column.getLength());
+    Assert.assertEquals(Character[].class, column.getType());
+    Assert.assertEquals("latest_text", column.getName());
+
+    column = alertHistoryAlertTextCapture.getValue();
+    Assert.assertNull(column.getLength());
+    Assert.assertEquals(Character[].class, column.getType());
+    Assert.assertEquals("alert_text", column.getName());
+
+  }
+
+  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
+    Module module = new Module() {
+      @Override
+      public void configure(Binder binder) {
+        binder.bind(DBAccessor.class).toInstance(dbAccessor);
+      }
+    };
+
+    Injector injector = Guice.createInjector(module);
+    return injector.getInstance(UpgradeCatalog203.class);
+  }
+}
\ No newline at end of file