service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "regexp"
  7. "time"
  8. "go-common/app/job/main/dm2/conf"
  9. "go-common/app/job/main/dm2/dao"
  10. "go-common/app/job/main/dm2/model"
  11. "go-common/app/job/main/dm2/model/oplog"
  12. arcCli "go-common/app/service/main/archive/api/gorpc"
  13. filterCli "go-common/app/service/main/filter/api/grpc/v1"
  14. seqMdl "go-common/app/service/main/seq-server/model"
  15. seqCli "go-common/app/service/main/seq-server/rpc/client"
  16. "go-common/library/log"
  17. "go-common/library/log/infoc"
  18. "go-common/library/queue/databus"
  19. "go-common/library/sync/pipeline/fanout"
  20. "go-common/library/xstr"
  21. )
  22. const (
  23. _routineSizeDefault = 10
  24. _chanSize = 10240
  25. _batchSize = 1000
  26. _maxUpRecent = 1000
  27. )
  28. // Service service struct
  29. type Service struct {
  30. conf *conf.Config
  31. dao *dao.Dao
  32. cache *fanout.Fanout
  33. arcRPC *arcCli.Service2
  34. // seq serer
  35. seqArg *seqMdl.ArgBusiness
  36. seqRPC *seqCli.Service2
  37. indexCsmr *databus.Databus
  38. subjectCsmr *databus.Databus
  39. actionCsmr *databus.Databus
  40. reportCsmr *databus.Databus
  41. videoupCsmr *databus.Databus
  42. subtitleAuditCsmr *databus.Databus
  43. flushMergeChan []chan *model.Flush
  44. flushSegChan []chan *model.FlushDMSeg
  45. dmRecentChan []chan *model.DM
  46. routineSize int
  47. realname map[int64]int64 // key:分区id,value:cid,即该分区中大于cid的视频开启实名制
  48. // filter service
  49. filterRPC filterCli.FilterClient
  50. maskMid []int64
  51. dmOperationLogSvc *infoc.Infoc
  52. opsLogCh chan *oplog.Infoc
  53. // bnj
  54. bnjAid int64
  55. bnjSubAids map[int64]struct{}
  56. bnjCsmr *databus.Databus
  57. bnjliveRoomID int64
  58. bnjStart time.Time
  59. bnjIgnoreBeginTime time.Duration
  60. bnjIgnoreEndTime time.Duration
  61. bnjArcVideos []*model.Video
  62. bnjIgnoreRate int64
  63. bnjUserLevel int32
  64. }
  65. // New new service.
  66. func New(c *conf.Config) (s *Service) {
  67. s = &Service{
  68. conf: c,
  69. dao: dao.New(c),
  70. cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
  71. arcRPC: arcCli.New2(c.ArchiveRPC),
  72. seqArg: &seqMdl.ArgBusiness{BusinessID: c.Seq.BusinessID, Token: c.Seq.Token},
  73. seqRPC: seqCli.New2(c.SeqRPC),
  74. subjectCsmr: databus.New(c.Databus.SubjectCsmr),
  75. indexCsmr: databus.New(c.Databus.IndexCsmr),
  76. actionCsmr: databus.New(c.Databus.ActionCsmr),
  77. reportCsmr: databus.New(c.Databus.ReportCsmr),
  78. videoupCsmr: databus.New(c.Databus.VideoupCsmr),
  79. subtitleAuditCsmr: databus.New(c.Databus.SubtitleAuditCsmr),
  80. routineSize: c.RoutineSize,
  81. realname: make(map[int64]int64),
  82. dmOperationLogSvc: infoc.New(c.Infoc2),
  83. opsLogCh: make(chan *oplog.Infoc, 1024),
  84. }
  85. if c.RoutineSize <= 0 {
  86. s.routineSize = _routineSizeDefault
  87. }
  88. s.flushMergeChan = make([]chan *model.Flush, s.routineSize)
  89. s.flushSegChan = make([]chan *model.FlushDMSeg, s.routineSize)
  90. s.dmRecentChan = make([]chan *model.DM, s.routineSize)
  91. filterRPC, err := filterCli.NewClient(c.FliterRPC)
  92. if err != nil {
  93. panic(err)
  94. }
  95. s.filterRPC = filterRPC
  96. for idStr, cid := range conf.Conf.Realname {
  97. ids, err := xstr.SplitInts(idStr)
  98. if err != nil {
  99. panic(err)
  100. }
  101. for _, id := range ids {
  102. if _, ok := s.realname[id]; !ok {
  103. s.realname[id] = cid
  104. }
  105. }
  106. }
  107. // laji bnj
  108. if s.conf.BNJ != nil {
  109. //bnj count
  110. s.initBnj()
  111. }
  112. //消费DMReport-T消息
  113. go s.reportCsmproc()
  114. // 消费DMAction-T消息
  115. go s.actionCsmproc()
  116. // 消费DMSubject-T消息
  117. go s.subjectCsmproc()
  118. // 消费DMMeta-T消息
  119. go s.indexCsmproc()
  120. // 消费 Videoup2Bvc消息
  121. go s.videoupCsmrproc()
  122. // 消费 字幕 提交 消息
  123. go s.subtitleAuditProc()
  124. // 刷新全段弹幕
  125. for i := 0; i < s.routineSize; i++ {
  126. flushChan := make(chan *model.Flush, _chanSize)
  127. s.flushMergeChan[i] = flushChan
  128. go s.flushmergeproc(flushChan)
  129. }
  130. // 刷新分段弹幕
  131. for i := 0; i < s.routineSize; i++ {
  132. flushSegChan := make(chan *model.FlushDMSeg, _chanSize)
  133. s.flushSegChan[i] = flushSegChan
  134. go s.flushSegproc(flushSegChan)
  135. }
  136. // 异步处理创作中心最新弹幕列表缓存
  137. for i := 0; i < s.routineSize; i++ {
  138. recentChan := make(chan *model.DM, _chanSize)
  139. s.dmRecentChan[i] = recentChan
  140. go s.dmRecentproc(recentChan)
  141. }
  142. go s.transferProc()
  143. // 处理热门二级分类视频的弹幕蒙版
  144. go s.maskProc()
  145. // 刷新开启蒙版mid
  146. s.maskMid, _ = s.dao.MaskMids(context.TODO())
  147. log.Info("update mask mid(%v)", s.maskMid)
  148. go s.maskMidProc()
  149. // dm task
  150. go s.taskResProc()
  151. go s.taskDelProc()
  152. // oplog
  153. go s.oplogproc()
  154. return
  155. }
  156. // Ping check thrid resource.
  157. func (s *Service) Ping(c context.Context) (err error) {
  158. return s.dao.Ping(c)
  159. }
  160. func (s *Service) subjectCsmproc() {
  161. var (
  162. err error
  163. c = context.TODO()
  164. regexSubject = regexp.MustCompile("dm_subject_[0-9]+")
  165. )
  166. for {
  167. msg, ok := <-s.subjectCsmr.Messages()
  168. if !ok {
  169. log.Error("subject binlog consumer exit")
  170. return
  171. }
  172. m := &model.BinlogMsg{}
  173. if err = json.Unmarshal(msg.Value, &m); err != nil {
  174. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  175. continue
  176. }
  177. log.Info("%s", m)
  178. if regexSubject.MatchString(m.Table) {
  179. if err = s.trackSubject(c, m); err != nil {
  180. log.Error("s.trackSubject(%s) error(%v)", m, err)
  181. continue
  182. }
  183. }
  184. if err = msg.Commit(); err != nil {
  185. log.Error("commit offset(%v) error(%v)", msg, err)
  186. }
  187. }
  188. }
  189. func (s *Service) indexCsmproc() {
  190. var (
  191. err error
  192. c = context.TODO()
  193. regexIndex = regexp.MustCompile("dm_index_[0-9]+")
  194. )
  195. for {
  196. msg, ok := <-s.indexCsmr.Messages()
  197. if !ok {
  198. log.Error("index binlog consumer exit")
  199. return
  200. }
  201. m := &model.BinlogMsg{}
  202. if err = json.Unmarshal(msg.Value, &m); err != nil {
  203. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  204. continue
  205. }
  206. log.Info("%s", m)
  207. if regexIndex.MatchString(m.Table) {
  208. if err = s.trackIndex(c, m); err != nil {
  209. log.Error("s.traceIndex(%s) error(%v)", m, err)
  210. continue
  211. }
  212. }
  213. if err = msg.Commit(); err != nil {
  214. log.Error("commit offset(%v) error(%v)", msg, err)
  215. }
  216. }
  217. }
  218. func (s *Service) flushmergeproc(flushChan chan *model.Flush) {
  219. var (
  220. flushs = make(map[int64]*model.Flush)
  221. ticker = time.NewTicker(60 * time.Second)
  222. err error
  223. )
  224. for {
  225. select {
  226. case flush, ok := <-flushChan:
  227. if !ok {
  228. log.Error("action channel closed")
  229. return
  230. }
  231. if _, ok := flushs[flush.Oid]; !ok || flush.Force { // key不存在或者需要强制刷新的
  232. flushs[flush.Oid] = flush
  233. }
  234. if len(flushs) < _batchSize {
  235. continue
  236. }
  237. case <-ticker.C:
  238. }
  239. if len(flushs) > 0 {
  240. for _, flush := range flushs {
  241. if err = s.flushDmCache(context.TODO(), flush); err != nil {
  242. log.Error("action:flushmergeproc,flush:%+v,error(%v)", flush, err)
  243. }
  244. }
  245. flushs = make(map[int64]*model.Flush)
  246. }
  247. }
  248. }
  249. func keySegFlush(tp int32, oid, total, num int64) string {
  250. return fmt.Sprintf("f_%d_%d_%d_%d", tp, oid, total, num)
  251. }
  252. func (s *Service) flushSegproc(ch chan *model.FlushDMSeg) {
  253. var (
  254. key string
  255. merge = make(map[string]*model.FlushDMSeg)
  256. ticker = time.NewTicker(60 * time.Second)
  257. err error
  258. )
  259. for {
  260. select {
  261. case msg, ok := <-ch:
  262. if !ok {
  263. log.Error("action channel closed")
  264. return
  265. }
  266. key = keySegFlush(msg.Type, msg.Oid, msg.Page.Total, msg.Page.Num)
  267. if _, ok := merge[key]; !ok || msg.Force { // key不存在或者需要强制刷新的
  268. merge[key] = msg
  269. }
  270. if len(merge) < _batchSize {
  271. continue
  272. }
  273. case <-ticker.C:
  274. }
  275. if len(merge) > 0 {
  276. for _, v := range merge {
  277. if err = s.flushDmSegCache(context.TODO(), v); err != nil {
  278. log.Error("action:flushSegproc,data:%+v,error(%v)", v, err)
  279. continue
  280. }
  281. }
  282. merge = make(map[string]*model.FlushDMSeg)
  283. }
  284. }
  285. }
  286. func (s *Service) actionCsmproc() {
  287. for {
  288. msg, ok := <-s.actionCsmr.Messages()
  289. if !ok {
  290. log.Error("action consumer exit")
  291. return
  292. }
  293. act := &model.Action{}
  294. err := json.Unmarshal(msg.Value, &act)
  295. if err != nil {
  296. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  297. continue
  298. }
  299. start := time.Now()
  300. if err = s.actionAct(context.TODO(), act); err != nil {
  301. log.Error("action:%s,data:%s,error(%v)", act.Action, act.Data, err)
  302. continue
  303. }
  304. log.Info("partition:%d,offset:%d,key:%s,value:%s costing:%+v", msg.Partition, msg.Offset, msg.Key, msg.Value, time.Since(start))
  305. if err = msg.Commit(); err != nil {
  306. log.Error("commit offset(%v) error(%v)", msg, err)
  307. }
  308. }
  309. }
  310. // add recent dm
  311. func (s *Service) dmRecentproc(dmChan chan *model.DM) {
  312. var (
  313. count int64
  314. c = context.TODO()
  315. )
  316. for {
  317. dm, ok := <-dmChan
  318. if !ok {
  319. log.Error("recent dm channel is closed")
  320. return
  321. }
  322. sub, err := s.subject(c, dm.Type, dm.Oid)
  323. if err != nil {
  324. continue
  325. }
  326. if dm.State != model.StateNormal && dm.State != model.StateHide && dm.State != model.StateMonitorAfter {
  327. if err = s.dao.ZRemRecentDM(c, sub.Mid, dm.ID); err != nil {
  328. continue
  329. }
  330. } else {
  331. if dm.Content == nil {
  332. if dm.Content, err = s.dao.Content(c, dm.Oid, dm.ID); err != nil {
  333. continue
  334. }
  335. }
  336. if dm.Pool == model.PoolSpecial && dm.ContentSpe == nil {
  337. if dm.ContentSpe, err = s.dao.ContentSpecial(c, dm.ID); err != nil {
  338. continue
  339. }
  340. }
  341. if count, err = s.dao.AddRecentDM(c, sub.Mid, dm); err != nil {
  342. continue
  343. }
  344. if trimCnt := count - _maxUpRecent; trimCnt > 0 {
  345. if err = s.dao.TrimRecentDM(c, sub.Mid, trimCnt); err != nil {
  346. continue
  347. }
  348. }
  349. }
  350. }
  351. }
  352. func (s *Service) asyncAddRecent(c context.Context, dm *model.DM) {
  353. select {
  354. case s.dmRecentChan[dm.Oid%int64(s.routineSize)] <- dm:
  355. default:
  356. log.Warn("dm recent channel is full,dm(%+v)", dm)
  357. }
  358. }
  359. func (s *Service) reportCsmproc() {
  360. for {
  361. msg, ok := <-s.reportCsmr.Messages()
  362. if !ok {
  363. log.Error("report consumer exit")
  364. return
  365. }
  366. log.Info("partition:%d,offset:%d,value:%s", msg.Partition, msg.Offset, msg.Value)
  367. act := &model.ReportAction{}
  368. err := json.Unmarshal(msg.Value, &act)
  369. if err != nil {
  370. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  371. continue
  372. }
  373. if diff := act.HideTime - time.Now().Unix(); diff > 0 {
  374. log.Info("action:%+v will be processed after %d seconds", act, diff)
  375. time.Sleep(time.Duration(diff) * time.Second)
  376. }
  377. if _, err = s.dao.DelDMHideState(context.TODO(), 1, act.Cid, act.Did); err != nil {
  378. log.Error("DelDMHideState(%+v) error(%v)", act, err)
  379. } else {
  380. log.Info("DelDMHideState(%+v) success ", act)
  381. }
  382. if err = msg.Commit(); err != nil {
  383. log.Error("commit offset(%v) error(%v)", msg, err)
  384. }
  385. }
  386. }
  387. func (s *Service) videoupCsmrproc() {
  388. var (
  389. err error
  390. c = context.TODO()
  391. )
  392. for {
  393. msg, ok := <-s.videoupCsmr.Messages()
  394. if !ok {
  395. log.Error("videoup consumer exit")
  396. return
  397. }
  398. log.Info("partition:%d,offset:%d,key:%s,value:%s", msg.Partition, msg.Offset, msg.Key, msg.Value)
  399. m := &model.VideoupMsg{}
  400. if err = json.Unmarshal(msg.Value, m); err != nil {
  401. log.Error("json.Unmarshal(%v) error(%v)", string(msg.Value), err)
  402. continue
  403. }
  404. if m.Route == model.RouteSecondRound || m.Route == model.RouteAutoOpen ||
  405. m.Route == model.RouteForceSync || m.Route == model.RouteDelayOpen {
  406. if err = s.trackVideoup(c, m.Aid); err != nil {
  407. continue
  408. }
  409. }
  410. if err = msg.Commit(); err != nil {
  411. log.Error("commit offset(%v) error(%v)", msg, err)
  412. }
  413. }
  414. }
  415. func (s *Service) subtitleAuditProc() {
  416. var (
  417. err error
  418. c = context.Background()
  419. )
  420. for {
  421. msg, ok := <-s.subtitleAuditCsmr.Messages()
  422. if !ok {
  423. log.Error("subtitle_audit consumer exit")
  424. return
  425. }
  426. log.Info("partition:%d,offset:%d,key:%s,value:%s", msg.Partition, msg.Offset, msg.Key, msg.Value)
  427. m := &model.SubtitleAuditMsg{}
  428. if err = json.Unmarshal(msg.Value, m); err != nil {
  429. log.Error("json.Unmarshal(%v) error(%v)", string(msg.Value), err)
  430. continue
  431. }
  432. if err = s.SubtitleFilter(c, m.Oid, m.SubtitleID); err != nil {
  433. log.Error("SubtitleFilter(oid:%v,subtitleID:%v),error(%v)", m.Oid, m.SubtitleID, err)
  434. continue
  435. }
  436. if err = msg.Commit(); err != nil {
  437. log.Error("commit offset(%v) error(%v)", msg, err)
  438. }
  439. }
  440. }