main.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. ch := make(chan bool, 1)
  55. <-ch
  56. }
  57. var pool = make(chan bool, 20)
  58. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  59. defer util.Catch()
  60. switch act {
  61. case udp.OP_TYPE_DATA: //上个节点的数据
  62. var mapInfo map[string]interface{}
  63. err := json.Unmarshal(data, &mapInfo)
  64. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  65. if err != nil {
  66. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  67. } else if mapInfo != nil {
  68. key, _ := mapInfo["key"].(string)
  69. if key == "" {
  70. key = "udpok"
  71. }
  72. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  73. tasktype, _ := mapInfo["stype"].(string)
  74. switch tasktype {
  75. case "bidding":
  76. pool <- true
  77. go func() {
  78. defer func() {
  79. <-pool
  80. }()
  81. biddingTask(data, mapInfo)
  82. }()
  83. case "bidding_history": //增量id段历史数据
  84. pool <- true
  85. go func() {
  86. defer func() {
  87. <-pool
  88. }()
  89. biddingTask(data, mapInfo)
  90. }()
  91. case "bidding_all": //id段存量数据
  92. pool <- true
  93. go func() {
  94. defer func() {
  95. <-pool
  96. }()
  97. biddingAllTask(data, mapInfo)
  98. }()
  99. case "monitor":
  100. log.Info("monitor", zap.Any("mapInfo:", mapInfo))
  101. default:
  102. pool <- true
  103. go func() {
  104. defer func() {
  105. <-pool
  106. }()
  107. log.Error("err", zap.Any("mapinfo", mapInfo))
  108. }()
  109. }
  110. }
  111. case udp.OP_NOOP:
  112. ok := string(data)
  113. if ok != "" {
  114. log.Info("udp re", zap.String("data:", ok))
  115. UdpTaskMap.Delete(ok)
  116. }
  117. }
  118. }
  119. type UdpNode struct {
  120. data []byte
  121. addr *net.UDPAddr
  122. timestamp int64
  123. retry int
  124. }
  125. func NextNode(mapInfo map[string]interface{}, stype string) {
  126. var next = &net.UDPAddr{
  127. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  128. Port: util.IntAll(config.Conf.Udp.Next.Port),
  129. }
  130. mapInfo["stype"] = stype
  131. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), stype)
  132. mapInfo["key"] = key
  133. log.Info("udp es node", zap.Any("mapinfo:", mapInfo))
  134. datas, _ := json.Marshal(mapInfo)
  135. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  136. UdpTaskMap.Store(key, node)
  137. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  138. }
  139. func NextNodePro(mapInfo map[string]interface{}, stype string) {
  140. var next = &net.UDPAddr{
  141. IP: net.ParseIP(config.Conf.Udp.Project.Addr),
  142. Port: util.IntAll(config.Conf.Udp.Project.Port),
  143. }
  144. if stype == "bidding_history" {
  145. mapInfo["stype"] = "project_history"
  146. } else {
  147. mapInfo["stype"] = "project"
  148. }
  149. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  150. mapInfo["key"] = key
  151. log.Info("udp project node", zap.Any("mapinfo:", mapInfo))
  152. datas, _ := json.Marshal(mapInfo)
  153. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  154. UdpTaskMap.Store(key, node)
  155. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  156. }
  157. func NextNodeBidData(mapInfo map[string]interface{}) {
  158. next := &net.UDPAddr{
  159. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  160. Port: util.IntAll(config.Conf.Udp.Next.Port),
  161. }
  162. mapInfo["stype"] = "biddingdata"
  163. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  164. log.Info("udp es node", zap.Any("mapinfo:", mapInfo))
  165. datas, _ := json.Marshal(mapInfo)
  166. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  167. }
  168. func NextNodeTidbQyxy(mapInfo map[string]interface{}) {
  169. next := &net.UDPAddr{
  170. IP: net.ParseIP(config.Conf.Udp.Tidb.Addr),
  171. Port: util.IntAll(config.Conf.Udp.Tidb.Port),
  172. }
  173. mapInfo["stype"] = config.Conf.Udp.Tidb.Stype
  174. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  175. log.Info("udp tidb-qyxy node", zap.Any("mapinfo:", mapInfo))
  176. datas, _ := json.Marshal(mapInfo)
  177. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  178. UdpTaskMap.Store(mapInfo["key"], node)
  179. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  180. }
  181. func NextNodeTidb(mapInfo map[string]interface{}, stype string) {
  182. next := &net.UDPAddr{
  183. IP: net.ParseIP(config.Conf.Udp.Tidb1.Addr),
  184. Port: util.IntAll(config.Conf.Udp.Tidb1.Port),
  185. }
  186. mapInfo["stype"] = stype
  187. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  188. log.Info("udp tidb-bidding node", zap.Any("mapinfo:", mapInfo))
  189. datas, _ := json.Marshal(mapInfo)
  190. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  191. UdpTaskMap.Store(mapInfo["key"], node)
  192. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  193. }
  194. // NextNodeHn @Description 郑坤 海南数据处理
  195. // @Author J 2022/10/28 09:26
  196. func NextNodeHn(mapInfo map[string]interface{}) {
  197. next := &net.UDPAddr{
  198. IP: net.ParseIP(config.Conf.Udp.Tidb2.Addr),
  199. Port: util.IntAll(config.Conf.Udp.Tidb2.Port),
  200. }
  201. mapInfo["stype"] = "hainan"
  202. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  203. log.Info("NextNodeTidb", zap.Any("mapinfo:", mapInfo))
  204. datas, _ := json.Marshal(mapInfo)
  205. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  206. UdpTaskMap.Store(mapInfo["key"], node)
  207. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  208. }
  209. func UpdateBidding() {
  210. arru := make([][]map[string]interface{}, MgoBulkSize)
  211. indexu := 0
  212. for {
  213. select {
  214. case v := <-updateBidPool:
  215. arru[indexu] = v
  216. indexu++
  217. if indexu == MgoBulkSize {
  218. updateBidSp <- true
  219. go func(arru [][]map[string]interface{}) {
  220. defer func() {
  221. <-updateBidSp
  222. }()
  223. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  224. }(arru)
  225. arru = make([][]map[string]interface{}, MgoBulkSize)
  226. indexu = 0
  227. }
  228. case <-time.After(1000 * time.Millisecond):
  229. if indexu > 0 {
  230. updateBidSp <- true
  231. go func(arru [][]map[string]interface{}) {
  232. defer func() {
  233. <-updateBidSp
  234. }()
  235. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  236. }(arru[:indexu])
  237. arru = make([][]map[string]interface{}, MgoBulkSize)
  238. indexu = 0
  239. }
  240. }
  241. }
  242. }
  243. func UpdateExtract() {
  244. arru := make([][]map[string]interface{}, MgoBulkSize)
  245. indexu := 0
  246. for {
  247. select {
  248. case v := <-updateExtPool:
  249. arru[indexu] = v
  250. indexu++
  251. if indexu == MgoBulkSize {
  252. updateExtSp <- true
  253. go func(arru [][]map[string]interface{}) {
  254. defer func() {
  255. <-updateExtSp
  256. }()
  257. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  258. }(arru)
  259. arru = make([][]map[string]interface{}, MgoBulkSize)
  260. indexu = 0
  261. }
  262. case <-time.After(1000 * time.Millisecond):
  263. if indexu > 0 {
  264. updateExtSp <- true
  265. go func(arru [][]map[string]interface{}) {
  266. defer func() {
  267. <-updateExtSp
  268. }()
  269. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  270. }(arru[:indexu])
  271. arru = make([][]map[string]interface{}, MgoBulkSize)
  272. indexu = 0
  273. }
  274. }
  275. }
  276. }
  277. func checkMapJob() {
  278. if config.Conf.Mail.Send {
  279. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  280. for {
  281. UdpTaskMap.Range(func(k, v interface{}) bool {
  282. now := time.Now().Unix()
  283. node, _ := v.(*UdpNode)
  284. if now-node.timestamp > 120 {
  285. node.retry++
  286. if node.retry > 5 {
  287. UdpTaskMap.Delete(k)
  288. 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)))
  289. if err == nil {
  290. defer res.Body.Close()
  291. read, err := ioutil.ReadAll(res.Body)
  292. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  293. }
  294. } else {
  295. log.Info("udp重发", zap.Any("k:", k))
  296. //UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  297. }
  298. } else if now-node.timestamp > 10 {
  299. log.Info("udp任务超时中..", zap.Any("k:", k))
  300. }
  301. return true
  302. })
  303. time.Sleep(60 * time.Second)
  304. }
  305. }
  306. }
  307. // @Description nsq处理id不变,内容替换的竞品数据
  308. // @Author J 2022/8/10 11:40
  309. func nsqMethod() {
  310. var err error
  311. Mcmer, err = gonsq.NewConsumer(&gonsq.Cconfig{
  312. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  313. Addr: config.Conf.Nsq.Addr,
  314. ConnectType: 0, //默认连接nsqd
  315. Topic: config.Conf.Nsq.Topic,
  316. Channel: config.Conf.Nsq.Channel,
  317. Concurrent: config.Conf.Nsq.Concurrent, //并发数
  318. })
  319. if err != nil {
  320. log.Error("nsqMethod err", zap.Error(err))
  321. }
  322. for {
  323. select {
  324. case obj := <-Mcmer.Ch: //从通道读取即可
  325. id := strings.Split(util.ObjToString(obj), "=")
  326. if bson.IsObjectIdHex(id[1]) {
  327. taskinfo(id[1])
  328. } else {
  329. log.Info("jy nsq id err", zap.String("id", id[1]))
  330. }
  331. }
  332. }
  333. }