tree: 7d5b689de4d15af559d6e278cb27e210cc7a8170 [path history] [tgz]
  1. admin/
  2. aliasshim/
  3. apiv1/
  4. executor/
  5. internal/
  6. kokoro/
  7. spannertest/
  8. spansql/
  9. test/
  10. batch.go
  11. batch_test.go
  12. big_pdml_test.go
  13. CHANGES.md
  14. client.go
  15. client_benchmarks_test.go
  16. client_test.go
  17. cmp_test.go
  18. doc.go
  19. emulator_test.sh
  20. errors.go
  21. errors112.go
  22. errors113.go
  23. errors_test.go
  24. examples_test.go
  25. go.mod
  26. go.sum
  27. integration_test.go
  28. key.go
  29. key_test.go
  30. mocks_test.go
  31. mutation.go
  32. mutation_test.go
  33. oc_test.go
  34. ot_metrics.go
  35. pdml.go
  36. pdml_test.go
  37. protoutils.go
  38. read.go
  39. read_test.go
  40. README.md
  41. retry.go
  42. retry_test.go
  43. row.go
  44. row_test.go
  45. session.go
  46. session_test.go
  47. sessionclient.go
  48. sessionclient_test.go
  49. statement.go
  50. statement_test.go
  51. stats.go
  52. timestampbound.go
  53. timestampbound_test.go
  54. transaction.go
  55. transaction_test.go
  56. value.go
  57. value_benchmarks_test.go
  58. value_test.go
spanner/README.md

Cloud Spanner Go Reference

Example Usage

First create a spanner.Client to use throughout your application:

client, err := spanner.NewClient(ctx, "projects/P/instances/I/databases/D")
if err != nil {
	log.Fatal(err)
}
// Simple Reads And Writes
_, err = client.Apply(ctx, []*spanner.Mutation{
	spanner.Insert("Users",
		[]string{"name", "email"},
		[]interface{}{"alice", "a@example.com"})})
if err != nil {
	log.Fatal(err)
}
row, err := client.Single().ReadRow(ctx, "Users",
	spanner.Key{"alice"}, []string{"email"})
if err != nil {
	log.Fatal(err)
}

Session Leak

A Client object of the Client Library has a limit on the number of maximum sessions. For example the default value of MaxOpened, which is the maximum number of sessions allowed by the session pool in the Golang Client Library, is 400. You can configure these values at the time of creating a Client by passing custom SessionPoolConfig as part of ClientConfig. When all the sessions are checked out of the session pool, every new transaction has to wait until a session is returned to the pool. If a session is never returned to the pool (hence causing a session leak), the transactions will have to wait indefinitely and your application will be blocked.

Common Root Causes

The most common reason for session leaks in the Golang client library are:

  1. Not stopping a RowIterator that is returned by Query, Read and other methods. Always use RowIterator.Stop() to ensure that the RowIterator is always closed.
  2. Not closing a ReadOnlyTransaction when you no longer need it. Always call ReadOnlyTransaction.Close() after use, to ensure that the ReadOnlyTransaction is always closed.

As shown in the example below, the txn.Close() statement releases the session after it is complete. If you fail to call txn.Close(), the session is not released back to the pool. The recommended way is to use defer as shown below.

client, err := spanner.NewClient(ctx, "projects/P/instances/I/databases/D")
if err != nil {
  log.Fatal(err)
}
txn := client.ReadOnlyTransaction()
defer txn.Close()

Debugging and Resolving Session Leaks

Logging inactive transactions

This option logs warnings when you have exhausted >95% of your session pool. It is enabled by default. This could mean two things; either you need to increase the max sessions in your session pool (as the number of queries run using the client side database object is greater than your session pool can serve), or you may have a session leak. To help debug which transactions may be causing this session leak, the logs will also contain stack traces of transactions which have been running longer than expected if TrackSessionHandles under SessionPoolConfig is enabled.

sessionPoolConfig := spanner.SessionPoolConfig{
    TrackSessionHandles: true,
    InactiveTransactionRemovalOptions: spanner.InactiveTransactionRemovalOptions{
      ActionOnInactiveTransaction: spanner.Warn,
    },
}
client, err := spanner.NewClientWithConfig(
	ctx, database, spanner.ClientConfig{SessionPoolConfig: sessionPoolConfig},
)
if err != nil {
	log.Fatal(err)
}
defer client.Close()

// Example Log message to warn presence of long running transactions
// session <session-info> checked out of pool at <session-checkout-time> is long running due to possible session leak for goroutine
// <Stack Trace of transaction>

Automatically clean inactive transactions

When the option to automatically clean inactive transactions is enabled, the client library will automatically detect problematic transactions that are running for a very long time (thus causing session leaks) and close them. The session will be removed from the pool and be replaced by a new session. To dig deeper into which transactions are being closed, you can check the logs to see the stack trace of the transactions which might be causing these leaks and further debug them.

sessionPoolConfig := spanner.SessionPoolConfig{
    TrackSessionHandles: true,
    InactiveTransactionRemovalOptions: spanner.InactiveTransactionRemovalOptions{
      ActionOnInactiveTransaction: spanner.WarnAndClose,
    },
}
client, err := spanner.NewClientWithConfig(
	ctx, database, spanner.ClientConfig{SessionPoolConfig: sessionPoolConfig},
)
if err != nil {
log.Fatal(err)
}
defer client.Close()

// Example Log message for when transaction is recycled
// session <session-info> checked out of pool at <session-checkout-time> is long running and will be removed due to possible session leak for goroutine 
// <Stack Trace of transaction>