|
| 1 | +package com.example.tidbstmtcache; |
| 2 | + |
| 3 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 4 | +import org.springframework.web.bind.annotation.GetMapping; |
| 5 | +import org.springframework.web.bind.annotation.PathVariable; |
| 6 | +import org.springframework.web.bind.annotation.RestController; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Endpoints that drive prepared-statement traffic against TiDB :4000. |
| 13 | + * |
| 14 | + * The orphan-EXECUTE scenario being exercised: |
| 15 | + * |
| 16 | + * 1. /api/kv/{v} prepares "SELECT ? AS v" with useServerPrepStmts + |
| 17 | + * cachePrepStmts on. |
| 18 | + * 2. First call on Connection-A: Connector/J emits COM_STMT_PREPARE |
| 19 | + * then COM_STMT_EXECUTE; recorder captures both (PREPARE mock + |
| 20 | + * EXECUTE mock with the SAME connID/stmtID pair). |
| 21 | + * 3. Subsequent call on Connection-A (HikariCP LIFO): Connector/J |
| 22 | + * finds the PreparedStatement in its client cache and emits ONLY |
| 23 | + * COM_STMT_EXECUTE using the cached stmtID. Recorder captures an |
| 24 | + * EXECUTE-only mock. |
| 25 | + * 4. At replay time, the matcher tries to pair the "EXECUTE-only" mock |
| 26 | + * against the incoming COM_STMT_EXECUTE. If the recorder's connID |
| 27 | + * attribution or HikariCP's pool rotation makes the PREPARE entry |
| 28 | + * invisible to buildRecordedPrepIndex for this stmtID, expectedQuery |
| 29 | + * comes back empty -- which is the case keploy/keploy@b2e68adb |
| 30 | + * handles by accepting the EXECUTE on parameters alone instead of |
| 31 | + * crashing the connection with "no matching mock". |
| 32 | + * |
| 33 | + * Two endpoints with different SQL shapes are exposed so the matcher |
| 34 | + * gets nontrivial work to do (it cannot just memoize one stmtID). |
| 35 | + */ |
| 36 | +@RestController |
| 37 | +public class QueryController { |
| 38 | + |
| 39 | + private final JdbcTemplate jdbc; |
| 40 | + |
| 41 | + public QueryController(JdbcTemplate jdbc) { |
| 42 | + this.jdbc = jdbc; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Lightweight liveness probe. Plain query, no prepared statement -- |
| 47 | + * used by the CI script's wait_for_app loop so app readiness is not |
| 48 | + * coupled to TiDB prep-cache behaviour. |
| 49 | + */ |
| 50 | + @GetMapping("/api/health") |
| 51 | + public Map<String, Object> health() { |
| 52 | + Integer one = jdbc.queryForObject("SELECT 1", Integer.class); |
| 53 | + Map<String, Object> out = new HashMap<>(); |
| 54 | + out.put("status", "ok"); |
| 55 | + out.put("db", one); |
| 56 | + return out; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Prepared SELECT with one parameter. Same SQL across calls, so |
| 61 | + * Connector/J's cachePrepStmts cache hits on the second-and-later |
| 62 | + * call landing on the same physical connection. |
| 63 | + */ |
| 64 | + @GetMapping("/api/kv/{v}") |
| 65 | + public Map<String, Object> selectParam(@PathVariable("v") int v) { |
| 66 | + Integer echoed = jdbc.queryForObject("SELECT ? AS v", Integer.class, v); |
| 67 | + Map<String, Object> out = new HashMap<>(); |
| 68 | + out.put("echoed", echoed); |
| 69 | + return out; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Prepared INSERT, then prepared SELECT against the same row. Two |
| 74 | + * distinct prepared statements ("INSERT INTO kv ..." and "SELECT v |
| 75 | + * FROM kv WHERE id=?") that both go through the Connector/J cache. |
| 76 | + * Gives the matcher more than one (connID, stmtID) pair to track |
| 77 | + * concurrently per connection. |
| 78 | + */ |
| 79 | + @GetMapping("/api/kv/insert-select/{v}") |
| 80 | + public Map<String, Object> insertThenSelect(@PathVariable("v") int v) { |
| 81 | + jdbc.update("INSERT INTO kv (v) VALUES (?)", v); |
| 82 | + Integer last = jdbc.queryForObject( |
| 83 | + "SELECT v FROM kv ORDER BY id DESC LIMIT 1", Integer.class); |
| 84 | + Map<String, Object> out = new HashMap<>(); |
| 85 | + out.put("inserted", v); |
| 86 | + out.put("readback", last); |
| 87 | + return out; |
| 88 | + } |
| 89 | +} |
0 commit comments