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.

216 lines
5.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
internal/proxy: add initial set of abci metrics (#7115) This PR adds an initial set of metrics for use ABCI. The initial metrics enable the calculation of timing histograms and call counts for each of the ABCI methods. The metrics are also labeled as either 'sync' or 'async' to determine if the method call was performed using ABCI's `*Async` methods. An example of these metrics is included here for reference: ``` tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0001"} 0 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0004"} 5 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.002"} 12 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.009"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.02"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.1"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.65"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="2"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="6"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="25"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="+Inf"} 13 tendermint_abci_connection_method_timing_sum{chain_id="ci",method="commit",type="sync"} 0.007802058000000001 tendermint_abci_connection_method_timing_count{chain_id="ci",method="commit",type="sync"} 13 ``` These metrics can easily be graphed using prometheus's `histogram_quantile(...)` method to pick out a particular quantile to graph or examine. I chose buckets that were somewhat of an estimate of expected range of times for ABCI operations. They start at .0001 seconds and range to 25 seconds. The hope is that this range captures enough possible times to be useful for us and operators.
3 years ago
internal/proxy: add initial set of abci metrics (#7115) This PR adds an initial set of metrics for use ABCI. The initial metrics enable the calculation of timing histograms and call counts for each of the ABCI methods. The metrics are also labeled as either 'sync' or 'async' to determine if the method call was performed using ABCI's `*Async` methods. An example of these metrics is included here for reference: ``` tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0001"} 0 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0004"} 5 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.002"} 12 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.009"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.02"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.1"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.65"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="2"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="6"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="25"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="+Inf"} 13 tendermint_abci_connection_method_timing_sum{chain_id="ci",method="commit",type="sync"} 0.007802058000000001 tendermint_abci_connection_method_timing_count{chain_id="ci",method="commit",type="sync"} 13 ``` These metrics can easily be graphed using prometheus's `histogram_quantile(...)` method to pick out a particular quantile to graph or examine. I chose buckets that were somewhat of an estimate of expected range of times for ABCI operations. They start at .0001 seconds and range to 25 seconds. The hope is that this range captures enough possible times to be useful for us and operators.
3 years ago
8 years ago
8 years ago
8 years ago
  1. package proxy
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "syscall"
  7. abciclient "github.com/tendermint/tendermint/abci/client"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/libs/service"
  10. )
  11. const (
  12. connConsensus = "consensus"
  13. connMempool = "mempool"
  14. connQuery = "query"
  15. connSnapshot = "snapshot"
  16. )
  17. // AppConns is the Tendermint's interface to the application that consists of
  18. // multiple connections.
  19. type AppConns interface {
  20. service.Service
  21. // Mempool connection
  22. Mempool() AppConnMempool
  23. // Consensus connection
  24. Consensus() AppConnConsensus
  25. // Query connection
  26. Query() AppConnQuery
  27. // Snapshot connection
  28. Snapshot() AppConnSnapshot
  29. }
  30. // NewAppConns calls NewMultiAppConn.
  31. func NewAppConns(clientCreator abciclient.Creator, logger log.Logger, metrics *Metrics) AppConns {
  32. return NewMultiAppConn(clientCreator, logger, metrics)
  33. }
  34. // multiAppConn implements AppConns.
  35. //
  36. // A multiAppConn is made of a few appConns and manages their underlying abci
  37. // clients.
  38. // TODO: on app restart, clients must reboot together
  39. type multiAppConn struct {
  40. service.BaseService
  41. logger log.Logger
  42. metrics *Metrics
  43. consensusConn AppConnConsensus
  44. mempoolConn AppConnMempool
  45. queryConn AppConnQuery
  46. snapshotConn AppConnSnapshot
  47. consensusConnClient stoppableClient
  48. mempoolConnClient stoppableClient
  49. queryConnClient stoppableClient
  50. snapshotConnClient stoppableClient
  51. clientCreator abciclient.Creator
  52. }
  53. // TODO: this is a totally internal and quasi permanent shim for
  54. // clients. eventually we can have a single client and have some kind
  55. // of reasonable lifecycle witout needing an explicit stop method.
  56. type stoppableClient interface {
  57. abciclient.Client
  58. Stop()
  59. }
  60. // NewMultiAppConn makes all necessary abci connections to the application.
  61. func NewMultiAppConn(clientCreator abciclient.Creator, logger log.Logger, metrics *Metrics) AppConns {
  62. multiAppConn := &multiAppConn{
  63. logger: logger,
  64. metrics: metrics,
  65. clientCreator: clientCreator,
  66. }
  67. multiAppConn.BaseService = *service.NewBaseService(logger, "multiAppConn", multiAppConn)
  68. return multiAppConn
  69. }
  70. func (app *multiAppConn) Mempool() AppConnMempool { return app.mempoolConn }
  71. func (app *multiAppConn) Consensus() AppConnConsensus { return app.consensusConn }
  72. func (app *multiAppConn) Query() AppConnQuery { return app.queryConn }
  73. func (app *multiAppConn) Snapshot() AppConnSnapshot { return app.snapshotConn }
  74. func (app *multiAppConn) OnStart(ctx context.Context) error {
  75. var err error
  76. defer func() {
  77. if err != nil {
  78. app.stopAllClients()
  79. }
  80. }()
  81. app.queryConnClient, err = app.abciClientFor(ctx, connQuery)
  82. if err != nil {
  83. return err
  84. }
  85. app.queryConn = NewAppConnQuery(app.queryConnClient, app.metrics)
  86. app.snapshotConnClient, err = app.abciClientFor(ctx, connSnapshot)
  87. if err != nil {
  88. return err
  89. }
  90. app.snapshotConn = NewAppConnSnapshot(app.snapshotConnClient, app.metrics)
  91. app.mempoolConnClient, err = app.abciClientFor(ctx, connMempool)
  92. if err != nil {
  93. return err
  94. }
  95. app.mempoolConn = NewAppConnMempool(app.mempoolConnClient, app.metrics)
  96. app.consensusConnClient, err = app.abciClientFor(ctx, connConsensus)
  97. if err != nil {
  98. return err
  99. }
  100. app.consensusConn = NewAppConnConsensus(app.consensusConnClient, app.metrics)
  101. // Kill Tendermint if the ABCI application crashes.
  102. app.startWatchersForClientErrorToKillTendermint(ctx)
  103. return nil
  104. }
  105. func (app *multiAppConn) OnStop() { app.stopAllClients() }
  106. func (app *multiAppConn) startWatchersForClientErrorToKillTendermint(ctx context.Context) {
  107. // this function starts a number of threads (per abci client)
  108. // that will SIGTERM's our own PID if any of the ABCI clients
  109. // exit/return early. If the context is canceled then these
  110. // functions will not kill tendermint.
  111. killFn := func(conn string, err error, logger log.Logger) {
  112. logger.Error(
  113. fmt.Sprintf("%s connection terminated. Did the application crash? Please restart tendermint", conn),
  114. "err", err)
  115. if killErr := kill(); killErr != nil {
  116. logger.Error("Failed to kill this process - please do so manually", "err", killErr)
  117. }
  118. }
  119. for _, client := range []struct {
  120. connClient stoppableClient
  121. name string
  122. }{
  123. {
  124. connClient: app.consensusConnClient,
  125. name: connConsensus,
  126. },
  127. {
  128. connClient: app.mempoolConnClient,
  129. name: connMempool,
  130. },
  131. {
  132. connClient: app.queryConnClient,
  133. name: connQuery,
  134. },
  135. {
  136. connClient: app.snapshotConnClient,
  137. name: connSnapshot,
  138. },
  139. } {
  140. go func(name string, client stoppableClient) {
  141. client.Wait()
  142. if ctx.Err() != nil {
  143. return
  144. }
  145. if err := client.Error(); err != nil {
  146. killFn(name, err, app.logger)
  147. }
  148. }(client.name, client.connClient)
  149. }
  150. }
  151. func (app *multiAppConn) stopAllClients() {
  152. for _, client := range []stoppableClient{
  153. app.consensusConnClient,
  154. app.mempoolConnClient,
  155. app.queryConnClient,
  156. app.snapshotConnClient,
  157. } {
  158. if client != nil {
  159. client.Stop()
  160. }
  161. }
  162. }
  163. func (app *multiAppConn) abciClientFor(ctx context.Context, conn string) (stoppableClient, error) {
  164. c, err := app.clientCreator(app.logger.With(
  165. "module", "abci-client",
  166. "connection", conn))
  167. if err != nil {
  168. return nil, fmt.Errorf("error creating ABCI client (%s connection): %w", conn, err)
  169. }
  170. if err := c.Start(ctx); err != nil {
  171. return nil, fmt.Errorf("error starting ABCI client (%s connection): %w", conn, err)
  172. }
  173. client, ok := c.(stoppableClient)
  174. if !ok {
  175. return nil, fmt.Errorf("%T is not a stoppable client", c)
  176. }
  177. return client, nil
  178. }
  179. func kill() error {
  180. p, err := os.FindProcess(os.Getpid())
  181. if err != nil {
  182. return err
  183. }
  184. return p.Signal(syscall.SIGTERM)
  185. }