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.

501 lines
14 KiB

8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748) * use READ lock/unlock in ConsensusState#GetLastHeight Refs #2721 * do not use defers when there's no need * fix peer formatting (output its address instead of the pointer) ``` [54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout" ``` https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581 * panic if peer has no state https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165 It's confusing that sometimes we check if peer has a state, but most of the times we expect it to be there 1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138 2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited) I will change everything to always assume peer has a state and panic otherwise that should help identify issues earlier * abci/localclient: extend lock on app callback App callback should be protected by lock as well (note this was already done for InitChainAsync, why not for others???). Otherwise, when we execute the block, tx might come in and call the callback in the same time we're updating it in execBlockOnProxyApp => DATA RACE Fixes #2721 Consensus state is locked ``` goroutine 113333 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*RWMutex).RLock(0xc001800090) /usr/local/go/src/sync/rwmutex.go:50 +0x4e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248 9c0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236 ``` because localClient is locked ``` goroutine 1899 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670 created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132 ``` tx comes in and CheckTx is executed right when we execute the block ``` goroutine 111044 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0x0) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8 reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...) /usr/local/go/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344) /usr/local/go/src/reflect/value.go:308 +0xa4 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188 net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2361 +0x127 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394 net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2741 +0xab net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0) /usr/local/go/src/net/http/server.go:1847 +0x646 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2851 +0x2f5 ``` * consensus: use read lock in Receive#VoteMessage * use defer to unlock mutex because application might panic * use defer in every method of the localClient * add a changelog entry * drain channels before Unsubscribe(All) Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13 for the detailed explanation of the issue. We'll need to fix it someday. Make sure to keep an eye on https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md * retry instead of panic when peer has no state in reactors other than consensus in /dump_consensus_state RPC endpoint, skip a peer with no state * rpc/core/mempool: simplify error messages * rpc/core/mempool: use time.After instead of timer also, do not log DeliverTx result (to be consistent with other memthods) * unlock before calling the callback in reqRes#SetCallback
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. package abcicli
  2. import (
  3. "bufio"
  4. "container/list"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "reflect"
  10. "time"
  11. "github.com/tendermint/tendermint/abci/types"
  12. tmnet "github.com/tendermint/tendermint/libs/net"
  13. "github.com/tendermint/tendermint/libs/service"
  14. tmsync "github.com/tendermint/tendermint/libs/sync"
  15. "github.com/tendermint/tendermint/libs/timer"
  16. )
  17. const (
  18. reqQueueSize = 256 // TODO make configurable
  19. flushThrottleMS = 20 // Don't wait longer than...
  20. )
  21. // This is goroutine-safe, but users should beware that the application in
  22. // general is not meant to be interfaced with concurrent callers.
  23. type socketClient struct {
  24. service.BaseService
  25. addr string
  26. mustConnect bool
  27. conn net.Conn
  28. reqQueue chan *ReqRes
  29. flushTimer *timer.ThrottleTimer
  30. mtx tmsync.Mutex
  31. err error
  32. reqSent *list.List // list of requests sent, waiting for response
  33. resCb func(*types.Request, *types.Response) // called on all requests, if set.
  34. }
  35. var _ Client = (*socketClient)(nil)
  36. // NewSocketClient creates a new socket client, which connects to a given
  37. // address. If mustConnect is true, the client will return an error upon start
  38. // if it fails to connect.
  39. func NewSocketClient(addr string, mustConnect bool) Client {
  40. cli := &socketClient{
  41. reqQueue: make(chan *ReqRes, reqQueueSize),
  42. flushTimer: timer.NewThrottleTimer("socketClient", flushThrottleMS),
  43. mustConnect: mustConnect,
  44. addr: addr,
  45. reqSent: list.New(),
  46. resCb: nil,
  47. }
  48. cli.BaseService = *service.NewBaseService(nil, "socketClient", cli)
  49. return cli
  50. }
  51. // OnStart implements Service by connecting to the server and spawning reading
  52. // and writing goroutines.
  53. func (cli *socketClient) OnStart() error {
  54. var (
  55. err error
  56. conn net.Conn
  57. )
  58. for {
  59. conn, err = tmnet.Connect(cli.addr)
  60. if err != nil {
  61. if cli.mustConnect {
  62. return err
  63. }
  64. cli.Logger.Error(fmt.Sprintf("abci.socketClient failed to connect to %v. Retrying after %vs...",
  65. cli.addr, dialRetryIntervalSeconds), "err", err)
  66. time.Sleep(time.Second * dialRetryIntervalSeconds)
  67. continue
  68. }
  69. cli.conn = conn
  70. go cli.sendRequestsRoutine(conn)
  71. go cli.recvResponseRoutine(conn)
  72. return nil
  73. }
  74. }
  75. // OnStop implements Service by closing connection and flushing all queues.
  76. func (cli *socketClient) OnStop() {
  77. if cli.conn != nil {
  78. cli.conn.Close()
  79. }
  80. cli.flushQueue()
  81. cli.flushTimer.Stop()
  82. }
  83. // Error returns an error if the client was stopped abruptly.
  84. func (cli *socketClient) Error() error {
  85. cli.mtx.Lock()
  86. defer cli.mtx.Unlock()
  87. return cli.err
  88. }
  89. // SetResponseCallback sets a callback, which will be executed for each
  90. // non-error & non-empty response from the server.
  91. //
  92. // NOTE: callback may get internally generated flush responses.
  93. func (cli *socketClient) SetResponseCallback(resCb Callback) {
  94. cli.mtx.Lock()
  95. cli.resCb = resCb
  96. cli.mtx.Unlock()
  97. }
  98. //----------------------------------------
  99. func (cli *socketClient) sendRequestsRoutine(conn io.Writer) {
  100. w := bufio.NewWriter(conn)
  101. for {
  102. select {
  103. case reqres := <-cli.reqQueue:
  104. // cli.Logger.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request)
  105. cli.willSendReq(reqres)
  106. err := types.WriteMessage(reqres.Request, w)
  107. if err != nil {
  108. cli.stopForError(fmt.Errorf("write to buffer: %w", err))
  109. return
  110. }
  111. // If it's a flush request, flush the current buffer.
  112. if _, ok := reqres.Request.Value.(*types.Request_Flush); ok {
  113. err = w.Flush()
  114. if err != nil {
  115. cli.stopForError(fmt.Errorf("flush buffer: %w", err))
  116. return
  117. }
  118. }
  119. case <-cli.flushTimer.Ch: // flush queue
  120. select {
  121. case cli.reqQueue <- NewReqRes(types.ToRequestFlush()):
  122. default:
  123. // Probably will fill the buffer, or retry later.
  124. }
  125. case <-cli.Quit():
  126. return
  127. }
  128. }
  129. }
  130. func (cli *socketClient) recvResponseRoutine(conn io.Reader) {
  131. r := bufio.NewReader(conn)
  132. for {
  133. var res = &types.Response{}
  134. err := types.ReadMessage(r, res)
  135. if err != nil {
  136. cli.stopForError(fmt.Errorf("read message: %w", err))
  137. return
  138. }
  139. // cli.Logger.Debug("Received response", "responseType", reflect.TypeOf(res), "response", res)
  140. switch r := res.Value.(type) {
  141. case *types.Response_Exception: // app responded with error
  142. // XXX After setting cli.err, release waiters (e.g. reqres.Done())
  143. cli.stopForError(errors.New(r.Exception.Error))
  144. return
  145. default:
  146. err := cli.didRecvResponse(res)
  147. if err != nil {
  148. cli.stopForError(err)
  149. return
  150. }
  151. }
  152. }
  153. }
  154. func (cli *socketClient) willSendReq(reqres *ReqRes) {
  155. cli.mtx.Lock()
  156. defer cli.mtx.Unlock()
  157. cli.reqSent.PushBack(reqres)
  158. }
  159. func (cli *socketClient) didRecvResponse(res *types.Response) error {
  160. cli.mtx.Lock()
  161. defer cli.mtx.Unlock()
  162. // Get the first ReqRes.
  163. next := cli.reqSent.Front()
  164. if next == nil {
  165. return fmt.Errorf("unexpected %v when nothing expected", reflect.TypeOf(res.Value))
  166. }
  167. reqres := next.Value.(*ReqRes)
  168. if !resMatchesReq(reqres.Request, res) {
  169. return fmt.Errorf("unexpected %v when response to %v expected",
  170. reflect.TypeOf(res.Value), reflect.TypeOf(reqres.Request.Value))
  171. }
  172. reqres.Response = res
  173. reqres.Done() // release waiters
  174. cli.reqSent.Remove(next) // pop first item from linked list
  175. // Notify client listener if set (global callback).
  176. if cli.resCb != nil {
  177. cli.resCb(reqres.Request, res)
  178. }
  179. // Notify reqRes listener if set (request specific callback).
  180. //
  181. // NOTE: It is possible this callback isn't set on the reqres object. At this
  182. // point, in which case it will be called after, when it is set.
  183. if cb := reqres.GetCallback(); cb != nil {
  184. cb(res)
  185. }
  186. return nil
  187. }
  188. //----------------------------------------
  189. func (cli *socketClient) EchoAsync(msg string) *ReqRes {
  190. return cli.queueRequest(types.ToRequestEcho(msg))
  191. }
  192. func (cli *socketClient) FlushAsync() *ReqRes {
  193. return cli.queueRequest(types.ToRequestFlush())
  194. }
  195. func (cli *socketClient) InfoAsync(req types.RequestInfo) *ReqRes {
  196. return cli.queueRequest(types.ToRequestInfo(req))
  197. }
  198. func (cli *socketClient) DeliverTxAsync(req types.RequestDeliverTx) *ReqRes {
  199. return cli.queueRequest(types.ToRequestDeliverTx(req))
  200. }
  201. func (cli *socketClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes {
  202. return cli.queueRequest(types.ToRequestCheckTx(req))
  203. }
  204. func (cli *socketClient) QueryAsync(req types.RequestQuery) *ReqRes {
  205. return cli.queueRequest(types.ToRequestQuery(req))
  206. }
  207. func (cli *socketClient) CommitAsync() *ReqRes {
  208. return cli.queueRequest(types.ToRequestCommit())
  209. }
  210. func (cli *socketClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
  211. return cli.queueRequest(types.ToRequestInitChain(req))
  212. }
  213. func (cli *socketClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
  214. return cli.queueRequest(types.ToRequestBeginBlock(req))
  215. }
  216. func (cli *socketClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
  217. return cli.queueRequest(types.ToRequestEndBlock(req))
  218. }
  219. func (cli *socketClient) ListSnapshotsAsync(req types.RequestListSnapshots) *ReqRes {
  220. return cli.queueRequest(types.ToRequestListSnapshots(req))
  221. }
  222. func (cli *socketClient) OfferSnapshotAsync(req types.RequestOfferSnapshot) *ReqRes {
  223. return cli.queueRequest(types.ToRequestOfferSnapshot(req))
  224. }
  225. func (cli *socketClient) LoadSnapshotChunkAsync(req types.RequestLoadSnapshotChunk) *ReqRes {
  226. return cli.queueRequest(types.ToRequestLoadSnapshotChunk(req))
  227. }
  228. func (cli *socketClient) ApplySnapshotChunkAsync(req types.RequestApplySnapshotChunk) *ReqRes {
  229. return cli.queueRequest(types.ToRequestApplySnapshotChunk(req))
  230. }
  231. //----------------------------------------
  232. func (cli *socketClient) FlushSync() error {
  233. reqRes := cli.queueRequest(types.ToRequestFlush())
  234. if err := cli.Error(); err != nil {
  235. return err
  236. }
  237. reqRes.Wait() // NOTE: if we don't flush the queue, its possible to get stuck here
  238. return cli.Error()
  239. }
  240. func (cli *socketClient) EchoSync(msg string) (*types.ResponseEcho, error) {
  241. reqres := cli.queueRequest(types.ToRequestEcho(msg))
  242. if err := cli.FlushSync(); err != nil {
  243. return nil, err
  244. }
  245. return reqres.Response.GetEcho(), cli.Error()
  246. }
  247. func (cli *socketClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
  248. reqres := cli.queueRequest(types.ToRequestInfo(req))
  249. if err := cli.FlushSync(); err != nil {
  250. return nil, err
  251. }
  252. return reqres.Response.GetInfo(), cli.Error()
  253. }
  254. func (cli *socketClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) {
  255. reqres := cli.queueRequest(types.ToRequestDeliverTx(req))
  256. if err := cli.FlushSync(); err != nil {
  257. return nil, err
  258. }
  259. return reqres.Response.GetDeliverTx(), cli.Error()
  260. }
  261. func (cli *socketClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) {
  262. reqres := cli.queueRequest(types.ToRequestCheckTx(req))
  263. if err := cli.FlushSync(); err != nil {
  264. return nil, err
  265. }
  266. return reqres.Response.GetCheckTx(), cli.Error()
  267. }
  268. func (cli *socketClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
  269. reqres := cli.queueRequest(types.ToRequestQuery(req))
  270. if err := cli.FlushSync(); err != nil {
  271. return nil, err
  272. }
  273. return reqres.Response.GetQuery(), cli.Error()
  274. }
  275. func (cli *socketClient) CommitSync() (*types.ResponseCommit, error) {
  276. reqres := cli.queueRequest(types.ToRequestCommit())
  277. if err := cli.FlushSync(); err != nil {
  278. return nil, err
  279. }
  280. return reqres.Response.GetCommit(), cli.Error()
  281. }
  282. func (cli *socketClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
  283. reqres := cli.queueRequest(types.ToRequestInitChain(req))
  284. if err := cli.FlushSync(); err != nil {
  285. return nil, err
  286. }
  287. return reqres.Response.GetInitChain(), cli.Error()
  288. }
  289. func (cli *socketClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
  290. reqres := cli.queueRequest(types.ToRequestBeginBlock(req))
  291. if err := cli.FlushSync(); err != nil {
  292. return nil, err
  293. }
  294. return reqres.Response.GetBeginBlock(), cli.Error()
  295. }
  296. func (cli *socketClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
  297. reqres := cli.queueRequest(types.ToRequestEndBlock(req))
  298. if err := cli.FlushSync(); err != nil {
  299. return nil, err
  300. }
  301. return reqres.Response.GetEndBlock(), cli.Error()
  302. }
  303. func (cli *socketClient) ListSnapshotsSync(req types.RequestListSnapshots) (*types.ResponseListSnapshots, error) {
  304. reqres := cli.queueRequest(types.ToRequestListSnapshots(req))
  305. if err := cli.FlushSync(); err != nil {
  306. return nil, err
  307. }
  308. return reqres.Response.GetListSnapshots(), cli.Error()
  309. }
  310. func (cli *socketClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) {
  311. reqres := cli.queueRequest(types.ToRequestOfferSnapshot(req))
  312. if err := cli.FlushSync(); err != nil {
  313. return nil, err
  314. }
  315. return reqres.Response.GetOfferSnapshot(), cli.Error()
  316. }
  317. func (cli *socketClient) LoadSnapshotChunkSync(
  318. req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
  319. reqres := cli.queueRequest(types.ToRequestLoadSnapshotChunk(req))
  320. if err := cli.FlushSync(); err != nil {
  321. return nil, err
  322. }
  323. return reqres.Response.GetLoadSnapshotChunk(), cli.Error()
  324. }
  325. func (cli *socketClient) ApplySnapshotChunkSync(
  326. req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
  327. reqres := cli.queueRequest(types.ToRequestApplySnapshotChunk(req))
  328. if err := cli.FlushSync(); err != nil {
  329. return nil, err
  330. }
  331. return reqres.Response.GetApplySnapshotChunk(), cli.Error()
  332. }
  333. //----------------------------------------
  334. func (cli *socketClient) queueRequest(req *types.Request) *ReqRes {
  335. reqres := NewReqRes(req)
  336. // TODO: set cli.err if reqQueue times out
  337. cli.reqQueue <- reqres
  338. // Maybe auto-flush, or unset auto-flush
  339. switch req.Value.(type) {
  340. case *types.Request_Flush:
  341. cli.flushTimer.Unset()
  342. default:
  343. cli.flushTimer.Set()
  344. }
  345. return reqres
  346. }
  347. func (cli *socketClient) flushQueue() {
  348. cli.mtx.Lock()
  349. defer cli.mtx.Unlock()
  350. // mark all in-flight messages as resolved (they will get cli.Error())
  351. for req := cli.reqSent.Front(); req != nil; req = req.Next() {
  352. reqres := req.Value.(*ReqRes)
  353. reqres.Done()
  354. }
  355. // mark all queued messages as resolved
  356. LOOP:
  357. for {
  358. select {
  359. case reqres := <-cli.reqQueue:
  360. reqres.Done()
  361. default:
  362. break LOOP
  363. }
  364. }
  365. }
  366. //----------------------------------------
  367. func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {
  368. switch req.Value.(type) {
  369. case *types.Request_Echo:
  370. _, ok = res.Value.(*types.Response_Echo)
  371. case *types.Request_Flush:
  372. _, ok = res.Value.(*types.Response_Flush)
  373. case *types.Request_Info:
  374. _, ok = res.Value.(*types.Response_Info)
  375. case *types.Request_DeliverTx:
  376. _, ok = res.Value.(*types.Response_DeliverTx)
  377. case *types.Request_CheckTx:
  378. _, ok = res.Value.(*types.Response_CheckTx)
  379. case *types.Request_Commit:
  380. _, ok = res.Value.(*types.Response_Commit)
  381. case *types.Request_Query:
  382. _, ok = res.Value.(*types.Response_Query)
  383. case *types.Request_InitChain:
  384. _, ok = res.Value.(*types.Response_InitChain)
  385. case *types.Request_BeginBlock:
  386. _, ok = res.Value.(*types.Response_BeginBlock)
  387. case *types.Request_EndBlock:
  388. _, ok = res.Value.(*types.Response_EndBlock)
  389. case *types.Request_ApplySnapshotChunk:
  390. _, ok = res.Value.(*types.Response_ApplySnapshotChunk)
  391. case *types.Request_LoadSnapshotChunk:
  392. _, ok = res.Value.(*types.Response_LoadSnapshotChunk)
  393. case *types.Request_ListSnapshots:
  394. _, ok = res.Value.(*types.Response_ListSnapshots)
  395. case *types.Request_OfferSnapshot:
  396. _, ok = res.Value.(*types.Response_OfferSnapshot)
  397. }
  398. return ok
  399. }
  400. func (cli *socketClient) stopForError(err error) {
  401. if !cli.IsRunning() {
  402. return
  403. }
  404. cli.mtx.Lock()
  405. if cli.err == nil {
  406. cli.err = err
  407. }
  408. cli.mtx.Unlock()
  409. cli.Logger.Error(fmt.Sprintf("Stopping abci.socketClient for error: %v", err.Error()))
  410. if err := cli.Stop(); err != nil {
  411. cli.Logger.Error("Error stopping abci.socketClient", "err", err)
  412. }
  413. }