|
| 1 | +package dev.dbos.transact.database; |
| 2 | + |
| 3 | +import dev.dbos.transact.json.SerializationUtil; |
| 4 | +import dev.dbos.transact.workflow.internal.StepResult; |
| 5 | + |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import javax.sql.DataSource; |
| 14 | + |
| 15 | +class StreamsDAO { |
| 16 | + |
| 17 | + private final DataSource dataSource; |
| 18 | + private final String schema; |
| 19 | + |
| 20 | + StreamsDAO(DataSource dataSource, String schema) { |
| 21 | + this.dataSource = dataSource; |
| 22 | + this.schema = schema; |
| 23 | + } |
| 24 | + |
| 25 | + public void writeStreamFromStep( |
| 26 | + String workflowId, int functionId, String key, Object value, String serializationFormat) |
| 27 | + throws SQLException { |
| 28 | + try (Connection conn = dataSource.getConnection()) { |
| 29 | + insertStream(conn, workflowId, functionId, key, value, serializationFormat); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public void writeStreamFromWorkflow( |
| 34 | + String workflowId, int functionId, String key, Object value, String serializationFormat) |
| 35 | + throws SQLException { |
| 36 | + String functionName = |
| 37 | + STREAM_CLOSED_SENTINEL.equals(value) ? "DBOS.closeStream" : "DBOS.writeStream"; |
| 38 | + long startTime = System.currentTimeMillis(); |
| 39 | + |
| 40 | + try (Connection conn = dataSource.getConnection()) { |
| 41 | + conn.setAutoCommit(false); |
| 42 | + |
| 43 | + try { |
| 44 | + StepResult recordedOutput = |
| 45 | + StepsDAO.checkStepExecutionTxn(workflowId, functionId, functionName, conn, schema); |
| 46 | + |
| 47 | + if (recordedOutput != null) { |
| 48 | + logger.debug("Replaying writeStream, id: {}, key: {}", functionId, key); |
| 49 | + conn.commit(); |
| 50 | + return; |
| 51 | + } else { |
| 52 | + logger.debug("Running writeStream, id: {}, key: {}", functionId, key); |
| 53 | + } |
| 54 | + |
| 55 | + insertStream(conn, workflowId, functionId, key, value, serializationFormat); |
| 56 | + |
| 57 | + var output = new StepResult(workflowId, functionId, functionName, null, null, null, null); |
| 58 | + StepsDAO.recordStepResultTxn(output, startTime, System.currentTimeMillis(), conn, schema); |
| 59 | + |
| 60 | + conn.commit(); |
| 61 | + |
| 62 | + } catch (Exception e) { |
| 63 | + try { |
| 64 | + conn.rollback(); |
| 65 | + } catch (SQLException rollbackEx) { |
| 66 | + e.addSuppressed(rollbackEx); |
| 67 | + } |
| 68 | + throw e; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void insertStream( |
| 74 | + Connection conn, |
| 75 | + String workflowId, |
| 76 | + int functionId, |
| 77 | + String key, |
| 78 | + Object value, |
| 79 | + String serializationFormat) |
| 80 | + throws SQLException { |
| 81 | + var serialized = SerializationUtil.serializeValue(value, serializationFormat, null); |
| 82 | + int offset = getNextOffsetTx(conn, workflowId, key); |
| 83 | + |
| 84 | + String sql = |
| 85 | + """ |
| 86 | + INSERT INTO "%s".streams (workflow_uuid, key, value, "offset", function_id, serialization) |
| 87 | + VALUES (?, ?, ?, ?, ?, ?) |
| 88 | + """ |
| 89 | + .formatted(schema); |
| 90 | + |
| 91 | + try (var stmt = conn.prepareStatement(sql)) { |
| 92 | + stmt.setString(1, workflowId); |
| 93 | + stmt.setString(2, key); |
| 94 | + stmt.setString(3, serialized.serializedValue()); |
| 95 | + stmt.setInt(4, offset); |
| 96 | + stmt.setInt(5, functionId); |
| 97 | + stmt.setString(6, serialized.serialization()); |
| 98 | + stmt.executeUpdate(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private int getNextOffsetTx(Connection conn, String workflowId, String key) throws SQLException { |
| 103 | + String sql = |
| 104 | + """ |
| 105 | + SELECT COALESCE(MAX("offset"), -1) + 1 |
| 106 | + FROM "%s".streams |
| 107 | + WHERE workflow_uuid = ? AND key = ? |
| 108 | + """ |
| 109 | + .formatted(schema); |
| 110 | + |
| 111 | + try (var stmt = conn.prepareStatement(sql)) { |
| 112 | + stmt.setString(1, workflowId); |
| 113 | + stmt.setString(2, key); |
| 114 | + try (var rs = stmt.executeQuery()) { |
| 115 | + if (rs.next()) { |
| 116 | + return rs.getInt(1); |
| 117 | + } |
| 118 | + return 0; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public void closeStream(String workflowId, int functionId, String key) throws SQLException { |
| 124 | + writeStreamFromWorkflow(workflowId, functionId, key, STREAM_CLOSED_SENTINEL, "portable_json"); |
| 125 | + } |
| 126 | + |
| 127 | + public Object readStream(String workflowId, String key, int offset) throws SQLException { |
| 128 | + String sql = |
| 129 | + """ |
| 130 | + SELECT value, serialization |
| 131 | + FROM "%s".streams |
| 132 | + WHERE workflow_uuid = ? AND key = ? AND "offset" = ? |
| 133 | + """ |
| 134 | + .formatted(schema); |
| 135 | + |
| 136 | + try (Connection conn = dataSource.getConnection(); |
| 137 | + var stmt = conn.prepareStatement(sql)) { |
| 138 | + stmt.setString(1, workflowId); |
| 139 | + stmt.setString(2, key); |
| 140 | + stmt.setInt(3, offset); |
| 141 | + try (var rs = stmt.executeQuery()) { |
| 142 | + if (rs.next()) { |
| 143 | + String value = rs.getString("value"); |
| 144 | + String serialization = rs.getString("serialization"); |
| 145 | + Object deserialized = SerializationUtil.deserializeValue(value, serialization, null); |
| 146 | + if (STREAM_CLOSED_SENTINEL.equals(deserialized)) { |
| 147 | + throw new IllegalStateException("Stream closed for key: " + key); |
| 148 | + } |
| 149 | + return deserialized; |
| 150 | + } |
| 151 | + throw new IllegalArgumentException( |
| 152 | + "No value found for workflow=" + workflowId + ", key=" + key + ", offset=" + offset); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public Map<String, List<Object>> getAllStreamEntries(String workflowId) throws SQLException { |
| 158 | + String sql = |
| 159 | + """ |
| 160 | + SELECT key, value, serialization |
| 161 | + FROM "%s".streams |
| 162 | + WHERE workflow_uuid = ? |
| 163 | + ORDER BY key, "offset" |
| 164 | + """ |
| 165 | + .formatted(schema); |
| 166 | + |
| 167 | + var streams = new LinkedHashMap<String, List<Object>>(); |
| 168 | + try (Connection conn = dataSource.getConnection(); |
| 169 | + var stmt = conn.prepareStatement(sql)) { |
| 170 | + stmt.setString(1, workflowId); |
| 171 | + try (var rs = stmt.executeQuery()) { |
| 172 | + while (rs.next()) { |
| 173 | + String key = rs.getString("key"); |
| 174 | + String value = rs.getString("value"); |
| 175 | + String serialization = rs.getString("serialization"); |
| 176 | + |
| 177 | + Object deserialized = SerializationUtil.deserializeValue(value, serialization, null); |
| 178 | + if (STREAM_CLOSED_SENTINEL.equals(deserialized)) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + streams.computeIfAbsent(key, k -> new ArrayList<>()).add(deserialized); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + return streams; |
| 187 | + } |
| 188 | + |
| 189 | + private static final org.slf4j.Logger logger = |
| 190 | + org.slf4j.LoggerFactory.getLogger(StreamsDAO.class); |
| 191 | + |
| 192 | + static final String STREAM_CLOSED_SENTINEL = "__DBOS_STREAM_CLOSED__"; |
| 193 | +} |
0 commit comments