query_executor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package gocql
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. type ExecutableQuery interface {
  7. execute(conn *Conn) *Iter
  8. attempt(keyspace string, end, start time.Time, iter *Iter, host *HostInfo)
  9. retryPolicy() RetryPolicy
  10. speculativeExecutionPolicy() SpeculativeExecutionPolicy
  11. GetRoutingKey() ([]byte, error)
  12. Keyspace() string
  13. Cancel()
  14. IsIdempotent() bool
  15. RetryableQuery
  16. }
  17. type queryExecutor struct {
  18. pool *policyConnPool
  19. policy HostSelectionPolicy
  20. }
  21. type queryResponse struct {
  22. iter *Iter
  23. err error
  24. }
  25. func (q *queryExecutor) attemptQuery(qry ExecutableQuery, conn *Conn) *Iter {
  26. start := time.Now()
  27. iter := qry.execute(conn)
  28. end := time.Now()
  29. qry.attempt(q.pool.keyspace, end, start, iter, conn.host)
  30. return iter
  31. }
  32. func (q *queryExecutor) executeQuery(qry ExecutableQuery) (*Iter, error) {
  33. // check if the query is not marked as idempotent, if
  34. // it is, we force the policy to NonSpeculative
  35. sp := qry.speculativeExecutionPolicy()
  36. if !qry.IsIdempotent() {
  37. sp = NonSpeculativeExecution{}
  38. }
  39. results := make(chan queryResponse, 1)
  40. stop := make(chan struct{})
  41. defer close(stop)
  42. var specWG sync.WaitGroup
  43. // Launch the main execution
  44. specWG.Add(1)
  45. go q.run(qry, &specWG, results, stop)
  46. // The speculative executions are launched _in addition_ to the main
  47. // execution, on a timer. So Speculation{2} would make 3 executions running
  48. // in total.
  49. go func() {
  50. // Handle the closing of the resources. We do it here because it's
  51. // right after we finish launching executions. Otherwise clearing the
  52. // wait group is complicated.
  53. defer func() {
  54. specWG.Wait()
  55. close(results)
  56. }()
  57. // setup a ticker
  58. ticker := time.NewTicker(sp.Delay())
  59. defer ticker.Stop()
  60. for i := 0; i < sp.Attempts(); i++ {
  61. select {
  62. case <-ticker.C:
  63. // Launch the additional execution
  64. specWG.Add(1)
  65. go q.run(qry, &specWG, results, stop)
  66. case <-qry.GetContext().Done():
  67. // not starting additional executions
  68. return
  69. case <-stop:
  70. // not starting additional executions
  71. return
  72. }
  73. }
  74. }()
  75. res := <-results
  76. if res.iter == nil && res.err == nil {
  77. // if we're here, the results channel was closed, so no more hosts
  78. return nil, ErrNoConnections
  79. }
  80. return res.iter, res.err
  81. }
  82. func (q *queryExecutor) run(qry ExecutableQuery, specWG *sync.WaitGroup, results chan queryResponse, stop chan struct{}) {
  83. // Handle the wait group
  84. defer specWG.Done()
  85. hostIter := q.policy.Pick(qry)
  86. selectedHost := hostIter()
  87. rt := qry.retryPolicy()
  88. var iter *Iter
  89. for selectedHost != nil {
  90. host := selectedHost.Info()
  91. if host == nil || !host.IsUp() {
  92. selectedHost = hostIter()
  93. continue
  94. }
  95. pool, ok := q.pool.getPool(host)
  96. if !ok {
  97. selectedHost = hostIter()
  98. continue
  99. }
  100. conn := pool.Pick()
  101. if conn == nil {
  102. selectedHost = hostIter()
  103. continue
  104. }
  105. select {
  106. case <-stop:
  107. // stop this execution and return
  108. return
  109. default:
  110. // Run the query
  111. iter = q.attemptQuery(qry, conn)
  112. iter.host = selectedHost.Info()
  113. // Update host
  114. selectedHost.Mark(iter.err)
  115. // Exit if the query was successful
  116. // or no retry policy defined or retry attempts were reached
  117. if iter.err == nil || rt == nil || !rt.Attempt(qry) {
  118. results <- queryResponse{iter: iter}
  119. return
  120. }
  121. // If query is unsuccessful, check the error with RetryPolicy to retry
  122. switch rt.GetRetryType(iter.err) {
  123. case Retry:
  124. // retry on the same host
  125. continue
  126. case Rethrow:
  127. results <- queryResponse{err: iter.err}
  128. return
  129. case Ignore:
  130. results <- queryResponse{iter: iter}
  131. return
  132. case RetryNextHost:
  133. // retry on the next host
  134. selectedHost = hostIter()
  135. if selectedHost == nil {
  136. results <- queryResponse{iter: iter}
  137. return
  138. }
  139. continue
  140. default:
  141. // Undefined? Return nil and error, this will panic in the requester
  142. results <- queryResponse{iter: nil, err: ErrUnknownRetryType}
  143. return
  144. }
  145. }
  146. }
  147. // All hosts are exhausted, return nothing
  148. }