|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package com.cloud.upgrade.dao; |
| 19 | + |
| 20 | +import com.cloud.upgrade.SystemVmTemplateRegistration; |
| 21 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 22 | +import org.apache.cloudstack.api.response.UsageTypeResponse; |
| 23 | +import org.apache.cloudstack.usage.UsageTypes; |
| 24 | +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; |
| 25 | +import org.apache.commons.lang3.time.DateUtils; |
| 26 | +import org.apache.log4j.Logger; |
| 27 | + |
| 28 | +import java.io.InputStream; |
| 29 | +import java.sql.Connection; |
| 30 | +import java.sql.PreparedStatement; |
| 31 | +import java.sql.ResultSet; |
| 32 | +import java.sql.SQLException; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Date; |
| 35 | +import java.util.LinkedHashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class Upgrade41700to41800 implements DbUpgrade, DbUpgradeSystemVmTemplate { |
| 40 | + |
| 41 | + final static Logger LOG = Logger.getLogger(Upgrade41700to41800.class); |
| 42 | + private SystemVmTemplateRegistration systemVmTemplateRegistration; |
| 43 | + |
| 44 | + @Override |
| 45 | + public String[] getUpgradableVersionRange() { |
| 46 | + return new String[] {"4.17.0.0", "4.18.0.0"}; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getUpgradedVersion() { |
| 51 | + return "4.18.0.0"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean supportsRollingUpgrade() { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public InputStream[] getPrepareScripts() { |
| 61 | + final String scriptFile = "META-INF/db/schema-41700to41800.sql"; |
| 62 | + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); |
| 63 | + if (script == null) { |
| 64 | + throw new CloudRuntimeException("Unable to find " + scriptFile); |
| 65 | + } |
| 66 | + |
| 67 | + return new InputStream[] {script}; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void performDataMigration(Connection conn) { |
| 72 | + convertQuotaTariffsToNewParadigm(conn); |
| 73 | + convertVmResourcesQuotaTypesToRunningVmQuotaType(conn); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public InputStream[] getCleanupScripts() { |
| 78 | + final String scriptFile = "META-INF/db/schema-41700to41800-cleanup.sql"; |
| 79 | + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); |
| 80 | + if (script == null) { |
| 81 | + throw new CloudRuntimeException("Unable to find " + scriptFile); |
| 82 | + } |
| 83 | + |
| 84 | + return new InputStream[] {script}; |
| 85 | + } |
| 86 | + |
| 87 | + private void initSystemVmTemplateRegistration() { |
| 88 | + systemVmTemplateRegistration = new SystemVmTemplateRegistration(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void updateSystemVmTemplates(Connection conn) { |
| 93 | + LOG.debug("Updating System Vm template IDs"); |
| 94 | + initSystemVmTemplateRegistration(); |
| 95 | + try { |
| 96 | + systemVmTemplateRegistration.updateSystemVmTemplates(conn); |
| 97 | + } catch (Exception e) { |
| 98 | + throw new CloudRuntimeException("Failed to find / register SystemVM template(s)"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + protected void convertQuotaTariffsToNewParadigm(Connection conn) { |
| 103 | + LOG.info("Converting quota tariffs to new paradigm."); |
| 104 | + |
| 105 | + List<UsageTypeResponse> usageTypeResponses = UsageTypes.listUsageTypes(); |
| 106 | + |
| 107 | + for (UsageTypeResponse usageTypeResponse : usageTypeResponses) { |
| 108 | + Integer usageType = usageTypeResponse.getUsageType(); |
| 109 | + |
| 110 | + String tariffTypeDescription = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(usageTypeResponse, "description", "usageType"); |
| 111 | + |
| 112 | + LOG.info(String.format("Converting quota tariffs of type %s to new paradigm.", tariffTypeDescription)); |
| 113 | + |
| 114 | + for (boolean previousTariff : Arrays.asList(true, false)) { |
| 115 | + Map<Long, Date> tariffs = selectTariffs(conn, usageType, previousTariff, tariffTypeDescription); |
| 116 | + |
| 117 | + int tariffsSize = tariffs.size(); |
| 118 | + if (tariffsSize < 2) { |
| 119 | + LOG.info(String.format("Quota tariff of type %s has [%s] %s register(s). Tariffs with less than 2 register do not need to be converted to new paradigm.", |
| 120 | + tariffTypeDescription, tariffsSize, previousTariff ? "previous of current" : "next to current")); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + executeUpdateQuotaTariffSetEndDateAndRemoved(conn, usageType, tariffs, previousTariff, tariffTypeDescription); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + protected Map<Long, Date> selectTariffs(Connection conn, Integer usageType, boolean previousTariff, String tariffTypeDescription) { |
| 130 | + Map<Long, Date> quotaTariffs = new LinkedHashMap<>(); |
| 131 | + |
| 132 | + String selectQuotaTariffs = String.format("SELECT id, effective_on FROM cloud_usage.quota_tariff WHERE %s AND usage_type = ? ORDER BY effective_on, updated_on;", |
| 133 | + previousTariff ? "usage_name = name" : "removed is null"); |
| 134 | + |
| 135 | + LOG.info(String.format("Selecting %s quota tariffs of type [%s] according to SQL [%s].", previousTariff ? "previous of current" : "next to current", |
| 136 | + tariffTypeDescription, selectQuotaTariffs)); |
| 137 | + |
| 138 | + try (PreparedStatement pstmt = conn.prepareStatement(selectQuotaTariffs)) { |
| 139 | + pstmt.setInt(1, usageType); |
| 140 | + |
| 141 | + try (ResultSet result = pstmt.executeQuery()) { |
| 142 | + while (result.next()) { |
| 143 | + quotaTariffs.put(result.getLong("id"), result.getDate("effective_on")); |
| 144 | + } |
| 145 | + } |
| 146 | + return quotaTariffs; |
| 147 | + } catch (SQLException e) { |
| 148 | + String message = String.format("Unable to retrieve %s quota tariffs of type [%s] due to [%s].", previousTariff ? "previous" : "next", tariffTypeDescription, |
| 149 | + e.getMessage()); |
| 150 | + LOG.error(message, e); |
| 151 | + throw new CloudRuntimeException(message, e); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + protected void executeUpdateQuotaTariffSetEndDateAndRemoved(Connection conn, Integer usageType, Map<Long, Date> tariffs, boolean setRemoved, String tariffTypeDescription) { |
| 156 | + String updateQuotaTariff = String.format("UPDATE cloud_usage.quota_tariff SET end_date = ? %s WHERE id = ?;", setRemoved ? ", removed = ?" : ""); |
| 157 | + |
| 158 | + Object[] ids = tariffs.keySet().toArray(); |
| 159 | + |
| 160 | + LOG.info(String.format("Updating %s registers of %s quota tariffs of type [%s] with SQL [%s].", tariffs.size() -1, setRemoved ? "previous of current" : |
| 161 | + "next to current", tariffTypeDescription, updateQuotaTariff)); |
| 162 | + |
| 163 | + for (int i = 0; i < tariffs.size() - 1; i++) { |
| 164 | + Long id = Long.valueOf(String.valueOf(ids[i])); |
| 165 | + Long nextId = Long.valueOf(String.valueOf(ids[i + 1])); |
| 166 | + |
| 167 | + Date endDate = tariffs.get(nextId); |
| 168 | + |
| 169 | + if (!DateUtils.isSameDay(endDate, tariffs.get(id))) { |
| 170 | + endDate = DateUtils.addDays(endDate, -1); |
| 171 | + } |
| 172 | + |
| 173 | + try (PreparedStatement pstmt = conn.prepareStatement(updateQuotaTariff)) { |
| 174 | + java.sql.Date sqlEndDate = new java.sql.Date(endDate.getTime()); |
| 175 | + pstmt.setDate(1, sqlEndDate); |
| 176 | + |
| 177 | + String updateRemoved = ""; |
| 178 | + if (setRemoved) { |
| 179 | + pstmt.setDate(2, sqlEndDate); |
| 180 | + pstmt.setLong(3, id); |
| 181 | + |
| 182 | + updateRemoved = String.format("and \"removed\" to [%s] ", sqlEndDate); |
| 183 | + } else { |
| 184 | + pstmt.setLong(2, id); |
| 185 | + } |
| 186 | + |
| 187 | + LOG.info(String.format("Updating \"end_date\" to [%s] %sof quota tariff with ID [%s].", sqlEndDate, updateRemoved, id)); |
| 188 | + pstmt.executeUpdate(); |
| 189 | + } catch (SQLException e) { |
| 190 | + String message = String.format("Unable to update \"end_date\" %s of quota tariffs of usage type [%s] due to [%s].", setRemoved ? "and \"removed\"" : "", |
| 191 | + usageType, e.getMessage()); |
| 192 | + LOG.error(message, e); |
| 193 | + throw new CloudRuntimeException(message, e); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + protected void convertVmResourcesQuotaTypesToRunningVmQuotaType(Connection conn) { |
| 199 | + LOG.info("Converting quota tariffs of type \"vCPU\", \"CPU_SPEED\" and \"MEMORY\" to \"RUNNING_VM\"."); |
| 200 | + |
| 201 | + String insertSql = String.format("INSERT INTO cloud_usage.quota_tariff (usage_type, usage_name, usage_unit, usage_discriminator, currency_value, effective_on, updated_on," |
| 202 | + + " updated_by, uuid, name, description, removed, end_date, activation_rule)\n" |
| 203 | + + "SELECT 1, 'RUNNING_VM', usage_unit, '', 0, effective_on, updated_on, updated_by, UUID(), name, description, removed, end_date,\n" |
| 204 | + + " CASE\n" |
| 205 | + + " WHEN usage_type = 15 THEN CONCAT('((value.computingResources ? (value.computingResources.cpuSpeed * value.computingResources.cpuNumber) : 0) / 100) * ', currency_value)\n" |
| 206 | + + " WHEN usage_type = 16 THEN CONCAT('(value.computingResources ? value.computingResources.cpuNumber : 0) * ', currency_value)\n" |
| 207 | + + " WHEN usage_type = 17 THEN CONCAT('(value.computingResources ? value.computingResources.memory : 0)* ', currency_value)\n" |
| 208 | + + " END\n" |
| 209 | + + "FROM cloud_usage.quota_tariff \n" |
| 210 | + + "WHERE usage_type in (15, 16, 17) \n" |
| 211 | + + "AND currency_value > 0.0;"); |
| 212 | + |
| 213 | + try (PreparedStatement pstmt = conn.prepareStatement(insertSql)) { |
| 214 | + pstmt.executeUpdate(); |
| 215 | + } catch (SQLException e) { |
| 216 | + String message = String.format("Failed to convert quota tariffs of type \"vCPU\", \"CPU_SPEED\" and \"MEMORY\" to \"RUNNING_VM\" due to [%s].", e.getMessage()); |
| 217 | + LOG.error(message, e); |
| 218 | + throw new CloudRuntimeException(message, e); |
| 219 | + } |
| 220 | + |
| 221 | + LOG.info("Disabling unused quota tariffs of type \"vCPU\", \"CPU_SPEED\" and \"MEMORY\"."); |
| 222 | + |
| 223 | + String updateSql = "UPDATE cloud_usage.quota_tariff SET removed = now() WHERE usage_type in (15, 16, 17) and removed is null;"; |
| 224 | + |
| 225 | + try (PreparedStatement pstmt = conn.prepareStatement(updateSql)) { |
| 226 | + pstmt.executeUpdate(); |
| 227 | + } catch (SQLException e) { |
| 228 | + String message = String.format("Failed disable quota tariffs of type \"vCPU\", \"CPU_SPEED\" and \"MEMORY\" due to [%s].", e.getMessage()); |
| 229 | + LOG.error(message, e); |
| 230 | + throw new CloudRuntimeException(message, e); |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments