Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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