Selaa lähdekoodia

AMBARI-18255. Add unique constraint to host_version table (ncole)

Nate Cole 9 vuotta sitten
vanhempi
commit
4e9ed5c026

+ 4 - 0
ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java

@@ -203,6 +203,10 @@ public class HostVersionOutOfSyncListener {
           RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion();
           HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity,
                   repositoryVersion, RepositoryVersionState.OUT_OF_SYNC);
+
+          LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+              missingHostVersion.getHostName(), missingHostVersion.getState(),
+              missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId());
           hostVersionDAO.get().create(missingHostVersion);
           cluster.recalculateClusterVersionState(repositoryVersion);
         }

+ 2 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java

@@ -31,10 +31,11 @@ import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
+import javax.persistence.UniqueConstraint;
 
 import org.apache.ambari.server.state.RepositoryVersionState;
 
-@Table(name = "host_version")
+@Table(name = "host_version", uniqueConstraints = @UniqueConstraint(name = "UQ_host_repo", columnNames = { "repo_version_id", "host_id" }))
 @Entity
 @TableGenerator(name = "host_version_id_generator",
     table = "ambari_sequences", pkColumnName = "sequence_name", valueColumnName = "sequence_value"

+ 13 - 0
ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java

@@ -1480,6 +1480,10 @@ public class ClusterImpl implements Cluster {
         HostVersionEntity hostVersionEntity = new HostVersionEntity(hostEntity,
             sourceClusterVersion.getRepositoryVersion(), repositoryVersionState);
 
+        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
+            hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId());
+
         hostVersionDAO.create(hostVersionEntity);
       } else {
         // Update existing host stack version
@@ -1731,6 +1735,11 @@ public class ClusterImpl implements Cluster {
           performingInitialBootstrap = true;
         }
         hostVersionEntity = new HostVersionEntity(host, repositoryVersion, RepositoryVersionState.INSTALLING);
+
+        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
+            hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId());
+
         hostVersionDAO.create(hostVersionEntity);
       }
 
@@ -1954,6 +1963,10 @@ public class ClusterImpl implements Cluster {
               HostVersionEntity hve = new HostVersionEntity(hostEntity,
                 existingClusterVersion.getRepositoryVersion(), state);
 
+              LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+                  hve.getHostName(), hve.getState(),
+                  hve.getRepositoryVersion().getVersion(), hve.getRepositoryVersion().getId());
+
               hostVersionDAO.create(hve);
             }
           }

+ 1 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java

