transaction.proto 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. syntax = "proto3";
  15. package google.spanner.v1;
  16. import "google/protobuf/duration.proto";
  17. import "google/protobuf/timestamp.proto";
  18. import "google/api/annotations.proto";
  19. option csharp_namespace = "Google.Cloud.Spanner.V1";
  20. option go_package = "google.golang.org/genproto/googleapis/spanner/v1;spanner";
  21. option java_multiple_files = true;
  22. option java_outer_classname = "TransactionProto";
  23. option java_package = "com.google.spanner.v1";
  24. option php_namespace = "Google\\Cloud\\Spanner\\V1";
  25. option ruby_package = "Google::Cloud::Spanner::V1";
  26. // # Transactions
  27. //
  28. //
  29. // Each session can have at most one active transaction at a time (note that
  30. // standalone reads and queries use a transaction internally and do count
  31. // towards the one transaction limit). After the active transaction is
  32. // completed, the session can immediately be re-used for the next transaction.
  33. // It is not necessary to create a new session for each transaction.
  34. //
  35. // # Transaction Modes
  36. //
  37. // Cloud Spanner supports three transaction modes:
  38. //
  39. // 1. Locking read-write. This type of transaction is the only way
  40. // to write data into Cloud Spanner. These transactions rely on
  41. // pessimistic locking and, if necessary, two-phase commit.
  42. // Locking read-write transactions may abort, requiring the
  43. // application to retry.
  44. //
  45. // 2. Snapshot read-only. This transaction type provides guaranteed
  46. // consistency across several reads, but does not allow
  47. // writes. Snapshot read-only transactions can be configured to
  48. // read at timestamps in the past. Snapshot read-only
  49. // transactions do not need to be committed.
  50. //
  51. // 3. Partitioned DML. This type of transaction is used to execute
  52. // a single Partitioned DML statement. Partitioned DML partitions
  53. // the key space and runs the DML statement over each partition
  54. // in parallel using separate, internal transactions that commit
  55. // independently. Partitioned DML transactions do not need to be
  56. // committed.
  57. //
  58. // For transactions that only read, snapshot read-only transactions
  59. // provide simpler semantics and are almost always faster. In
  60. // particular, read-only transactions do not take locks, so they do
  61. // not conflict with read-write transactions. As a consequence of not
  62. // taking locks, they also do not abort, so retry loops are not needed.
  63. //
  64. // Transactions may only read/write data in a single database. They
  65. // may, however, read/write data in different tables within that
  66. // database.
  67. //
  68. // ## Locking Read-Write Transactions
  69. //
  70. // Locking transactions may be used to atomically read-modify-write
  71. // data anywhere in a database. This type of transaction is externally
  72. // consistent.
  73. //
  74. // Clients should attempt to minimize the amount of time a transaction
  75. // is active. Faster transactions commit with higher probability
  76. // and cause less contention. Cloud Spanner attempts to keep read locks
  77. // active as long as the transaction continues to do reads, and the
  78. // transaction has not been terminated by
  79. // [Commit][google.spanner.v1.Spanner.Commit] or
  80. // [Rollback][google.spanner.v1.Spanner.Rollback]. Long periods of
  81. // inactivity at the client may cause Cloud Spanner to release a
  82. // transaction's locks and abort it.
  83. //
  84. // Conceptually, a read-write transaction consists of zero or more
  85. // reads or SQL statements followed by
  86. // [Commit][google.spanner.v1.Spanner.Commit]. At any time before
  87. // [Commit][google.spanner.v1.Spanner.Commit], the client can send a
  88. // [Rollback][google.spanner.v1.Spanner.Rollback] request to abort the
  89. // transaction.
  90. //
  91. // ## Semantics
  92. //
  93. // Cloud Spanner can commit the transaction if all read locks it acquired
  94. // are still valid at commit time, and it is able to acquire write
  95. // locks for all writes. Cloud Spanner can abort the transaction for any
  96. // reason. If a commit attempt returns `ABORTED`, Cloud Spanner guarantees
  97. // that the transaction has not modified any user data in Cloud Spanner.
  98. //
  99. // Unless the transaction commits, Cloud Spanner makes no guarantees about
  100. // how long the transaction's locks were held for. It is an error to
  101. // use Cloud Spanner locks for any sort of mutual exclusion other than
  102. // between Cloud Spanner transactions themselves.
  103. //
  104. // ## Retrying Aborted Transactions
  105. //
  106. // When a transaction aborts, the application can choose to retry the
  107. // whole transaction again. To maximize the chances of successfully
  108. // committing the retry, the client should execute the retry in the
  109. // same session as the original attempt. The original session's lock
  110. // priority increases with each consecutive abort, meaning that each
  111. // attempt has a slightly better chance of success than the previous.
  112. //
  113. // Under some circumstances (e.g., many transactions attempting to
  114. // modify the same row(s)), a transaction can abort many times in a
  115. // short period before successfully committing. Thus, it is not a good
  116. // idea to cap the number of retries a transaction can attempt;
  117. // instead, it is better to limit the total amount of wall time spent
  118. // retrying.
  119. //
  120. // ## Idle Transactions
  121. //
  122. // A transaction is considered idle if it has no outstanding reads or
  123. // SQL queries and has not started a read or SQL query within the last 10
  124. // seconds. Idle transactions can be aborted by Cloud Spanner so that they
  125. // don't hold on to locks indefinitely. In that case, the commit will
  126. // fail with error `ABORTED`.
  127. //
  128. // If this behavior is undesirable, periodically executing a simple
  129. // SQL query in the transaction (e.g., `SELECT 1`) prevents the
  130. // transaction from becoming idle.
  131. //
  132. // ## Snapshot Read-Only Transactions
  133. //
  134. // Snapshot read-only transactions provides a simpler method than
  135. // locking read-write transactions for doing several consistent
  136. // reads. However, this type of transaction does not support writes.
  137. //
  138. // Snapshot transactions do not take locks. Instead, they work by
  139. // choosing a Cloud Spanner timestamp, then executing all reads at that
  140. // timestamp. Since they do not acquire locks, they do not block
  141. // concurrent read-write transactions.
  142. //
  143. // Unlike locking read-write transactions, snapshot read-only
  144. // transactions never abort. They can fail if the chosen read
  145. // timestamp is garbage collected; however, the default garbage
  146. // collection policy is generous enough that most applications do not
  147. // need to worry about this in practice.
  148. //
  149. // Snapshot read-only transactions do not need to call
  150. // [Commit][google.spanner.v1.Spanner.Commit] or
  151. // [Rollback][google.spanner.v1.Spanner.Rollback] (and in fact are not
  152. // permitted to do so).
  153. //
  154. // To execute a snapshot transaction, the client specifies a timestamp
  155. // bound, which tells Cloud Spanner how to choose a read timestamp.
  156. //
  157. // The types of timestamp bound are:
  158. //
  159. // - Strong (the default).
  160. // - Bounded staleness.
  161. // - Exact staleness.
  162. //
  163. // If the Cloud Spanner database to be read is geographically distributed,
  164. // stale read-only transactions can execute more quickly than strong
  165. // or read-write transaction, because they are able to execute far
  166. // from the leader replica.
  167. //
  168. // Each type of timestamp bound is discussed in detail below.
  169. //
  170. // ## Strong
  171. //
  172. // Strong reads are guaranteed to see the effects of all transactions
  173. // that have committed before the start of the read. Furthermore, all
  174. // rows yielded by a single read are consistent with each other -- if
  175. // any part of the read observes a transaction, all parts of the read
  176. // see the transaction.
  177. //
  178. // Strong reads are not repeatable: two consecutive strong read-only
  179. // transactions might return inconsistent results if there are
  180. // concurrent writes. If consistency across reads is required, the
  181. // reads should be executed within a transaction or at an exact read
  182. // timestamp.
  183. //
  184. // See [TransactionOptions.ReadOnly.strong][google.spanner.v1.TransactionOptions.ReadOnly.strong].
  185. //
  186. // ## Exact Staleness
  187. //
  188. // These timestamp bounds execute reads at a user-specified
  189. // timestamp. Reads at a timestamp are guaranteed to see a consistent
  190. // prefix of the global transaction history: they observe
  191. // modifications done by all transactions with a commit timestamp <=
  192. // the read timestamp, and observe none of the modifications done by
  193. // transactions with a larger commit timestamp. They will block until
  194. // all conflicting transactions that may be assigned commit timestamps
  195. // <= the read timestamp have finished.
  196. //
  197. // The timestamp can either be expressed as an absolute Cloud Spanner commit
  198. // timestamp or a staleness relative to the current time.
  199. //
  200. // These modes do not require a "negotiation phase" to pick a
  201. // timestamp. As a result, they execute slightly faster than the
  202. // equivalent boundedly stale concurrency modes. On the other hand,
  203. // boundedly stale reads usually return fresher results.
  204. //
  205. // See [TransactionOptions.ReadOnly.read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.read_timestamp] and
  206. // [TransactionOptions.ReadOnly.exact_staleness][google.spanner.v1.TransactionOptions.ReadOnly.exact_staleness].
  207. //
  208. // ## Bounded Staleness
  209. //
  210. // Bounded staleness modes allow Cloud Spanner to pick the read timestamp,
  211. // subject to a user-provided staleness bound. Cloud Spanner chooses the
  212. // newest timestamp within the staleness bound that allows execution
  213. // of the reads at the closest available replica without blocking.
  214. //
  215. // All rows yielded are consistent with each other -- if any part of
  216. // the read observes a transaction, all parts of the read see the
  217. // transaction. Boundedly stale reads are not repeatable: two stale
  218. // reads, even if they use the same staleness bound, can execute at
  219. // different timestamps and thus return inconsistent results.
  220. //
  221. // Boundedly stale reads execute in two phases: the first phase
  222. // negotiates a timestamp among all replicas needed to serve the
  223. // read. In the second phase, reads are executed at the negotiated
  224. // timestamp.
  225. //
  226. // As a result of the two phase execution, bounded staleness reads are
  227. // usually a little slower than comparable exact staleness
  228. // reads. However, they are typically able to return fresher
  229. // results, and are more likely to execute at the closest replica.
  230. //
  231. // Because the timestamp negotiation requires up-front knowledge of
  232. // which rows will be read, it can only be used with single-use
  233. // read-only transactions.
  234. //
  235. // See [TransactionOptions.ReadOnly.max_staleness][google.spanner.v1.TransactionOptions.ReadOnly.max_staleness] and
  236. // [TransactionOptions.ReadOnly.min_read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.min_read_timestamp].
  237. //
  238. // ## Old Read Timestamps and Garbage Collection
  239. //
  240. // Cloud Spanner continuously garbage collects deleted and overwritten data
  241. // in the background to reclaim storage space. This process is known
  242. // as "version GC". By default, version GC reclaims versions after they
  243. // are one hour old. Because of this, Cloud Spanner cannot perform reads
  244. // at read timestamps more than one hour in the past. This
  245. // restriction also applies to in-progress reads and/or SQL queries whose
  246. // timestamp become too old while executing. Reads and SQL queries with
  247. // too-old read timestamps fail with the error `FAILED_PRECONDITION`.
  248. //
  249. // ## Partitioned DML Transactions
  250. //
  251. // Partitioned DML transactions are used to execute DML statements with a
  252. // different execution strategy that provides different, and often better,
  253. // scalability properties for large, table-wide operations than DML in a
  254. // ReadWrite transaction. Smaller scoped statements, such as an OLTP workload,
  255. // should prefer using ReadWrite transactions.
  256. //
  257. // Partitioned DML partitions the keyspace and runs the DML statement on each
  258. // partition in separate, internal transactions. These transactions commit
  259. // automatically when complete, and run independently from one another.
  260. //
  261. // To reduce lock contention, this execution strategy only acquires read locks
  262. // on rows that match the WHERE clause of the statement. Additionally, the
  263. // smaller per-partition transactions hold locks for less time.
  264. //
  265. // That said, Partitioned DML is not a drop-in replacement for standard DML used
  266. // in ReadWrite transactions.
  267. //
  268. // - The DML statement must be fully-partitionable. Specifically, the statement
  269. // must be expressible as the union of many statements which each access only
  270. // a single row of the table.
  271. //
  272. // - The statement is not applied atomically to all rows of the table. Rather,
  273. // the statement is applied atomically to partitions of the table, in
  274. // independent transactions. Secondary index rows are updated atomically
  275. // with the base table rows.
  276. //
  277. // - Partitioned DML does not guarantee exactly-once execution semantics
  278. // against a partition. The statement will be applied at least once to each
  279. // partition. It is strongly recommended that the DML statement should be
  280. // idempotent to avoid unexpected results. For instance, it is potentially
  281. // dangerous to run a statement such as
  282. // `UPDATE table SET column = column + 1` as it could be run multiple times
  283. // against some rows.
  284. //
  285. // - The partitions are committed automatically - there is no support for
  286. // Commit or Rollback. If the call returns an error, or if the client issuing
  287. // the ExecuteSql call dies, it is possible that some rows had the statement
  288. // executed on them successfully. It is also possible that statement was
  289. // never executed against other rows.
  290. //
  291. // - Partitioned DML transactions may only contain the execution of a single
  292. // DML statement via ExecuteSql or ExecuteStreamingSql.
  293. //
  294. // - If any error is encountered during the execution of the partitioned DML
  295. // operation (for instance, a UNIQUE INDEX violation, division by zero, or a
  296. // value that cannot be stored due to schema constraints), then the
  297. // operation is stopped at that point and an error is returned. It is
  298. // possible that at this point, some partitions have been committed (or even
  299. // committed multiple times), and other partitions have not been run at all.
  300. //
  301. // Given the above, Partitioned DML is good fit for large, database-wide,
  302. // operations that are idempotent, such as deleting old rows from a very large
  303. // table.
  304. message TransactionOptions {
  305. // Message type to initiate a read-write transaction. Currently this
  306. // transaction type has no options.
  307. message ReadWrite {
  308. }
  309. // Message type to initiate a Partitioned DML transaction.
  310. message PartitionedDml {
  311. }
  312. // Message type to initiate a read-only transaction.
  313. message ReadOnly {
  314. // How to choose the timestamp for the read-only transaction.
  315. oneof timestamp_bound {
  316. // Read at a timestamp where all previously committed transactions
  317. // are visible.
  318. bool strong = 1;
  319. // Executes all reads at a timestamp >= `min_read_timestamp`.
  320. //
  321. // This is useful for requesting fresher data than some previous
  322. // read, or data that is fresh enough to observe the effects of some
  323. // previously committed transaction whose timestamp is known.
  324. //
  325. // Note that this option can only be used in single-use transactions.
  326. //
  327. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  328. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  329. google.protobuf.Timestamp min_read_timestamp = 2;
  330. // Read data at a timestamp >= `NOW - max_staleness`
  331. // seconds. Guarantees that all writes that have committed more
  332. // than the specified number of seconds ago are visible. Because
  333. // Cloud Spanner chooses the exact timestamp, this mode works even if
  334. // the client's local clock is substantially skewed from Cloud Spanner
  335. // commit timestamps.
  336. //
  337. // Useful for reading the freshest data available at a nearby
  338. // replica, while bounding the possible staleness if the local
  339. // replica has fallen behind.
  340. //
  341. // Note that this option can only be used in single-use
  342. // transactions.
  343. google.protobuf.Duration max_staleness = 3;
  344. // Executes all reads at the given timestamp. Unlike other modes,
  345. // reads at a specific timestamp are repeatable; the same read at
  346. // the same timestamp always returns the same data. If the
  347. // timestamp is in the future, the read will block until the
  348. // specified timestamp, modulo the read's deadline.
  349. //
  350. // Useful for large scale consistent reads such as mapreduces, or
  351. // for coordinating many reads against a consistent snapshot of the
  352. // data.
  353. //
  354. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  355. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  356. google.protobuf.Timestamp read_timestamp = 4;
  357. // Executes all reads at a timestamp that is `exact_staleness`
  358. // old. The timestamp is chosen soon after the read is started.
  359. //
  360. // Guarantees that all writes that have committed more than the
  361. // specified number of seconds ago are visible. Because Cloud Spanner
  362. // chooses the exact timestamp, this mode works even if the client's
  363. // local clock is substantially skewed from Cloud Spanner commit
  364. // timestamps.
  365. //
  366. // Useful for reading at nearby replicas without the distributed
  367. // timestamp negotiation overhead of `max_staleness`.
  368. google.protobuf.Duration exact_staleness = 5;
  369. }
  370. // If true, the Cloud Spanner-selected read timestamp is included in
  371. // the [Transaction][google.spanner.v1.Transaction] message that describes the transaction.
  372. bool return_read_timestamp = 6;
  373. }
  374. // Required. The type of transaction.
  375. oneof mode {
  376. // Transaction may write.
  377. //
  378. // Authorization to begin a read-write transaction requires
  379. // `spanner.databases.beginOrRollbackReadWriteTransaction` permission
  380. // on the `session` resource.
  381. ReadWrite read_write = 1;
  382. // Partitioned DML transaction.
  383. //
  384. // Authorization to begin a Partitioned DML transaction requires
  385. // `spanner.databases.beginPartitionedDmlTransaction` permission
  386. // on the `session` resource.
  387. PartitionedDml partitioned_dml = 3;
  388. // Transaction will not write.
  389. //
  390. // Authorization to begin a read-only transaction requires
  391. // `spanner.databases.beginReadOnlyTransaction` permission
  392. // on the `session` resource.
  393. ReadOnly read_only = 2;
  394. }
  395. }
  396. // A transaction.
  397. message Transaction {
  398. // `id` may be used to identify the transaction in subsequent
  399. // [Read][google.spanner.v1.Spanner.Read],
  400. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql],
  401. // [Commit][google.spanner.v1.Spanner.Commit], or
  402. // [Rollback][google.spanner.v1.Spanner.Rollback] calls.
  403. //
  404. // Single-use read-only transactions do not have IDs, because
  405. // single-use transactions do not support multiple requests.
  406. bytes id = 1;
  407. // For snapshot read-only transactions, the read timestamp chosen
  408. // for the transaction. Not returned by default: see
  409. // [TransactionOptions.ReadOnly.return_read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.return_read_timestamp].
  410. //
  411. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  412. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  413. google.protobuf.Timestamp read_timestamp = 2;
  414. }
  415. // This message is used to select the transaction in which a
  416. // [Read][google.spanner.v1.Spanner.Read] or
  417. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] call runs.
  418. //
  419. // See [TransactionOptions][google.spanner.v1.TransactionOptions] for more information about transactions.
  420. message TransactionSelector {
  421. // If no fields are set, the default is a single use transaction
  422. // with strong concurrency.
  423. oneof selector {
  424. // Execute the read or SQL query in a temporary transaction.
  425. // This is the most efficient way to execute a transaction that
  426. // consists of a single SQL query.
  427. TransactionOptions single_use = 1;
  428. // Execute the read or SQL query in a previously-started transaction.
  429. bytes id = 2;
  430. // Begin a new transaction and execute this read or SQL query in
  431. // it. The transaction ID of the new transaction is returned in
  432. // [ResultSetMetadata.transaction][google.spanner.v1.ResultSetMetadata.transaction], which is a [Transaction][google.spanner.v1.Transaction].
  433. TransactionOptions begin = 3;
  434. }
  435. }