main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package main
  2. import (
  3. "encoding/json"
  4. "field_sync/config"
  5. "field_sync/oss"
  6. "fmt"
  7. "go.uber.org/zap"
  8. "gopkg.in/mgo.v2/bson"
  9. "io/ioutil"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  13. gonsq "jygit.jydev.jianyu360.cn/data_processing/common_utils/nsq"
  14. "jygit.jydev.jianyu360.cn/data_processing/common_utils/redis"
  15. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  16. "net"
  17. "net/http"
  18. "strings"
  19. "sync"
  20. "time"
  21. )
  22. var (
  23. MgoB *mongodb.MongodbSim
  24. MgoE *mongodb.MongodbSim
  25. MgoQ *mongodb.MongodbSim // 企业
  26. MgoP *mongodb.MongodbSim // 凭安企业
  27. UdpClient udp.UdpClient
  28. UdpTaskMap = &sync.Map{}
  29. Mcmer *gonsq.Consumer
  30. MgoBulkSize = 200 // mgo批量保存大小
  31. updateBidPool = make(chan []map[string]interface{}, 5000)
  32. updateBidSp = make(chan bool, 5)
  33. updateExtPool = make(chan []map[string]interface{}, 5000)
  34. updateExtSp = make(chan bool, 5)
  35. )
  36. func init() {
  37. config.Init("./common.toml")
  38. oss.InitOss()
  39. InitFileInfo()
  40. InitLog()
  41. InitMgo()
  42. inits()
  43. redis.InitRedis1(config.Conf.DB.Redis.Addr, config.Conf.DB.Redis.DbIndex)
  44. log.Info("init success")
  45. }
  46. func main() {
  47. go checkMapJob()
  48. go nsqMethod()
  49. go UpdateBidding()
  50. go UpdateExtract()
  51. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  52. UdpClient.Listen(processUdpMsg)
  53. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  54. info, _ := MgoB.Find("bidding_processing_ids", `{"dataprocess": 6}`, bson.M{"_id": 1}, nil, false, -1, -1)
  55. util.Debug(len(*info))
  56. log.Info("", zap.Int("size", len(*info)))
  57. if len(*info) > 0 {
  58. for i, m := range *info {
  59. mapInfo := make(map[string]interface{})
  60. mapInfo["gtid"] = util.ObjToString(m["gtid"])
  61. mapInfo["lteid"] = util.ObjToString(m["lteid"])
  62. mapInfo["stype"] = "bidding"
  63. mapInfo["key"] = fmt.Sprintf("%s-%s-bidding", util.ObjToString(m["gtid"]), util.ObjToString(m["lteid"]))
  64. log.Info(fmt.Sprint(i), zap.Any("--", mapInfo))
  65. biddingTask(nil, mapInfo)
  66. }
  67. }
  68. ch := make(chan bool, 1)
  69. <-ch
  70. }
  71. var pool = make(chan bool, 20)
  72. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  73. defer util.Catch()
  74. switch act {
  75. case udp.OP_TYPE_DATA: //上个节点的数据
  76. var mapInfo map[string]interface{}
  77. err := json.Unmarshal(data, &mapInfo)
  78. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  79. if err != nil {
  80. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  81. } else if mapInfo != nil {
  82. key, _ := mapInfo["key"].(string)
  83. if key == "" {
  84. key = "udpok"
  85. }
  86. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  87. tasktype, _ := mapInfo["stype"].(string)
  88. switch tasktype {
  89. case "bidding":
  90. pool <- true
  91. go func() {
  92. defer func() {
  93. <-pool
  94. }()
  95. biddingTask(data, mapInfo)
  96. }()
  97. case "bidding_history": //增量id段历史数据
  98. pool <- true
  99. go func() {
  100. defer func() {
  101. <-pool
  102. }()
  103. biddingTask(data, mapInfo)
  104. }()
  105. case "bidding_all": //id段存量数据
  106. pool <- true
  107. go func() {
  108. defer func() {
  109. <-pool
  110. }()
  111. biddingAllTask(data, mapInfo)
  112. }()
  113. case "monitor":
  114. //
  115. default:
  116. pool <- true
  117. go func() {
  118. defer func() {
  119. <-pool
  120. }()
  121. log.Error("err", zap.Any("mapinfo", mapInfo))
  122. }()
  123. }
  124. }
  125. case udp.OP_NOOP:
  126. ok := string(data)
  127. if ok != "" {
  128. log.Info("udp re", zap.String("data:", ok))
  129. UdpTaskMap.Delete(ok)
  130. }
  131. }
  132. }
  133. type UdpNode struct {
  134. data []byte
  135. addr *net.UDPAddr
  136. timestamp int64
  137. retry int
  138. }
  139. func NextNode(mapInfo map[string]interface{}, stype string) {
  140. var next = &net.UDPAddr{
  141. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  142. Port: util.IntAll(config.Conf.Udp.Next.Port),
  143. }
  144. mapInfo["stype"] = stype
  145. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), stype)
  146. mapInfo["key"] = key
  147. log.Info("udp es node", zap.Any("mapinfo:", mapInfo))
  148. datas, _ := json.Marshal(mapInfo)
  149. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  150. UdpTaskMap.Store(key, node)
  151. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  152. }
  153. func NextNodePro(mapInfo map[string]interface{}, stype string) {
  154. var next = &net.UDPAddr{
  155. IP: net.ParseIP(config.Conf.Udp.Project.Addr),
  156. Port: util.IntAll(config.Conf.Udp.Project.Port),
  157. }
  158. if stype == "bidding_history" {
  159. mapInfo["stype"] = "project_history"
  160. } else {
  161. mapInfo["stype"] = "project"
  162. }
  163. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  164. mapInfo["key"] = key
  165. log.Info("udp project node", zap.Any("mapinfo:", mapInfo))
  166. datas, _ := json.Marshal(mapInfo)
  167. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  168. UdpTaskMap.Store(key, node)
  169. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  170. }
  171. func NextNodeBidData(mapInfo map[string]interface{}) {
  172. next := &net.UDPAddr{
  173. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  174. Port: util.IntAll(config.Conf.Udp.Next.Port),
  175. }
  176. mapInfo["stype"] = "biddingdata"
  177. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  178. log.Info("udp es node", zap.Any("mapinfo:", mapInfo))
  179. datas, _ := json.Marshal(mapInfo)
  180. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  181. }
  182. func NextNodeTidbQyxy(mapInfo map[string]interface{}) {
  183. next := &net.UDPAddr{
  184. IP: net.ParseIP(config.Conf.Udp.Tidb.Addr),
  185. Port: util.IntAll(config.Conf.Udp.Tidb.Port),
  186. }
  187. mapInfo["stype"] = config.Conf.Udp.Tidb.Stype
  188. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  189. log.Info("udp tidb-qyxy node", zap.Any("mapinfo:", mapInfo))
  190. datas, _ := json.Marshal(mapInfo)
  191. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  192. UdpTaskMap.Store(mapInfo["key"], node)
  193. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  194. }
  195. func NextNodeTidb(mapInfo map[string]interface{}, stype string) {
  196. next := &net.UDPAddr{
  197. IP: net.ParseIP(config.Conf.Udp.Tidb1.Addr),
  198. Port: util.IntAll(config.Conf.Udp.Tidb1.Port),
  199. }
  200. mapInfo["stype"] = stype
  201. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  202. log.Info("udp tidb-bidding node", zap.Any("mapinfo:", mapInfo))
  203. datas, _ := json.Marshal(mapInfo)
  204. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  205. UdpTaskMap.Store(mapInfo["key"], node)
  206. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  207. }
  208. // NextNodeHn @Description 郑坤 海南数据处理
  209. // @Author J 2022/10/28 09:26
  210. func NextNodeHn(mapInfo map[string]interface{}) {
  211. next := &net.UDPAddr{
  212. IP: net.ParseIP(config.Conf.Udp.Tidb2.Addr),
  213. Port: util.IntAll(config.Conf.Udp.Tidb2.Port),
  214. }
  215. mapInfo["stype"] = "hainan"
  216. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  217. log.Info("NextNodeTidb", zap.Any("mapinfo:", mapInfo))
  218. datas, _ := json.Marshal(mapInfo)
  219. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  220. UdpTaskMap.Store(mapInfo["key"], node)
  221. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  222. }
  223. func UpdateBidding() {
  224. arru := make([][]map[string]interface{}, MgoBulkSize)
  225. indexu := 0
  226. for {
  227. select {
  228. case v := <-updateBidPool:
  229. arru[indexu] = v
  230. indexu++
  231. if indexu == MgoBulkSize {
  232. updateBidSp <- true
  233. go func(arru [][]map[string]interface{}) {
  234. defer func() {
  235. <-updateBidSp
  236. }()
  237. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  238. }(arru)
  239. arru = make([][]map[string]interface{}, MgoBulkSize)
  240. indexu = 0
  241. }
  242. case <-time.After(1000 * time.Millisecond):
  243. if indexu > 0 {
  244. updateBidSp <- true
  245. go func(arru [][]map[string]interface{}) {
  246. defer func() {
  247. <-updateBidSp
  248. }()
  249. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  250. }(arru[:indexu])
  251. arru = make([][]map[string]interface{}, MgoBulkSize)
  252. indexu = 0
  253. }
  254. }
  255. }
  256. }
  257. func UpdateExtract() {
  258. arru := make([][]map[string]interface{}, MgoBulkSize)
  259. indexu := 0
  260. for {
  261. select {
  262. case v := <-updateExtPool:
  263. arru[indexu] = v
  264. indexu++
  265. if indexu == MgoBulkSize {
  266. updateExtSp <- true
  267. go func(arru [][]map[string]interface{}) {
  268. defer func() {
  269. <-updateExtSp
  270. }()
  271. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  272. }(arru)
  273. arru = make([][]map[string]interface{}, MgoBulkSize)
  274. indexu = 0
  275. }
  276. case <-time.After(1000 * time.Millisecond):
  277. if indexu > 0 {
  278. updateExtSp <- true
  279. go func(arru [][]map[string]interface{}) {
  280. defer func() {
  281. <-updateExtSp
  282. }()
  283. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  284. }(arru[:indexu])
  285. arru = make([][]map[string]interface{}, MgoBulkSize)
  286. indexu = 0
  287. }
  288. }
  289. }
  290. }
  291. func checkMapJob() {
  292. if config.Conf.Mail.Send {
  293. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  294. for {
  295. UdpTaskMap.Range(func(k, v interface{}) bool {
  296. now := time.Now().Unix()
  297. node, _ := v.(*UdpNode)
  298. if now-node.timestamp > 120 {
  299. node.retry++
  300. if node.retry > 5 {
  301. UdpTaskMap.Delete(k)
  302. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", config.Conf.Mail.Api, config.Conf.Mail.To, "field-sync-send-fail", k.(string)))
  303. if err == nil {
  304. defer res.Body.Close()
  305. read, err := ioutil.ReadAll(res.Body)
  306. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  307. }
  308. } else {
  309. log.Info("udp重发", zap.Any("k:", k))
  310. //UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  311. }
  312. } else if now-node.timestamp > 10 {
  313. log.Info("udp任务超时中..", zap.Any("k:", k))
  314. }
  315. return true
  316. })
  317. time.Sleep(60 * time.Second)
  318. }
  319. }
  320. }
  321. // @Description nsq处理id不变,内容替换的竞品数据
  322. // @Author J 2022/8/10 11:40
  323. func nsqMethod() {
  324. var err error
  325. Mcmer, err = gonsq.NewConsumer(&gonsq.Cconfig{
  326. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  327. Addr: config.Conf.Nsq.Addr,
  328. ConnectType: 0, //默认连接nsqd
  329. Topic: config.Conf.Nsq.Topic,
  330. Channel: config.Conf.Nsq.Channel,
  331. Concurrent: config.Conf.Nsq.Concurrent, //并发数
  332. })
  333. if err != nil {
  334. log.Error("nsqMethod err", zap.Error(err))
  335. }
  336. for {
  337. select {
  338. case obj := <-Mcmer.Ch: //从通道读取即可
  339. id := strings.Split(util.ObjToString(obj), "=")
  340. if bson.IsObjectIdHex(id[1]) {
  341. taskinfo(id[1])
  342. } else {
  343. log.Info("jy nsq id err", zap.String("id", id[1]))
  344. }
  345. }
  346. }
  347. }