main.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/elastic"
  5. "app.yhyue.com/data_processing/common_utils/log"
  6. "app.yhyue.com/data_processing/common_utils/mongodb"
  7. "app.yhyue.com/data_processing/common_utils/udp"
  8. "encoding/json"
  9. "esindex/config"
  10. "esindex/oss"
  11. "fmt"
  12. "github.com/robfig/cron"
  13. "go.uber.org/zap"
  14. "io/ioutil"
  15. "net"
  16. "net/http"
  17. "sync"
  18. "time"
  19. )
  20. var (
  21. MgoB *mongodb.MongodbSim
  22. MgoP *mongodb.MongodbSim
  23. MgoQ *mongodb.MongodbSim
  24. Es, Es1 *elastic.Elastic
  25. UdpClient udp.UdpClient
  26. UdpTaskMap = &sync.Map{}
  27. JyUdpAddr *net.UDPAddr
  28. EsBulkSize = 100 // es批量保存大小
  29. updateBiddingPool = make(chan []map[string]interface{}, 5000) //更新bingding数据
  30. updateBiddingSp = make(chan bool, 5)
  31. saveEsPool = make(chan map[string]interface{}, 5000) //保存binding数据到es
  32. saveEsSp = make(chan bool, 5)
  33. saveProjectEsPool = make(chan map[string]interface{}, 5000) //保存project数据到es
  34. saveProjectSp = make(chan bool, 5)
  35. saveEsAllPool = make(chan map[string]interface{}, 5000)
  36. saveEsAllSp = make(chan bool, 5)
  37. saveErrBidPool = make(chan map[string]interface{}, 5000)
  38. saveBidSp = make(chan bool, 5)
  39. detailLength = 50000 // es保存detail长度
  40. fileLength = 50000 // es保存附件文本长度
  41. pscopeLength = 32766 // projectscope长度
  42. )
  43. func init() {
  44. config.Init("./common.toml")
  45. InitLog()
  46. InitMgo()
  47. InitEs()
  48. InitField()
  49. oss.InitOss()
  50. JyUdpAddr = &net.UDPAddr{
  51. IP: net.ParseIP(config.Conf.Udp.JyAddr),
  52. Port: util.IntAll(config.Conf.Udp.JyPort),
  53. }
  54. log.Info("init success")
  55. }
  56. func main() {
  57. go checkMapJob() //udp 发送邮件
  58. go task_index() //定时同步更新winner_enterprise、buyer_enterprise ES索引;这个功能很少变动,几乎不需要维护
  59. go UpdateBidding() //更新bidding表数据
  60. go SaveEsMethod()
  61. go SaveAllEsMethod()
  62. go SaveProjectEs()
  63. go SaveBidErr()
  64. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  65. UdpClient.Listen(processUdpMsg)
  66. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  67. ch := make(chan bool, 1)
  68. <-ch
  69. }
  70. var pool = make(chan bool, 20)
  71. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  72. switch act {
  73. case udp.OP_TYPE_DATA:
  74. var mapInfo map[string]interface{}
  75. err := json.Unmarshal(data, &mapInfo)
  76. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  77. if err != nil {
  78. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  79. } else if mapInfo != nil {
  80. key, _ := mapInfo["key"].(string)
  81. if key == "" {
  82. key = "udpok"
  83. }
  84. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  85. tasktype, _ := mapInfo["stype"].(string)
  86. switch tasktype {
  87. case "index-by-id": //单个索引
  88. pool <- true
  89. go func() {
  90. defer func() {
  91. <-pool
  92. }()
  93. biddingTaskById(mapInfo)
  94. }()
  95. case "bidding":
  96. pool <- true
  97. go func() {
  98. defer func() {
  99. <-pool
  100. }()
  101. biddingTask(mapInfo)
  102. }()
  103. case "biddingall":
  104. pool <- true
  105. go func() {
  106. defer func() {
  107. <-pool
  108. }()
  109. biddingAllTask(mapInfo)
  110. }()
  111. case "bidding_history":
  112. pool <- true
  113. go func() {
  114. defer func() {
  115. <-pool
  116. }()
  117. biddingTask(mapInfo)
  118. }()
  119. case "project":
  120. pool <- true
  121. go func() {
  122. defer func() {
  123. <-pool
  124. }()
  125. projectTask(data, mapInfo)
  126. }()
  127. case "biddingdata": //bidding全量数据
  128. pool <- true
  129. go func() {
  130. defer func() {
  131. <-pool
  132. }()
  133. biddingDataTask(data, mapInfo)
  134. }()
  135. case "biddingdelbyextracttype": //根据bidding表extracttype=-1,删除es中重复数据
  136. pool <- true
  137. go func() {
  138. defer func() {
  139. <-pool
  140. }()
  141. biddingDelByExtracttype(data, mapInfo)
  142. }()
  143. default:
  144. pool <- true
  145. go func() {
  146. defer func() {
  147. <-pool
  148. }()
  149. log.Info("err", zap.Any("mapInfo", mapInfo))
  150. }()
  151. }
  152. }
  153. case udp.OP_NOOP: //下个节点回应
  154. ok := string(data)
  155. if ok != "" {
  156. log.Info("udp re", zap.String("data:", ok))
  157. UdpTaskMap.Delete(ok)
  158. }
  159. }
  160. }
  161. func task_index() {
  162. c := cron.New()
  163. _ = c.AddFunc("0 0 0 * * ?", func() { task_winneres() }) //每天凌晨执行一次winner生索引
  164. _ = c.AddFunc("0 0 1 * * ?", func() { task_buyeres() }) //每天1点执行一次buyer生索引
  165. c.Start()
  166. }
  167. func task_winneres() {
  168. log.Info("定时任务,winneres")
  169. winnerEsTaskOnce()
  170. }
  171. func task_buyeres() {
  172. log.Info("定时任务,buyeres")
  173. buyerEsTaskOnce()
  174. }
  175. type UdpNode struct {
  176. data []byte
  177. addr *net.UDPAddr
  178. timestamp int64
  179. retry int
  180. }
  181. //UpdateBidding 更新bidding表数据
  182. func UpdateBidding() {
  183. arru := make([][]map[string]interface{}, 200)
  184. indexu := 0
  185. for {
  186. select {
  187. case v := <-updateBiddingPool:
  188. arru[indexu] = v
  189. indexu++
  190. if indexu == 200 {
  191. updateBiddingSp <- true
  192. go func(arru [][]map[string]interface{}) {
  193. defer func() {
  194. <-updateBiddingSp
  195. }()
  196. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  197. }(arru)
  198. arru = make([][]map[string]interface{}, 200)
  199. indexu = 0
  200. }
  201. case <-time.After(1000 * time.Millisecond):
  202. if indexu > 0 {
  203. updateBiddingSp <- true
  204. go func(arru [][]map[string]interface{}) {
  205. defer func() {
  206. <-updateBiddingSp
  207. }()
  208. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  209. }(arru[:indexu])
  210. arru = make([][]map[string]interface{}, 200)
  211. indexu = 0
  212. }
  213. }
  214. }
  215. }
  216. func SaveBidErr() {
  217. arru := make([]map[string]interface{}, 200)
  218. indexu := 0
  219. for {
  220. select {
  221. case v := <-saveErrBidPool:
  222. arru[indexu] = v
  223. indexu++
  224. if indexu == 200 {
  225. saveBidSp <- true
  226. go func(arru []map[string]interface{}) {
  227. defer func() {
  228. <-saveBidSp
  229. }()
  230. MgoB.SaveBulk("bidding_es_err_record", arru...)
  231. }(arru)
  232. arru = make([]map[string]interface{}, 200)
  233. indexu = 0
  234. }
  235. case <-time.After(1000 * time.Millisecond):
  236. if indexu > 0 {
  237. saveBidSp <- true
  238. go func(arru []map[string]interface{}) {
  239. defer func() {
  240. <-saveBidSp
  241. }()
  242. MgoB.SaveBulk("bidding_es_err_record", arru...)
  243. }(arru[:indexu])
  244. arru = make([]map[string]interface{}, 200)
  245. indexu = 0
  246. }
  247. }
  248. }
  249. }
  250. //SaveEsMethod 保存到es
  251. func SaveEsMethod() {
  252. arru := make([]map[string]interface{}, EsBulkSize)
  253. indexu := 0
  254. for {
  255. select {
  256. case v := <-saveEsPool:
  257. arru[indexu] = v
  258. indexu++
  259. if indexu == EsBulkSize {
  260. saveEsSp <- true
  261. go func(arru []map[string]interface{}) {
  262. defer func() {
  263. <-saveEsSp
  264. }()
  265. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  266. }(arru)
  267. arru = make([]map[string]interface{}, EsBulkSize)
  268. indexu = 0
  269. }
  270. case <-time.After(1000 * time.Millisecond):
  271. if indexu > 0 {
  272. saveEsSp <- true
  273. go func(arru []map[string]interface{}) {
  274. defer func() {
  275. <-saveEsSp
  276. }()
  277. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  278. }(arru[:indexu])
  279. arru = make([]map[string]interface{}, EsBulkSize)
  280. indexu = 0
  281. }
  282. }
  283. }
  284. }
  285. func SaveAllEsMethod() {
  286. arru := make([]map[string]interface{}, EsBulkSize)
  287. indexu := 0
  288. for {
  289. select {
  290. case v := <-saveEsAllPool:
  291. arru[indexu] = v
  292. indexu++
  293. if indexu == EsBulkSize {
  294. saveEsAllSp <- true
  295. go func(arru []map[string]interface{}) {
  296. defer func() {
  297. <-saveEsAllSp
  298. }()
  299. Es1.BulkSave("biddingall", arru)
  300. }(arru)
  301. arru = make([]map[string]interface{}, EsBulkSize)
  302. indexu = 0
  303. }
  304. case <-time.After(1000 * time.Millisecond):
  305. if indexu > 0 {
  306. saveEsAllSp <- true
  307. go func(arru []map[string]interface{}) {
  308. defer func() {
  309. <-saveEsAllSp
  310. }()
  311. Es1.BulkSave("biddingall", arru)
  312. }(arru[:indexu])
  313. arru = make([]map[string]interface{}, EsBulkSize)
  314. indexu = 0
  315. }
  316. }
  317. }
  318. }
  319. func SaveProjectEs() {
  320. arru := make([]map[string]interface{}, EsBulkSize)
  321. indexu := 0
  322. for {
  323. select {
  324. case v := <-saveProjectEsPool:
  325. arru[indexu] = v
  326. indexu++
  327. if indexu == EsBulkSize {
  328. saveProjectSp <- true
  329. go func(arru []map[string]interface{}) {
  330. defer func() {
  331. <-saveProjectSp
  332. }()
  333. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  334. }(arru)
  335. arru = make([]map[string]interface{}, EsBulkSize)
  336. indexu = 0
  337. }
  338. case <-time.After(1000 * time.Millisecond):
  339. if indexu > 0 {
  340. saveProjectSp <- true
  341. go func(arru []map[string]interface{}) {
  342. defer func() {
  343. <-saveProjectSp
  344. }()
  345. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  346. }(arru[:indexu])
  347. arru = make([]map[string]interface{}, EsBulkSize)
  348. indexu = 0
  349. }
  350. }
  351. }
  352. }
  353. func checkMapJob() {
  354. if config.Conf.Mail.Send {
  355. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  356. for {
  357. UdpTaskMap.Range(func(k, v interface{}) bool {
  358. now := time.Now().Unix()
  359. node, _ := v.(*UdpNode)
  360. if now-node.timestamp > 120 {
  361. node.retry++
  362. if node.retry > 5 {
  363. UdpTaskMap.Delete(k)
  364. 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)))
  365. if err == nil {
  366. defer res.Body.Close()
  367. read, err := ioutil.ReadAll(res.Body)
  368. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  369. }
  370. } else {
  371. log.Info("udp重发", zap.Any("k:", k))
  372. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  373. }
  374. } else if now-node.timestamp > 10 {
  375. log.Info("udp任务超时中..", zap.Any("k:", k))
  376. }
  377. return true
  378. })
  379. time.Sleep(60 * time.Second)
  380. }
  381. }
  382. }
  383. func task() {
  384. sess := MgoB.GetMgoConn()
  385. defer MgoB.DestoryMongoConn(sess)
  386. ch := make(chan bool, 10)
  387. wg := &sync.WaitGroup{}
  388. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  389. count := 0
  390. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  391. if count%1000 == 0 {
  392. util.Debug("current ---", count)
  393. }
  394. ch <- true
  395. wg.Add(1)
  396. go func(tmp map[string]interface{}) {
  397. defer func() {
  398. <-ch
  399. wg.Done()
  400. }()
  401. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  402. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  403. }
  404. }(tmp)
  405. tmp = make(map[string]interface{})
  406. }
  407. wg.Wait()
  408. util.Debug("over ---", count)
  409. }