mysql.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package dao
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go-common/app/service/video/stream-mng/common"
  7. "go-common/app/service/video/stream-mng/model"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. "go-common/library/sync/errgroup"
  11. )
  12. // RawStreamFullInfo 直接从数据库中查询流信息,可传入流名, 也可传入rid
  13. func (d *Dao) RawStreamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error) {
  14. var (
  15. official []*model.OfficialStream
  16. backup []*model.StreamBase
  17. mainStream *model.MainStream
  18. )
  19. if sname != "" {
  20. official, err = d.GetOfficialStreamByName(c, sname)
  21. // 可以从原表中查询到
  22. if err == nil && official != nil && len(official) > 0 {
  23. id = official[0].RoomID
  24. goto END
  25. }
  26. var backUpInfo *model.BackupStream
  27. // 原表中查询不到
  28. backUpInfo, err = d.GetBackupStreamByStreamName(c, sname)
  29. if err != nil {
  30. log.Errorv(c, log.KV("log", fmt.Sprintf("sql backup_stream err = %v", err)))
  31. return
  32. }
  33. if backUpInfo == nil {
  34. err = fmt.Errorf("can not find any info by %s", sname)
  35. return
  36. }
  37. id = backUpInfo.RoomID
  38. }
  39. END:
  40. // todo 这里用老的errgroup, 新errgroup2 暂时未有人用,bug未知
  41. group, errCtx := errgroup.WithContext(c)
  42. // 如果还未查sv_ls_stream则需要查询
  43. if id > 0 && len(official) == 0 {
  44. group.Go(func() (err error) {
  45. log.Warn("group offical")
  46. if official, err = d.GetOfficialStreamByRoomID(errCtx, id); err != nil {
  47. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group offical err=%v", err)))
  48. }
  49. return nil
  50. })
  51. }
  52. if id > 0 {
  53. group.Go(func() (err error) {
  54. log.Warn("group main")
  55. if mainStream, err = d.GetMainStreamFromDB(errCtx, id, ""); err != nil {
  56. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group main err=%v", err)))
  57. }
  58. return nil
  59. })
  60. group.Go(func() (err error) {
  61. log.Warn("group back")
  62. back, err := d.GetBackupStreamByRoomID(errCtx, id)
  63. if err != nil {
  64. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group backup err=%v", err)))
  65. } else {
  66. backup = d.formatBackup2BaseInfo(c, back)
  67. }
  68. return nil
  69. })
  70. }
  71. err = group.Wait()
  72. if err != nil {
  73. return
  74. }
  75. if len(official) == 0 {
  76. err = fmt.Errorf("can not find any info by room_id=%d", id)
  77. return
  78. }
  79. return d.formatStreamFullInfo(c, official, backup, mainStream)
  80. }
  81. // RawStreamRIDByName 查询rid
  82. func (d *Dao) RawStreamRIDByName(c context.Context, sname string) (res *model.StreamFullInfo, err error) {
  83. return d.RawStreamFullInfo(c, 0, sname)
  84. }
  85. // RawMultiStreamInfo 批量查询流信息
  86. func (d *Dao) RawMultiStreamInfo(c context.Context, rids []int64) (res map[int64]*model.StreamFullInfo, err error) {
  87. var (
  88. official []*model.OfficialStream
  89. backup []*model.BackupStream
  90. mainStream []*model.MainStream
  91. )
  92. group, errCtx := errgroup.WithContext(c)
  93. group.Go(func() (err error) {
  94. if official, err = d.GetMultiOfficalStreamByRID(errCtx, rids); err != nil {
  95. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group offical err=%v", err)))
  96. }
  97. return nil
  98. })
  99. group.Go(func() (err error) {
  100. if backup, err = d.GetMultiBackupStreamByRID(errCtx, rids); err != nil {
  101. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group back err=%v", err)))
  102. }
  103. return nil
  104. })
  105. group.Go(func() (err error) {
  106. if mainStream, err = d.GetMultiMainStreamFromDB(errCtx, rids); err != nil {
  107. log.Errorv(errCtx, log.KV("log", fmt.Sprintf("group back err=%v", err)))
  108. }
  109. return nil
  110. })
  111. err = group.Wait()
  112. if err != nil {
  113. return
  114. }
  115. // 把rid相同的放为一组
  116. ridMapOfficial := map[int64][]*model.OfficialStream{}
  117. for _, v := range official {
  118. ridMapOfficial[v.RoomID] = append(ridMapOfficial[v.RoomID], v)
  119. }
  120. ridMapBackup := map[int64][]*model.BackupStream{}
  121. for _, v := range backup {
  122. ridMapBackup[v.RoomID] = append(ridMapBackup[v.RoomID], v)
  123. }
  124. ridMapBackupBase := map[int64][]*model.StreamBase{}
  125. for id, v := range ridMapBackup {
  126. ridMapBackupBase[id] = d.formatBackup2BaseInfo(c, v)
  127. }
  128. ridMapMain := map[int64]*model.MainStream{}
  129. for _, v := range mainStream {
  130. ridMapMain[v.RoomID] = v
  131. }
  132. infos := map[int64]*model.StreamFullInfo{}
  133. flag := false
  134. for id, v := range ridMapOfficial {
  135. flag = true
  136. infos[id], _ = d.formatStreamFullInfo(c, v, ridMapBackupBase[id], ridMapMain[id])
  137. }
  138. if flag {
  139. return infos, nil
  140. }
  141. log.Errorv(c, log.KV("log", fmt.Errorf("can not find any info by room_ids=%d", rids)))
  142. return nil, nil
  143. }
  144. // formatStreamFullInfo 格式化流信息
  145. func (d *Dao) formatStreamFullInfo(c context.Context, official []*model.OfficialStream, backup []*model.StreamBase, main *model.MainStream) (*model.StreamFullInfo, error) {
  146. resp := &model.StreamFullInfo{}
  147. resp.List = []*model.StreamBase{}
  148. var roomID int64
  149. roomID = official[0].RoomID
  150. resp.RoomID = official[0].RoomID
  151. base := &model.StreamBase{}
  152. base.StreamName = official[0].Name
  153. base.Type = 1
  154. base.Key = official[0].Key
  155. if main != nil {
  156. base.Options = main.Options
  157. if 4&base.Options == 4 {
  158. base.Wmask = true
  159. }
  160. if 8&base.Options == 8 {
  161. base.Mmask = true
  162. }
  163. }
  164. for _, item := range official {
  165. if item.UpRank == 1 {
  166. if val, ok := common.SrcMapBitwise[item.Src]; ok {
  167. // todo origin为main-stream取
  168. if main != nil {
  169. base.Origin = main.OriginUpstream
  170. } else {
  171. // 做个兜底逻辑, main-stream中没有这个数据,但是sv_ls_stream确实在播
  172. base.Origin = val
  173. }
  174. base.DefaultUpStream = val
  175. } else {
  176. // 如果上行不在现在的任意一家, 则重新设置上行
  177. if err := d.UpdateOfficialStreamStatus(c, roomID, common.BVCSrc); err == nil {
  178. if main != nil {
  179. base.Origin = main.OriginUpstream
  180. } else {
  181. base.Origin = common.BitWiseBVC
  182. }
  183. base.DefaultUpStream = common.BitWiseBVC
  184. go func(c context.Context, rid int64, fromOrigin int8, toOrigin int64, sname string) {
  185. d.UpdateStreamStatusCache(c, &model.StreamStatus{
  186. RoomID: rid,
  187. StreamName: sname,
  188. DefaultChange: true,
  189. DefaultUpStream: toOrigin,
  190. })
  191. // 插入日志
  192. d.InsertChangeLog(c, &model.StreamChangeLog{
  193. RoomID: rid,
  194. FromOrigin: int64(fromOrigin),
  195. ToOrigin: toOrigin,
  196. Reason: fmt.Sprintf("上行不在五家CDN,old origin=%d", fromOrigin),
  197. OperateName: "auto_change",
  198. Source: "background",
  199. })
  200. }(metadata.WithContext(c), roomID, item.Src, common.BitWiseBVC, item.Name)
  201. }
  202. }
  203. } else if item.UpRank == 2 {
  204. if val, ok := common.SrcMapBitwise[item.Src]; ok {
  205. base.Forward = append(base.Forward, val)
  206. }
  207. }
  208. }
  209. resp.List = append(resp.List, base)
  210. if len(backup) > 0 {
  211. for _, v := range backup {
  212. resp.List = append(resp.List, v)
  213. }
  214. }
  215. d.liveAside.Do(c, func(ctx context.Context) {
  216. d.diffStreamInfo(ctx, resp, main)
  217. })
  218. return resp, nil
  219. }
  220. // formatBackup2Base backup 格式化为base
  221. func (d *Dao) formatBackup2BaseInfo(c context.Context, back []*model.BackupStream) (resp []*model.StreamBase) {
  222. if len(back) > 0 {
  223. for _, b := range back {
  224. bs := &model.StreamBase{}
  225. bs.StreamName = b.StreamName
  226. bs.Type = 2
  227. bs.Key = b.Key
  228. // 原始上行
  229. bs.Origin = b.OriginUpstream
  230. bs.DefaultUpStream = b.DefaultVendor
  231. bs.Options = b.Options
  232. // 位运算:可满足9家cdn
  233. var n int64
  234. for n = 256; n > 0; n /= 2 {
  235. if (b.Streaming&n) == n && n != bs.Origin {
  236. bs.Forward = append(bs.Forward, n)
  237. }
  238. }
  239. resp = append(resp, bs)
  240. }
  241. }
  242. return
  243. }
  244. // 比较新表和老表
  245. func (d *Dao) diffStreamInfo(c context.Context, info *model.StreamFullInfo, mainStream *model.MainStream) {
  246. if info != nil && info.RoomID != 0 && len(info.List) > 0 {
  247. if mainStream == nil {
  248. d.syncMainStream(c, info.RoomID, "")
  249. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:can find any info, room_id=%d", info.RoomID)))
  250. return
  251. }
  252. offical := info.List[0]
  253. if mainStream.StreamName != offical.StreamName {
  254. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:stream name is different,room_id=%d", info.RoomID)))
  255. return
  256. }
  257. if mainStream.Key != offical.Key {
  258. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:key is different,room_id=%d", info.RoomID)))
  259. return
  260. }
  261. if mainStream.DefaultVendor != offical.DefaultUpStream {
  262. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:DefaultVendor is different,room_id=%d,main=%d,offical=%d", info.RoomID, mainStream.DefaultVendor, offical.DefaultUpStream)))
  263. return
  264. }
  265. if mainStream.OriginUpstream != 0 && (mainStream.OriginUpstream != mainStream.DefaultVendor) {
  266. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:OriginUpstream is different,room_id=%d, main origin=%d, main default=%d", info.RoomID, mainStream.OriginUpstream, mainStream.DefaultVendor)))
  267. return
  268. }
  269. streaming := offical.DefaultUpStream
  270. for _, v := range offical.Forward {
  271. streaming += v
  272. }
  273. if mainStream.Streaming != streaming {
  274. log.Infov(c, log.KV("log", fmt.Sprintf("diff_err:Streaming is different,room_id=%d, main=%d, offical=%d", info.RoomID, mainStream.Streaming, streaming)))
  275. return
  276. }
  277. }
  278. }
  279. func (d *Dao) syncMainStream(c context.Context, roomID int64, streamName string) error {
  280. if roomID <= 0 && streamName == "" {
  281. return errors.New("invalid params")
  282. }
  283. var err error
  284. exists, err := d.GetMainStreamFromDB(c, roomID, streamName)
  285. if err != nil && err.Error() != "sql: no rows in result set" {
  286. log.Errorv(c, log.KV("log", fmt.Sprintf("sync_stream_data_error = %v", err)))
  287. return err
  288. }
  289. if exists != nil && (exists.RoomID == roomID || exists.StreamName == streamName) {
  290. return nil
  291. }
  292. var full *model.StreamFullInfo
  293. if roomID > 0 && streamName == "" {
  294. full, err = d.StreamFullInfo(c, roomID, "")
  295. } else if roomID <= 0 && streamName != "" {
  296. full, err = d.StreamFullInfo(c, 0, streamName)
  297. }
  298. if err != nil {
  299. return err
  300. }
  301. if full == nil {
  302. return errors.New("unknow response")
  303. }
  304. for _, ss := range full.List {
  305. if ss.Type == 1 {
  306. ms := &model.MainStream{
  307. RoomID: full.RoomID,
  308. StreamName: ss.StreamName,
  309. Key: ss.Key,
  310. DefaultVendor: ss.DefaultUpStream,
  311. Status: 1,
  312. }
  313. if ms.DefaultVendor == 0 {
  314. ms.DefaultVendor = 1
  315. }
  316. _, err := d.CreateNewStream(c, ms)
  317. if err != nil {
  318. log.Errorv(c, log.KV("log", fmt.Sprintf("sync_stream_data_error = %v", err)))
  319. }
  320. break
  321. }
  322. }
  323. return nil
  324. }