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.
 
 
 

888 lines
27 KiB

  1. /*
  2. Copyright 2015 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 bigtable // import "cloud.google.com/go/bigtable"
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "strconv"
  19. "time"
  20. "cloud.google.com/go/bigtable/internal/gax"
  21. btopt "cloud.google.com/go/bigtable/internal/option"
  22. "github.com/golang/protobuf/proto"
  23. "golang.org/x/net/context"
  24. "google.golang.org/api/option"
  25. gtransport "google.golang.org/api/transport/grpc"
  26. btpb "google.golang.org/genproto/googleapis/bigtable/v2"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/metadata"
  30. "google.golang.org/grpc/status"
  31. )
  32. const prodAddr = "bigtable.googleapis.com:443"
  33. // Client is a client for reading and writing data to tables in an instance.
  34. //
  35. // A Client is safe to use concurrently, except for its Close method.
  36. type Client struct {
  37. conn *grpc.ClientConn
  38. client btpb.BigtableClient
  39. project, instance string
  40. // App Profiles are part of the private alpha release of Cloud Bigtable replication.
  41. // This feature
  42. // is not currently available to most Cloud Bigtable customers. This feature
  43. // might be changed in backward-incompatible ways and is not recommended for
  44. // production use. It is not subject to any SLA or deprecation policy.
  45. appProfile string
  46. }
  47. // ClientConfig has configurations for the client.
  48. type ClientConfig struct {
  49. // The id of the app profile to associate with all data operations sent from this client.
  50. // If unspecified, the default app profile for the instance will be used.
  51. AppProfile string
  52. }
  53. // NewClient creates a new Client for a given project and instance.
  54. // The default ClientConfig will be used.
  55. func NewClient(ctx context.Context, project, instance string, opts ...option.ClientOption) (*Client, error) {
  56. return NewClientWithConfig(ctx, project, instance, ClientConfig{}, opts...)
  57. }
  58. func NewClientWithConfig(ctx context.Context, project, instance string, config ClientConfig, opts ...option.ClientOption) (*Client, error) {
  59. o, err := btopt.DefaultClientOptions(prodAddr, Scope, clientUserAgent)
  60. if err != nil {
  61. return nil, err
  62. }
  63. // Default to a small connection pool that can be overridden.
  64. o = append(o,
  65. option.WithGRPCConnectionPool(4),
  66. // Set the max size to correspond to server-side limits.
  67. option.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(100<<20), grpc.MaxCallRecvMsgSize(100<<20))),
  68. // TODO(grpc/grpc-go#1388) using connection pool without WithBlock
  69. // can cause RPCs to fail randomly. We can delete this after the issue is fixed.
  70. option.WithGRPCDialOption(grpc.WithBlock()))
  71. o = append(o, opts...)
  72. conn, err := gtransport.Dial(ctx, o...)
  73. if err != nil {
  74. return nil, fmt.Errorf("dialing: %v", err)
  75. }
  76. return &Client{
  77. conn: conn,
  78. client: btpb.NewBigtableClient(conn),
  79. project: project,
  80. instance: instance,
  81. appProfile: config.AppProfile,
  82. }, nil
  83. }
  84. // Close closes the Client.
  85. func (c *Client) Close() error {
  86. return c.conn.Close()
  87. }
  88. var (
  89. idempotentRetryCodes = []codes.Code{codes.DeadlineExceeded, codes.Unavailable, codes.Aborted}
  90. isIdempotentRetryCode = make(map[codes.Code]bool)
  91. retryOptions = []gax.CallOption{
  92. gax.WithDelayTimeoutSettings(100*time.Millisecond, 2000*time.Millisecond, 1.2),
  93. gax.WithRetryCodes(idempotentRetryCodes),
  94. }
  95. )
  96. func init() {
  97. for _, code := range idempotentRetryCodes {
  98. isIdempotentRetryCode[code] = true
  99. }
  100. }
  101. func (c *Client) fullTableName(table string) string {
  102. return fmt.Sprintf("projects/%s/instances/%s/tables/%s", c.project, c.instance, table)
  103. }
  104. // A Table refers to a table.
  105. //
  106. // A Table is safe to use concurrently.
  107. type Table struct {
  108. c *Client
  109. table string
  110. // Metadata to be sent with each request.
  111. md metadata.MD
  112. }
  113. // Open opens a table.
  114. func (c *Client) Open(table string) *Table {
  115. return &Table{
  116. c: c,
  117. table: table,
  118. md: metadata.Pairs(resourcePrefixHeader, c.fullTableName(table)),
  119. }
  120. }
  121. // TODO(dsymonds): Read method that returns a sequence of ReadItems.
  122. // ReadRows reads rows from a table. f is called for each row.
  123. // If f returns false, the stream is shut down and ReadRows returns.
  124. // f owns its argument, and f is called serially in order by row key.
  125. //
  126. // By default, the yielded rows will contain all values in all cells.
  127. // Use RowFilter to limit the cells returned.
  128. func (t *Table) ReadRows(ctx context.Context, arg RowSet, f func(Row) bool, opts ...ReadOption) error {
  129. ctx = mergeOutgoingMetadata(ctx, t.md)
  130. var prevRowKey string
  131. var err error
  132. ctx = traceStartSpan(ctx, "cloud.google.com/go/bigtable.ReadRows")
  133. defer func() { traceEndSpan(ctx, err) }()
  134. attrMap := make(map[string]interface{})
  135. err = gax.Invoke(ctx, func(ctx context.Context) error {
  136. if !arg.valid() {
  137. // Empty row set, no need to make an API call.
  138. // NOTE: we must return early if arg == RowList{} because reading
  139. // an empty RowList from bigtable returns all rows from that table.
  140. return nil
  141. }
  142. req := &btpb.ReadRowsRequest{
  143. TableName: t.c.fullTableName(t.table),
  144. AppProfileId: t.c.appProfile,
  145. Rows: arg.proto(),
  146. }
  147. for _, opt := range opts {
  148. opt.set(req)
  149. }
  150. ctx, cancel := context.WithCancel(ctx) // for aborting the stream
  151. defer cancel()
  152. startTime := time.Now()
  153. stream, err := t.c.client.ReadRows(ctx, req)
  154. if err != nil {
  155. return err
  156. }
  157. cr := newChunkReader()
  158. for {
  159. res, err := stream.Recv()
  160. if err == io.EOF {
  161. break
  162. }
  163. if err != nil {
  164. // Reset arg for next Invoke call.
  165. arg = arg.retainRowsAfter(prevRowKey)
  166. attrMap["rowKey"] = prevRowKey
  167. attrMap["error"] = err.Error()
  168. attrMap["time_secs"] = time.Since(startTime).Seconds()
  169. tracePrintf(ctx, attrMap, "Retry details in ReadRows")
  170. return err
  171. }
  172. attrMap["time_secs"] = time.Since(startTime).Seconds()
  173. attrMap["rowCount"] = len(res.Chunks)
  174. tracePrintf(ctx, attrMap, "Details in ReadRows")
  175. for _, cc := range res.Chunks {
  176. row, err := cr.Process(cc)
  177. if err != nil {
  178. // No need to prepare for a retry, this is an unretryable error.
  179. return err
  180. }
  181. if row == nil {
  182. continue
  183. }
  184. prevRowKey = row.Key()
  185. if !f(row) {
  186. // Cancel and drain stream.
  187. cancel()
  188. for {
  189. if _, err := stream.Recv(); err != nil {
  190. // The stream has ended. We don't return an error
  191. // because the caller has intentionally interrupted the scan.
  192. return nil
  193. }
  194. }
  195. }
  196. }
  197. if err := cr.Close(); err != nil {
  198. // No need to prepare for a retry, this is an unretryable error.
  199. return err
  200. }
  201. }
  202. return err
  203. }, retryOptions...)
  204. return err
  205. }
  206. // ReadRow is a convenience implementation of a single-row reader.
  207. // A missing row will return a zero-length map and a nil error.
  208. func (t *Table) ReadRow(ctx context.Context, row string, opts ...ReadOption) (Row, error) {
  209. var r Row
  210. err := t.ReadRows(ctx, SingleRow(row), func(rr Row) bool {
  211. r = rr
  212. return true
  213. }, opts...)
  214. return r, err
  215. }
  216. // decodeFamilyProto adds the cell data from f to the given row.
  217. func decodeFamilyProto(r Row, row string, f *btpb.Family) {
  218. fam := f.Name // does not have colon
  219. for _, col := range f.Columns {
  220. for _, cell := range col.Cells {
  221. ri := ReadItem{
  222. Row: row,
  223. Column: fam + ":" + string(col.Qualifier),
  224. Timestamp: Timestamp(cell.TimestampMicros),
  225. Value: cell.Value,
  226. }
  227. r[fam] = append(r[fam], ri)
  228. }
  229. }
  230. }
  231. // RowSet is a set of rows to be read. It is satisfied by RowList, RowRange and RowRangeList.
  232. // The serialized size of the RowSet must be no larger than 1MiB.
  233. type RowSet interface {
  234. proto() *btpb.RowSet
  235. // retainRowsAfter returns a new RowSet that does not include the
  236. // given row key or any row key lexicographically less than it.
  237. retainRowsAfter(lastRowKey string) RowSet
  238. // Valid reports whether this set can cover at least one row.
  239. valid() bool
  240. }
  241. // RowList is a sequence of row keys.
  242. type RowList []string
  243. func (r RowList) proto() *btpb.RowSet {
  244. keys := make([][]byte, len(r))
  245. for i, row := range r {
  246. keys[i] = []byte(row)
  247. }
  248. return &btpb.RowSet{RowKeys: keys}
  249. }
  250. func (r RowList) retainRowsAfter(lastRowKey string) RowSet {
  251. var retryKeys RowList
  252. for _, key := range r {
  253. if key > lastRowKey {
  254. retryKeys = append(retryKeys, key)
  255. }
  256. }
  257. return retryKeys
  258. }
  259. func (r RowList) valid() bool {
  260. return len(r) > 0
  261. }
  262. // A RowRange is a half-open interval [Start, Limit) encompassing
  263. // all the rows with keys at least as large as Start, and less than Limit.
  264. // (Bigtable string comparison is the same as Go's.)
  265. // A RowRange can be unbounded, encompassing all keys at least as large as Start.
  266. type RowRange struct {
  267. start string
  268. limit string
  269. }
  270. // NewRange returns the new RowRange [begin, end).
  271. func NewRange(begin, end string) RowRange {
  272. return RowRange{
  273. start: begin,
  274. limit: end,
  275. }
  276. }
  277. // Unbounded tests whether a RowRange is unbounded.
  278. func (r RowRange) Unbounded() bool {
  279. return r.limit == ""
  280. }
  281. // Contains says whether the RowRange contains the key.
  282. func (r RowRange) Contains(row string) bool {
  283. return r.start <= row && (r.limit == "" || r.limit > row)
  284. }
  285. // String provides a printable description of a RowRange.
  286. func (r RowRange) String() string {
  287. a := strconv.Quote(r.start)
  288. if r.Unbounded() {
  289. return fmt.Sprintf("[%s,∞)", a)
  290. }
  291. return fmt.Sprintf("[%s,%q)", a, r.limit)
  292. }
  293. func (r RowRange) proto() *btpb.RowSet {
  294. rr := &btpb.RowRange{
  295. StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte(r.start)},
  296. }
  297. if !r.Unbounded() {
  298. rr.EndKey = &btpb.RowRange_EndKeyOpen{EndKeyOpen: []byte(r.limit)}
  299. }
  300. return &btpb.RowSet{RowRanges: []*btpb.RowRange{rr}}
  301. }
  302. func (r RowRange) retainRowsAfter(lastRowKey string) RowSet {
  303. if lastRowKey == "" || lastRowKey < r.start {
  304. return r
  305. }
  306. // Set the beginning of the range to the row after the last scanned.
  307. start := lastRowKey + "\x00"
  308. if r.Unbounded() {
  309. return InfiniteRange(start)
  310. }
  311. return NewRange(start, r.limit)
  312. }
  313. func (r RowRange) valid() bool {
  314. return r.Unbounded() || r.start < r.limit
  315. }
  316. // RowRangeList is a sequence of RowRanges representing the union of the ranges.
  317. type RowRangeList []RowRange
  318. func (r RowRangeList) proto() *btpb.RowSet {
  319. ranges := make([]*btpb.RowRange, len(r))
  320. for i, rr := range r {
  321. // RowRange.proto() returns a RowSet with a single element RowRange array
  322. ranges[i] = rr.proto().RowRanges[0]
  323. }
  324. return &btpb.RowSet{RowRanges: ranges}
  325. }
  326. func (r RowRangeList) retainRowsAfter(lastRowKey string) RowSet {
  327. if lastRowKey == "" {
  328. return r
  329. }
  330. // Return a list of any range that has not yet been completely processed
  331. var ranges RowRangeList
  332. for _, rr := range r {
  333. retained := rr.retainRowsAfter(lastRowKey)
  334. if retained.valid() {
  335. ranges = append(ranges, retained.(RowRange))
  336. }
  337. }
  338. return ranges
  339. }
  340. func (r RowRangeList) valid() bool {
  341. for _, rr := range r {
  342. if rr.valid() {
  343. return true
  344. }
  345. }
  346. return false
  347. }
  348. // SingleRow returns a RowSet for reading a single row.
  349. func SingleRow(row string) RowSet {
  350. return RowList{row}
  351. }
  352. // PrefixRange returns a RowRange consisting of all keys starting with the prefix.
  353. func PrefixRange(prefix string) RowRange {
  354. return RowRange{
  355. start: prefix,
  356. limit: prefixSuccessor(prefix),
  357. }
  358. }
  359. // InfiniteRange returns the RowRange consisting of all keys at least as
  360. // large as start.
  361. func InfiniteRange(start string) RowRange {
  362. return RowRange{
  363. start: start,
  364. limit: "",
  365. }
  366. }
  367. // prefixSuccessor returns the lexically smallest string greater than the
  368. // prefix, if it exists, or "" otherwise. In either case, it is the string
  369. // needed for the Limit of a RowRange.
  370. func prefixSuccessor(prefix string) string {
  371. if prefix == "" {
  372. return "" // infinite range
  373. }
  374. n := len(prefix)
  375. for n--; n >= 0 && prefix[n] == '\xff'; n-- {
  376. }
  377. if n == -1 {
  378. return ""
  379. }
  380. ans := []byte(prefix[:n])
  381. ans = append(ans, prefix[n]+1)
  382. return string(ans)
  383. }
  384. // A ReadOption is an optional argument to ReadRows.
  385. type ReadOption interface {
  386. set(req *btpb.ReadRowsRequest)
  387. }
  388. // RowFilter returns a ReadOption that applies f to the contents of read rows.
  389. //
  390. // If multiple RowFilters are provided, only the last is used. To combine filters,
  391. // use ChainFilters or InterleaveFilters instead.
  392. func RowFilter(f Filter) ReadOption { return rowFilter{f} }
  393. type rowFilter struct{ f Filter }
  394. func (rf rowFilter) set(req *btpb.ReadRowsRequest) { req.Filter = rf.f.proto() }
  395. // LimitRows returns a ReadOption that will limit the number of rows to be read.
  396. func LimitRows(limit int64) ReadOption { return limitRows{limit} }
  397. type limitRows struct{ limit int64 }
  398. func (lr limitRows) set(req *btpb.ReadRowsRequest) { req.RowsLimit = lr.limit }
  399. // mutationsAreRetryable returns true if all mutations are idempotent
  400. // and therefore retryable. A mutation is idempotent iff all cell timestamps
  401. // have an explicit timestamp set and do not rely on the timestamp being set on the server.
  402. func mutationsAreRetryable(muts []*btpb.Mutation) bool {
  403. serverTime := int64(ServerTime)
  404. for _, mut := range muts {
  405. setCell := mut.GetSetCell()
  406. if setCell != nil && setCell.TimestampMicros == serverTime {
  407. return false
  408. }
  409. }
  410. return true
  411. }
  412. // Apply applies a Mutation to a specific row.
  413. func (t *Table) Apply(ctx context.Context, row string, m *Mutation, opts ...ApplyOption) error {
  414. ctx = mergeOutgoingMetadata(ctx, t.md)
  415. after := func(res proto.Message) {
  416. for _, o := range opts {
  417. o.after(res)
  418. }
  419. }
  420. var err error
  421. ctx = traceStartSpan(ctx, "cloud.google.com/go/bigtable/Apply")
  422. defer func() { traceEndSpan(ctx, err) }()
  423. var callOptions []gax.CallOption
  424. if m.cond == nil {
  425. req := &btpb.MutateRowRequest{
  426. TableName: t.c.fullTableName(t.table),
  427. AppProfileId: t.c.appProfile,
  428. RowKey: []byte(row),
  429. Mutations: m.ops,
  430. }
  431. if mutationsAreRetryable(m.ops) {
  432. callOptions = retryOptions
  433. }
  434. var res *btpb.MutateRowResponse
  435. err := gax.Invoke(ctx, func(ctx context.Context) error {
  436. var err error
  437. res, err = t.c.client.MutateRow(ctx, req)
  438. return err
  439. }, callOptions...)
  440. if err == nil {
  441. after(res)
  442. }
  443. return err
  444. }
  445. req := &btpb.CheckAndMutateRowRequest{
  446. TableName: t.c.fullTableName(t.table),
  447. AppProfileId: t.c.appProfile,
  448. RowKey: []byte(row),
  449. PredicateFilter: m.cond.proto(),
  450. }
  451. if m.mtrue != nil {
  452. if m.mtrue.cond != nil {
  453. return errors.New("bigtable: conditional mutations cannot be nested")
  454. }
  455. req.TrueMutations = m.mtrue.ops
  456. }
  457. if m.mfalse != nil {
  458. if m.mfalse.cond != nil {
  459. return errors.New("bigtable: conditional mutations cannot be nested")
  460. }
  461. req.FalseMutations = m.mfalse.ops
  462. }
  463. if mutationsAreRetryable(req.TrueMutations) && mutationsAreRetryable(req.FalseMutations) {
  464. callOptions = retryOptions
  465. }
  466. var cmRes *btpb.CheckAndMutateRowResponse
  467. err = gax.Invoke(ctx, func(ctx context.Context) error {
  468. var err error
  469. cmRes, err = t.c.client.CheckAndMutateRow(ctx, req)
  470. return err
  471. }, callOptions...)
  472. if err == nil {
  473. after(cmRes)
  474. }
  475. return err
  476. }
  477. // An ApplyOption is an optional argument to Apply.
  478. type ApplyOption interface {
  479. after(res proto.Message)
  480. }
  481. type applyAfterFunc func(res proto.Message)
  482. func (a applyAfterFunc) after(res proto.Message) { a(res) }
  483. // GetCondMutationResult returns an ApplyOption that reports whether the conditional
  484. // mutation's condition matched.
  485. func GetCondMutationResult(matched *bool) ApplyOption {
  486. return applyAfterFunc(func(res proto.Message) {
  487. if res, ok := res.(*btpb.CheckAndMutateRowResponse); ok {
  488. *matched = res.PredicateMatched
  489. }
  490. })
  491. }
  492. // Mutation represents a set of changes for a single row of a table.
  493. type Mutation struct {
  494. ops []*btpb.Mutation
  495. // for conditional mutations
  496. cond Filter
  497. mtrue, mfalse *Mutation
  498. }
  499. // NewMutation returns a new mutation.
  500. func NewMutation() *Mutation {
  501. return new(Mutation)
  502. }
  503. // NewCondMutation returns a conditional mutation.
  504. // The given row filter determines which mutation is applied:
  505. // If the filter matches any cell in the row, mtrue is applied;
  506. // otherwise, mfalse is applied.
  507. // Either given mutation may be nil.
  508. func NewCondMutation(cond Filter, mtrue, mfalse *Mutation) *Mutation {
  509. return &Mutation{cond: cond, mtrue: mtrue, mfalse: mfalse}
  510. }
  511. // Set sets a value in a specified column, with the given timestamp.
  512. // The timestamp will be truncated to millisecond granularity.
  513. // A timestamp of ServerTime means to use the server timestamp.
  514. func (m *Mutation) Set(family, column string, ts Timestamp, value []byte) {
  515. m.ops = append(m.ops, &btpb.Mutation{Mutation: &btpb.Mutation_SetCell_{SetCell: &btpb.Mutation_SetCell{
  516. FamilyName: family,
  517. ColumnQualifier: []byte(column),
  518. TimestampMicros: int64(ts.TruncateToMilliseconds()),
  519. Value: value,
  520. }}})
  521. }
  522. // DeleteCellsInColumn will delete all the cells whose columns are family:column.
  523. func (m *Mutation) DeleteCellsInColumn(family, column string) {
  524. m.ops = append(m.ops, &btpb.Mutation{Mutation: &btpb.Mutation_DeleteFromColumn_{DeleteFromColumn: &btpb.Mutation_DeleteFromColumn{
  525. FamilyName: family,
  526. ColumnQualifier: []byte(column),
  527. }}})
  528. }
  529. // DeleteTimestampRange deletes all cells whose columns are family:column
  530. // and whose timestamps are in the half-open interval [start, end).
  531. // If end is zero, it will be interpreted as infinity.
  532. // The timestamps will be truncated to millisecond granularity.
  533. func (m *Mutation) DeleteTimestampRange(family, column string, start, end Timestamp) {
  534. m.ops = append(m.ops, &btpb.Mutation{Mutation: &btpb.Mutation_DeleteFromColumn_{DeleteFromColumn: &btpb.Mutation_DeleteFromColumn{
  535. FamilyName: family,
  536. ColumnQualifier: []byte(column),
  537. TimeRange: &btpb.TimestampRange{
  538. StartTimestampMicros: int64(start.TruncateToMilliseconds()),
  539. EndTimestampMicros: int64(end.TruncateToMilliseconds()),
  540. },
  541. }}})
  542. }
  543. // DeleteCellsInFamily will delete all the cells whose columns are family:*.
  544. func (m *Mutation) DeleteCellsInFamily(family string) {
  545. m.ops = append(m.ops, &btpb.Mutation{Mutation: &btpb.Mutation_DeleteFromFamily_{DeleteFromFamily: &btpb.Mutation_DeleteFromFamily{
  546. FamilyName: family,
  547. }}})
  548. }
  549. // DeleteRow deletes the entire row.
  550. func (m *Mutation) DeleteRow() {
  551. m.ops = append(m.ops, &btpb.Mutation{Mutation: &btpb.Mutation_DeleteFromRow_{DeleteFromRow: &btpb.Mutation_DeleteFromRow{}}})
  552. }
  553. // entryErr is a container that combines an entry with the error that was returned for it.
  554. // Err may be nil if no error was returned for the Entry, or if the Entry has not yet been processed.
  555. type entryErr struct {
  556. Entry *btpb.MutateRowsRequest_Entry
  557. Err error
  558. }
  559. // ApplyBulk applies multiple Mutations, up to a maximum of 100,000.
  560. // Each mutation is individually applied atomically,
  561. // but the set of mutations may be applied in any order.
  562. //
  563. // Two types of failures may occur. If the entire process
  564. // fails, (nil, err) will be returned. If specific mutations
  565. // fail to apply, ([]err, nil) will be returned, and the errors
  566. // will correspond to the relevant rowKeys/muts arguments.
  567. //
  568. // Conditional mutations cannot be applied in bulk and providing one will result in an error.
  569. func (t *Table) ApplyBulk(ctx context.Context, rowKeys []string, muts []*Mutation, opts ...ApplyOption) ([]error, error) {
  570. ctx = mergeOutgoingMetadata(ctx, t.md)
  571. if len(rowKeys) != len(muts) {
  572. return nil, fmt.Errorf("mismatched rowKeys and mutation array lengths: %d, %d", len(rowKeys), len(muts))
  573. }
  574. origEntries := make([]*entryErr, len(rowKeys))
  575. for i, key := range rowKeys {
  576. mut := muts[i]
  577. if mut.cond != nil {
  578. return nil, errors.New("conditional mutations cannot be applied in bulk")
  579. }
  580. origEntries[i] = &entryErr{Entry: &btpb.MutateRowsRequest_Entry{RowKey: []byte(key), Mutations: mut.ops}}
  581. }
  582. // entries will be reduced after each invocation to just what needs to be retried.
  583. entries := make([]*entryErr, len(rowKeys))
  584. copy(entries, origEntries)
  585. var err error
  586. ctx = traceStartSpan(ctx, "cloud.google.com/go/bigtable/ApplyBulk")
  587. defer func() { traceEndSpan(ctx, err) }()
  588. attrMap := make(map[string]interface{})
  589. err = gax.Invoke(ctx, func(ctx context.Context) error {
  590. attrMap["rowCount"] = len(entries)
  591. tracePrintf(ctx, attrMap, "Row count in ApplyBulk")
  592. err := t.doApplyBulk(ctx, entries, opts...)
  593. if err != nil {
  594. // We want to retry the entire request with the current entries
  595. return err
  596. }
  597. entries = t.getApplyBulkRetries(entries)
  598. if len(entries) > 0 && len(idempotentRetryCodes) > 0 {
  599. // We have at least one mutation that needs to be retried.
  600. // Return an arbitrary error that is retryable according to callOptions.
  601. return status.Errorf(idempotentRetryCodes[0], "Synthetic error: partial failure of ApplyBulk")
  602. }
  603. return nil
  604. }, retryOptions...)
  605. if err != nil {
  606. return nil, err
  607. }
  608. // Accumulate all of the errors into an array to return, interspersed with nils for successful
  609. // entries. The absence of any errors means we should return nil.
  610. var errs []error
  611. var foundErr bool
  612. for _, entry := range origEntries {
  613. if entry.Err != nil {
  614. foundErr = true
  615. }
  616. errs = append(errs, entry.Err)
  617. }
  618. if foundErr {
  619. return errs, nil
  620. }
  621. return nil, nil
  622. }
  623. // getApplyBulkRetries returns the entries that need to be retried
  624. func (t *Table) getApplyBulkRetries(entries []*entryErr) []*entryErr {
  625. var retryEntries []*entryErr
  626. for _, entry := range entries {
  627. err := entry.Err
  628. if err != nil && isIdempotentRetryCode[grpc.Code(err)] && mutationsAreRetryable(entry.Entry.Mutations) {
  629. // There was an error and the entry is retryable.
  630. retryEntries = append(retryEntries, entry)
  631. }
  632. }
  633. return retryEntries
  634. }
  635. // doApplyBulk does the work of a single ApplyBulk invocation
  636. func (t *Table) doApplyBulk(ctx context.Context, entryErrs []*entryErr, opts ...ApplyOption) error {
  637. after := func(res proto.Message) {
  638. for _, o := range opts {
  639. o.after(res)
  640. }
  641. }
  642. entries := make([]*btpb.MutateRowsRequest_Entry, len(entryErrs))
  643. for i, entryErr := range entryErrs {
  644. entries[i] = entryErr.Entry
  645. }
  646. req := &btpb.MutateRowsRequest{
  647. TableName: t.c.fullTableName(t.table),
  648. AppProfileId: t.c.appProfile,
  649. Entries: entries,
  650. }
  651. stream, err := t.c.client.MutateRows(ctx, req)
  652. if err != nil {
  653. return err
  654. }
  655. for {
  656. res, err := stream.Recv()
  657. if err == io.EOF {
  658. break
  659. }
  660. if err != nil {
  661. return err
  662. }
  663. for i, entry := range res.Entries {
  664. s := entry.Status
  665. if s.Code == int32(codes.OK) {
  666. entryErrs[i].Err = nil
  667. } else {
  668. entryErrs[i].Err = status.Errorf(codes.Code(s.Code), s.Message)
  669. }
  670. }
  671. after(res)
  672. }
  673. return nil
  674. }
  675. // Timestamp is in units of microseconds since 1 January 1970.
  676. type Timestamp int64
  677. // ServerTime is a specific Timestamp that may be passed to (*Mutation).Set.
  678. // It indicates that the server's timestamp should be used.
  679. const ServerTime Timestamp = -1
  680. // Time converts a time.Time into a Timestamp.
  681. func Time(t time.Time) Timestamp { return Timestamp(t.UnixNano() / 1e3) }
  682. // Now returns the Timestamp representation of the current time on the client.
  683. func Now() Timestamp { return Time(time.Now()) }
  684. // Time converts a Timestamp into a time.Time.
  685. func (ts Timestamp) Time() time.Time { return time.Unix(0, int64(ts)*1e3) }
  686. // TruncateToMilliseconds truncates a Timestamp to millisecond granularity,
  687. // which is currently the only granularity supported.
  688. func (ts Timestamp) TruncateToMilliseconds() Timestamp {
  689. if ts == ServerTime {
  690. return ts
  691. }
  692. return ts - ts%1000
  693. }
  694. // ApplyReadModifyWrite applies a ReadModifyWrite to a specific row.
  695. // It returns the newly written cells.
  696. func (t *Table) ApplyReadModifyWrite(ctx context.Context, row string, m *ReadModifyWrite) (Row, error) {
  697. ctx = mergeOutgoingMetadata(ctx, t.md)
  698. req := &btpb.ReadModifyWriteRowRequest{
  699. TableName: t.c.fullTableName(t.table),
  700. AppProfileId: t.c.appProfile,
  701. RowKey: []byte(row),
  702. Rules: m.ops,
  703. }
  704. res, err := t.c.client.ReadModifyWriteRow(ctx, req)
  705. if err != nil {
  706. return nil, err
  707. }
  708. if res.Row == nil {
  709. return nil, errors.New("unable to apply ReadModifyWrite: res.Row=nil")
  710. }
  711. r := make(Row)
  712. for _, fam := range res.Row.Families { // res is *btpb.Row, fam is *btpb.Family
  713. decodeFamilyProto(r, row, fam)
  714. }
  715. return r, nil
  716. }
  717. // ReadModifyWrite represents a set of operations on a single row of a table.
  718. // It is like Mutation but for non-idempotent changes.
  719. // When applied, these operations operate on the latest values of the row's cells,
  720. // and result in a new value being written to the relevant cell with a timestamp
  721. // that is max(existing timestamp, current server time).
  722. //
  723. // The application of a ReadModifyWrite is atomic; concurrent ReadModifyWrites will
  724. // be executed serially by the server.
  725. type ReadModifyWrite struct {
  726. ops []*btpb.ReadModifyWriteRule
  727. }
  728. // NewReadModifyWrite returns a new ReadModifyWrite.
  729. func NewReadModifyWrite() *ReadModifyWrite { return new(ReadModifyWrite) }
  730. // AppendValue appends a value to a specific cell's value.
  731. // If the cell is unset, it will be treated as an empty value.
  732. func (m *ReadModifyWrite) AppendValue(family, column string, v []byte) {
  733. m.ops = append(m.ops, &btpb.ReadModifyWriteRule{
  734. FamilyName: family,
  735. ColumnQualifier: []byte(column),
  736. Rule: &btpb.ReadModifyWriteRule_AppendValue{AppendValue: v},
  737. })
  738. }
  739. // Increment interprets the value in a specific cell as a 64-bit big-endian signed integer,
  740. // and adds a value to it. If the cell is unset, it will be treated as zero.
  741. // If the cell is set and is not an 8-byte value, the entire ApplyReadModifyWrite
  742. // operation will fail.
  743. func (m *ReadModifyWrite) Increment(family, column string, delta int64) {
  744. m.ops = append(m.ops, &btpb.ReadModifyWriteRule{
  745. FamilyName: family,
  746. ColumnQualifier: []byte(column),
  747. Rule: &btpb.ReadModifyWriteRule_IncrementAmount{IncrementAmount: delta},
  748. })
  749. }
  750. // mergeOutgoingMetadata returns a context populated by the existing outgoing metadata,
  751. // if any, joined with internal metadata.
  752. func mergeOutgoingMetadata(ctx context.Context, md metadata.MD) context.Context {
  753. mdCopy, _ := metadata.FromOutgoingContext(ctx)
  754. return metadata.NewOutgoingContext(ctx, metadata.Join(mdCopy, md))
  755. }
  756. func (t *Table) SampleRowKeys(ctx context.Context) ([]string, error) {
  757. ctx = mergeOutgoingMetadata(ctx, t.md)
  758. var sampledRowKeys []string
  759. err := gax.Invoke(ctx, func(ctx context.Context) error {
  760. sampledRowKeys = nil
  761. req := &btpb.SampleRowKeysRequest{
  762. TableName: t.c.fullTableName(t.table),
  763. AppProfileId: t.c.appProfile,
  764. }
  765. ctx, cancel := context.WithCancel(ctx) // for aborting the stream
  766. defer cancel()
  767. stream, err := t.c.client.SampleRowKeys(ctx, req)
  768. if err != nil {
  769. return err
  770. }
  771. for {
  772. res, err := stream.Recv()
  773. if err == io.EOF {
  774. break
  775. }
  776. if err != nil {
  777. return err
  778. }
  779. key := string(res.RowKey)
  780. if key == "" {
  781. continue
  782. }
  783. sampledRowKeys = append(sampledRowKeys, key)
  784. }
  785. return nil
  786. }, retryOptions...)
  787. return sampledRowKeys, err
  788. }