Quellcode durchsuchen

AMBARI-11219. Create Default Upgrade Catalog to upgrade the schema version to the current Ambari Server Versions. (mpapirkovskyy)

Myroslav Papirkovskyy vor 10 Jahren
Ursprung
Commit
3c4e9111ef

+ 13 - 0
ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java

@@ -842,6 +842,19 @@ public class Configuration {
     return properties.getProperty(SERVER_VERSION_FILE);
   }
 
+  /**
+   * Gets ambari server version
+   * @return version String
+   */
+  public String getServerVersion() {
+    try {
+      return FileUtils.readFileToString(new File(getServerVersionFilePath())).trim();
+    } catch (IOException e) {
+      LOG.error("Unable to read server version file", e);
+    }
+    return null;
+  }
+
   /**
    * Check to see if the API should be authenticated or not
    * @return false if not, true if the authentication is enabled.

+ 11 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java

@@ -76,6 +76,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
   @Inject
   public AbstractUpgradeCatalog(Injector injector) {
     this.injector = injector;
+    injector.injectMembers(this);
     registerCatalog(this);
   }
 
@@ -100,6 +101,11 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
     @Override
     public int compare(UpgradeCatalog upgradeCatalog1,
                        UpgradeCatalog upgradeCatalog2) {
+      //make sure FinalUpgradeCatalog runs last
+      if (upgradeCatalog1.isFinal() ^ upgradeCatalog2.isFinal()) {
+        return Boolean.compare(upgradeCatalog1.isFinal(), upgradeCatalog2.isFinal());
+      }
+
       return VersionUtils.compareVersions(upgradeCatalog1.getTargetVersion(),
         upgradeCatalog2.getTargetVersion(), 3);
     }
@@ -382,6 +388,11 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
     updateMetaInfoVersion(getTargetVersion());
   }
 
+  @Override
+  public boolean isFinal() {
+    return false;
+  }
+
   protected abstract void executeDDLUpdates() throws AmbariException, SQLException;
 
   /**

+ 66 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/FinalUpgradeCatalog.java

@@ -0,0 +1,66 @@
+/**
+ * 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.utils.VersionUtils;
+
+import java.sql.SQLException;
+
+/**
+ * Final upgrade catalog which simply updates database version (in case if no db changes between releases)
+ */
+public class FinalUpgradeCatalog extends AbstractUpgradeCatalog {
+
+  @Inject
+  public FinalUpgradeCatalog(Injector injector) {
+    super(injector);
+  }
+
+  @Override
+  protected void executeDDLUpdates() throws AmbariException, SQLException {
+    //noop
+  }
+
+  @Override
+  protected void executePreDMLUpdates() throws AmbariException, SQLException {
+    //noop
+  }
+
+  @Override
+  protected void executeDMLUpdates() throws AmbariException, SQLException {
+    //noop
+  }
+
+  @Override
+  public String getTargetVersion() {
+    return getFinalVersion();
+  }
+
+  @Override
+  public boolean isFinal() {
+    return true;
+  }
+
+  private String getFinalVersion() {
+    return VersionUtils.getVersionSubstring(configuration.getServerVersion());
+  }
+}

+ 2 - 7
ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java

@@ -108,13 +108,7 @@ public class SchemaUpgradeHelper {
    * @return
    */
   protected String getAmbariServerVersion() {
-    String versionFilePath = configuration.getServerVersionFilePath();
-    try {
-      return FileUtils.readFileToString(new File(versionFilePath)).trim();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-    return null;
+    return configuration.getServerVersion();
   }
 
   /**
@@ -175,6 +169,7 @@ public class SchemaUpgradeHelper {
       catalogBinder.addBinding().to(UpgradeCatalog170.class);
       catalogBinder.addBinding().to(UpgradeCatalog200.class);
       catalogBinder.addBinding().to(UpgradeCatalog210.class);
+      catalogBinder.addBinding().to(FinalUpgradeCatalog.class);
     }
   }
 

+ 6 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog.java

@@ -46,6 +46,12 @@ public interface UpgradeCatalog {
    */
   void upgradeData() throws AmbariException, SQLException;
 
+  /**
+   * Defines if Upgrade Catalog should be executed last
+   * @return
+   */
+  boolean isFinal();
+
   /**
    * Called after {@link #upgradeSchema()} and {@link #upgradeData()}, this
    * method is used to perform any operations after the catalog has finished.

+ 5 - 0
ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeTest.java

@@ -74,6 +74,8 @@ import com.google.inject.Key;
 import com.google.inject.TypeLiteral;
 import com.google.inject.persist.PersistService;
 
+import static org.junit.Assert.assertTrue;
+
 @RunWith(Parameterized.class)
 public class UpgradeTest {
   private static final Logger LOG = LoggerFactory.getLogger(UpgradeTest.class);
@@ -199,6 +201,9 @@ public class UpgradeTest {
     List<UpgradeCatalog> upgradeCatalogs =
         schemaUpgradeHelper.getUpgradePath(sourceVersion, targetVersion);
 
+    assertTrue("Final Upgrade Catalog should be run last",
+      !upgradeCatalogs.isEmpty() && upgradeCatalogs.get(upgradeCatalogs.size() - 1).isFinal());
+
     try {
       schemaUpgradeHelper.executeUpgrade(upgradeCatalogs);
     } catch (Exception e) {