|
22 | 22 | import com.google.cloud.spanner.Dialect; |
23 | 23 | import com.google.cloud.spanner.Options; |
24 | 24 | import com.google.cloud.spanner.Options.RpcPriority; |
| 25 | +import com.google.cloud.spanner.ReadOnlyTransaction; |
25 | 26 | import com.google.cloud.spanner.ResultSet; |
26 | 27 | import com.google.cloud.spanner.Statement; |
27 | 28 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.InitialPartition; |
|
32 | 33 | */ |
33 | 34 | public class ChangeStreamDao { |
34 | 35 |
|
| 36 | + // new enum for partition mode |
| 37 | + protected enum PartitionMode { |
| 38 | + UNKNOWN, |
| 39 | + MUTABLE_KEY_RANGE, |
| 40 | + IMMUTABLE_KEY_RANGE |
| 41 | + } |
| 42 | + |
35 | 43 | private final String changeStreamName; |
36 | 44 | private final DatabaseClient databaseClient; |
37 | 45 | private final RpcPriority rpcPriority; |
38 | 46 | private final String jobName; |
39 | 47 | private final Dialect dialect; |
40 | 48 |
|
| 49 | + // Always non-null to satisfy nullness checker. |
| 50 | + // Start UNKNOWN until we fetch and cache the real mode. |
| 51 | + private volatile PartitionMode partitionMode = PartitionMode.UNKNOWN; |
| 52 | + |
41 | 53 | /** |
42 | 54 | * Constructs a change stream dao. All the queries performed by this class will be for the given |
43 | 55 | * change stream name with the specified rpc priority. The job name will be used to tag all the |
@@ -91,8 +103,20 @@ public ChangeStreamResultSet changeStreamQuery( |
91 | 103 | String query = ""; |
92 | 104 | Statement statement; |
93 | 105 | if (this.isPostgres()) { |
94 | | - query = |
95 | | - "SELECT * FROM \"spanner\".\"read_json_" + changeStreamName + "\"($1, $2, $3, $4, null)"; |
| 106 | + // Ensure we have determined whether change stream uses mutable key range |
| 107 | + boolean isMutable = isMutableKeyRangeChangeStream(); |
| 108 | + |
| 109 | + if (isMutable) { |
| 110 | + query = |
| 111 | + "SELECT * FROM \"spanner\".\"read_proto_bytes_" |
| 112 | + + changeStreamName |
| 113 | + + "\"($1, $2, $3, $4, null)"; |
| 114 | + } else { |
| 115 | + query = |
| 116 | + "SELECT * FROM \"spanner\".\"read_json_" |
| 117 | + + changeStreamName |
| 118 | + + "\"($1, $2, $3, $4, null)"; |
| 119 | + } |
96 | 120 | statement = |
97 | 121 | Statement.newBuilder(query) |
98 | 122 | .bind("p1") |
@@ -138,4 +162,67 @@ public ChangeStreamResultSet changeStreamQuery( |
138 | 162 | private boolean isPostgres() { |
139 | 163 | return this.dialect == Dialect.POSTGRESQL; |
140 | 164 | } |
| 165 | + |
| 166 | + // Returns the PartitionMode, fetching from Spanner on first call and caching. |
| 167 | + protected PartitionMode getPartitionMode() { |
| 168 | + PartitionMode mode = this.partitionMode; |
| 169 | + if (mode != PartitionMode.UNKNOWN) { |
| 170 | + return mode; |
| 171 | + } |
| 172 | + synchronized (this) { |
| 173 | + if (this.partitionMode == PartitionMode.UNKNOWN) { |
| 174 | + String fetchedPartitionMode = |
| 175 | + fetchPartitionMode(this.databaseClient, this.dialect, this.changeStreamName); |
| 176 | + if (fetchedPartitionMode.isEmpty() |
| 177 | + || fetchedPartitionMode.equalsIgnoreCase("IMMUTABLE_KEY_RANGE")) { |
| 178 | + mode = PartitionMode.IMMUTABLE_KEY_RANGE; |
| 179 | + } else { |
| 180 | + mode = PartitionMode.MUTABLE_KEY_RANGE; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + return mode; |
| 185 | + } |
| 186 | + |
| 187 | + // Convenience boolean method kept for compatibility |
| 188 | + protected boolean isMutableKeyRangeChangeStream() { |
| 189 | + return getPartitionMode() == PartitionMode.MUTABLE_KEY_RANGE; |
| 190 | + } |
| 191 | + |
| 192 | + // Returns the partition_mode option value for the given change stream. |
| 193 | + private static String fetchPartitionMode( |
| 194 | + DatabaseClient databaseClient, Dialect dialect, String changeStreamName) { |
| 195 | + try (ReadOnlyTransaction tx = databaseClient.readOnlyTransaction()) { |
| 196 | + Statement statement; |
| 197 | + if (dialect == Dialect.POSTGRESQL) { |
| 198 | + statement = |
| 199 | + Statement.newBuilder( |
| 200 | + "select option_name, option_value\n" |
| 201 | + + "from information_schema.change_stream_options\n" |
| 202 | + + "where change_stream_name = $1") |
| 203 | + .bind("p1") |
| 204 | + .to(changeStreamName) |
| 205 | + .build(); |
| 206 | + } else { |
| 207 | + statement = |
| 208 | + Statement.newBuilder( |
| 209 | + "select option_name, option_value\n" |
| 210 | + + "from information_schema.change_stream_options\n" |
| 211 | + + "where change_stream_name = @changeStreamName") |
| 212 | + .bind("changeStreamName") |
| 213 | + .to(changeStreamName) |
| 214 | + .build(); |
| 215 | + } |
| 216 | + |
| 217 | + try (ResultSet resultSet = tx.executeQuery(statement)) { |
| 218 | + while (resultSet.next()) { |
| 219 | + String optionName = resultSet.getString(0); |
| 220 | + if ("partition_mode".equalsIgnoreCase(optionName)) { |
| 221 | + return resultSet.getString(1); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + return ""; |
| 227 | + } |
141 | 228 | } |
0 commit comments