本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

216 行
4.9KB

  1. package couchbase
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/couchbase/goutils/logging"
  6. "io"
  7. "io/ioutil"
  8. "math/rand"
  9. "net"
  10. "net/http"
  11. "time"
  12. "unsafe"
  13. )
  14. // Bucket auto-updater gets the latest version of the bucket config from
  15. // the server. If the configuration has changed then updated the local
  16. // bucket information. If the bucket has been deleted then notify anyone
  17. // who is holding a reference to this bucket
  18. const MAX_RETRY_COUNT = 5
  19. const DISCONNECT_PERIOD = 120 * time.Second
  20. type NotifyFn func(bucket string, err error)
  21. // Use TCP keepalive to detect half close sockets
  22. var updaterTransport http.RoundTripper = &http.Transport{
  23. Proxy: http.ProxyFromEnvironment,
  24. Dial: (&net.Dialer{
  25. Timeout: 30 * time.Second,
  26. KeepAlive: 30 * time.Second,
  27. }).Dial,
  28. }
  29. var updaterHTTPClient = &http.Client{Transport: updaterTransport}
  30. func doHTTPRequestForUpdate(req *http.Request) (*http.Response, error) {
  31. var err error
  32. var res *http.Response
  33. for i := 0; i < HTTP_MAX_RETRY; i++ {
  34. res, err = updaterHTTPClient.Do(req)
  35. if err != nil && isHttpConnError(err) {
  36. continue
  37. }
  38. break
  39. }
  40. if err != nil {
  41. return nil, err
  42. }
  43. return res, err
  44. }
  45. func (b *Bucket) RunBucketUpdater(notify NotifyFn) {
  46. go func() {
  47. err := b.UpdateBucket()
  48. if err != nil {
  49. if notify != nil {
  50. notify(b.GetName(), err)
  51. }
  52. logging.Errorf(" Bucket Updater exited with err %v", err)
  53. }
  54. }()
  55. }
  56. func (b *Bucket) replaceConnPools2(with []*connectionPool, bucketLocked bool) {
  57. if !bucketLocked {
  58. b.Lock()
  59. defer b.Unlock()
  60. }
  61. old := b.connPools
  62. b.connPools = unsafe.Pointer(&with)
  63. if old != nil {
  64. for _, pool := range *(*[]*connectionPool)(old) {
  65. if pool != nil && pool.inUse == false {
  66. pool.Close()
  67. }
  68. }
  69. }
  70. return
  71. }
  72. func (b *Bucket) UpdateBucket() error {
  73. var failures int
  74. var returnErr error
  75. var poolServices PoolServices
  76. var err error
  77. tlsConfig := b.pool.client.tlsConfig
  78. if tlsConfig != nil {
  79. poolServices, err = b.pool.client.GetPoolServices("default")
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. for {
  85. if failures == MAX_RETRY_COUNT {
  86. logging.Errorf(" Maximum failures reached. Exiting loop...")
  87. return fmt.Errorf("Max failures reached. Last Error %v", returnErr)
  88. }
  89. nodes := b.Nodes()
  90. if len(nodes) < 1 {
  91. return fmt.Errorf("No healthy nodes found")
  92. }
  93. startNode := rand.Intn(len(nodes))
  94. node := nodes[(startNode)%len(nodes)]
  95. streamUrl := fmt.Sprintf("http://%s/pools/default/bucketsStreaming/%s", node.Hostname, b.GetName())
  96. logging.Infof(" Trying with %s", streamUrl)
  97. req, err := http.NewRequest("GET", streamUrl, nil)
  98. if err != nil {
  99. return err
  100. }
  101. // Lock here to avoid having pool closed under us.
  102. b.RLock()
  103. err = maybeAddAuth(req, b.pool.client.ah)
  104. b.RUnlock()
  105. if err != nil {
  106. return err
  107. }
  108. res, err := doHTTPRequestForUpdate(req)
  109. if err != nil {
  110. return err
  111. }
  112. if res.StatusCode != 200 {
  113. bod, _ := ioutil.ReadAll(io.LimitReader(res.Body, 512))
  114. logging.Errorf("Failed to connect to host, unexpected status code: %v. Body %s", res.StatusCode, bod)
  115. res.Body.Close()
  116. returnErr = fmt.Errorf("Failed to connect to host. Status %v Body %s", res.StatusCode, bod)
  117. failures++
  118. continue
  119. }
  120. dec := json.NewDecoder(res.Body)
  121. tmpb := &Bucket{}
  122. for {
  123. err := dec.Decode(&tmpb)
  124. if err != nil {
  125. returnErr = err
  126. res.Body.Close()
  127. break
  128. }
  129. // if we got here, reset failure count
  130. failures = 0
  131. b.Lock()
  132. // mark all the old connection pools for deletion
  133. pools := b.getConnPools(true /* already locked */)
  134. for _, pool := range pools {
  135. if pool != nil {
  136. pool.inUse = false
  137. }
  138. }
  139. newcps := make([]*connectionPool, len(tmpb.VBSMJson.ServerList))
  140. for i := range newcps {
  141. // get the old connection pool and check if it is still valid
  142. pool := b.getConnPoolByHost(tmpb.VBSMJson.ServerList[i], true /* bucket already locked */)
  143. if pool != nil && pool.inUse == false {
  144. // if the hostname and index is unchanged then reuse this pool
  145. newcps[i] = pool
  146. pool.inUse = true
  147. continue
  148. }
  149. // else create a new pool
  150. hostport := tmpb.VBSMJson.ServerList[i]
  151. if tlsConfig != nil {
  152. hostport, err = MapKVtoSSL(hostport, &poolServices)
  153. if err != nil {
  154. b.Unlock()
  155. return err
  156. }
  157. }
  158. if b.ah != nil {
  159. newcps[i] = newConnectionPool(hostport,
  160. b.ah, false, PoolSize, PoolOverflow, b.pool.client.tlsConfig, b.Name)
  161. } else {
  162. newcps[i] = newConnectionPool(hostport,
  163. b.authHandler(true /* bucket already locked */),
  164. false, PoolSize, PoolOverflow, b.pool.client.tlsConfig, b.Name)
  165. }
  166. }
  167. b.replaceConnPools2(newcps, true /* bucket already locked */)
  168. tmpb.ah = b.ah
  169. b.vBucketServerMap = unsafe.Pointer(&tmpb.VBSMJson)
  170. b.nodeList = unsafe.Pointer(&tmpb.NodesJSON)
  171. b.Unlock()
  172. logging.Infof("Got new configuration for bucket %s", b.GetName())
  173. }
  174. // we are here because of an error
  175. failures++
  176. continue
  177. }
  178. return nil
  179. }
上海开阖软件有限公司 沪ICP备12045867号-1