@@ -189,6 +189,7 @@ public class SchemaUpgradeHelper {
       catalogBinder.addBinding().to(UpgradeCatalog222.class);
       catalogBinder.addBinding().to(UpgradeCatalog230.class);
       catalogBinder.addBinding().to(UpgradeCatalog240.class);
+      catalogBinder.addBinding().to(UpgradeCatalog250.class);
       catalogBinder.addBinding().to(FinalUpgradeCatalog.class);
 
       EventBusSynchronizer.synchronizeAmbariEventPublisher(binder());

+ 111 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java

@@ -0,0 +1,111 @@
+/*
+ * 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 java.sql.SQLException;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.orm.dao.DaoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+/**
+ * Upgrade catalog for version 2.5.0.
+ */
+public class UpgradeCatalog250 extends AbstractUpgradeCatalog {
+
+  protected static final String HOST_VERSION_TABLE = "host_version";
+
+  /**
+   * Logger.
+   */
+  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog250.class);
+
+  @Inject
+  DaoUtils daoUtils;
+
+  // ----- Constructors ------------------------------------------------------
+
+  /**
+   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
+   *
+   * @param injector Guice injector to track dependencies and uses bindings to inject them.
+   */
+  @Inject
+  public UpgradeCatalog250(Injector injector) {
+    super(injector);
+
+    daoUtils = injector.getInstance(DaoUtils.class);
+  }
+
+  // ----- UpgradeCatalog ----------------------------------------------------
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String getTargetVersion() {
+    return "2.5.0";
+  }
+
+  // ----- AbstractUpgradeCatalog --------------------------------------------
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String getSourceVersion() {
+    return "2.4.0";
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected void executeDDLUpdates() throws AmbariException, SQLException {
+    updateHostVersionTable();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected void executePreDMLUpdates() throws AmbariException, SQLException {
+
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected void executeDMLUpdates() throws AmbariException, SQLException {
+  }
+
+  protected void updateHostVersionTable() throws SQLException {
+    LOG.info("Updating the {} table", HOST_VERSION_TABLE);
+
+    // Add the unique constraint to the host_version table
+    dbAccessor.addUniqueConstraint(HOST_VERSION_TABLE, "UQ_host_repo", "repo_version_id", "host_id");
+  }
+
+
+}
+

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql

@@ -248,7 +248,8 @@ CREATE TABLE host_version (
   state VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE servicedesiredstate (
   cluster_id BIGINT NOT NULL,

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql

@@ -248,7 +248,8 @@ CREATE TABLE host_version (
   state VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE servicedesiredstate (
   cluster_id BIGINT NOT NULL,

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql

@@ -239,7 +239,8 @@ CREATE TABLE host_version (
   state VARCHAR2(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE servicedesiredstate (
   cluster_id NUMBER(19) NOT NULL,

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql

@@ -248,7 +248,8 @@ CREATE TABLE host_version (
   state VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE servicedesiredstate (
   cluster_id BIGINT NOT NULL,

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql

@@ -296,7 +296,8 @@ CREATE TABLE ambari.host_version (
   state VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id)
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)
 );
 GRANT ALL PRIVILEGES ON TABLE ambari.host_version TO :username;
 

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql

@@ -237,7 +237,8 @@ CREATE TABLE host_version (
   state VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE servicedesiredstate (
   cluster_id NUMERIC(19) NOT NULL,

+ 2 - 1
ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql

@@ -694,7 +694,8 @@ CREATE TABLE host_version (
   STATE VARCHAR(32) NOT NULL,
   CONSTRAINT PK_host_version PRIMARY KEY CLUSTERED (id),
   CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id),
-  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id));
+  CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id),
+  CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id));
 
 CREATE TABLE artifact (
   artifact_name VARCHAR(255) NOT NULL,

+ 28 - 4
ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java

@@ -26,7 +26,14 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
-import org.apache.ambari.server.orm.entities.*;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ResourceEntity;
+import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.security.authorization.ResourceType;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.StackId;
@@ -44,7 +51,7 @@ import com.google.inject.persist.PersistService;
  * {@link org.apache.ambari.server.orm.dao.HostVersionDAO} unit tests.
  */
 public class HostVersionDAOTest {
-  
+
   private static Injector injector;
   private ResourceTypeDAO resourceTypeDAO;
   private ClusterDAO clusterDAO;
@@ -133,7 +140,7 @@ public class HostVersionDAOTest {
     hostEntities.add(host1);
     hostEntities.add(host2);
     hostEntities.add(host3);
-    
+
     // Both sides of relation should be set when modifying in runtime
     host1.setClusterEntities(Arrays.asList(clusterEntity));
     host2.setClusterEntities(Arrays.asList(clusterEntity));
@@ -145,7 +152,7 @@ public class HostVersionDAOTest {
 
     clusterEntity.setHostEntities(hostEntities);
     clusterDAO.merge(clusterEntity);
-    
+
     // Create the Host Versions
     HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.CURRENT);
     HostVersionEntity hostVersionEntity2 = new HostVersionEntity(host2, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.INSTALLED);
@@ -334,6 +341,23 @@ public class HostVersionDAOTest {
     Assert.assertEquals(hostVersionEntity3LastExpected, new HostVersionEntity(hostVersionEntity3LastActual));
   }
 
+  @Test
+  public void testDuplicates() throws Exception {
+    HostEntity host1 = hostDAO.findByName("test_host1");
+
+    RepositoryVersionEntity repoVersion = helper.getOrCreateRepositoryVersion(HDP_22_STACK, repoVersion_2200);
+
+    HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, repoVersion, RepositoryVersionState.CURRENT);
+
+    try {
+      hostVersionDAO.create(hostVersionEntity1);
+      Assert.fail("Each host can have a relationship to a repo version, but cannot have more than one for the same repo");
+    } catch (Exception e) {
+      // expected
+    }
+
+  }
+
   @After
   public void after() {
     injector.getInstance(PersistService.class).stop();

+ 89 - 0
ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java

@@ -0,0 +1,89 @@
+/*
+ * 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 static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.createStrictMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+
+import javax.persistence.EntityManager;
+
+import org.apache.ambari.server.orm.DBAccessor;
+import org.apache.ambari.server.state.stack.OsFamily;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.inject.Binder;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import com.google.inject.Provider;
+
+/**
+ * {@link UpgradeCatalog250} unit tests.
+ */
+public class UpgradeCatalog250Test {
+
+//  private Injector injector;
+  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
+  private EntityManager entityManager = createNiceMock(EntityManager.class);
+
+  @Before
+  public void init() {
+    reset(entityManagerProvider);
+    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
+    replay(entityManagerProvider);
+
+  }
+
+  @After
+  public void tearDown() {
+  }
+
+  @Test
+  public void testExecuteDDLUpdates() throws Exception {
+    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+
+    dbAccessor.addUniqueConstraint("host_version", "UQ_host_repo", "repo_version_id", "host_id");
+    expectLastCall().once();
+
+    replay(dbAccessor);
+
+    Module module = new Module() {
+      @Override
+      public void configure(Binder binder) {
+        binder.bind(DBAccessor.class).toInstance(dbAccessor);
+        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+        binder.bind(EntityManager.class).toInstance(entityManager);
+      }
+    };
+
+    Injector injector = Guice.createInjector(module);
+    UpgradeCatalog250 upgradeCatalog250 = injector.getInstance(UpgradeCatalog250.class);
+    upgradeCatalog250.executeDDLUpdates();
+
+    verify(dbAccessor);
+  }
+
+}