main.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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)
  30. updateBiddingSp = make(chan bool, 5)
  31. saveEsPool = make(chan map[string]interface{}, 5000)
  32. saveEsSp = make(chan bool, 5)
  33. saveProjectEsPool = make(chan map[string]interface{}, 5000)
  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()
  58. //go task_index()
  59. //go UpdateBidding()
  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": //根据extracttype删除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. func UpdateBidding() {
  182. arru := make([][]map[string]interface{}, 200)
  183. indexu := 0
  184. for {
  185. select {
  186. case v := <-updateBiddingPool:
  187. arru[indexu] = v
  188. indexu++
  189. if indexu == 200 {
  190. updateBiddingSp <- true
  191. go func(arru [][]map[string]interface{}) {
  192. defer func() {
  193. <-updateBiddingSp
  194. }()
  195. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  196. }(arru)
  197. arru = make([][]map[string]interface{}, 200)
  198. indexu = 0
  199. }
  200. case <-time.After(1000 * time.Millisecond):
  201. if indexu > 0 {
  202. updateBiddingSp <- true
  203. go func(arru [][]map[string]interface{}) {
  204. defer func() {
  205. <-updateBiddingSp
  206. }()
  207. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  208. }(arru[:indexu])
  209. arru = make([][]map[string]interface{}, 200)
  210. indexu = 0
  211. }
  212. }
  213. }
  214. }
  215. func SaveBidErr() {
  216. arru := make([]map[string]interface{}, 200)
  217. indexu := 0
  218. for {
  219. select {
  220. case v := <-saveErrBidPool:
  221. arru[indexu] = v
  222. indexu++
  223. if indexu == 200 {
  224. saveBidSp <- true
  225. go func(arru []map[string]interface{}) {
  226. defer func() {
  227. <-saveBidSp
  228. }()
  229. MgoB.SaveBulk("bidding_es_err_record", arru...)
  230. }(arru)
  231. arru = make([]map[string]interface{}, 200)
  232. indexu = 0
  233. }
  234. case <-time.After(1000 * time.Millisecond):
  235. if indexu > 0 {
  236. saveBidSp <- true
  237. go func(arru []map[string]interface{}) {
  238. defer func() {
  239. <-saveBidSp
  240. }()
  241. MgoB.SaveBulk("bidding_es_err_record", arru...)
  242. }(arru[:indexu])
  243. arru = make([]map[string]interface{}, 200)
  244. indexu = 0
  245. }
  246. }
  247. }
  248. }
  249. func SaveEsMethod() {
  250. arru := make([]map[string]interface{}, EsBulkSize)
  251. indexu := 0
  252. for {
  253. select {
  254. case v := <-saveEsPool:
  255. arru[indexu] = v
  256. indexu++
  257. if indexu == EsBulkSize {
  258. saveEsSp <- true
  259. go func(arru []map[string]interface{}) {
  260. defer func() {
  261. <-saveEsSp
  262. }()
  263. Es.BulkSave(config.Conf.DB.Es.IndexB, config.Conf.DB.Es.TypeB, &arru, true)
  264. }(arru)
  265. arru = make([]map[string]interface{}, EsBulkSize)
  266. indexu = 0
  267. }
  268. case <-time.After(1000 * time.Millisecond):
  269. if indexu > 0 {
  270. saveEsSp <- true
  271. go func(arru []map[string]interface{}) {
  272. defer func() {
  273. <-saveEsSp
  274. }()
  275. Es.BulkSave(config.Conf.DB.Es.IndexB, config.Conf.DB.Es.TypeB, &arru, true)
  276. }(arru[:indexu])
  277. arru = make([]map[string]interface{}, EsBulkSize)
  278. indexu = 0
  279. }
  280. }
  281. }
  282. }
  283. func SaveAllEsMethod() {
  284. arru := make([]map[string]interface{}, EsBulkSize)
  285. indexu := 0
  286. for {
  287. select {
  288. case v := <-saveEsAllPool:
  289. arru[indexu] = v
  290. indexu++
  291. if indexu == EsBulkSize {
  292. saveEsAllSp <- true
  293. go func(arru []map[string]interface{}) {
  294. defer func() {
  295. <-saveEsAllSp
  296. }()
  297. Es1.BulkSave("biddingall", "bidding", &arru, true)
  298. }(arru)
  299. arru = make([]map[string]interface{}, EsBulkSize)
  300. indexu = 0
  301. }
  302. case <-time.After(1000 * time.Millisecond):
  303. if indexu > 0 {
  304. saveEsAllSp <- true
  305. go func(arru []map[string]interface{}) {
  306. defer func() {
  307. <-saveEsAllSp
  308. }()
  309. Es1.BulkSave("biddingall", "bidding", &arru, true)
  310. }(arru[:indexu])
  311. arru = make([]map[string]interface{}, EsBulkSize)
  312. indexu = 0
  313. }
  314. }
  315. }
  316. }
  317. func SaveProjectEs() {
  318. arru := make([]map[string]interface{}, EsBulkSize)
  319. indexu := 0
  320. for {
  321. select {
  322. case v := <-saveProjectEsPool:
  323. arru[indexu] = v
  324. indexu++
  325. if indexu == EsBulkSize {
  326. saveProjectSp <- true
  327. go func(arru []map[string]interface{}) {
  328. defer func() {
  329. <-saveProjectSp
  330. }()
  331. Es.BulkSave(config.Conf.DB.Es.IndexP, config.Conf.DB.Es.TypeP, &arru, true)
  332. }(arru)
  333. arru = make([]map[string]interface{}, EsBulkSize)
  334. indexu = 0
  335. }
  336. case <-time.After(1000 * time.Millisecond):
  337. if indexu > 0 {
  338. saveProjectSp <- true
  339. go func(arru []map[string]interface{}) {
  340. defer func() {
  341. <-saveProjectSp
  342. }()
  343. Es.BulkSave(config.Conf.DB.Es.IndexP, config.Conf.DB.Es.TypeP, &arru, true)
  344. }(arru[:indexu])
  345. arru = make([]map[string]interface{}, EsBulkSize)
  346. indexu = 0
  347. }
  348. }
  349. }
  350. }
  351. func checkMapJob() {
  352. if config.Conf.Mail.Send {
  353. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  354. for {
  355. UdpTaskMap.Range(func(k, v interface{}) bool {
  356. now := time.Now().Unix()
  357. node, _ := v.(*UdpNode)
  358. if now-node.timestamp > 120 {
  359. node.retry++
  360. if node.retry > 5 {
  361. UdpTaskMap.Delete(k)
  362. 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)))
  363. if err == nil {
  364. defer res.Body.Close()
  365. read, err := ioutil.ReadAll(res.Body)
  366. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  367. }
  368. } else {
  369. log.Info("udp重发", zap.Any("k:", k))
  370. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  371. }
  372. } else if now-node.timestamp > 10 {
  373. log.Info("udp任务超时中..", zap.Any("k:", k))
  374. }
  375. return true
  376. })
  377. time.Sleep(60 * time.Second)
  378. }
  379. }
  380. }
  381. func task() {
  382. sess := MgoB.GetMgoConn()
  383. defer MgoB.DestoryMongoConn(sess)
  384. ch := make(chan bool, 10)
  385. wg := &sync.WaitGroup{}
  386. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  387. count := 0
  388. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  389. if count%1000 == 0 {
  390. util.Debug("current ---", count)
  391. }
  392. ch <- true
  393. wg.Add(1)
  394. go func(tmp map[string]interface{}) {
  395. defer func() {
  396. <-ch
  397. wg.Done()
  398. }()
  399. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  400. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  401. }
  402. }(tmp)
  403. tmp = make(map[string]interface{})
  404. }
  405. wg.Wait()
  406. util.Debug("over ---", count)
  407. }