Copilot commented on code in PR #13325:
URL: https://github.com/apache/cloudstack/pull/13325#discussion_r4129622427


##########
engine/schema/src/main/java/com/cloud/upgrade/NetworkRateBackfill.java:
##########
@@ -0,0 +1,251 @@
+// 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 com.cloud.upgrade;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+import org.apache.cloudstack.framework.config.dao.ConfigurationDaoImpl;
+import org.apache.cloudstack.resourcedetail.dao.VpcDetailsDao;
+import org.apache.cloudstack.resourcedetail.dao.VpcDetailsDaoImpl;
+
+import com.cloud.dc.DataCenterDetailVO;
+import com.cloud.dc.dao.DataCenterDetailsDaoImpl;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkDaoImpl;
+import com.cloud.network.dao.NetworkDetailsDao;
+import com.cloud.network.dao.NetworkDetailsDaoImpl;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.service.ServiceOfferingVO;
+import com.cloud.service.dao.ServiceOfferingDao;
+import com.cloud.service.dao.ServiceOfferingDaoImpl;
+import com.cloud.utils.db.TransactionLegacy;
+import com.cloud.vm.VMInstanceVO;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.dao.VMInstanceDao;
+import com.cloud.vm.dao.VMInstanceDaoImpl;
+
+/**
+ * Backfills {@code nics.network_rate} and the {@code network_details} 
"networkrate" entry for
+ * pre-existing NICs/networks, deliberately frozen to the pre-feature 
precedence of
+ * {@link com.cloud.network.NetworkModelImpl#getNetworkRate} - do not redirect 
this to call the
+ * live method, whose precedence will keep evolving. Also backfills the {@code 
vpc_details}
+ * "publicnetworkrate" entry for pre-existing VPCs with a fixed "unlimited" 
value, since both
+ * {@code vpc_offerings.public_nw_rate} and the 
"vpc.public.network.throttling.rate" config are
+ * introduced by this same release and can't yet hold a pre-existing value.
+ */
+public class NetworkRateBackfill {
+    protected static Logger LOGGER = 
LogManager.getLogger(NetworkRateBackfill.class);
+
+    private static final String CONFIG_NETWORK_THROTTLING_RATE = 
"network.throttling.rate";
+    private static final String CONFIG_VM_NETWORK_THROTTLING_RATE = 
"vm.network.throttling.rate";
+    private static final String NETWORKRATE_DETAIL_NAME = "networkrate";
+    private static final String PUBLIC_NETWORK_RATE_DETAIL_NAME = 
"publicnetworkrate";
+    private static final int DEFAULT_THROTTLING_RATE = 200;
+    private static final int UNLIMITED_RATE = -1;
+
+    private final VMInstanceDao vmInstanceDao = new VMInstanceDaoImpl();
+    private final NetworkDao networkDao = new NetworkDaoImpl();
+    private final NetworkDetailsDao networkDetailsDao = new 
NetworkDetailsDaoImpl();
+    private final VpcDetailsDao vpcDetailsDao = new VpcDetailsDaoImpl();
+    private final ServiceOfferingDao serviceOfferingDao = new 
ServiceOfferingDaoImpl();
+    private final DataCenterDetailsDaoImpl dataCenterDetailsDao = new 
DataCenterDetailsDaoImpl();
+    private final ConfigurationDao configurationDao = new 
ConfigurationDaoImpl();
+
+    public void backfillNetworkRates() {
+        backfillNicNetworkRates();
+        backfillNetworkDetailsRates();
+        backfillVpcPublicNetworkRates();
+    }
+
+    private void backfillNicNetworkRates() {
+        final String sql = "SELECT id, network_id, instance_id, default_nic 
FROM nics " +
+                "WHERE removed IS NULL AND network_rate IS NULL AND 
instance_id IS NOT NULL";
+        try (PreparedStatement pstmt = 
TransactionLegacy.currentTxn().prepareStatement(sql);
+             ResultSet rs = pstmt.executeQuery()) {
+            while (rs.next()) {
+                final long nicId = rs.getLong("id");
+                final long networkId = rs.getLong("network_id");
+                final long instanceId = rs.getLong("instance_id");
+                final boolean defaultNic = rs.getBoolean("default_nic");
+                try {
+                    final Integer rate = 
computeLegacyNicNetworkRate(networkId, instanceId, defaultNic);
+                    if (rate != null && rate != 0) {
+                        updateNicNetworkRate(nicId, rate);
+                    }
+                } catch (Exception e) {
+                    LOGGER.warn("Failed to backfill network_rate for nic id=" 
+ nicId + ": " + e.getMessage());
+                }
+            }
+        } catch (SQLException e) {
+            LOGGER.warn("Failed to backfill nic network rates: " + 
e.getMessage());
+        }

Review Comment:
   A failure opening or iterating the backfill query is only logged here, after 
which `performDataMigration` returns normally and `DatabaseUpgradeChecker` 
records the 24.0.0 version. That can leave existing NICs/networks without the 
persisted effective rates while making the upgrade appear complete; propagate 
the migration failure (or provide an explicit retry/status mechanism) instead 
of committing a partial backfill.



##########
ui/src/config/section/offering.js:
##########
@@ -597,7 +597,7 @@ export default {
         icon: 'edit-outlined',
         label: 'label.edit',
         dataView: true,
-        args: ['name', 'displaytext']
+        args: ['name', 'displaytext', 'publicnetworkrate']

Review Comment:
   This adds `publicnetworkrate` to the generic edit form, but the VPC-offering 
response normalizes an unset or zero rate to `-1`. The edit form copies that 
value and submits it unchanged, while `UpdateVPCOfferingCmd` rejects every 
negative rate, so editing an unlimited offering fails. Map `-1` to the accepted 
unlimited value `0` (or omit it) before submitting the edit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to