ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject ambari git commit: AMBARI-9095. Errors in Oracle DB DDL (yshylov via dlysnichenko)
Date Tue, 13 Jan 2015 14:28:00 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 967f9c96f -> 666aec8aa


AMBARI-9095. Errors in Oracle DB DDL (yshylov via dlysnichenko)


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

Branch: refs/heads/trunk
Commit: 666aec8aac40970036c5b264ca6b0cb0f148d16d
Parents: 967f9c9
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Tue Jan 13 16:27:12 2015 +0200
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Tue Jan 13 16:27:12 2015 +0200

----------------------------------------------------------------------
 .../AmbariManagementControllerImpl.java         |   6 +-
 .../RepositoryVersionResourceProvider.java      | 121 +----------
 .../orm/entities/RepositoryVersionEntity.java   |  12 +-
 .../stack/upgrade/RepositoryVersionHelper.java  | 205 +++++++++++++++++++
 .../main/resources/Ambari-DDL-Oracle-CREATE.sql |  18 +-
 .../RepositoryVersionResourceProviderTest.java  |  16 --
 .../upgrade/RepositoryVersionHelperTest.java    |  65 ++++++
 7 files changed, 304 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index 7ac9d95..6dabcbb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -125,6 +125,7 @@ import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.configgroup.ConfigGroupFactory;
 import org.apache.ambari.server.state.scheduler.RequestExecutionFactory;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostInstallEvent;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent;
@@ -206,6 +207,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   private AmbariLdapDataPopulator ldapDataPopulator;
   @Inject
   private RepositoryVersionDAO repositoryVersionDAO;
+  @Inject
+  private RepositoryVersionHelper repositoryVersionHelper;
 
   private MaintenanceStateHelper maintenanceStateHelper;
 
@@ -370,7 +373,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         RepositoryVersionEntity existingRepositoryVersion = repositoryVersionDAO.findByStackAndVersion(newStackId.getStackId(),
newStackId.getStackVersion());
         if (existingRepositoryVersion == null) {
           repositoryVersionDAO.create(newStackId.getStackId(), newStackId.getStackVersion(),
newStackId.getStackId(),
-              "", RepositoryVersionResourceProvider.serializeOperatingSystems(stackInfo.getRepositories()));
+              repositoryVersionHelper.getUpgradePackageNameSafe(newStackId.getStackId(),
newStackId.getStackVersion(), newStackId.getStackVersion()),
+              repositoryVersionHelper.serializeOperatingSystems(stackInfo.getRepositories()));
         }
         c.createClusterVersion(stackId.getStackId(), stackId.getStackVersion(), getAuthName(),
RepositoryVersionState.CURRENT);
       } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
index 58817a8..a463439 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
@@ -29,6 +29,7 @@ import java.util.regex.Pattern;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.api.resources.RepositoryResourceDefinition;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
@@ -53,6 +54,7 @@ import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.UpgradePack;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.commons.lang.StringUtils;
 
 import com.google.common.collect.ArrayListMultimap;
@@ -109,7 +111,8 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
     }
   };
 
-  private static Gson gson = new Gson();
+  @Inject
+  private Gson gson;
 
   @Inject
   private RepositoryVersionDAO repositoryVersionDAO;
@@ -120,6 +123,9 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
   @Inject
   private AmbariMetaInfo ambariMetaInfo;
 
