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


##########
engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42300to2400.java:
##########
@@ -27,4 +33,19 @@ public String[] getUpgradableVersionRange() {
     public String getUpgradedVersion() {
         return "24.0.0";
     }
+
+    @Override
+    public InputStream[] getPrepareScripts() {
+        final String scriptFile = "META-INF/db/schema-42300to2400.sql";
+        final InputStream script = 
Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
+        if (script == null) {
+            throw new CloudRuntimeException("Unable to find " + scriptFile);
+        }
+        return new InputStream[]{script};
+    }
+
+    @Override
+    public void performDataMigration(Connection conn) {
+        new NetworkRateBackfill().backfillNetworkRates();
+    }

Review Comment:
   The migration ignores the provided `Connection conn` and instead relies on 
`TransactionLegacy.currentTxn()` inside `NetworkRateBackfill`. In upgrade 
contexts, that transaction/connection binding may not exist (or may use a 
different connection), which can cause upgrade failures or partial migrations. 
Consider passing `conn` into the backfill class and using it consistently (or 
explicitly opening/binding a TransactionLegacy transaction scoped to `conn`) so 
the upgrade framework controls connection lifecycle/transactionality.



##########
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:
   This backfill does per-row lookups/updates (NIC -> network lookup -> VM 
lookup -> offering/config lookup -> update), which can be very slow on large 
installations and may hold upgrade windows open for a long time. If feasible, 
consider reducing round-trips by batching (e.g., process NICs in chunks and 
cache commonly-used lookups like network offering rates/config values per 
zone/offering) or using set-based SQL updates for the common cases.



##########
server/src/main/java/com/cloud/api/ApiResponseHelper.java:
##########
@@ -2715,6 +2720,9 @@ public NetworkResponse createNetworkResponse(ResponseView 
view, Network network)
         response.setNetworkDomain(network.getNetworkDomain());
         response.setPublicMtu(network.getPublicMtu());
         response.setPrivateMtu(network.getPrivateMtu());
+        NetworkDetailVO networkRateDetail = 
networkDetailsDao.findDetail(network.getId(), ApiConstants.NETWORKRATE);
+        int networkRate = networkRateDetail != null ? 
NumberUtils.toInt(networkRateDetail.getValue(), -1) : -1;
+        response.setNetworkRate(networkRate > 0 ? networkRate : -1);

Review Comment:
   This introduces an extra DAO lookup per network response (`findDetail`), 
which can easily become an N+1 query pattern in list APIs. Since you already 
extended join views for NIC network rate, consider doing the same for network 
rate (e.g., add the effective rate to the network join/view query) or 
batch-load details for all returned network IDs in one query.



-- 
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