databus.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package databus
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math/rand"
  9. "net/url"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "go-common/library/cache/redis"
  14. "go-common/library/conf/env"
  15. "go-common/library/container/pool"
  16. "go-common/library/log"
  17. "go-common/library/naming"
  18. "go-common/library/naming/discovery"
  19. "go-common/library/net/netutil"
  20. "go-common/library/net/trace"
  21. "go-common/library/stat/prom"
  22. xtime "go-common/library/time"
  23. )
  24. const (
  25. _appid = "middleware.databus"
  26. )
  27. type dial func() (redis.Conn, error)
  28. // Config databus config.
  29. type Config struct {
  30. Key string
  31. Secret string
  32. Group string
  33. Topic string
  34. Action string // shoule be "pub" or "sub" or "pubsub"
  35. Buffer int
  36. Name string // redis name, for trace
  37. Proto string
  38. Addr string
  39. Auth string
  40. Active int // pool
  41. Idle int // pool
  42. DialTimeout xtime.Duration
  43. ReadTimeout xtime.Duration
  44. WriteTimeout xtime.Duration
  45. IdleTimeout xtime.Duration
  46. Direct bool
  47. }
  48. const (
  49. _family = "databus"
  50. _actionSub = "sub"
  51. _actionPub = "pub"
  52. _actionAll = "pubsub"
  53. _cmdPub = "SET"
  54. _cmdSub = "MGET"
  55. _authFormat = "%s:%s@%s/topic=%s&role=%s"
  56. _open = int32(0)
  57. _closed = int32(1)
  58. _scheme = "databus"
  59. )
  60. var (
  61. // ErrAction action error.
  62. ErrAction = errors.New("action unknown")
  63. // ErrFull chan full
  64. ErrFull = errors.New("chan full")
  65. // ErrNoInstance no instances
  66. ErrNoInstance = errors.New("no databus instances found")
  67. bk = netutil.DefaultBackoffConfig
  68. stats = prom.LibClient
  69. )
  70. // Message Data.
  71. type Message struct {
  72. Key string `json:"key"`
  73. Value json.RawMessage `json:"value"`
  74. Topic string `json:"topic"`
  75. Partition int32 `json:"partition"`
  76. Offset int64 `json:"offset"`
  77. Timestamp int64 `json:"timestamp"`
  78. d *Databus
  79. }
  80. // Commit ack message.
  81. func (m *Message) Commit() (err error) {
  82. m.d.lock.Lock()
  83. if m.Offset >= m.d.marked[m.Partition] {
  84. m.d.marked[m.Partition] = m.Offset
  85. }
  86. m.d.lock.Unlock()
  87. return nil
  88. }
  89. // Databus databus struct.
  90. type Databus struct {
  91. conf *Config
  92. d dial
  93. p *redis.Pool
  94. dis naming.Resolver
  95. msgs chan *Message
  96. lock sync.RWMutex
  97. marked map[int32]int64
  98. idx int64
  99. closed int32
  100. }
  101. // New new a databus.
  102. func New(c *Config) *Databus {
  103. if c.Buffer == 0 {
  104. c.Buffer = 1024
  105. }
  106. d := &Databus{
  107. conf: c,
  108. msgs: make(chan *Message, c.Buffer),
  109. marked: make(map[int32]int64),
  110. closed: _open,
  111. }
  112. if !c.Direct && env.DeployEnv != "" && env.DeployEnv != env.DeployEnvDev {
  113. d.dis = discovery.Build(_appid)
  114. e := d.dis.Watch()
  115. select {
  116. case <-e:
  117. d.disc()
  118. case <-time.After(10 * time.Second):
  119. panic("init discovery err")
  120. }
  121. go d.discoveryproc(e)
  122. log.Info("init databus discvoery info successfully")
  123. }
  124. if c.Action == _actionSub || c.Action == _actionAll {
  125. if d.dis == nil {
  126. d.d = d.dial
  127. } else {
  128. d.d = d.dialInstance
  129. }
  130. go d.subproc()
  131. }
  132. if c.Action == _actionPub || c.Action == _actionAll {
  133. // new pool
  134. d.p = d.redisPool(c)
  135. if d.dis != nil {
  136. d.p.New = func(ctx context.Context) (io.Closer, error) {
  137. return d.dialInstance()
  138. }
  139. }
  140. }
  141. return d
  142. }
  143. func (d *Databus) redisPool(c *Config) *redis.Pool {
  144. config := &redis.Config{
  145. Name: c.Name,
  146. Proto: c.Proto,
  147. Addr: c.Addr,
  148. Auth: fmt.Sprintf(_authFormat, c.Key, c.Secret, c.Group, c.Topic, c.Action),
  149. DialTimeout: c.DialTimeout,
  150. ReadTimeout: c.ReadTimeout,
  151. WriteTimeout: c.WriteTimeout,
  152. }
  153. config.Config = &pool.Config{
  154. Active: c.Active,
  155. Idle: c.Idle,
  156. IdleTimeout: c.IdleTimeout,
  157. }
  158. stat := redis.DialStats(statfunc)
  159. return redis.NewPool(config, stat)
  160. }
  161. func statfunc(cmd string, err *error) func() {
  162. now := time.Now()
  163. return func() {
  164. stats.Timing(fmt.Sprintf("databus:%s", cmd), int64(time.Since(now)/time.Millisecond))
  165. if err != nil && *err != nil {
  166. stats.Incr("databus", (*err).Error())
  167. }
  168. }
  169. }
  170. func (d *Databus) redisOptions() []redis.DialOption {
  171. cnop := redis.DialConnectTimeout(time.Duration(d.conf.DialTimeout))
  172. rdop := redis.DialReadTimeout(time.Duration(d.conf.ReadTimeout))
  173. wrop := redis.DialWriteTimeout(time.Duration(d.conf.WriteTimeout))
  174. auop := redis.DialPassword(fmt.Sprintf(_authFormat, d.conf.Key, d.conf.Secret, d.conf.Group, d.conf.Topic, d.conf.Action))
  175. stat := redis.DialStats(statfunc)
  176. return []redis.DialOption{cnop, rdop, wrop, auop, stat}
  177. }
  178. func (d *Databus) dial() (redis.Conn, error) {
  179. return redis.Dial(d.conf.Proto, d.conf.Addr, d.redisOptions()...)
  180. }
  181. func (d *Databus) dialInstance() (redis.Conn, error) {
  182. if insMap, ok := d.dis.Fetch(context.Background()); ok {
  183. ins, ok := insMap[env.Zone]
  184. if !ok || len(ins) == 0 {
  185. for _, is := range insMap {
  186. ins = append(ins, is...)
  187. }
  188. }
  189. if len(ins) > 0 {
  190. var in *naming.Instance
  191. if d.conf.Action == "pub" {
  192. i := atomic.AddInt64(&d.idx, 1)
  193. in = ins[i%int64(len(ins))]
  194. } else {
  195. in = ins[rand.Intn(len(ins))]
  196. }
  197. for _, addr := range in.Addrs {
  198. u, err := url.Parse(addr)
  199. if err == nil && u.Scheme == _scheme {
  200. return redis.Dial("tcp", u.Host, d.redisOptions()...)
  201. }
  202. }
  203. }
  204. }
  205. if d.conf.Proto != "" && d.conf.Addr != "" {
  206. log.Warn("Databus: no instances(%s,%s) found in discovery,Use config(%s,%s)", _appid, env.Zone, d.conf.Proto, d.conf.Addr)
  207. return redis.Dial(d.conf.Proto, d.conf.Addr, d.redisOptions()...)
  208. }
  209. return nil, ErrNoInstance
  210. }
  211. func (d *Databus) disc() {
  212. if d.p != nil {
  213. op := d.p
  214. np := d.redisPool(d.conf)
  215. np.New = func(ctx context.Context) (io.Closer, error) {
  216. return d.dialInstance()
  217. }
  218. d.p = np
  219. op.Close()
  220. op = nil
  221. log.Info("discovery event renew redis pool group(%s) topic(%s)", d.conf.Group, d.conf.Topic)
  222. }
  223. if insMap, ok := d.dis.Fetch(context.Background()); ok {
  224. if ins, ok := insMap[env.Zone]; ok && len(ins) > 0 {
  225. log.Info("get databus instances len(%d)", len(ins))
  226. }
  227. }
  228. }
  229. func (d *Databus) discoveryproc(e <-chan struct{}) {
  230. if d.dis == nil {
  231. return
  232. }
  233. for {
  234. <-e
  235. d.disc()
  236. }
  237. }
  238. func (d *Databus) subproc() {
  239. var (
  240. err error
  241. r []byte
  242. res [][]byte
  243. c redis.Conn
  244. retry int
  245. commited = make(map[int32]int64)
  246. commit = make(map[int32]int64)
  247. )
  248. for {
  249. if atomic.LoadInt32(&d.closed) == _closed {
  250. if c != nil {
  251. c.Close()
  252. }
  253. close(d.msgs)
  254. return
  255. }
  256. if err != nil {
  257. time.Sleep(bk.Backoff(retry))
  258. retry++
  259. } else {
  260. retry = 0
  261. }
  262. if c == nil || c.Err() != nil {
  263. if c, err = d.d(); err != nil {
  264. log.Error("redis.Dial(%s@%s) group(%s) retry error(%v)", d.conf.Proto, d.conf.Addr, d.conf.Group, err)
  265. continue
  266. }
  267. }
  268. d.lock.RLock()
  269. for k, v := range d.marked {
  270. if commited[k] != v {
  271. commit[k] = v
  272. }
  273. }
  274. d.lock.RUnlock()
  275. // TODO pipeline commit offset
  276. for k, v := range commit {
  277. if _, err = c.Do("SET", k, v); err != nil {
  278. c.Close()
  279. log.Error("group(%s) conn.Do(SET,%d,%d) commit error(%v)", d.conf.Group, k, v, err)
  280. break
  281. }
  282. delete(commit, k)
  283. commited[k] = v
  284. }
  285. if err != nil {
  286. continue
  287. }
  288. // pull messages
  289. if res, err = redis.ByteSlices(c.Do(_cmdSub, "")); err != nil {
  290. c.Close()
  291. log.Error("group(%s) conn.Do(MGET) error(%v)", d.conf.Group, err)
  292. continue
  293. }
  294. for _, r = range res {
  295. msg := &Message{d: d}
  296. if err = json.Unmarshal(r, msg); err != nil {
  297. log.Error("json.Unmarshal(%s) error(%v)", r, err)
  298. continue
  299. }
  300. d.msgs <- msg
  301. }
  302. }
  303. }
  304. // Messages get message chan.
  305. func (d *Databus) Messages() <-chan *Message {
  306. return d.msgs
  307. }
  308. // Send send message to databus.
  309. func (d *Databus) Send(c context.Context, k string, v interface{}) (err error) {
  310. var b []byte
  311. // trace info
  312. if t, ok := trace.FromContext(c); ok {
  313. t = t.Fork(_family, _cmdPub)
  314. t.SetTag(trace.String(trace.TagAddress, d.conf.Addr), trace.String(trace.TagComment, k))
  315. defer t.Finish(&err)
  316. }
  317. // send message
  318. if b, err = json.Marshal(v); err != nil {
  319. log.Error("json.Marshal(%v) error(%v)", v, err)
  320. return
  321. }
  322. conn := d.p.Get(context.TODO())
  323. if _, err = conn.Do(_cmdPub, k, b); err != nil {
  324. log.Error("conn.Do(%s,%s,%s) error(%v)", _cmdPub, k, b, err)
  325. }
  326. conn.Close()
  327. return
  328. }
  329. // Close close databus conn.
  330. func (d *Databus) Close() (err error) {
  331. if !atomic.CompareAndSwapInt32(&d.closed, _open, _closed) {
  332. return
  333. }
  334. if d.p != nil {
  335. d.p.Close()
  336. }
  337. return nil
  338. }