+  @Inject
+  private RepositoryVersionHelper repositoryVersionHelper;
+
   /**
    * Create a new resource provider.
    *
@@ -250,7 +256,7 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
               final Object operatingSystems = propertyMap.get(SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID);
               final String operatingSystemsJson = gson.toJson(operatingSystems);
               try {
-                parseOperatingSystems(operatingSystemsJson);
+                repositoryVersionHelper.parseOperatingSystems(operatingSystemsJson);
               } catch (Exception ex) {
                 throw new AmbariException("Json structure for operating systems is incorrect",
ex);
               }
@@ -370,122 +376,15 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
     final Object operatingSystems = properties.get(SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID);
     final String operatingSystemsJson = gson.toJson(operatingSystems);
     try {
-      parseOperatingSystems(operatingSystemsJson);
+      repositoryVersionHelper.parseOperatingSystems(operatingSystemsJson);
     } catch (Exception ex) {
       throw new AmbariException("Json structure for operating systems is incorrect", ex);
     }
     entity.setOperatingSystems(operatingSystemsJson);
-    entity.setUpgradePackage(getUpgradePackageName(stackName, stackVersion, entity.getVersion()));
+    entity.setUpgradePackage(repositoryVersionHelper.getUpgradePackageName(stackName, stackVersion,
entity.getVersion()));
     return entity;
   }
 
-
-  /**
-   * Scans the given stack for upgrade packages which can be applied to update the cluster
to given repository version.
-   *
-   * @param stackName stack name
-   * @param stackVersion stack version
-   * @param repositoryVersion target repository version
-   * @return upgrade pack name
-   * @throws AmbariException if no upgrade packs suit the requirements
-   */
-  private String getUpgradePackageName(String stackName, String stackVersion, String repositoryVersion)
throws AmbariException {
-    final Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks(stackName,
stackVersion);
-    for (Entry<String, UpgradePack> upgradePackEntry : upgradePacks.entrySet()) {
-      final UpgradePack upgradePack = upgradePackEntry.getValue();
-      final String upgradePackName = upgradePackEntry.getKey();
-      // check that upgrade pack has <target> node
-      if (StringUtils.isBlank(upgradePack.getTarget())) {
-        LOG.error("Upgrade pack " + upgradePackName + " is corrupted, it should contain <target>
node");
-        continue;
-      }
-
-      // check that upgrade pack can be applied to selected stack
-      // converting 2.2.*.* -> 2\.2(\.\d+)?(\.\d+)?(-\d+)?
-      String regexPattern = upgradePack.getTarget();
-      regexPattern = regexPattern.replaceAll("\\.", "\\\\."); // . -> \.
-      regexPattern = regexPattern.replaceAll("\\\\\\.\\*", "(\\\\\\.\\\\d+)?"); // \.* ->
(\.\d+)?
-      regexPattern = regexPattern.concat("(-\\d+)?");
-      if (Pattern.matches(regexPattern, repositoryVersion)) {
-        return upgradePackName;
-      }
-    }
-    throw new AmbariException("There were no suitable upgrade packs for stack " + stackName
+ " " + stackVersion);
-  }
-
-  /**
-   * Parses operating systems json to a list of entities. Expects json like:
-   * <pre>
-   * [
-   *    {
-   *       "repositories":[
-   *          {
-   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
-   *             "Repositories/repo_name":"HDP-UTILS",
-   *             "Repositories/repo_id":"HDP-UTILS-1.1.0.20"
-   *          },
-   *          {
-   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
-   *             "Repositories/repo_name":"HDP",
-   *             "Repositories/repo_id":"HDP-2.2"
-   *          }
-   *       ],
-   *       "OperatingSystems/os_type":"redhat5"
-   *    }
-   * ]
-   * </pre>
-   * @param repositoriesJson operating systems json
-   * @return list of operating system entities
-   * @throws Exception if any kind of json parsing error happened
-   */
-  public static List<OperatingSystemEntity> parseOperatingSystems(String repositoriesJson)
throws Exception {
-    final List<OperatingSystemEntity> operatingSystems = new ArrayList<OperatingSystemEntity>();
-    final JsonArray rootJson = new JsonParser().parse(repositoriesJson).getAsJsonArray();
-    for (JsonElement operatingSystemJson: rootJson) {
-      final OperatingSystemEntity operatingSystemEntity = new OperatingSystemEntity();
-      operatingSystemEntity.setOsType(operatingSystemJson.getAsJsonObject().get(OperatingSystemResourceProvider.OPERATING_SYSTEM_OS_TYPE_PROPERTY_ID).getAsString());
-      for (JsonElement repositoryJson: operatingSystemJson.getAsJsonObject().get(SUBRESOURCE_REPOSITORIES_PROPERTY_ID).getAsJsonArray())
{
-        final RepositoryEntity repositoryEntity = new RepositoryEntity();
-        repositoryEntity.setBaseUrl(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID).getAsString());
-        repositoryEntity.setName(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID).getAsString());
-        repositoryEntity.setRepositoryId(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID).getAsString());
-        operatingSystemEntity.getRepositories().add(repositoryEntity);
-      }
-      operatingSystems.add(operatingSystemEntity);
-    }
-    return operatingSystems;
-  }
-
-  /**
-   * Serializes repository info to json for storing to DB.
-   * Produces json like:
-   *
-   * @param repositories list of repository infos
-   * @return serialized list of operating systems
-   */
-  public static String serializeOperatingSystems(List<RepositoryInfo> repositories)
{
-    final JsonArray rootJson = new JsonArray();
-    final Multimap<String, RepositoryInfo> operatingSystems = ArrayListMultimap.create();
-    for (RepositoryInfo repository: repositories) {
-      operatingSystems.put(repository.getOsType(), repository);
-    }
-    for (Entry<String, Collection<RepositoryInfo>> operatingSystem : operatingSystems.asMap().entrySet())
{
-      final JsonObject operatingSystemJson = new JsonObject();
-      final JsonArray repositoriesJson = new JsonArray();
-      for (RepositoryInfo repository : operatingSystem.getValue()) {
-        final JsonObject repositoryJson = new JsonObject();
-        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID,
repository.getBaseUrl());
-        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID,
repository.getRepoName());
-        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID,
repository.getRepoId());
-        repositoriesJson.add(repositoryJson);
-      }
-      operatingSystemJson.add(SUBRESOURCE_REPOSITORIES_PROPERTY_ID, repositoriesJson);
-      operatingSystemJson.addProperty(OperatingSystemResourceProvider.OPERATING_SYSTEM_OS_TYPE_PROPERTY_ID,
operatingSystem.getKey());
-      rootJson.add(operatingSystemJson);
-    }
-    return gson.toJson(rootJson);
-  }
-
   protected StackId getStackInformationFromUrl(Map<String, Object> propertyMap) {
     if (propertyMap.containsKey(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID) && propertyMap.containsKey(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID))
{
       return new StackId(propertyMap.get(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID).toString(),
propertyMap.get(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID).toString());

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
index 4efb794..78cc277 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
@@ -32,12 +32,16 @@ import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 import javax.persistence.UniqueConstraint;
 
-import org.apache.ambari.server.controller.internal.RepositoryVersionResourceProvider;
+import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
 @Entity
 @Table(name = "repo_version", uniqueConstraints = {
     @UniqueConstraint(columnNames = {"display_name"}),
@@ -56,10 +60,14 @@ import org.slf4j.LoggerFactory;
   @NamedQuery(name = "repositoryVersionByStackVersion", query = "SELECT repoversion FROM
RepositoryVersionEntity repoversion WHERE repoversion.stack=:stack AND repoversion.version=:version"),
   @NamedQuery(name = "repositoryVersionByStack", query = "SELECT repoversion FROM RepositoryVersionEntity
repoversion WHERE repoversion.stack=:stack")
 })
+@StaticallyInject
 public class RepositoryVersionEntity {
 
   private static Logger LOG = LoggerFactory.getLogger(RepositoryVersionEntity.class);
 
+  @Inject
+  private static Provider<RepositoryVersionHelper> repositoryVersionHelperProvider;
+
   @Id
   @Column(name = "repo_version_id")
   @GeneratedValue(strategy = GenerationType.TABLE, generator = "repository_version_id_generator")
@@ -151,7 +159,7 @@ public class RepositoryVersionEntity {
   public List<OperatingSystemEntity> getOperatingSystems() {
     if (StringUtils.isNotBlank(operatingSystems)) {
       try {
-        return RepositoryVersionResourceProvider.parseOperatingSystems(operatingSystems);
+        return repositoryVersionHelperProvider.get().parseOperatingSystems(operatingSystems);
       } catch (Exception ex) {
         // Should never happen as we validate json before storing it to DB
         LOG.error("Could not parse operating systems json stored in database:" + operatingSystems,
ex);

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
new file mode 100644
index 0000000..a7d3c3d
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
@@ -0,0 +1,205 @@
+/**
+ * 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.state.stack.upgrade;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.regex.Pattern;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.internal.OperatingSystemResourceProvider;
+import org.apache.ambari.server.controller.internal.RepositoryResourceProvider;
+import org.apache.ambari.server.controller.internal.RepositoryVersionResourceProvider;
+import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
+import org.apache.ambari.server.orm.entities.RepositoryEntity;
+import org.apache.ambari.server.state.RepositoryInfo;
+import org.apache.ambari.server.state.stack.UpgradePack;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+/**
+ * Provides helper methods to manage repository versions.
+ */
+@Singleton
+public class RepositoryVersionHelper {
+
+  private static final Logger LOG = LoggerFactory.getLogger(RepositoryVersionHelper.class);
+
+  @Inject
+  private Gson gson;
+
+  @Inject(optional = true)
+  private AmbariMetaInfo ambariMetaInfo;
+
+  /**
+   * Parses operating systems json to a list of entities. Expects json like:
+   * <pre>
+   * [
+   *    {
+   *       "repositories":[
+   *          {
+   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
+   *             "Repositories/repo_name":"HDP-UTILS",
+   *             "Repositories/repo_id":"HDP-UTILS-1.1.0.20"
+   *          },
+   *          {
+   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
+   *             "Repositories/repo_name":"HDP",
+   *             "Repositories/repo_id":"HDP-2.2"
+   *          }
+   *       ],
+   *       "OperatingSystems/os_type":"redhat5"
+   *    }
+   * ]
+   * </pre>
+   * @param repositoriesJson operating systems json
+   * @return list of operating system entities
+   * @throws Exception if any kind of json parsing error happened
+   */
+  public List<OperatingSystemEntity> parseOperatingSystems(String repositoriesJson)
throws Exception {
+    final List<OperatingSystemEntity> operatingSystems = new ArrayList<OperatingSystemEntity>();
+    final JsonArray rootJson = new JsonParser().parse(repositoriesJson).getAsJsonArray();
+    for (JsonElement operatingSystemJson: rootJson) {
+      final OperatingSystemEntity operatingSystemEntity = new OperatingSystemEntity();
+      operatingSystemEntity.setOsType(operatingSystemJson.getAsJsonObject().get(OperatingSystemResourceProvider.OPERATING_SYSTEM_OS_TYPE_PROPERTY_ID).getAsString());
+      for (JsonElement repositoryJson: operatingSystemJson.getAsJsonObject().get(RepositoryVersionResourceProvider.SUBRESOURCE_REPOSITORIES_PROPERTY_ID).getAsJsonArray())
{
+        final RepositoryEntity repositoryEntity = new RepositoryEntity();
+        repositoryEntity.setBaseUrl(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID).getAsString());
+        repositoryEntity.setName(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID).getAsString());
+        repositoryEntity.setRepositoryId(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID).getAsString());
+        operatingSystemEntity.getRepositories().add(repositoryEntity);
+      }
+      operatingSystems.add(operatingSystemEntity);
+    }
+    return operatingSystems;
+  }
+
+  /**
+   * Serializes repository info to json for storing to DB.
+   * Produces json like:
+   * <pre>
+   * [
+   *    {
+   *       "repositories":[
+   *          {
+   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
+   *             "Repositories/repo_name":"HDP-UTILS",
+   *             "Repositories/repo_id":"HDP-UTILS-1.1.0.20"
+   *          },
+   *          {
+   *             "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
+   *             "Repositories/repo_name":"HDP",
+   *             "Repositories/repo_id":"HDP-2.2"
+   *          }
+   *       ],
+   *       "OperatingSystems/os_type":"redhat5"
+   *    }
+   * ]
+   * </pre>
+   *
+   * @param repositories list of repository infos
+   * @return serialized list of operating systems
+   */
+  public String serializeOperatingSystems(List<RepositoryInfo> repositories) {
+    final JsonArray rootJson = new JsonArray();
+    final Multimap<String, RepositoryInfo> operatingSystems = ArrayListMultimap.create();
+    for (RepositoryInfo repository: repositories) {
+      operatingSystems.put(repository.getOsType(), repository);
+    }
+    for (Entry<String, Collection<RepositoryInfo>> operatingSystem : operatingSystems.asMap().entrySet())
{
+      final JsonObject operatingSystemJson = new JsonObject();
+      final JsonArray repositoriesJson = new JsonArray();
+      for (RepositoryInfo repository : operatingSystem.getValue()) {
+        final JsonObject repositoryJson = new JsonObject();
+        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID,
repository.getBaseUrl());
+        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID,
repository.getRepoName());
+        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID,
repository.getRepoId());
+        repositoriesJson.add(repositoryJson);
+      }
+      operatingSystemJson.add(RepositoryVersionResourceProvider.SUBRESOURCE_REPOSITORIES_PROPERTY_ID,
repositoriesJson);
+      operatingSystemJson.addProperty(OperatingSystemResourceProvider.OPERATING_SYSTEM_OS_TYPE_PROPERTY_ID,
operatingSystem.getKey());
+      rootJson.add(operatingSystemJson);
+    }
+    return gson.toJson(rootJson);
+  }
+
+  /**
+   * Scans the given stack for upgrade packages which can be applied to update the cluster
to given repository version.
+   *
+   * @param stackName stack name
+   * @param stackVersion stack version
+   * @param repositoryVersion target repository version
+   * @return upgrade pack name
+   * @throws AmbariException if no upgrade packs suit the requirements
+   */
+  public String getUpgradePackageName(String stackName, String stackVersion, String repositoryVersion)
throws AmbariException {
+    final Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks(stackName,
stackVersion);
+    for (Entry<String, UpgradePack> upgradePackEntry : upgradePacks.entrySet()) {
+      final UpgradePack upgradePack = upgradePackEntry.getValue();
+      final String upgradePackName = upgradePackEntry.getKey();
+      // check that upgrade pack has <target> node
+      if (StringUtils.isBlank(upgradePack.getTarget())) {
+        LOG.error("Upgrade pack " + upgradePackName + " is corrupted, it should contain <target>
node");
+        continue;
+      }
+
+      // check that upgrade pack can be applied to selected stack
+      // converting 2.2.*.* -> 2\.2(\.\d+)?(\.\d+)?(-\d+)?
+      String regexPattern = upgradePack.getTarget();
+      regexPattern = regexPattern.replaceAll("\\.", "\\\\."); // . -> \.
+      regexPattern = regexPattern.replaceAll("\\\\\\.\\*", "(\\\\\\.\\\\d+)?"); // \.* ->
(\.\d+)?
+      regexPattern = regexPattern.concat("(-\\d+)?");
+      if (Pattern.matches(regexPattern, repositoryVersion)) {
+        return upgradePackName;
+      }
+    }
+    throw new AmbariException("There were no suitable upgrade packs for stack " + stackName
+ " " + stackVersion);
+  }
+
+  /**
+   * Scans the given stack for upgrade packages which can be applied to update the cluster
to given repository version.
+   * Returns NONE if there were no suitable packages.
+   *
+   * @param stackName stack name
+   * @param stackVersion stack version
+   * @param repositoryVersion target repository version
+   * @return upgrade pack name or NONE
+   */
+  public String getUpgradePackageNameSafe(String stackName, String stackVersion, String repositoryVersion)
{
+    try {
+      return getUpgradePackageName(stackName, stackVersion, repositoryVersion);
+    } catch (AmbariException ex) {
+      return "NONE";
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/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 4b915aa..3714060 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -72,7 +72,7 @@ CREATE TABLE clusterstate (
   PRIMARY KEY (cluster_id));
 
 CREATE TABLE cluster_version (
-  id BIGINT NUMBER(19) NULL,
+  id NUMBER(19) NULL,
   repo_version_id NUMBER(19) NOT NULL,
   cluster_id NUMBER(19) NOT NULL,
   state VARCHAR2(32) NOT NULL,
@@ -694,9 +694,9 @@ CREATE INDEX idx_alert_notice_state on alert_notice(notify_state);
 
 -- upgrade tables
 CREATE TABLE upgrade (
-  upgrade_id BIGINT NOT NULL,
-  cluster_id BIGINT NOT NULL,
-  request_id BIGINT NOT NULL,
+  upgrade_id NUMBER(19) NOT NULL,
+  cluster_id NUMBER(19) NOT NULL,
+  request_id NUMBER(19) NOT NULL,
   state VARCHAR2(255) DEFAULT 'NONE' NOT NULL,
   PRIMARY KEY (upgrade_id),
   FOREIGN KEY (cluster_id) REFERENCES clusters(cluster_id),
@@ -704,8 +704,8 @@ CREATE TABLE upgrade (
 );
 
 CREATE TABLE upgrade_group (
-  upgrade_group_id BIGINT NOT NULL,
-  upgrade_id BIGINT NOT NULL,
+  upgrade_group_id NUMBER(19) NOT NULL,
+  upgrade_id NUMBER(19) NOT NULL,
   group_name VARCHAR2(255) DEFAULT '' NOT NULL,
   group_title VARCHAR2(1024) DEFAULT '' NOT NULL,
   PRIMARY KEY (upgrade_group_id),
@@ -713,9 +713,9 @@ CREATE TABLE upgrade_group (
 );
 
 CREATE TABLE upgrade_item (
-  upgrade_item_id BIGINT NOT NULL,
-  upgrade_group_id BIGINT NOT NULL,
-  stage_id BIGINT NOT NULL,
+  upgrade_item_id NUMBER(19) NOT NULL,
+  upgrade_group_id NUMBER(19) NOT NULL,
+  stage_id NUMBER(19) NOT NULL,
   state VARCHAR2(255) DEFAULT 'NONE' NOT NULL,
   hosts CLOB,
   tasks CLOB,

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
index 76ba75b..17fa2c2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
@@ -18,12 +18,10 @@
 
 package org.apache.ambari.server.controller.internal;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -42,7 +40,6 @@ import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.OperatingSystemInfo;
-import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.junit.After;
@@ -313,19 +310,6 @@ public class RepositoryVersionResourceProviderTest {
     Assert.assertEquals("name2", provider.getResources(getRequest, new AndPredicate(predicateStackName,
predicateStackVersion)).iterator().next().getPropertyValue(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID));
   }
 
-  @Test
-  public void testSerializeOperatingSystems() throws Exception {
-    final List<RepositoryInfo> repositories = new ArrayList<RepositoryInfo>();
-    final RepositoryInfo repository = new RepositoryInfo();
-    repository.setBaseUrl("baseurl");
-    repository.setOsType("os");
-    repository.setRepoId("repoId");
-    repositories.add(repository);
-
-    final String serialized = RepositoryVersionResourceProvider.serializeOperatingSystems(repositories);
-    Assert.assertEquals("[{\"repositories\":[{\"Repositories/base_url\":\"baseurl\",\"Repositories/repo_id\":\"repoId\"}],\"OperatingSystems/os_type\":\"os\"}]",
serialized);
-  }
-
   @After
   public void after() {
     injector.getInstance(PersistService.class).stop();

http://git-wip-us.apache.org/repos/asf/ambari/blob/666aec8a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
new file mode 100644
index 0000000..8d82d09
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
@@ -0,0 +1,65 @@
+/**
+ * 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.state.stack.upgrade;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.state.RepositoryInfo;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.gson.Gson;
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+/**
+ * Tests the {@link RepositoryVersionHelper} class
+ */
+public class RepositoryVersionHelperTest {
+
+  private RepositoryVersionHelper helper;
+
+  @Before
+  public void before() throws Exception {
+    final Injector injector = Guice.createInjector(new AbstractModule() {
+
+      @Override
+      protected void configure() {
+        bind(Gson.class).toInstance(new Gson());
+      }
+    });
+    helper = injector.getInstance(RepositoryVersionHelper.class);
+  }
+
+  @Test
+  public void testSerializeOperatingSystems() throws Exception {
+    final List<RepositoryInfo> repositories = new ArrayList<RepositoryInfo>();
+    final RepositoryInfo repository = new RepositoryInfo();
+    repository.setBaseUrl("baseurl");
+    repository.setOsType("os");
+    repository.setRepoId("repoId");
+    repositories.add(repository);
+
+    final String serialized = helper.serializeOperatingSystems(repositories);
+    Assert.assertEquals("[{\"repositories\":[{\"Repositories/base_url\":\"baseurl\",\"Repositories/repo_id\":\"repoId\"}],\"OperatingSystems/os_type\":\"os\"}]",
serialized);
+  }
+}


Mime
View raw message