You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

241 lines
8.8 KiB

  1. /*
  2. Copyright 2017 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "fmt"
  16. "time"
  17. pbd "github.com/golang/protobuf/ptypes/duration"
  18. pbt "github.com/golang/protobuf/ptypes/timestamp"
  19. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  20. )
  21. // timestampBoundType specifies the timestamp bound mode.
  22. type timestampBoundType int
  23. const (
  24. strong timestampBoundType = iota // strong reads
  25. exactStaleness // read with exact staleness
  26. maxStaleness // read with max staleness
  27. minReadTimestamp // read with min freshness
  28. readTimestamp // read data at exact timestamp
  29. )
  30. // TimestampBound defines how Cloud Spanner will choose a timestamp for a single
  31. // read/query or read-only transaction.
  32. //
  33. // There are three types of timestamp bound: strong, bounded staleness and exact
  34. // staleness. Strong is the default.
  35. //
  36. // If the Cloud Spanner database to be read is geographically distributed, stale
  37. // read-only transactions can execute more quickly than strong or read-write
  38. // transactions, because they are able to execute far from the leader replica.
  39. //
  40. // Each type of timestamp bound is discussed in detail below. A TimestampBound
  41. // can be specified when creating transactions, see the documentation of
  42. // spanner.Client for an example.
  43. //
  44. // Strong reads
  45. //
  46. // Strong reads are guaranteed to see the effects of all transactions that have
  47. // committed before the start of the read. Furthermore, all rows yielded by a
  48. // single read are consistent with each other: if any part of the read
  49. // observes a transaction, all parts of the read see the transaction.
  50. //
  51. // Strong reads are not repeatable: two consecutive strong read-only
  52. // transactions might return inconsistent results if there are concurrent
  53. // writes. If consistency across reads is required, the reads should be
  54. // executed within a transaction or at an exact read timestamp.
  55. //
  56. // Use StrongRead to create a bound of this type.
  57. //
  58. // Exact staleness
  59. //
  60. // An exact staleness timestamp bound executes reads at a user-specified timestamp.
  61. // Reads at a timestamp are guaranteed to see a consistent prefix of the global
  62. // transaction history: they observe modifications done by all transactions with a
  63. // commit timestamp less than or equal to the read timestamp, and observe none of the
  64. // modifications done by transactions with a larger commit timestamp. They will block
  65. // until all conflicting transactions that may be assigned commit timestamps less
  66. // than or equal to the read timestamp have finished.
  67. //
  68. // The timestamp can either be expressed as an absolute Cloud Spanner commit
  69. // timestamp or a staleness relative to the current time.
  70. //
  71. // These modes do not require a "negotiation phase" to pick a timestamp. As a
  72. // result, they execute slightly faster than the equivalent boundedly stale
  73. // concurrency modes. On the other hand, boundedly stale reads usually return
  74. // fresher results.
  75. //
  76. // Use ReadTimestamp and ExactStaleness to create a bound of this type.
  77. //
  78. // Bounded staleness
  79. //
  80. // Bounded staleness modes allow Cloud Spanner to pick the read timestamp, subject to
  81. // a user-provided staleness bound. Cloud Spanner chooses the newest timestamp within
  82. // the staleness bound that allows execution of the reads at the closest
  83. // available replica without blocking.
  84. //
  85. // All rows yielded are consistent with each other: if any part of the read
  86. // observes a transaction, all parts of the read see the transaction. Boundedly
  87. // stale reads are not repeatable: two stale reads, even if they use the same
  88. // staleness bound, can execute at different timestamps and thus return
  89. // inconsistent results.
  90. //
  91. // Boundedly stale reads execute in two phases. The first phase negotiates a
  92. // timestamp among all replicas needed to serve the read. In the second phase,
  93. // reads are executed at the negotiated timestamp.
  94. //
  95. // As a result of this two-phase execution, bounded staleness reads are usually
  96. // a little slower than comparable exact staleness reads. However, they are
  97. // typically able to return fresher results, and are more likely to execute at
  98. // the closest replica.
  99. //
  100. // Because the timestamp negotiation requires up-front knowledge of which rows
  101. // will be read, it can only be used with single-use reads and single-use
  102. // read-only transactions.
  103. //
  104. // Use MinReadTimestamp and MaxStaleness to create a bound of this type.
  105. //
  106. // Old read timestamps and garbage collection
  107. //
  108. // Cloud Spanner continuously garbage collects deleted and overwritten data in the
  109. // background to reclaim storage space. This process is known as "version
  110. // GC". By default, version GC reclaims versions after they are four hours
  111. // old. Because of this, Cloud Spanner cannot perform reads at read timestamps more
  112. // than four hours in the past. This restriction also applies to in-progress
  113. // reads and/or SQL queries whose timestamps become too old while
  114. // executing. Reads and SQL queries with too-old read timestamps fail with the
  115. // error ErrorCode.FAILED_PRECONDITION.
  116. type TimestampBound struct {
  117. mode timestampBoundType
  118. d time.Duration
  119. t time.Time
  120. }
  121. // StrongRead returns a TimestampBound that will perform reads and queries at a
  122. // timestamp where all previously committed transactions are visible.
  123. func StrongRead() TimestampBound {
  124. return TimestampBound{mode: strong}
  125. }
  126. // ExactStaleness returns a TimestampBound that will perform reads and queries
  127. // at an exact staleness.
  128. func ExactStaleness(d time.Duration) TimestampBound {
  129. return TimestampBound{
  130. mode: exactStaleness,
  131. d: d,
  132. }
  133. }
  134. // MaxStaleness returns a TimestampBound that will perform reads and queries at
  135. // a time chosen to be at most "d" stale.
  136. func MaxStaleness(d time.Duration) TimestampBound {
  137. return TimestampBound{
  138. mode: maxStaleness,
  139. d: d,
  140. }
  141. }
  142. // MinReadTimestamp returns a TimestampBound that bound that will perform reads
  143. // and queries at a time chosen to be at least "t".
  144. func MinReadTimestamp(t time.Time) TimestampBound {
  145. return TimestampBound{
  146. mode: minReadTimestamp,
  147. t: t,
  148. }
  149. }
  150. // ReadTimestamp returns a TimestampBound that will peform reads and queries at
  151. // the given time.
  152. func ReadTimestamp(t time.Time) TimestampBound {
  153. return TimestampBound{
  154. mode: readTimestamp,
  155. t: t,
  156. }
  157. }
  158. func (tb TimestampBound) String() string {
  159. switch tb.mode {
  160. case strong:
  161. return fmt.Sprintf("(strong)")
  162. case exactStaleness:
  163. return fmt.Sprintf("(exactStaleness: %s)", tb.d)
  164. case maxStaleness:
  165. return fmt.Sprintf("(maxStaleness: %s)", tb.d)
  166. case minReadTimestamp:
  167. return fmt.Sprintf("(minReadTimestamp: %s)", tb.t)
  168. case readTimestamp:
  169. return fmt.Sprintf("(readTimestamp: %s)", tb.t)
  170. default:
  171. return fmt.Sprintf("{mode=%v, d=%v, t=%v}", tb.mode, tb.d, tb.t)
  172. }
  173. }
  174. // durationProto takes a time.Duration and converts it into pdb.Duration for
  175. // calling gRPC APIs.
  176. func durationProto(d time.Duration) *pbd.Duration {
  177. n := d.Nanoseconds()
  178. return &pbd.Duration{
  179. Seconds: n / int64(time.Second),
  180. Nanos: int32(n % int64(time.Second)),
  181. }
  182. }
  183. // timestampProto takes a time.Time and converts it into pbt.Timestamp for calling
  184. // gRPC APIs.
  185. func timestampProto(t time.Time) *pbt.Timestamp {
  186. return &pbt.Timestamp{
  187. Seconds: t.Unix(),
  188. Nanos: int32(t.Nanosecond()),
  189. }
  190. }
  191. // buildTransactionOptionsReadOnly converts a spanner.TimestampBound into a sppb.TransactionOptions_ReadOnly
  192. // transaction option, which is then used in transactional reads.
  193. func buildTransactionOptionsReadOnly(tb TimestampBound, returnReadTimestamp bool) *sppb.TransactionOptions_ReadOnly {
  194. pb := &sppb.TransactionOptions_ReadOnly{
  195. ReturnReadTimestamp: returnReadTimestamp,
  196. }
  197. switch tb.mode {
  198. case strong:
  199. pb.TimestampBound = &sppb.TransactionOptions_ReadOnly_Strong{
  200. Strong: true,
  201. }
  202. case exactStaleness:
  203. pb.TimestampBound = &sppb.TransactionOptions_ReadOnly_ExactStaleness{
  204. ExactStaleness: durationProto(tb.d),
  205. }
  206. case maxStaleness:
  207. pb.TimestampBound = &sppb.TransactionOptions_ReadOnly_MaxStaleness{
  208. MaxStaleness: durationProto(tb.d),
  209. }
  210. case minReadTimestamp:
  211. pb.TimestampBound = &sppb.TransactionOptions_ReadOnly_MinReadTimestamp{
  212. MinReadTimestamp: timestampProto(tb.t),
  213. }
  214. case readTimestamp:
  215. pb.TimestampBound = &sppb.TransactionOptions_ReadOnly_ReadTimestamp{
  216. ReadTimestamp: timestampProto(tb.t),
  217. }
  218. default:
  219. panic(fmt.Sprintf("buildTransactionOptionsReadOnly(%v,%v)", tb, returnReadTimestamp))
  220. }
  221. return pb
  222. }