http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java index 0373aac..6249d2a 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java @@ -63,6 +63,7 @@ public class UpgradeCatalog210 extends AbstractUpgradeCatalog { private static final String HOST_CONFIG_MAPPING_TABLE = "hostconfigmapping"; private static final String CONFIG_GROUP_HOST_MAPPING_TABLE = "configgrouphostmapping"; private static final String KERBEROS_PRINCIPAL_HOST_TABLE = "kerberos_principal_host"; + private static final String SERVICE_CONFIG_HOSTS_TABLE = "serviceconfighosts"; private static final String CLUSTER_HOST_MAPPING_TABLE = "ClusterHostMapping"; private static final String WIDGET_TABLE = "widget"; private static final String WIDGET_LAYOUT_TABLE = "widget_layout"; @@ -254,10 +255,6 @@ public class UpgradeCatalog210 extends AbstractUpgradeCatalog { // TODO, for now, these still point to the host_name and will be fixed one table at a time to point to the host id. // Re-add the FKs - dbAccessor.addFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_host_name", - "host_name", HOSTS_TABLE, "host_name", false); - dbAccessor.addFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_hname", - "host_name", HOSTS_TABLE, "host_name", false); dbAccessor.addFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_host_name", "host_name", HOSTS_TABLE, "host_name", false); @@ -265,22 +262,31 @@ public class UpgradeCatalog210 extends AbstractUpgradeCatalog { // Add host_id to the host-related tables, and populate the host_id, one table at a time. // TODO, include other tables. String[] tablesToAddHostID = new String[] { + CONFIG_GROUP_HOST_MAPPING_TABLE, CLUSTER_HOST_MAPPING_TABLE, + HOST_CONFIG_MAPPING_TABLE, HOST_COMPONENT_STATE_TABLE, HOST_COMPONENT_DESIRED_STATE_TABLE, HOST_ROLE_COMMAND_TABLE, HOST_STATE_TABLE, - HOST_VERSION_TABLE + HOST_VERSION_TABLE, + SERVICE_CONFIG_HOSTS_TABLE }; for (String tableName : tablesToAddHostID) { dbAccessor.addColumn(tableName, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, true)); - dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = t.host_name) WHERE t.host_id IS NULL AND t.host_name IS NOT NULL"); - // For legacy reasons, the hostrolecommand table will contain "none" for some records where the host_name was not important. - // These records were populated during Finalize in Rolling Upgrade, so they must be updated to use a valid host_name. - if (tableName == HOST_ROLE_COMMAND_TABLE && StringUtils.isNotBlank(randomHostName)) { - dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = '" + randomHostName + "') WHERE t.host_id IS NULL AND t.host_name = 'none'"); + // The column name is different for one table + String hostNameColumnName = tableName == SERVICE_CONFIG_HOSTS_TABLE ? "hostname" : "host_name"; + + if (dbAccessor.tableHasData(tableName)) { + dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = t." + hostNameColumnName + ") WHERE t.host_id IS NULL AND t." + hostNameColumnName + " IS NOT NULL"); + + // For legacy reasons, the hostrolecommand table will contain "none" for some records where the host_name was not important. + // These records were populated during Finalize in Rolling Upgrade, so they must be updated to use a valid host_name. + if (tableName == HOST_ROLE_COMMAND_TABLE && StringUtils.isNotBlank(randomHostName)) { + dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = '" + randomHostName + "') WHERE t.host_id IS NULL AND t.host_name = 'none'"); + } } if (databaseType == Configuration.DatabaseType.DERBY) { @@ -293,24 +299,33 @@ public class UpgradeCatalog210 extends AbstractUpgradeCatalog { // These are the FKs that have already been corrected. // TODO, include other tables. + dbAccessor.addFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_host_id", + "host_id", HOSTS_TABLE, "host_id", false); dbAccessor.addFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_clusterhostmapping_host_id", "host_id", HOSTS_TABLE, "host_id", false); + dbAccessor.addFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_host_id", + "host_id", HOSTS_TABLE, "host_id", false); dbAccessor.addFKConstraint(HOST_COMPONENT_STATE_TABLE, "FK_hostcomponentstate_host_id", "host_id", HOSTS_TABLE, "host_id", false); dbAccessor.addFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "FK_hcdesiredstate_host_id", "host_id", HOSTS_TABLE, "host_id", false); dbAccessor.addFKConstraint(HOST_STATE_TABLE, "FK_hoststate_host_id", "host_id", HOSTS_TABLE, "host_id", false); + dbAccessor.addFKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "FK_scvhosts_host_id", + "host_id", HOSTS_TABLE, "host_id", false); // For any tables where the host_name was part of the PK, need to drop the PK, and recreate it with the host_id // TODO, include other tables. String[] tablesWithHostNameInPK = new String[] { + CONFIG_GROUP_HOST_MAPPING_TABLE, CLUSTER_HOST_MAPPING_TABLE, + HOST_CONFIG_MAPPING_TABLE, HOST_COMPONENT_STATE_TABLE, HOST_COMPONENT_DESIRED_STATE_TABLE, - HOST_STATE_TABLE + HOST_STATE_TABLE, + SERVICE_CONFIG_HOSTS_TABLE }; if (databaseType == Configuration.DatabaseType.DERBY) { @@ -321,29 +336,43 @@ public class UpgradeCatalog210 extends AbstractUpgradeCatalog { } } } else { + dbAccessor.executeQuery("ALTER TABLE " + CONFIG_GROUP_HOST_MAPPING_TABLE + " DROP CONSTRAINT configgrouphostmapping_pkey"); dbAccessor.executeQuery("ALTER TABLE " + CLUSTER_HOST_MAPPING_TABLE + " DROP CONSTRAINT clusterhostmapping_pkey"); + dbAccessor.executeQuery("ALTER TABLE " + HOST_CONFIG_MAPPING_TABLE + " DROP CONSTRAINT hostconfigmapping_pkey"); dbAccessor.executeQuery("ALTER TABLE " + HOST_COMPONENT_STATE_TABLE + " DROP CONSTRAINT hostcomponentstate_pkey"); dbAccessor.executeQuery("ALTER TABLE " + HOST_COMPONENT_DESIRED_STATE_TABLE + " DROP CONSTRAINT hostcomponentdesiredstate_pkey"); dbAccessor.executeQuery("ALTER TABLE " + HOST_STATE_TABLE + " DROP CONSTRAINT hoststate_pkey"); + dbAccessor.executeQuery("ALTER TABLE " + SERVICE_CONFIG_HOSTS_TABLE + " DROP CONSTRAINT serviceconfighosts_pkey"); // TODO, include other tables. } + dbAccessor.executeQuery("ALTER TABLE " + CONFIG_GROUP_HOST_MAPPING_TABLE + + " ADD CONSTRAINT configgrouphostmapping_pkey PRIMARY KEY (config_group_id, host_id)"); dbAccessor.executeQuery("ALTER TABLE " + CLUSTER_HOST_MAPPING_TABLE + " ADD CONSTRAINT clusterhostmapping_pkey PRIMARY KEY (cluster_id, host_id)"); + dbAccessor.executeQuery("ALTER TABLE " + HOST_CONFIG_MAPPING_TABLE + + " ADD CONSTRAINT hostconfigmapping_pkey PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp)"); dbAccessor.executeQuery("ALTER TABLE " + HOST_COMPONENT_STATE_TABLE + " ADD CONSTRAINT hostcomponentstate_pkey PRIMARY KEY (cluster_id, component_name, host_id, service_name)"); dbAccessor.executeQuery("ALTER TABLE " + HOST_COMPONENT_DESIRED_STATE_TABLE + " ADD CONSTRAINT hostcomponentdesiredstate_pkey PRIMARY KEY (cluster_id, component_name, host_id, service_name)"); dbAccessor.executeQuery("ALTER TABLE " + HOST_STATE_TABLE + " ADD CONSTRAINT hoststate_pkey PRIMARY KEY (host_id)"); + dbAccessor.executeQuery("ALTER TABLE " + SERVICE_CONFIG_HOSTS_TABLE + + " ADD CONSTRAINT serviceconfighosts_pkey PRIMARY KEY (service_config_id, host_id)"); // TODO, include other tables. // Finish by deleting the unnecessary host_name columns. + dbAccessor.dropColumn(CONFIG_GROUP_HOST_MAPPING_TABLE, "host_name"); dbAccessor.dropColumn(CLUSTER_HOST_MAPPING_TABLE, "host_name"); + dbAccessor.dropColumn(HOST_CONFIG_MAPPING_TABLE, "host_name"); dbAccessor.dropColumn(HOST_COMPONENT_STATE_TABLE, "host_name"); dbAccessor.dropColumn(HOST_COMPONENT_DESIRED_STATE_TABLE, "host_name"); dbAccessor.dropColumn(HOST_ROLE_COMMAND_TABLE, "host_name"); dbAccessor.dropColumn(HOST_STATE_TABLE, "host_name"); dbAccessor.dropColumn(HOST_VERSION_TABLE, "host_name"); + + // Notice that the column name doesn't have an underscore here. + dbAccessor.dropColumn(SERVICE_CONFIG_HOSTS_TABLE, "hostname"); // TODO, include other tables. // view columns for cluster association
http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql index b6f2aaa..1a146e0 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql @@ -78,10 +78,8 @@ CREATE TABLE serviceconfig ( CREATE TABLE serviceconfighosts ( service_config_id BIGINT NOT NULL, - hostname VARCHAR(255) NOT NULL, - -- host_id BIGINT NOT NULL, - PRIMARY KEY(service_config_id, hostname)); - -- PRIMARY KEY(service_config_id, host_id)); + host_id BIGINT NOT NULL, + PRIMARY KEY(service_config_id, host_id)); CREATE TABLE serviceconfigmapping ( service_config_id BIGINT NOT NULL, @@ -315,16 +313,14 @@ CREATE TABLE clusterconfigmapping ( CREATE TABLE hostconfigmapping ( create_timestamp BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - -- host_id BIGINT NOT NULL, + host_id BIGINT NOT NULL, cluster_id BIGINT NOT NULL, type_name VARCHAR(255) NOT NULL, selected INTEGER NOT NULL DEFAULT 0, service_name VARCHAR(255), version_tag VARCHAR(255) NOT NULL, user_name VARCHAR(255) NOT NULL DEFAULT '_db', - PRIMARY KEY (create_timestamp, host_name, cluster_id, type_name)); - -- PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name)); + PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name)); CREATE TABLE metainfo ( `metainfo_key` VARCHAR(255), @@ -362,10 +358,8 @@ CREATE TABLE configgroup ( CREATE TABLE configgrouphostmapping ( config_group_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - -- host_id BIGINT NOT NULL, - PRIMARY KEY(config_group_id, host_name)); - -- PRIMARY KEY(config_group_id, host_id)); + host_id BIGINT NOT NULL, + PRIMARY KEY(config_group_id, host_id)); CREATE TABLE requestschedule ( schedule_id bigint, @@ -633,17 +627,16 @@ ALTER TABLE request ADD CONSTRAINT FK_request_schedule_id FOREIGN KEY (request_s ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clhostmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clusterhostmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); -ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name); --- ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id); -ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id); ALTER TABLE configgroup ADD CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_confg FOREIGN KEY (cluster_id, config_type, version_tag) REFERENCES clusterconfig (cluster_id, type_name, version_tag); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_cgccm_gid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_cgid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); -ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_hname FOREIGN KEY (host_name) REFERENCES hosts (host_name); --- ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE requestschedulebatchrequest ADD CONSTRAINT FK_rsbatchrequest_schedule_id FOREIGN KEY (schedule_id) REFERENCES requestschedule (schedule_id); ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name); ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup(blueprint_name, name); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 25685e5..bc6bd32 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql @@ -69,8 +69,8 @@ CREATE TABLE serviceconfig ( CREATE TABLE serviceconfighosts ( service_config_id NUMBER(19) NOT NULL, - hostname VARCHAR(255) NOT NULL, - PRIMARY KEY(service_config_id, hostname)); + host_id NUMBER(19) NOT NULL, + PRIMARY KEY(service_config_id, host_id)); CREATE TABLE serviceconfigmapping ( service_config_id NUMBER(19) NOT NULL, @@ -305,16 +305,14 @@ CREATE TABLE clusterconfigmapping ( CREATE TABLE hostconfigmapping ( create_timestamp NUMBER(19) NOT NULL, - host_name VARCHAR2(255) NOT NULL, - --host_id NUMBER(19) NOT NULL, + host_id NUMBER(19) NOT NULL, cluster_id NUMBER(19) NOT NULL, type_name VARCHAR2(255) NOT NULL, selected NUMBER(10) NOT NULL, service_name VARCHAR2(255) NULL, version_tag VARCHAR2(255) NOT NULL, user_name VARCHAR(255) DEFAULT '_db', - PRIMARY KEY (create_timestamp, host_name, cluster_id, type_name)); - --PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name)); + PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name)); CREATE TABLE metainfo ( "metainfo_key" VARCHAR2(255) NOT NULL, @@ -352,10 +350,8 @@ CREATE TABLE confgroupclusterconfigmapping ( CREATE TABLE configgrouphostmapping ( config_group_id NUMBER(19) NOT NULL, - host_name VARCHAR2(255) NOT NULL, - --host_id NUMBER(19) NOT NULL, - PRIMARY KEY(config_group_id, host_name)); - --PRIMARY KEY(config_group_id, host_id)); + host_id NUMBER(19) NOT NULL, + PRIMARY KEY(config_group_id, host_id)); CREATE TABLE requestschedule ( schedule_id NUMBER(19), @@ -599,7 +595,8 @@ ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_stack_version UNIQUE (st ALTER TABLE members ADD CONSTRAINT FK_members_group_id FOREIGN KEY (group_id) REFERENCES groups (group_id); ALTER TABLE members ADD CONSTRAINT FK_members_user_id FOREIGN KEY (user_id) REFERENCES users (user_id); ALTER TABLE clusterconfig ADD CONSTRAINT FK_clusterconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); -ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id); ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); @@ -623,16 +620,14 @@ ALTER TABLE request ADD CONSTRAINT FK_request_schedule_id FOREIGN KEY (request_s ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clhostmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clusterhostmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); -ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id); ALTER TABLE configgroup ADD CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_confg FOREIGN KEY (version_tag, config_type, cluster_id) REFERENCES clusterconfig (version_tag, type_name, cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_cgccm_gid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_cgid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); -ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_hname FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE requestschedulebatchrequest ADD CONSTRAINT FK_rsbatchrequest_schedule_id FOREIGN KEY (schedule_id) REFERENCES requestschedule (schedule_id); ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name); ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup(blueprint_name, name); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql index 9ade56f..f2a016f 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql @@ -78,8 +78,8 @@ CREATE TABLE serviceconfig ( CREATE TABLE serviceconfighosts ( service_config_id BIGINT NOT NULL, - hostname VARCHAR(255) NOT NULL, - PRIMARY KEY(service_config_id, hostname)); + host_id BIGINT NOT NULL, + PRIMARY KEY(service_config_id, host_id)); CREATE TABLE serviceconfigmapping ( service_config_id BIGINT NOT NULL, @@ -313,16 +313,14 @@ CREATE TABLE key_value_store ( CREATE TABLE hostconfigmapping ( cluster_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - --host_id BIGINT NOT NULL, + host_id BIGINT NOT NULL, type_name VARCHAR(255) NOT NULL, version_tag VARCHAR(255) NOT NULL, service_name VARCHAR(255), create_timestamp BIGINT NOT NULL, selected INTEGER NOT NULL DEFAULT 0, user_name VARCHAR(255) NOT NULL DEFAULT '_db', - PRIMARY KEY (cluster_id, host_name, type_name, create_timestamp)); - --PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp)); + PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp)); CREATE TABLE metainfo ( "metainfo_key" VARCHAR(255), @@ -354,10 +352,8 @@ CREATE TABLE confgroupclusterconfigmapping ( CREATE TABLE configgrouphostmapping ( config_group_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - --host_id BIGINT NOT NULL, - PRIMARY KEY(config_group_id, host_name)); - --PRIMARY KEY(config_group_id, host_id)); + host_id BIGINT NOT NULL, + PRIMARY KEY(config_group_id, host_id)); CREATE TABLE requestschedule ( schedule_id bigint, @@ -624,14 +620,12 @@ ALTER TABLE request ADD CONSTRAINT FK_request_schedule_id FOREIGN KEY (request_s ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clhostmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clusterhostmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); -ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE configgroup ADD CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_confg FOREIGN KEY (version_tag, config_type, cluster_id) REFERENCES clusterconfig (version_tag, type_name, cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_cgccm_gid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_cgid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); -ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_hname FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE requestschedulebatchrequest ADD CONSTRAINT FK_rsbatchrequest_schedule_id FOREIGN KEY (schedule_id) REFERENCES requestschedule (schedule_id); ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name); ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup (blueprint_name, name); @@ -657,7 +651,8 @@ ALTER TABLE users ADD CONSTRAINT FK_users_principal_id FOREIGN KEY (principal_id ALTER TABLE groups ADD CONSTRAINT FK_groups_principal_id FOREIGN KEY (principal_id) REFERENCES adminprincipal(principal_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id); -ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id); ALTER TABLE clusters ADD CONSTRAINT FK_clusters_resource_id FOREIGN KEY (resource_id) REFERENCES adminresource(resource_id); ALTER TABLE widget_layout_user_widget ADD CONSTRAINT FK_widget_layout_id FOREIGN KEY (widget_layout_id) REFERENCES widget_layout(id); ALTER TABLE widget_layout_user_widget ADD CONSTRAINT FK_widget_id FOREIGN KEY (widget_id) REFERENCES widget(id); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql index feaeae9..38a241d 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql @@ -94,8 +94,8 @@ GRANT ALL PRIVILEGES ON TABLE ambari.serviceconfig TO :username; CREATE TABLE ambari.serviceconfighosts ( service_config_id BIGINT NOT NULL, - hostname VARCHAR(255) NOT NULL, - PRIMARY KEY(service_config_id, hostname)); + host_id BIGINT NOT NULL, + PRIMARY KEY(service_config_id, host_id)); GRANT ALL PRIVILEGES ON TABLE ambari.serviceconfighosts TO :username; CREATE TABLE ambari.serviceconfigmapping ( @@ -353,16 +353,14 @@ GRANT ALL PRIVILEGES ON TABLE ambari.key_value_store TO :username; CREATE TABLE ambari.hostconfigmapping ( cluster_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - --host_id BIGINT NOT NULL, + host_id BIGINT NOT NULL, type_name VARCHAR(255) NOT NULL, version_tag VARCHAR(255) NOT NULL, service_name VARCHAR(255), create_timestamp BIGINT NOT NULL, selected INTEGER NOT NULL DEFAULT 0, user_name VARCHAR(255) NOT NULL DEFAULT '_db', - PRIMARY KEY (cluster_id, host_name, type_name, create_timestamp)); - --PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp)); + PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp)); GRANT ALL PRIVILEGES ON TABLE ambari.hostconfigmapping TO :username; CREATE TABLE ambari.metainfo ( @@ -399,10 +397,8 @@ GRANT ALL PRIVILEGES ON TABLE ambari.confgroupclusterconfigmapping TO :username; CREATE TABLE ambari.configgrouphostmapping ( config_group_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, - --host_id BIGINT NOT NULL, - PRIMARY KEY(config_group_id, host_name)); - --PRIMARY KEY(config_group_id, host_id)); + host_id BIGINT NOT NULL, + PRIMARY KEY(config_group_id, host_id)); GRANT ALL PRIVILEGES ON TABLE ambari.configgrouphostmapping TO :username; CREATE TABLE ambari.requestschedule ( @@ -697,12 +693,10 @@ ALTER TABLE ambari.request ADD CONSTRAINT FK_request_schedule_id FOREIGN KEY (re ALTER TABLE ambari.ClusterHostMapping ADD CONSTRAINT FK_clhostmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id); ALTER TABLE ambari.ClusterHostMapping ADD CONSTRAINT FK_clusterhostmapping_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (host_id); ALTER TABLE ambari.hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id); -ALTER TABLE ambari.hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_name FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name); ---ALTER TABLE ambari.hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (host_id); +ALTER TABLE ambari.hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (host_id); ALTER TABLE ambari.configgroup ADD CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id); ALTER TABLE ambari.configgrouphostmapping ADD CONSTRAINT FK_cghm_cgid FOREIGN KEY (config_group_id) REFERENCES ambari.configgroup (group_id); -ALTER TABLE ambari.configgrouphostmapping ADD CONSTRAINT FK_cghm_hname FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name); ---ALTER TABLE ambari.configgrouphostmapping ADD CONSTRAINT FK_cghm_hid FOREIGN KEY (host_id) REFERENCES ambari.hosts (id); +ALTER TABLE ambari.configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (id); ALTER TABLE ambari.requestschedulebatchrequest ADD CONSTRAINT FK_rsbatchrequest_schedule_id FOREIGN KEY (schedule_id) REFERENCES ambari.requestschedule (schedule_id); ALTER TABLE ambari.hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES ambari.blueprint(blueprint_name); ALTER TABLE ambari.hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES ambari.hostgroup (blueprint_name, name); @@ -720,7 +714,8 @@ ALTER TABLE ambari.confgroupclusterconfigmapping ADD CONSTRAINT FK_confg FOREIGN ALTER TABLE ambari.confgroupclusterconfigmapping ADD CONSTRAINT FK_cgccm_gid FOREIGN KEY (config_group_id) REFERENCES ambari.configgroup (group_id); ALTER TABLE ambari.serviceconfigmapping ADD CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES ambari.serviceconfig(service_config_id); ALTER TABLE ambari.serviceconfigmapping ADD CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES ambari.clusterconfig(config_id); -ALTER TABLE ambari.serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES ambari.serviceconfig(service_config_id); +ALTER TABLE ambari.serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES ambari.serviceconfig(service_config_id); +ALTER TABLE ambari.serviceconfighosts ADD CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts(host_id); ALTER TABLE ambari.adminresource ADD CONSTRAINT FK_resource_resource_type_id FOREIGN KEY (resource_type_id) REFERENCES ambari.adminresourcetype(resource_type_id); ALTER TABLE ambari.adminprincipal ADD CONSTRAINT FK_principal_principal_type_id FOREIGN KEY (principal_type_id) REFERENCES ambari.adminprincipaltype(principal_type_id); ALTER TABLE ambari.adminpermission ADD CONSTRAINT FK_permission_resource_type_id FOREIGN KEY (resource_type_id) REFERENCES ambari.adminresourcetype(resource_type_id); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql index 03f1ec8..30959d0 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql @@ -82,8 +82,8 @@ CREATE TABLE serviceconfig ( CREATE TABLE serviceconfighosts ( service_config_id BIGINT NOT NULL, - hostname VARCHAR(255) NOT NULL, - PRIMARY KEY CLUSTERED (service_config_id, hostname) + host_id BIGINT NOT NULL, + PRIMARY KEY CLUSTERED (service_config_id, host_id) ); CREATE TABLE serviceconfigmapping ( @@ -345,7 +345,7 @@ CREATE TABLE key_value_store ( CREATE TABLE hostconfigmapping ( cluster_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, + host_id BIGINT NOT NULL, type_name VARCHAR(255) NOT NULL, version_tag VARCHAR(255) NOT NULL, service_name VARCHAR(255), @@ -354,7 +354,7 @@ CREATE TABLE hostconfigmapping ( user_name VARCHAR(255) NOT NULL DEFAULT '_db', PRIMARY KEY CLUSTERED ( cluster_id, - host_name, + host_id, type_name, create_timestamp ) @@ -398,10 +398,10 @@ CREATE TABLE confgroupclusterconfigmapping ( CREATE TABLE configgrouphostmapping ( config_group_id BIGINT NOT NULL, - host_name VARCHAR(255) NOT NULL, + host_id BIGINT NOT NULL, PRIMARY KEY CLUSTERED ( config_group_id, - host_name + host_id ) ); @@ -737,14 +737,12 @@ ALTER TABLE request ADD CONSTRAINT FK_request_schedule_id FOREIGN KEY (request_s ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clhostmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE ClusterHostMapping ADD CONSTRAINT FK_clusterhostmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); -ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE hostconfigmapping ADD CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE configgroup ADD CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_confg FOREIGN KEY (cluster_id, config_type, version_tag) REFERENCES clusterconfig (cluster_id, type_name, version_tag); ALTER TABLE confgroupclusterconfigmapping ADD CONSTRAINT FK_cgccm_gid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_cgid FOREIGN KEY (config_group_id) REFERENCES configgroup (group_id); -ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_hname FOREIGN KEY (host_name) REFERENCES hosts (host_name); ---ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); +ALTER TABLE configgrouphostmapping ADD CONSTRAINT FK_cghm_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id); ALTER TABLE requestschedulebatchrequest ADD CONSTRAINT FK_rsbatchrequest_schedule_id FOREIGN KEY (schedule_id) REFERENCES requestschedule (schedule_id); ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name); ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup (blueprint_name, name); @@ -770,7 +768,8 @@ ALTER TABLE users ADD CONSTRAINT FK_users_principal_id FOREIGN KEY (principal_id ALTER TABLE groups ADD CONSTRAINT FK_groups_principal_id FOREIGN KEY (principal_id) REFERENCES adminprincipal(principal_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); ALTER TABLE serviceconfigmapping ADD CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id); -ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id); +ALTER TABLE serviceconfighosts ADD CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id); ALTER TABLE clusters ADD CONSTRAINT FK_clusters_resource_id FOREIGN KEY (resource_id) REFERENCES adminresource(resource_id); ALTER TABLE widget_layout_user_widget ADD CONSTRAINT FK_widget_layout_id FOREIGN KEY (widget_layout_id) REFERENCES widget_layout(id); ALTER TABLE widget_layout_user_widget ADD CONSTRAINT FK_widget_id FOREIGN KEY (widget_id) REFERENCES widget(id); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java index 06f9e8a..cba560a 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java @@ -96,6 +96,7 @@ import org.apache.ambari.server.orm.dao.HostDAO; import org.apache.ambari.server.orm.dao.WidgetDAO; import org.apache.ambari.server.orm.dao.WidgetLayoutDAO; import org.apache.ambari.server.orm.entities.ExecutionCommandEntity; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.ambari.server.orm.entities.WidgetEntity; import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; @@ -199,6 +200,7 @@ public class AmbariManagementControllerTest { private ConfigGroupFactory configGroupFactory; private OrmTestHelper helper; private StageFactory stageFactory; + private HostDAO hostDAO; @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -226,6 +228,7 @@ public class AmbariManagementControllerTest { configGroupFactory = injector.getInstance(ConfigGroupFactory.class); helper = injector.getInstance(OrmTestHelper.class); stageFactory = injector.getInstance(StageFactory.class); + hostDAO = injector.getInstance(HostDAO.class); } @After @@ -332,12 +335,13 @@ public class AmbariManagementControllerTest { List<String> hosts, List<Config> configs) throws AmbariException { - Map<String, Host> hostMap = new HashMap<String, Host>(); + Map<Long, Host> hostMap = new HashMap<Long, Host>(); Map<String, Config> configMap = new HashMap<String, Config>(); for (String hostname : hosts) { Host host = clusters.getHost(hostname); - hostMap.put(host.getHostName(), host); + HostEntity hostEntity = hostDAO.findByName(hostname); + hostMap.put(hostEntity.getHostId(), host); } for (Config config : configs) { @@ -6701,7 +6705,7 @@ public class AmbariManagementControllerTest { // Associate the right host ConfigGroup configGroup = cluster.getConfigGroups().get(groupId); - configGroup.setHosts(new HashMap<String, Host>() {{ put("h3", + configGroup.setHosts(new HashMap<Long, Host>() {{ put(3L, clusters.getHost("h3")); }}); configGroup.persist(); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProviderTest.java index db324e5..5e0debf 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProviderTest.java @@ -17,6 +17,11 @@ */ package org.apache.ambari.server.controller.internal; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.util.Modules; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.ConfigGroupRequest; import org.apache.ambari.server.controller.ConfigGroupResponse; @@ -29,6 +34,9 @@ import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException; import org.apache.ambari.server.controller.spi.ResourceProvider; import org.apache.ambari.server.controller.utilities.PredicateBuilder; import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.dao.HostDAO; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; @@ -39,13 +47,19 @@ import org.apache.ambari.server.state.configgroup.ConfigGroupFactory; import org.easymock.Capture; import org.easymock.IAnswer; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; + + +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; + import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; @@ -57,19 +71,40 @@ import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; +import static org.easymock.EasyMock.createStrictMock; public class ConfigGroupResourceProviderTest { - ConfigGroupResourceProvider getConfigGroupResourceProvider - (AmbariManagementController managementController) { + private Injector injector; + + private HostDAO hostDAO = null; + @Before + public void setup() throws Exception { + hostDAO = createStrictMock(HostDAO.class); + + // Create injector after all mocks have been initialized + injector = Guice.createInjector(Modules.override( + new InMemoryDefaultTestModule()).with(new MockModule())); + } + + ConfigGroupResourceProvider getConfigGroupResourceProvider + (AmbariManagementController managementController) { Resource.Type type = Resource.Type.ConfigGroup; return (ConfigGroupResourceProvider) AbstractControllerResourceProvider.getResourceProvider( - type, - PropertyHelper.getPropertyIds(type), - PropertyHelper.getKeyPropertyIds(type), - managementController); + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + } + + + private class MockModule implements Module { + @Override + public void configure(Binder binder) { + binder.bind(HostDAO.class).toInstance(hostDAO); + } } @Test @@ -80,6 +115,8 @@ public class ConfigGroupResourceProviderTest { Cluster cluster = createNiceMock(Cluster.class); Host h1 = createNiceMock(Host.class); Host h2 = createNiceMock(Host.class); + HostEntity hostEntity1 = createMock(HostEntity.class); + HostEntity hostEntity2 = createMock(HostEntity.class); ConfigGroupFactory configGroupFactory = createNiceMock(ConfigGroupFactory.class); ConfigGroup configGroup = createNiceMock(ConfigGroup.class); @@ -89,6 +126,10 @@ public class ConfigGroupResourceProviderTest { expect(clusters.getHost("h2")).andReturn(h2); expect(managementController.getConfigGroupFactory()).andReturn(configGroupFactory); expect(managementController.getAuthName()).andReturn("admin").anyTimes(); + expect(hostDAO.findByName("h1")).andReturn(hostEntity1).atLeastOnce(); + expect(hostDAO.findByName("h2")).andReturn(hostEntity2).atLeastOnce(); + expect(hostEntity1.getHostId()).andReturn(1L).atLeastOnce(); + expect(hostEntity2.getHostId()).andReturn(2L).atLeastOnce(); Capture<Cluster> clusterCapture = new Capture<Cluster>(); Capture<String> captureName = new Capture<String>(); @@ -96,14 +137,14 @@ public class ConfigGroupResourceProviderTest { Capture<String> captureTag = new Capture<String>(); Capture<Map<String, Config>> captureConfigs = new Capture<Map<String, Config>>(); - Capture<Map<String, Host>> captureHosts = new Capture<Map<String, Host>>(); + Capture<Map<Long, Host>> captureHosts = new Capture<Map<Long, Host>>(); expect(configGroupFactory.createNew(capture(clusterCapture), capture(captureName), capture(captureTag), capture(captureDesc), capture(captureConfigs), capture(captureHosts))).andReturn(configGroup); replay(managementController, clusters, cluster, configGroupFactory, - configGroup, response); + configGroup, response, hostDAO, hostEntity1, hostEntity2); ResourceProvider provider = getConfigGroupResourceProvider (managementController); @@ -146,12 +187,12 @@ public class ConfigGroupResourceProviderTest { provider.createResources(request); verify(managementController, clusters, cluster, configGroupFactory, - configGroup, response); + configGroup, response, hostDAO, hostEntity1, hostEntity2); assertEquals("version100", captureConfigs.getValue().get("core-site") .getTag()); - assertTrue(captureHosts.getValue().containsKey("h1")); - assertTrue(captureHosts.getValue().containsKey("h2")); + assertTrue(captureHosts.getValue().containsKey(1L)); + assertTrue(captureHosts.getValue().containsKey(2L)); } @Test @@ -222,6 +263,9 @@ public class ConfigGroupResourceProviderTest { Cluster cluster = createNiceMock(Cluster.class); Host h1 = createNiceMock(Host.class); Host h2 = createNiceMock(Host.class); + HostEntity hostEntity1 = createMock(HostEntity.class); + HostEntity hostEntity2 = createMock(HostEntity.class); + final ConfigGroup configGroup = createNiceMock(ConfigGroup.class); ConfigGroupResponse configGroupResponse = createNiceMock (ConfigGroupResponse.class); @@ -231,6 +275,10 @@ public class ConfigGroupResourceProviderTest { expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getHost("h1")).andReturn(h1); expect(clusters.getHost("h2")).andReturn(h2); + expect(hostDAO.findByName("h1")).andReturn(hostEntity1).atLeastOnce(); + expect(hostDAO.findByName("h2")).andReturn(hostEntity2).atLeastOnce(); + expect(hostEntity1.getHostId()).andReturn(1L).atLeastOnce(); + expect(hostEntity2.getHostId()).andReturn(2L).atLeastOnce(); expect(configGroup.getName()).andReturn("test-1").anyTimes(); expect(configGroup.getId()).andReturn(25L).anyTimes(); @@ -253,7 +301,7 @@ public class ConfigGroupResourceProviderTest { expectLastCall().once(); replay(managementController, clusters, cluster, - configGroup, response, configGroupResponse, configHelper); + configGroup, response, configGroupResponse, configHelper, hostDAO, hostEntity1, hostEntity2); ResourceProvider provider = getConfigGroupResourceProvider (managementController); @@ -302,7 +350,7 @@ public class ConfigGroupResourceProviderTest { provider.updateResources(request, predicate); verify(managementController, clusters, cluster, - configGroup, response, configGroupResponse, configHelper); + configGroup, response, configGroupResponse, configHelper, hostDAO, hostEntity1, hostEntity2); } @SuppressWarnings("unchecked") @@ -312,6 +360,14 @@ public class ConfigGroupResourceProviderTest { Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); Host h1 = createNiceMock(Host.class); + final Long host1Id = 1L; + List<Long> hostIds = new ArrayList<Long>() {{ add(host1Id); }}; + List<String> hostNames = new ArrayList<String>() {{ add("h1"); }}; + HostEntity hostEntity1 = createMock(HostEntity.class); + + expect(hostDAO.getHostNamesByHostIds(hostIds)).andReturn(hostNames).atLeastOnce(); + expect(hostDAO.findByName("h1")).andReturn(hostEntity1).anyTimes(); + expect(hostEntity1.getHostId()).andReturn(host1Id).anyTimes(); ConfigGroup configGroup1 = createNiceMock(ConfigGroup.class); ConfigGroup configGroup2 = createNiceMock(ConfigGroup.class); @@ -350,8 +406,8 @@ public class ConfigGroupResourceProviderTest { expect(configGroup3.getTag()).andReturn("t3").anyTimes(); expect(configGroup4.getTag()).andReturn("t4").anyTimes(); - Map<String, Host> hostMap = new HashMap<String, Host>(); - hostMap.put("h1", h1); + Map<Long, Host> hostMap = new HashMap<Long, Host>(); + hostMap.put(host1Id, h1); expect(configGroup4.getHosts()).andReturn(hostMap).anyTimes(); @@ -373,9 +429,8 @@ public class ConfigGroupResourceProviderTest { hostObj.add(hostnames); expect(response4.getHosts()).andReturn(hostObj).anyTimes(); - replay(managementController, clusters, cluster, configGroup1, - configGroup2, configGroup3, configGroup4, response1, response2, - response3, response4); + replay(managementController, clusters, cluster, hostDAO, hostEntity1, + configGroup1, configGroup2, configGroup3, configGroup4, response1, response2, response3, response4); ResourceProvider resourceProvider = getConfigGroupResourceProvider (managementController); @@ -470,7 +525,7 @@ public class ConfigGroupResourceProviderTest { .CONFIGGROUP_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and() .property(ConfigGroupResourceProvider.CONFIGGROUP_TAG_PROPERTY_ID) .equals("t4").and().property(ConfigGroupResourceProvider - .CONFIGGROUP_HOSTS_PROPERTY_ID).equals("h1").toPredicate(); + .CONFIGGROUP_HOSTS_PROPERTY_ID).equals(host1Id).toPredicate(); resources = resourceProvider.getResources(request, predicate); @@ -513,9 +568,8 @@ public class ConfigGroupResourceProviderTest { } Assert.assertNotNull(resourceException); - verify(managementController, clusters, cluster, configGroup1, - configGroup2, configGroup3, configGroup4, response1, response2, - response3, response4); + verify(managementController, clusters, cluster, hostDAO, hostEntity1, + configGroup1, configGroup2, configGroup3, configGroup4, response1, response2, response3, response4); } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java index 2adbf9d..53af8a5 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.Set; +import com.google.inject.assistedinject.AssistedInject; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -38,6 +39,7 @@ import org.apache.ambari.server.orm.entities.HostEntity; 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.state.host.HostFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -55,6 +57,7 @@ public class ConfigGroupDAOTest { private HostDAO hostDAO; private ResourceTypeDAO resourceTypeDAO; private StackDAO stackDAO; + private HostFactory hostFactory; @Before public void setup() throws Exception { @@ -73,6 +76,7 @@ public class ConfigGroupDAOTest { (ConfigGroupHostMappingDAO.class); hostDAO = injector.getInstance(HostDAO.class); resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class); + hostFactory = injector.getInstance(HostFactory.class); } @After @@ -115,16 +119,14 @@ public class ConfigGroupDAOTest { configGroupDAO.create(configGroupEntity); if (hosts != null && !hosts.isEmpty()) { - List<ConfigGroupHostMappingEntity> hostMappingEntities = new - ArrayList<ConfigGroupHostMappingEntity>(); + List<ConfigGroupHostMappingEntity> hostMappingEntities = new ArrayList<ConfigGroupHostMappingEntity>(); for (HostEntity host : hosts) { host.setClusterEntities(Arrays.asList(clusterEntity)); hostDAO.create(host); - ConfigGroupHostMappingEntity hostMappingEntity = new - ConfigGroupHostMappingEntity(); - hostMappingEntity.setHostname(host.getHostName()); + ConfigGroupHostMappingEntity hostMappingEntity = new ConfigGroupHostMappingEntity(); + hostMappingEntity.setHostId(host.getHostId()); hostMappingEntity.setHostEntity(host); hostMappingEntity.setConfigGroupEntity(configGroupEntity); hostMappingEntity.setConfigGroupId(configGroupEntity.getGroupId()); @@ -212,27 +214,29 @@ public class ConfigGroupDAOTest { @Test public void testFindByHost() throws Exception { List<HostEntity> hosts = new ArrayList<HostEntity>(); + // Partially constructed HostEntity that will persisted in {@link createConfigGroup} HostEntity hostEntity = new HostEntity(); hostEntity.setHostName("h1"); hostEntity.setOsType("centOS"); + hosts.add(hostEntity); ConfigGroupEntity configGroupEntity = createConfigGroup("c1", "hdfs-1", "HDFS", "some description", hosts, null); + Assert.assertNotNull(hostEntity.getHostId()); + Assert.assertNotNull(configGroupEntity); Assert.assertTrue(configGroupEntity.getConfigGroupHostMappingEntities() .size() > 0); Assert.assertNotNull(configGroupEntity .getConfigGroupHostMappingEntities().iterator().next()); - Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO - .findByHost("h1"); + Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO.findByHostId(hostEntity.getHostId()); Assert.assertNotNull(hostMappingEntities); for (ConfigGroupHostMapping hostMappingEntity : hostMappingEntities) { - - Assert.assertEquals("h1", hostMappingEntity.getHostname()); + Assert.assertEquals(hostEntity.getHostId(), hostMappingEntity.getHostId()); Assert.assertEquals("centOS", hostMappingEntity.getHost().getOsType()); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java index ec1289a..0dcc471 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java @@ -19,6 +19,7 @@ package org.apache.ambari.server.orm.dao; import java.util.Set; +import com.google.inject.Inject; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -26,6 +27,7 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.cache.HostConfigMapping; import org.apache.ambari.server.orm.cache.HostConfigMappingImpl; +import org.apache.ambari.server.orm.entities.HostEntity; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,7 +42,12 @@ import com.google.inject.persist.PersistService; public class HostConfigMappingDAOTest { private Injector injector; + + @Inject private HostConfigMappingDAO hostConfigMappingDAO; + + @Inject + private HostDAO hostDAO; @Before public void setup() throws AmbariException{ @@ -48,6 +55,7 @@ public class HostConfigMappingDAOTest { injector.getInstance(GuiceJpaInitializer.class); hostConfigMappingDAO = injector.getInstance(HostConfigMappingDAO.class); + hostDAO = injector.getInstance(HostDAO.class); } @After @@ -55,19 +63,27 @@ public class HostConfigMappingDAOTest { injector.getInstance(PersistService.class).stop(); } - private HostConfigMapping createEntity(long clusterId, String host, String type, String version) throws Exception { - HostConfigMapping entity = new HostConfigMappingImpl(); - entity.setClusterId(Long.valueOf(clusterId)); - entity.setCreateTimestamp(Long.valueOf(System.currentTimeMillis())); - entity.setHostName(host); - entity.setSelected(1); - entity.setType(type); - entity.setVersion(version); - entity.setUser("_test"); - - hostConfigMappingDAO.create(entity); - - return entity; + private HostConfigMapping createEntity(long clusterId, String hostName, String type, String version) throws Exception { + HostConfigMapping hostConfigMappingEntity = new HostConfigMappingImpl(); + hostConfigMappingEntity.setClusterId(Long.valueOf(clusterId)); + hostConfigMappingEntity.setCreateTimestamp(Long.valueOf(System.currentTimeMillis())); + + HostEntity hostEntity = hostDAO.findByName(hostName); + if (hostEntity == null) { + hostEntity = new HostEntity(); + hostEntity.setHostName(hostName); + hostDAO.create(hostEntity); + } + + hostConfigMappingEntity.setHostId(hostEntity.getHostId()); + hostConfigMappingEntity.setSelected(1); + hostConfigMappingEntity.setType(type); + hostConfigMappingEntity.setVersion(version); + hostConfigMappingEntity.setUser("_test"); + + hostConfigMappingDAO.create(hostConfigMappingEntity); + + return hostConfigMappingEntity; } @Test @@ -79,8 +95,9 @@ public class HostConfigMappingDAOTest { @Test public void testFindByType() throws Exception { HostConfigMapping source = createEntity(1L, "h1", "global", "v1"); + HostEntity hostEntity = hostDAO.findByName("h1"); - Set<HostConfigMapping> target = hostConfigMappingDAO.findByType(1L, "h1", "global"); + Set<HostConfigMapping> target = hostConfigMappingDAO.findByType(1L, hostEntity.getHostId(), "global"); Assert.assertEquals("Expected one result", 1, target.size()); @@ -91,66 +108,58 @@ public class HostConfigMappingDAOTest { @Test public void testMerge() throws Exception { HostConfigMapping source = createEntity(1L, "h1", "global", "v1"); - - Set<HostConfigMapping> target = hostConfigMappingDAO.findByType(1L, "h1", "global"); + HostEntity hostEntity = hostDAO.findByName("h1"); + Set<HostConfigMapping> target = hostConfigMappingDAO.findByType(1L, hostEntity.getHostId(), "global"); Assert.assertEquals("Expected one result", 1, target.size()); HostConfigMapping toChange = null; for (HostConfigMapping item: target) { - Assert.assertEquals("Expected version 'v1'", source.getVersion(), item.getVersion()); Assert.assertEquals("Expected selected flag 1", 1, (int)item.getSelected()); - toChange = item; - toChange.setSelected(0); - } - - hostConfigMappingDAO.merge(toChange); - target = hostConfigMappingDAO.findByType(1L, "h1", "global"); - + target = hostConfigMappingDAO.findByType(1L, hostEntity.getHostId(), "global"); Assert.assertEquals("Expected one result", 1, target.size()); - for (HostConfigMapping item: target) { - Assert.assertEquals("Expected version 'v1'", source.getVersion(), item.getVersion()); Assert.assertEquals("Expected selected flag 0", 0, (int)item.getSelected()); - } } @Test public void testFindSelected() throws Exception { createEntity(1L, "h1", "global", "version1"); - HostConfigMapping entity2 = createEntity(1L, "h1", "core-site", "version1"); + HostConfigMapping coreSiteConfigV1 = createEntity(1L, "h1", "core-site", "version1"); + HostEntity hostEntity = hostDAO.findByName("h1"); - Set<HostConfigMapping> targets = hostConfigMappingDAO.findSelected(1L, "h1"); + Set<HostConfigMapping> targets = hostConfigMappingDAO.findSelected(1L, hostEntity.getHostId()); Assert.assertEquals("Expected two entities", 2, targets.size()); - - entity2.setSelected(0); - hostConfigMappingDAO.merge(entity2); + + coreSiteConfigV1.setSelected(0); + hostConfigMappingDAO.merge(coreSiteConfigV1); createEntity(1L, "h1", "core-site", "version2"); - targets = hostConfigMappingDAO.findSelected(1L, "h1"); + targets = hostConfigMappingDAO.findSelected(1L, hostEntity.getHostId()); Assert.assertEquals("Expected two entities", 2, targets.size()); } @Test public void testFindSelectedByType() throws Exception { HostConfigMapping entity1 = createEntity(1L, "h1", "global", "version1"); + HostEntity hostEntity = hostDAO.findByName("h1"); - HostConfigMapping target = hostConfigMappingDAO.findSelectedByType(1L, "h1", "core-site"); + HostConfigMapping target = hostConfigMappingDAO.findSelectedByType(1L, hostEntity.getHostId(), "core-site"); Assert.assertNull("Expected null entity for type 'core-site'", target); - target = hostConfigMappingDAO.findSelectedByType(1L, "h1", "global"); + target = hostConfigMappingDAO.findSelectedByType(1L, hostEntity.getHostId(), "global"); Assert.assertNotNull("Expected non-null entity for type 'global'", target); Assert.assertEquals("Expected version to be '" + entity1.getVersion() + "'", entity1.getVersion(), target.getVersion()); @@ -159,7 +168,7 @@ public class HostConfigMappingDAOTest { HostConfigMapping entity2 = createEntity(1L, "h1", "global", "version2"); - target = hostConfigMappingDAO.findSelectedByType(1L, "h1", "global"); + target = hostConfigMappingDAO.findSelectedByType(1L, hostEntity.getHostId(), "global"); Assert.assertNotNull("Expected non-null entity for type 'global'", target); Assert.assertEquals("Expected version to be '" + entity2.getVersion() + "'", entity2.getVersion(), target.getVersion()); @@ -169,9 +178,11 @@ public class HostConfigMappingDAOTest { @Test public void testEmptyTable() throws Exception { - + createEntity(1L, "h1", "global", "version1"); + + HostEntity hostEntity = hostDAO.findByName("h1"); hostConfigMappingDAO.removeHost(1L, "h1"); - HostConfigMapping target = hostConfigMappingDAO.findSelectedByType(1L, "h1", "core-site"); + HostConfigMapping target = hostConfigMappingDAO.findSelectedByType(1L, hostEntity.getHostId(), "core-site"); Assert.assertEquals(null, target); } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java index 28059c0..9bf969c 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigGroupTest.java @@ -103,10 +103,10 @@ public class ConfigGroupTest { Host host = clusters.getHost("h1"); Map<String, Config> configs = new HashMap<String, Config>(); - Map<String, Host> hosts = new HashMap<String, Host>(); + Map<Long, Host> hosts = new HashMap<Long, Host>(); configs.put(config.getType(), config); - hosts.put(host.getHostName(), host); + hosts.put(1L, host); ConfigGroup configGroup = configGroupFactory.createNew(cluster, "cg-test", "HDFS", "New HDFS configs for h1", configs, hosts); @@ -225,8 +225,9 @@ public class ConfigGroupTest { clusters.unmapHostFromCluster("h1", clusterName); Assert.assertNull(clusters.getHostsForCluster(clusterName).get("h1")); - Assert.assertTrue(configGroupHostMappingDAO.findByHost("h1").isEmpty()); - Assert.assertNull(configGroup.getHosts().get("h1")); + // Assumes that 1L is the id of host h1, as specified in createConfigGroup + Assert.assertTrue(configGroupHostMappingDAO.findByHostId(1L).isEmpty()); + Assert.assertFalse(configGroup.getHosts().containsKey(1L)); } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java index 930e45f..7fb8f66 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java @@ -179,12 +179,14 @@ public class ConfigHelperTest { private Long addConfigGroup(String name, String tag, List<String> hosts, List<Config> configs) throws AmbariException { - Map<String, Host> hostMap = new HashMap<String, Host>(); + Map<Long, Host> hostMap = new HashMap<Long, Host>(); Map<String, Config> configMap = new HashMap<String, Config>(); + Long hostId = 1L; for (String hostname : hosts) { Host host = clusters.getHost(hostname); - hostMap.put(host.getHostName(), host); + hostMap.put(hostId, host); + hostId++; } for (Config config : configs) { http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java index 9c129e8..319c2ee 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java @@ -44,6 +44,7 @@ import org.apache.ambari.server.controller.ConfigurationRequest; 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.dao.HostDAO; import org.apache.ambari.server.stack.HostsType; import org.apache.ambari.server.stack.MasterHostResolver; import org.apache.ambari.server.state.UpgradeHelper.UpgradeGroupHolder; @@ -80,7 +81,8 @@ public class UpgradeHelperTest { private MasterHostResolver m_masterHostResolver; private UpgradeHelper m_upgradeHelper; private ConfigHelper m_configHelper; - AmbariManagementController m_managementController; + private AmbariManagementController m_managementController; + private HostDAO m_hostDAO; @Before public void before() throws Exception { @@ -106,6 +108,7 @@ public class UpgradeHelperTest { m_upgradeHelper = injector.getInstance(UpgradeHelper.class); m_masterHostResolver = EasyMock.createMock(MasterHostResolver.class); m_managementController = injector.getInstance(AmbariManagementController.class); + m_hostDAO = injector.getInstance(HostDAO.class); } @After http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java index bc17c38..63c5440 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java @@ -42,6 +42,7 @@ import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.RollbackException; +import com.google.gson.Gson; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -55,11 +56,14 @@ import org.apache.ambari.server.controller.ServiceConfigVersionResponse; 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.dao.ClusterDAO; import org.apache.ambari.server.orm.dao.ClusterVersionDAO; import org.apache.ambari.server.orm.dao.HostComponentStateDAO; import org.apache.ambari.server.orm.dao.HostDAO; import org.apache.ambari.server.orm.dao.HostVersionDAO; import org.apache.ambari.server.orm.dao.RepositoryVersionDAO; +import org.apache.ambari.server.orm.dao.ResourceTypeDAO; +import org.apache.ambari.server.orm.dao.StackDAO; import org.apache.ambari.server.orm.entities.ClusterEntity; import org.apache.ambari.server.orm.entities.ClusterServiceEntity; import org.apache.ambari.server.orm.entities.ClusterStateEntity; @@ -69,6 +73,8 @@ import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.ambari.server.orm.entities.HostStateEntity; 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.ServiceDesiredStateEntity; import org.apache.ambari.server.orm.entities.StackEntity; import org.apache.ambari.server.state.AgentVersion; @@ -121,11 +127,15 @@ public class ClusterTest { private ConfigFactory configFactory; private ConfigGroupFactory configGroupFactory; private OrmTestHelper helper; + private StackDAO stackDAO; + private ResourceTypeDAO resourceTypeDAO; + private ClusterDAO clusterDAO; private HostDAO hostDAO; private ClusterVersionDAO clusterVersionDAO; private HostVersionDAO hostVersionDAO; private HostComponentStateDAO hostComponentStateDAO; private RepositoryVersionDAO repositoryVersionDAO; + private Gson gson; @Singleton static class ClusterVersionDAOMock extends ClusterVersionDAO { @@ -174,11 +184,15 @@ public class ClusterTest { configFactory = injector.getInstance(ConfigFactory.class); metaInfo = injector.getInstance(AmbariMetaInfo.class); helper = injector.getInstance(OrmTestHelper.class); + stackDAO = injector.getInstance(StackDAO.class); + resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class); + clusterDAO = injector.getInstance(ClusterDAO.class); hostDAO = injector.getInstance(HostDAO.class); clusterVersionDAO = injector.getInstance(ClusterVersionDAO.class); hostVersionDAO = injector.getInstance(HostVersionDAO.class); hostComponentStateDAO = injector.getInstance(HostComponentStateDAO.class); repositoryVersionDAO = injector.getInstance(RepositoryVersionDAO.class); + gson = injector.getInstance(Gson.class); } @After @@ -187,38 +201,56 @@ public class ClusterTest { } private void createDefaultCluster() throws Exception { - StackId stackId = new StackId("HDP-0.1"); - clusters.addCluster("c1", stackId); - c1 = clusters.getCluster("c1"); - Assert.assertEquals("c1", c1.getClusterName()); - Assert.assertEquals(1, c1.getClusterId()); + // TODO, use common function + StackId stackId = new StackId("HDP", "0.1"); + StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion()); + org.junit.Assert.assertNotNull(stackEntity); - clusters.addHost("h1"); - clusters.addHost("h2"); - Host host1 = clusters.getHost("h1"); - host1.setIPv4("ipv4"); - host1.setIPv6("ipv6"); + String clusterName = "c1"; - Host host2 = clusters.getHost("h2"); - host2.setIPv4("ipv4"); - host2.setIPv6("ipv6"); + ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findById(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE); + if (resourceTypeEntity == null) { + resourceTypeEntity = new ResourceTypeEntity(); + resourceTypeEntity.setId(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE); + resourceTypeEntity.setName(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE_NAME); + resourceTypeEntity = resourceTypeDAO.merge(resourceTypeEntity); + } + ResourceEntity resourceEntity = new ResourceEntity(); + resourceEntity.setResourceType(resourceTypeEntity); + + ClusterEntity clusterEntity = new ClusterEntity(); + clusterEntity.setClusterName(clusterName); + clusterEntity.setResource(resourceEntity); + clusterEntity.setDesiredStack(stackEntity); + clusterDAO.create(clusterEntity); Map<String, String> hostAttributes = new HashMap<String, String>(); hostAttributes.put("os_family", "redhat"); hostAttributes.put("os_release_version", "5.9"); - host1.setHostAttributes(hostAttributes); - host2.setHostAttributes(hostAttributes); - host1.persist(); - host2.persist(); + List<HostEntity> hostEntities = new ArrayList<HostEntity>(); + Set<String> hostNames = new HashSet<String>() {{ add("h1"); add("h2"); }}; + for (String hostName : hostNames) { + HostEntity hostEntity = new HostEntity(); + hostEntity.setHostName(hostName); + hostEntity.setIpv4("ipv4"); + hostEntity.setIpv6("ipv6"); + hostEntity.setHostAttributes(gson.toJson(hostAttributes)); + hostEntity.setClusterEntities(Arrays.asList(clusterEntity)); + hostEntities.add(hostEntity); + hostDAO.create(hostEntity); + } + + clusterEntity.setHostEntities(hostEntities); + clusterDAO.merge(clusterEntity); + c1 = clusters.getCluster(clusterName); helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion()); c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin", RepositoryVersionState.UPGRADING); c1.transitionClusterVersion(stackId, stackId.getStackVersion(), RepositoryVersionState.CURRENT); - clusters.mapHostToCluster("h1", "c1"); - clusters.mapHostToCluster("h2", "c1"); + ClusterVersionDAOMock.failOnCurrentVersionState = false; } @@ -898,6 +930,7 @@ public class ClusterTest { createDefaultCluster(); Host host1 = clusters.getHost("h1"); + HostEntity hostEntity1 = hostDAO.findByName("h1"); Map<String, Map<String, String>> propAttributes = new HashMap<String, Map<String,String>>(); propAttributes.put("final", new HashMap<String, String>()); @@ -909,18 +942,18 @@ public class ClusterTest { host1.addDesiredConfig(c1.getClusterId(), true, "test", config); - Map<String, Map<String, DesiredConfig>> configs = c1.getAllHostsDesiredConfigs(); + Map<Long, Map<String, DesiredConfig>> configs = c1.getAllHostsDesiredConfigs(); - assertTrue(configs.containsKey("h1")); - assertEquals(1, configs.get("h1").size()); + assertTrue(configs.containsKey(hostEntity1.getHostId())); + assertEquals(1, configs.get(hostEntity1.getHostId()).size()); - List<String> hostnames = new ArrayList<String>(); - hostnames.add("h1"); + List<Long> hostIds = new ArrayList<Long>(); + hostIds.add(hostEntity1.getHostId()); - configs = c1.getHostsDesiredConfigs(hostnames); + configs = c1.getHostsDesiredConfigs(hostIds); - assertTrue(configs.containsKey("h1")); - assertEquals(1, configs.get("h1").size()); + assertTrue(configs.containsKey(hostEntity1.getHostId())); + assertEquals(1, configs.get(hostEntity1.getHostId()).size()); } @Test @@ -1060,7 +1093,7 @@ public class ClusterTest { ConfigGroup configGroup = configGroupFactory.createNew(c1, "test group", "HDFS", "descr", Collections.singletonMap("hdfs-site", config2), - Collections.<String, Host>emptyMap()); + Collections.<Long, Host>emptyMap()); configGroup.persist(); @@ -1117,7 +1150,7 @@ public class ClusterTest { ConfigGroup configGroup2 = configGroupFactory.createNew(c1, "test group 2", "HDFS", "descr", Collections.singletonMap("hdfs-site", config4), - Collections.<String, Host>emptyMap()); + Collections.<Long, Host>emptyMap()); configGroup2.persist(); c1.addConfigGroup(configGroup2); @@ -1328,12 +1361,12 @@ public class ClusterTest { assertNotNull(entityHDP2); List<HostVersionEntity> hostVersionsH1Before = hostVersionDAO.findByClusterAndHost("c1", "h1"); - assertEquals(0, hostVersionsH1Before.size()); + assertEquals(1, hostVersionsH1Before.size()); c1.inferHostVersions(entityHDP2); List<HostVersionEntity> hostVersionsH1After = hostVersionDAO.findByClusterAndHost("c1", "h1"); - assertEquals(1, hostVersionsH1After.size()); + assertEquals(2, hostVersionsH1After.size()); boolean checked = false; for (HostVersionEntity entity : hostVersionsH1After) { @@ -1351,7 +1384,7 @@ public class ClusterTest { c1.inferHostVersions(entityHDP2); hostVersionsH1After = hostVersionDAO.findByClusterAndHost("c1", "h1"); - assertEquals(1, hostVersionsH1After.size()); + assertEquals(2, hostVersionsH1After.size()); checked = false; for (HostVersionEntity entity : hostVersionsH1After) {