transaction.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. "context"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. "google.golang.org/api/iterator"
  20. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/codes"
  23. "google.golang.org/grpc/metadata"
  24. )
  25. // transactionID stores a transaction ID which uniquely identifies a transaction in Cloud Spanner.
  26. type transactionID []byte
  27. // txReadEnv manages a read-transaction environment consisting of a session handle and a transaction selector.
  28. type txReadEnv interface {
  29. // acquire returns a read-transaction environment that can be used to perform a transactional read.
  30. acquire(ctx context.Context) (*sessionHandle, *sppb.TransactionSelector, error)
  31. // sets the transaction's read timestamp
  32. setTimestamp(time.Time)
  33. // release should be called at the end of every transactional read to deal with session recycling.
  34. release(error)
  35. }
  36. // txReadOnly contains methods for doing transactional reads.
  37. type txReadOnly struct {
  38. // read-transaction environment for performing transactional read operations.
  39. txReadEnv
  40. sequenceNumber int64 // Atomic. Only needed for DML statements, but used for all.
  41. }
  42. // errSessionClosed returns error for using a recycled/destroyed session
  43. func errSessionClosed(sh *sessionHandle) error {
  44. return spannerErrorf(codes.FailedPrecondition,
  45. "session is already recycled / destroyed: session_id = %q, rpc_client = %v", sh.getID(), sh.getClient())
  46. }
  47. // Read returns a RowIterator for reading multiple rows from the database.
  48. func (t *txReadOnly) Read(ctx context.Context, table string, keys KeySet, columns []string) *RowIterator {
  49. return t.ReadWithOptions(ctx, table, keys, columns, nil)
  50. }
  51. // ReadUsingIndex calls ReadWithOptions with ReadOptions{Index: index}.
  52. func (t *txReadOnly) ReadUsingIndex(ctx context.Context, table, index string, keys KeySet, columns []string) (ri *RowIterator) {
  53. return t.ReadWithOptions(ctx, table, keys, columns, &ReadOptions{Index: index})
  54. }
  55. // ReadOptions provides options for reading rows from a database.
  56. type ReadOptions struct {
  57. // The index to use for reading. If non-empty, you can only read columns that are
  58. // part of the index key, part of the primary key, or stored in the index due to
  59. // a STORING clause in the index definition.
  60. Index string
  61. // The maximum number of rows to read. A limit value less than 1 means no limit.
  62. Limit int
  63. }
  64. // ReadWithOptions returns a RowIterator for reading multiple rows from the database.
  65. // Pass a ReadOptions to modify the read operation.
  66. func (t *txReadOnly) ReadWithOptions(ctx context.Context, table string, keys KeySet, columns []string, opts *ReadOptions) (ri *RowIterator) {
  67. ctx = startSpan(ctx, "cloud.google.com/go/spanner.Read")
  68. defer func() { endSpan(ctx, ri.err) }()
  69. var (
  70. sh *sessionHandle
  71. ts *sppb.TransactionSelector
  72. err error
  73. )
  74. kset, err := keys.keySetProto()
  75. if err != nil {
  76. return &RowIterator{err: err}
  77. }
  78. if sh, ts, err = t.acquire(ctx); err != nil {
  79. return &RowIterator{err: err}
  80. }
  81. // Cloud Spanner will return "Session not found" on bad sessions.
  82. sid, client := sh.getID(), sh.getClient()
  83. if sid == "" || client == nil {
  84. // Might happen if transaction is closed in the middle of a API call.
  85. return &RowIterator{err: errSessionClosed(sh)}
  86. }
  87. index := ""
  88. limit := 0
  89. if opts != nil {
  90. index = opts.Index
  91. if opts.Limit > 0 {
  92. limit = opts.Limit
  93. }
  94. }
  95. return stream(
  96. contextWithOutgoingMetadata(ctx, sh.getMetadata()),
  97. func(ctx context.Context, resumeToken []byte) (streamingReceiver, error) {
  98. return client.StreamingRead(ctx,
  99. &sppb.ReadRequest{
  100. Session: sid,
  101. Transaction: ts,
  102. Table: table,
  103. Index: index,
  104. Columns: columns,
  105. KeySet: kset,
  106. ResumeToken: resumeToken,
  107. Limit: int64(limit),
  108. })
  109. },
  110. t.setTimestamp,
  111. t.release,
  112. )
  113. }
  114. // errRowNotFound returns error for not being able to read the row identified by key.
  115. func errRowNotFound(table string, key Key) error {
  116. return spannerErrorf(codes.NotFound, "row not found(Table: %v, PrimaryKey: %v)", table, key)
  117. }
  118. // ReadRow reads a single row from the database.
  119. //
  120. // If no row is present with the given key, then ReadRow returns an error where
  121. // spanner.ErrCode(err) is codes.NotFound.
  122. func (t *txReadOnly) ReadRow(ctx context.Context, table string, key Key, columns []string) (*Row, error) {
  123. iter := t.Read(ctx, table, key, columns)
  124. defer iter.Stop()
  125. row, err := iter.Next()
  126. switch err {
  127. case iterator.Done:
  128. return nil, errRowNotFound(table, key)
  129. case nil:
  130. return row, nil
  131. default:
  132. return nil, err
  133. }
  134. }
  135. // Query executes a query against the database. It returns a RowIterator
  136. // for retrieving the resulting rows.
  137. //
  138. // Query returns only row data, without a query plan or execution statistics.
  139. // Use QueryWithStats to get rows along with the plan and statistics.
  140. // Use AnalyzeQuery to get just the plan.
  141. func (t *txReadOnly) Query(ctx context.Context, statement Statement) *RowIterator {
  142. return t.query(ctx, statement, sppb.ExecuteSqlRequest_NORMAL)
  143. }
  144. // Query executes a SQL statement against the database. It returns a RowIterator
  145. // for retrieving the resulting rows. The RowIterator will also be populated
  146. // with a query plan and execution statistics.
  147. func (t *txReadOnly) QueryWithStats(ctx context.Context, statement Statement) *RowIterator {
  148. return t.query(ctx, statement, sppb.ExecuteSqlRequest_PROFILE)
  149. }
  150. // AnalyzeQuery returns the query plan for statement.
  151. func (t *txReadOnly) AnalyzeQuery(ctx context.Context, statement Statement) (*sppb.QueryPlan, error) {
  152. iter := t.query(ctx, statement, sppb.ExecuteSqlRequest_PLAN)
  153. defer iter.Stop()
  154. for {
  155. _, err := iter.Next()
  156. if err == iterator.Done {
  157. break
  158. }
  159. if err != nil {
  160. return nil, err
  161. }
  162. }
  163. if iter.QueryPlan == nil {
  164. return nil, spannerErrorf(codes.Internal, "query plan unavailable")
  165. }
  166. return iter.QueryPlan, nil
  167. }
  168. func (t *txReadOnly) query(ctx context.Context, statement Statement, mode sppb.ExecuteSqlRequest_QueryMode) (ri *RowIterator) {
  169. ctx = startSpan(ctx, "cloud.google.com/go/spanner.Query")
  170. defer func() { endSpan(ctx, ri.err) }()
  171. req, sh, err := t.prepareExecuteSQL(ctx, statement, mode)
  172. if err != nil {
  173. return &RowIterator{err: err}
  174. }
  175. client := sh.getClient()
  176. return stream(
  177. contextWithOutgoingMetadata(ctx, sh.getMetadata()),
  178. func(ctx context.Context, resumeToken []byte) (streamingReceiver, error) {
  179. req.ResumeToken = resumeToken
  180. return client.ExecuteStreamingSql(ctx, req)
  181. },
  182. t.setTimestamp,
  183. t.release)
  184. }
  185. func (t *txReadOnly) prepareExecuteSQL(ctx context.Context, stmt Statement, mode sppb.ExecuteSqlRequest_QueryMode) (
  186. *sppb.ExecuteSqlRequest, *sessionHandle, error) {
  187. sh, ts, err := t.acquire(ctx)
  188. if err != nil {
  189. return nil, nil, err
  190. }
  191. // Cloud Spanner will return "Session not found" on bad sessions.
  192. sid := sh.getID()
  193. if sid == "" {
  194. // Might happen if transaction is closed in the middle of a API call.
  195. return nil, nil, errSessionClosed(sh)
  196. }
  197. params, paramTypes, err := stmt.convertParams()
  198. if err != nil {
  199. return nil, nil, err
  200. }
  201. req := &sppb.ExecuteSqlRequest{
  202. Session: sid,
  203. Transaction: ts,
  204. Sql: stmt.SQL,
  205. QueryMode: mode,
  206. Seqno: atomic.AddInt64(&t.sequenceNumber, 1),
  207. Params: params,
  208. ParamTypes: paramTypes,
  209. }
  210. return req, sh, nil
  211. }
  212. // txState is the status of a transaction.
  213. type txState int
  214. const (
  215. // transaction is new, waiting to be initialized.
  216. txNew txState = iota
  217. // transaction is being initialized.
  218. txInit
  219. // transaction is active and can perform read/write.
  220. txActive
  221. // transaction is closed, cannot be used anymore.
  222. txClosed
  223. )
  224. // errRtsUnavailable returns error for read transaction's read timestamp being unavailable.
  225. func errRtsUnavailable() error {
  226. return spannerErrorf(codes.Internal, "read timestamp is unavailable")
  227. }
  228. // errTxClosed returns error for using a closed transaction.
  229. func errTxClosed() error {
  230. return spannerErrorf(codes.InvalidArgument, "cannot use a closed transaction")
  231. }
  232. // errUnexpectedTxState returns error for transaction enters an unexpected state.
  233. func errUnexpectedTxState(ts txState) error {
  234. return spannerErrorf(codes.FailedPrecondition, "unexpected transaction state: %v", ts)
  235. }
  236. // ReadOnlyTransaction provides a snapshot transaction with guaranteed
  237. // consistency across reads, but does not allow writes. Read-only
  238. // transactions can be configured to read at timestamps in the past.
  239. //
  240. // Read-only transactions do not take locks. Instead, they work by choosing a
  241. // Cloud Spanner timestamp, then executing all reads at that timestamp. Since they do
  242. // not acquire locks, they do not block concurrent read-write transactions.
  243. //
  244. // Unlike locking read-write transactions, read-only transactions never
  245. // abort. They can fail if the chosen read timestamp is garbage collected;
  246. // however, the default garbage collection policy is generous enough that most
  247. // applications do not need to worry about this in practice. See the
  248. // documentation of TimestampBound for more details.
  249. //
  250. // A ReadOnlyTransaction consumes resources on the server until Close is
  251. // called.
  252. type ReadOnlyTransaction struct {
  253. // txReadOnly contains methods for performing transactional reads.
  254. txReadOnly
  255. // singleUse indicates that the transaction can be used for only one read.
  256. singleUse bool
  257. // sp is the session pool for allocating a session to execute the read-only transaction. It is set only once during initialization of the ReadOnlyTransaction.
  258. sp *sessionPool
  259. // mu protects concurrent access to the internal states of ReadOnlyTransaction.
  260. mu sync.Mutex
  261. // tx is the transaction ID in Cloud Spanner that uniquely identifies the ReadOnlyTransaction.
  262. tx transactionID
  263. // txReadyOrClosed is for broadcasting that transaction ID has been returned by Cloud Spanner or that transaction is closed.
  264. txReadyOrClosed chan struct{}
  265. // state is the current transaction status of the ReadOnly transaction.
  266. state txState
  267. // sh is the sessionHandle allocated from sp.
  268. sh *sessionHandle
  269. // rts is the read timestamp returned by transactional reads.
  270. rts time.Time
  271. // tb is the read staleness bound specification for transactional reads.
  272. tb TimestampBound
  273. }
  274. // errTxInitTimeout returns error for timeout in waiting for initialization of the transaction.
  275. func errTxInitTimeout() error {
  276. return spannerErrorf(codes.Canceled, "timeout/context canceled in waiting for transaction's initialization")
  277. }
  278. // getTimestampBound returns the read staleness bound specified for the ReadOnlyTransaction.
  279. func (t *ReadOnlyTransaction) getTimestampBound() TimestampBound {
  280. t.mu.Lock()
  281. defer t.mu.Unlock()
  282. return t.tb
  283. }
  284. // begin starts a snapshot read-only Transaction on Cloud Spanner.
  285. func (t *ReadOnlyTransaction) begin(ctx context.Context) error {
  286. var (
  287. locked bool
  288. tx transactionID
  289. rts time.Time
  290. sh *sessionHandle
  291. err error
  292. )
  293. defer func() {
  294. if !locked {
  295. t.mu.Lock()
  296. // Not necessary, just to make it clear that t.mu is being held when locked == true.
  297. locked = true
  298. }
  299. if t.state != txClosed {
  300. // Signal other initialization routines.
  301. close(t.txReadyOrClosed)
  302. t.txReadyOrClosed = make(chan struct{})
  303. }
  304. t.mu.Unlock()
  305. if err != nil && sh != nil {
  306. // Got a valid session handle, but failed to initialize transaction on Cloud Spanner.
  307. if shouldDropSession(err) {
  308. sh.destroy()
  309. }
  310. // If sh.destroy was already executed, this becomes a noop.
  311. sh.recycle()
  312. }
  313. }()
  314. sh, err = t.sp.take(ctx)
  315. if err != nil {
  316. return err
  317. }
  318. err = runRetryable(contextWithOutgoingMetadata(ctx, sh.getMetadata()), func(ctx context.Context) error {
  319. res, e := sh.getClient().BeginTransaction(ctx, &sppb.BeginTransactionRequest{
  320. Session: sh.getID(),
  321. Options: &sppb.TransactionOptions{
  322. Mode: &sppb.TransactionOptions_ReadOnly_{
  323. ReadOnly: buildTransactionOptionsReadOnly(t.getTimestampBound(), true),
  324. },
  325. },
  326. })
  327. if e != nil {
  328. return e
  329. }
  330. tx = res.Id
  331. if res.ReadTimestamp != nil {
  332. rts = time.Unix(res.ReadTimestamp.Seconds, int64(res.ReadTimestamp.Nanos))
  333. }
  334. return nil
  335. })
  336. t.mu.Lock()
  337. locked = true // defer function will be executed with t.mu being held.
  338. if t.state == txClosed { // During the execution of t.begin(), t.Close() was invoked.
  339. return errSessionClosed(sh)
  340. }
  341. // If begin() fails, this allows other queries to take over the initialization.
  342. t.tx = nil
  343. if err == nil {
  344. t.tx = tx
  345. t.rts = rts
  346. t.sh = sh
  347. // State transite to txActive.
  348. t.state = txActive
  349. }
  350. return err
  351. }
  352. // acquire implements txReadEnv.acquire.
  353. func (t *ReadOnlyTransaction) acquire(ctx context.Context) (*sessionHandle, *sppb.TransactionSelector, error) {
  354. if err := checkNestedTxn(ctx); err != nil {
  355. return nil, nil, err
  356. }
  357. if t.singleUse {
  358. return t.acquireSingleUse(ctx)
  359. }
  360. return t.acquireMultiUse(ctx)
  361. }
  362. func (t *ReadOnlyTransaction) acquireSingleUse(ctx context.Context) (*sessionHandle, *sppb.TransactionSelector, error) {
  363. t.mu.Lock()
  364. defer t.mu.Unlock()
  365. switch t.state {
  366. case txClosed:
  367. // A closed single-use transaction can never be reused.
  368. return nil, nil, errTxClosed()
  369. case txNew:
  370. t.state = txClosed
  371. ts := &sppb.TransactionSelector{
  372. Selector: &sppb.TransactionSelector_SingleUse{
  373. SingleUse: &sppb.TransactionOptions{
  374. Mode: &sppb.TransactionOptions_ReadOnly_{
  375. ReadOnly: buildTransactionOptionsReadOnly(t.tb, true),
  376. },
  377. },
  378. },
  379. }
  380. sh, err := t.sp.take(ctx)
  381. if err != nil {
  382. return nil, nil, err
  383. }
  384. // Install session handle into t, which can be used for readonly operations later.
  385. t.sh = sh
  386. return sh, ts, nil
  387. }
  388. us := t.state
  389. // SingleUse transaction should only be in either txNew state or txClosed state.
  390. return nil, nil, errUnexpectedTxState(us)
  391. }
  392. func (t *ReadOnlyTransaction) acquireMultiUse(ctx context.Context) (*sessionHandle, *sppb.TransactionSelector, error) {
  393. for {
  394. t.mu.Lock()
  395. switch t.state {
  396. case txClosed:
  397. t.mu.Unlock()
  398. return nil, nil, errTxClosed()
  399. case txNew:
  400. // State transit to txInit so that no further TimestampBound change is accepted.
  401. t.state = txInit
  402. t.mu.Unlock()
  403. continue
  404. case txInit:
  405. if t.tx != nil {
  406. // Wait for a transaction ID to become ready.
  407. txReadyOrClosed := t.txReadyOrClosed
  408. t.mu.Unlock()
  409. select {
  410. case <-txReadyOrClosed:
  411. // Need to check transaction state again.
  412. continue
  413. case <-ctx.Done():
  414. // The waiting for initialization is timeout, return error directly.
  415. return nil, nil, errTxInitTimeout()
  416. }
  417. }
  418. // Take the ownership of initializing the transaction.
  419. t.tx = transactionID{}
  420. t.mu.Unlock()
  421. // Begin a read-only transaction.
  422. // TODO: consider adding a transaction option which allow queries to initiate transactions by themselves. Note that this option might not be
  423. // always good because the ID of the new transaction won't be ready till the query returns some data or completes.
  424. if err := t.begin(ctx); err != nil {
  425. return nil, nil, err
  426. }
  427. // If t.begin() succeeded, t.state should have been changed to txActive, so we can just continue here.
  428. continue
  429. case txActive:
  430. sh := t.sh
  431. ts := &sppb.TransactionSelector{
  432. Selector: &sppb.TransactionSelector_Id{
  433. Id: t.tx,
  434. },
  435. }
  436. t.mu.Unlock()
  437. return sh, ts, nil
  438. }
  439. state := t.state
  440. t.mu.Unlock()
  441. return nil, nil, errUnexpectedTxState(state)
  442. }
  443. }
  444. func (t *ReadOnlyTransaction) setTimestamp(ts time.Time) {
  445. t.mu.Lock()
  446. defer t.mu.Unlock()
  447. if t.rts.IsZero() {
  448. t.rts = ts
  449. }
  450. }
  451. // release implements txReadEnv.release.
  452. func (t *ReadOnlyTransaction) release(err error) {
  453. t.mu.Lock()
  454. sh := t.sh
  455. t.mu.Unlock()
  456. if sh != nil { // sh could be nil if t.acquire() fails.
  457. if shouldDropSession(err) {
  458. sh.destroy()
  459. }
  460. if t.singleUse {
  461. // If session handle is already destroyed, this becomes a noop.
  462. sh.recycle()
  463. }
  464. }
  465. }
  466. // Close closes a ReadOnlyTransaction, the transaction cannot perform any reads after being closed.
  467. func (t *ReadOnlyTransaction) Close() {
  468. if t.singleUse {
  469. return
  470. }
  471. t.mu.Lock()
  472. if t.state != txClosed {
  473. t.state = txClosed
  474. close(t.txReadyOrClosed)
  475. }
  476. sh := t.sh
  477. t.mu.Unlock()
  478. if sh == nil {
  479. return
  480. }
  481. // If session handle is already destroyed, this becomes a noop.
  482. // If there are still active queries and if the recycled session is reused before they complete, Cloud Spanner will cancel them
  483. // on behalf of the new transaction on the session.
  484. if sh != nil {
  485. sh.recycle()
  486. }
  487. }
  488. // Timestamp returns the timestamp chosen to perform reads and
  489. // queries in this transaction. The value can only be read after some
  490. // read or query has either returned some data or completed without
  491. // returning any data.
  492. func (t *ReadOnlyTransaction) Timestamp() (time.Time, error) {
  493. t.mu.Lock()
  494. defer t.mu.Unlock()
  495. if t.rts.IsZero() {
  496. return t.rts, errRtsUnavailable()
  497. }
  498. return t.rts, nil
  499. }
  500. // WithTimestampBound specifies the TimestampBound to use for read or query.
  501. // This can only be used before the first read or query is invoked. Note:
  502. // bounded staleness is not available with general ReadOnlyTransactions; use a
  503. // single-use ReadOnlyTransaction instead.
  504. //
  505. // The returned value is the ReadOnlyTransaction so calls can be chained.
  506. func (t *ReadOnlyTransaction) WithTimestampBound(tb TimestampBound) *ReadOnlyTransaction {
  507. t.mu.Lock()
  508. defer t.mu.Unlock()
  509. if t.state == txNew {
  510. // Only allow to set TimestampBound before the first query.
  511. t.tb = tb
  512. }
  513. return t
  514. }
  515. // ReadWriteTransaction provides a locking read-write transaction.
  516. //
  517. // This type of transaction is the only way to write data into Cloud Spanner;
  518. // (*Client).Apply and (*Client).ApplyAtLeastOnce use transactions
  519. // internally. These transactions rely on pessimistic locking and, if
  520. // necessary, two-phase commit. Locking read-write transactions may abort,
  521. // requiring the application to retry. However, the interface exposed by
  522. // (*Client).ReadWriteTransaction eliminates the need for applications to write
  523. // retry loops explicitly.
  524. //
  525. // Locking transactions may be used to atomically read-modify-write data
  526. // anywhere in a database. This type of transaction is externally consistent.
  527. //
  528. // Clients should attempt to minimize the amount of time a transaction is
  529. // active. Faster transactions commit with higher probability and cause less
  530. // contention. Cloud Spanner attempts to keep read locks active as long as the
  531. // transaction continues to do reads. Long periods of inactivity at the client
  532. // may cause Cloud Spanner to release a transaction's locks and abort it.
  533. //
  534. // Reads performed within a transaction acquire locks on the data being
  535. // read. Writes can only be done at commit time, after all reads have been
  536. // completed. Conceptually, a read-write transaction consists of zero or more
  537. // reads or SQL queries followed by a commit.
  538. //
  539. // See (*Client).ReadWriteTransaction for an example.
  540. //
  541. // Semantics
  542. //
  543. // Cloud Spanner can commit the transaction if all read locks it acquired are still
  544. // valid at commit time, and it is able to acquire write locks for all
  545. // writes. Cloud Spanner can abort the transaction for any reason. If a commit
  546. // attempt returns ABORTED, Cloud Spanner guarantees that the transaction has not
  547. // modified any user data in Cloud Spanner.
  548. //
  549. // Unless the transaction commits, Cloud Spanner makes no guarantees about how long
  550. // the transaction's locks were held for. It is an error to use Cloud Spanner locks
  551. // for any sort of mutual exclusion other than between Cloud Spanner transactions
  552. // themselves.
  553. //
  554. // Aborted transactions
  555. //
  556. // Application code does not need to retry explicitly; RunInTransaction will
  557. // automatically retry a transaction if an attempt results in an abort. The
  558. // lock priority of a transaction increases after each prior aborted
  559. // transaction, meaning that the next attempt has a slightly better chance of
  560. // success than before.
  561. //
  562. // Under some circumstances (e.g., many transactions attempting to modify the
  563. // same row(s)), a transaction can abort many times in a short period before
  564. // successfully committing. Thus, it is not a good idea to cap the number of
  565. // retries a transaction can attempt; instead, it is better to limit the total
  566. // amount of wall time spent retrying.
  567. //
  568. // Idle transactions
  569. //
  570. // A transaction is considered idle if it has no outstanding reads or SQL
  571. // queries and has not started a read or SQL query within the last 10
  572. // seconds. Idle transactions can be aborted by Cloud Spanner so that they don't hold
  573. // on to locks indefinitely. In that case, the commit will fail with error
  574. // ABORTED.
  575. //
  576. // If this behavior is undesirable, periodically executing a simple SQL query
  577. // in the transaction (e.g., SELECT 1) prevents the transaction from becoming
  578. // idle.
  579. type ReadWriteTransaction struct {
  580. // txReadOnly contains methods for performing transactional reads.
  581. txReadOnly
  582. // sh is the sessionHandle allocated from sp. It is set only once during the initialization of ReadWriteTransaction.
  583. sh *sessionHandle
  584. // tx is the transaction ID in Cloud Spanner that uniquely identifies the ReadWriteTransaction.
  585. // It is set only once in ReadWriteTransaction.begin() during the initialization of ReadWriteTransaction.
  586. tx transactionID
  587. // mu protects concurrent access to the internal states of ReadWriteTransaction.
  588. mu sync.Mutex
  589. // state is the current transaction status of the read-write transaction.
  590. state txState
  591. // wb is the set of buffered mutations waiting to be committed.
  592. wb []*Mutation
  593. }
  594. // BufferWrite adds a list of mutations to the set of updates that will be
  595. // applied when the transaction is committed. It does not actually apply the
  596. // write until the transaction is committed, so the operation does not
  597. // block. The effects of the write won't be visible to any reads (including
  598. // reads done in the same transaction) until the transaction commits.
  599. //
  600. // See the example for Client.ReadWriteTransaction.
  601. func (t *ReadWriteTransaction) BufferWrite(ms []*Mutation) error {
  602. t.mu.Lock()
  603. defer t.mu.Unlock()
  604. if t.state == txClosed {
  605. return errTxClosed()
  606. }
  607. if t.state != txActive {
  608. return errUnexpectedTxState(t.state)
  609. }
  610. t.wb = append(t.wb, ms...)
  611. return nil
  612. }
  613. // Update executes a DML statement against the database. It returns the number of
  614. // affected rows.
  615. // Update returns an error if the statement is a query. However, the
  616. // query is executed, and any data read will be validated upon commit.
  617. func (t *ReadWriteTransaction) Update(ctx context.Context, stmt Statement) (rowCount int64, err error) {
  618. ctx = startSpan(ctx, "cloud.google.com/go/spanner.Update")
  619. defer func() { endSpan(ctx, err) }()
  620. req, sh, err := t.prepareExecuteSQL(ctx, stmt, sppb.ExecuteSqlRequest_NORMAL)
  621. if err != nil {
  622. return 0, err
  623. }
  624. resultSet, err := sh.getClient().ExecuteSql(ctx, req)
  625. if err != nil {
  626. return 0, err
  627. }
  628. if resultSet.Stats == nil {
  629. return 0, spannerErrorf(codes.InvalidArgument, "query passed to Update: %q", stmt.SQL)
  630. }
  631. return extractRowCount(resultSet.Stats)
  632. }
  633. // acquire implements txReadEnv.acquire.
  634. func (t *ReadWriteTransaction) acquire(ctx context.Context) (*sessionHandle, *sppb.TransactionSelector, error) {
  635. ts := &sppb.TransactionSelector{
  636. Selector: &sppb.TransactionSelector_Id{
  637. Id: t.tx,
  638. },
  639. }
  640. t.mu.Lock()
  641. defer t.mu.Unlock()
  642. switch t.state {
  643. case txClosed:
  644. return nil, nil, errTxClosed()
  645. case txActive:
  646. return t.sh, ts, nil
  647. }
  648. return nil, nil, errUnexpectedTxState(t.state)
  649. }
  650. // release implements txReadEnv.release.
  651. func (t *ReadWriteTransaction) release(err error) {
  652. t.mu.Lock()
  653. sh := t.sh
  654. t.mu.Unlock()
  655. if sh != nil && shouldDropSession(err) {
  656. sh.destroy()
  657. }
  658. }
  659. func beginTransaction(ctx context.Context, sid string, client sppb.SpannerClient) (transactionID, error) {
  660. var tx transactionID
  661. err := runRetryable(ctx, func(ctx context.Context) error {
  662. res, e := client.BeginTransaction(ctx, &sppb.BeginTransactionRequest{
  663. Session: sid,
  664. Options: &sppb.TransactionOptions{
  665. Mode: &sppb.TransactionOptions_ReadWrite_{
  666. ReadWrite: &sppb.TransactionOptions_ReadWrite{},
  667. },
  668. },
  669. })
  670. if e != nil {
  671. return e
  672. }
  673. tx = res.Id
  674. return nil
  675. })
  676. if err != nil {
  677. return nil, err
  678. }
  679. return tx, nil
  680. }
  681. // begin starts a read-write transacton on Cloud Spanner, it is always called before any of the public APIs.
  682. func (t *ReadWriteTransaction) begin(ctx context.Context) error {
  683. if t.tx != nil {
  684. t.state = txActive
  685. return nil
  686. }
  687. tx, err := beginTransaction(contextWithOutgoingMetadata(ctx, t.sh.getMetadata()), t.sh.getID(), t.sh.getClient())
  688. if err == nil {
  689. t.tx = tx
  690. t.state = txActive
  691. return nil
  692. }
  693. if shouldDropSession(err) {
  694. t.sh.destroy()
  695. }
  696. return err
  697. }
  698. // commit tries to commit a readwrite transaction to Cloud Spanner. It also returns the commit timestamp for the transactions.
  699. func (t *ReadWriteTransaction) commit(ctx context.Context) (time.Time, error) {
  700. var ts time.Time
  701. t.mu.Lock()
  702. t.state = txClosed // No further operations after commit.
  703. mPb, err := mutationsProto(t.wb)
  704. t.mu.Unlock()
  705. if err != nil {
  706. return ts, err
  707. }
  708. // In case that sessionHandle was destroyed but transaction body fails to report it.
  709. sid, client := t.sh.getID(), t.sh.getClient()
  710. if sid == "" || client == nil {
  711. return ts, errSessionClosed(t.sh)
  712. }
  713. err = runRetryable(contextWithOutgoingMetadata(ctx, t.sh.getMetadata()), func(ctx context.Context) error {
  714. var trailer metadata.MD
  715. res, e := client.Commit(ctx, &sppb.CommitRequest{
  716. Session: sid,
  717. Transaction: &sppb.CommitRequest_TransactionId{
  718. TransactionId: t.tx,
  719. },
  720. Mutations: mPb,
  721. }, grpc.Trailer(&trailer))
  722. if e != nil {
  723. return toSpannerErrorWithMetadata(e, trailer)
  724. }
  725. if tstamp := res.GetCommitTimestamp(); tstamp != nil {
  726. ts = time.Unix(tstamp.Seconds, int64(tstamp.Nanos))
  727. }
  728. return nil
  729. })
  730. if shouldDropSession(err) {
  731. t.sh.destroy()
  732. }
  733. return ts, err
  734. }
  735. // rollback is called when a commit is aborted or the transaction body runs into error.
  736. func (t *ReadWriteTransaction) rollback(ctx context.Context) {
  737. t.mu.Lock()
  738. // Forbid further operations on rollbacked transaction.
  739. t.state = txClosed
  740. t.mu.Unlock()
  741. // In case that sessionHandle was destroyed but transaction body fails to report it.
  742. sid, client := t.sh.getID(), t.sh.getClient()
  743. if sid == "" || client == nil {
  744. return
  745. }
  746. err := runRetryable(contextWithOutgoingMetadata(ctx, t.sh.getMetadata()), func(ctx context.Context) error {
  747. _, e := client.Rollback(ctx, &sppb.RollbackRequest{
  748. Session: sid,
  749. TransactionId: t.tx,
  750. })
  751. return e
  752. })
  753. if shouldDropSession(err) {
  754. t.sh.destroy()
  755. }
  756. }
  757. // runInTransaction executes f under a read-write transaction context.
  758. func (t *ReadWriteTransaction) runInTransaction(ctx context.Context, f func(context.Context, *ReadWriteTransaction) error) (time.Time, error) {
  759. var (
  760. ts time.Time
  761. err error
  762. )
  763. if err = f(context.WithValue(ctx, transactionInProgressKey{}, 1), t); err == nil {
  764. // Try to commit if transaction body returns no error.
  765. ts, err = t.commit(ctx)
  766. }
  767. if err != nil {
  768. if isAbortErr(err) {
  769. // Retry the transaction using the same session on ABORT error.
  770. // Cloud Spanner will create the new transaction with the previous one's wound-wait priority.
  771. err = errRetry(err)
  772. return ts, err
  773. }
  774. // Not going to commit, according to API spec, should rollback the transaction.
  775. t.rollback(ctx)
  776. return ts, err
  777. }
  778. // err == nil, return commit timestamp.
  779. return ts, nil
  780. }
  781. // writeOnlyTransaction provides the most efficient way of doing write-only transactions. It essentially does blind writes to Cloud Spanner.
  782. type writeOnlyTransaction struct {
  783. // sp is the session pool which writeOnlyTransaction uses to get Cloud Spanner sessions for blind writes.
  784. sp *sessionPool
  785. }
  786. // applyAtLeastOnce commits a list of mutations to Cloud Spanner at least once, unless one of the following happens:
  787. // 1) Context times out.
  788. // 2) An unretryable error (e.g. database not found) occurs.
  789. // 3) There is a malformed Mutation object.
  790. func (t *writeOnlyTransaction) applyAtLeastOnce(ctx context.Context, ms ...*Mutation) (time.Time, error) {
  791. var (
  792. ts time.Time
  793. sh *sessionHandle
  794. )
  795. mPb, err := mutationsProto(ms)
  796. if err != nil {
  797. // Malformed mutation found, just return the error.
  798. return ts, err
  799. }
  800. err = runRetryable(ctx, func(ct context.Context) error {
  801. var e error
  802. var trailers metadata.MD
  803. if sh == nil || sh.getID() == "" || sh.getClient() == nil {
  804. // No usable session for doing the commit, take one from pool.
  805. sh, e = t.sp.take(ctx)
  806. if e != nil {
  807. // sessionPool.Take already retries for session creations/retrivals.
  808. return e
  809. }
  810. }
  811. res, e := sh.getClient().Commit(contextWithOutgoingMetadata(ctx, sh.getMetadata()), &sppb.CommitRequest{
  812. Session: sh.getID(),
  813. Transaction: &sppb.CommitRequest_SingleUseTransaction{
  814. SingleUseTransaction: &sppb.TransactionOptions{
  815. Mode: &sppb.TransactionOptions_ReadWrite_{
  816. ReadWrite: &sppb.TransactionOptions_ReadWrite{},
  817. },
  818. },
  819. },
  820. Mutations: mPb,
  821. }, grpc.Trailer(&trailers))
  822. if e != nil {
  823. if isAbortErr(e) {
  824. // Mask ABORT error as retryable, because aborted transactions are allowed to be retried.
  825. return errRetry(toSpannerErrorWithMetadata(e, trailers))
  826. }
  827. if shouldDropSession(e) {
  828. // Discard the bad session.
  829. sh.destroy()
  830. }
  831. return e
  832. }
  833. if tstamp := res.GetCommitTimestamp(); tstamp != nil {
  834. ts = time.Unix(tstamp.Seconds, int64(tstamp.Nanos))
  835. }
  836. return nil
  837. })
  838. if sh != nil {
  839. sh.recycle()
  840. }
  841. return ts, err
  842. }
  843. // isAbortedErr returns true if the error indicates that an gRPC call is aborted on the server side.
  844. func isAbortErr(err error) bool {
  845. if err == nil {
  846. return false
  847. }
  848. if ErrCode(err) == codes.Aborted {
  849. return true
  850. }
  851. return false
  852. }