main.go 10 KB

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