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.
 
 
 

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