main.go 9.8 KB

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