jian666wang-maker commented on code in PR #8042:
URL: https://github.com/apache/incubator-seata/pull/8042#discussion_r3069143572


##########
test-suite/seata-benchmark-cli/src/main/java/org/apache/seata/benchmark/saga/InventoryDbSagaService.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.benchmark.saga;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Database-backed inventory service for Saga benchmark.
+ * @author zihenzzz
+ */
+public class InventoryDbSagaService {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(InventoryDbSagaService.class);
+
+    private final DataSource dataSource;
+    private final int rollbackPercentage;
+    private final int simulatedDelayMs;
+    private final boolean failInjectionEnabled;
+    private final Random failureRandom;
+    private final boolean timeoutInjectionEnabled;
+    private final int timeoutMs;
+
+    public InventoryDbSagaService(
+            DataSource dataSource,
+            int rollbackPercentage,
+            int simulatedDelayMs,
+            boolean failInjectionEnabled,
+            Random failureRandom,
+            boolean timeoutInjectionEnabled,
+            int timeoutMs) {
+        this.dataSource = dataSource;
+        this.rollbackPercentage = rollbackPercentage;
+        this.simulatedDelayMs = simulatedDelayMs;
+        this.failInjectionEnabled = failInjectionEnabled;
+        this.failureRandom = failureRandom;
+        this.timeoutInjectionEnabled = timeoutInjectionEnabled;
+        this.timeoutMs = timeoutMs;
+    }
+
+    public Map<String, Object> reserveInventory(Map<String, Object> params) {
+        String productId = (String) params.get("productId");
+        Integer quantity = (Integer) params.get("quantity");
+
+        simulateDelay();
+        if (timeoutInjectionEnabled) {
+            simulateTimeout("inventory reservation");
+        }
+        if (shouldFail()) {
+            throw new RuntimeException("Simulated inventory reservation 
failure");
+        }
+
+        try (Connection conn = dataSource.getConnection()) {
+            conn.setAutoCommit(false);
+            int available = queryAvailableQuantity(conn, productId);
+            if (available < quantity) {
+                throw new RuntimeException("Insufficient inventory for product 
" + productId);
+            }
+            try (PreparedStatement pstmt = conn.prepareStatement(
+                    "UPDATE benchmark_inventory "
+                            + "SET available_qty = available_qty - ?, 
reserved_qty = reserved_qty + ? "
+                            + "WHERE product_id = ?")) {
+                pstmt.setInt(1, quantity);
+                pstmt.setInt(2, quantity);
+                pstmt.setString(3, productId);
+                pstmt.executeUpdate();

Review Comment:
   Classic TOCTOU — two threads can both pass the availability check and both 
decrement. Just fold the check into the update:
   
   ```sql
   UPDATE ... SET available_qty = available_qty - ? WHERE product_id = ? AND 
available_qty >= ?
   ```
   
   Then check affected rows. Same applies to the balance check in 
`PaymentDbSagaService`.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to