main.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 *elastic.Elastic
  25. UdpClient udp.UdpClient
  26. UdpTaskMap = &sync.Map{}
  27. JyUdpAddr *net.UDPAddr
  28. EsBulkSize = 200 // 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. detailLength = 50000 // es保存detail长度
  38. fileLength = 50000 // es保存附件文本长度
  39. pscopeLength = 32766 // projectscope长度
  40. )
  41. func init() {
  42. config.Init("./common.toml")
  43. InitLog()
  44. InitMgo()
  45. InitEs()
  46. oss.InitOss()
  47. JyUdpAddr = &net.UDPAddr{
  48. IP: net.ParseIP(config.Conf.Udp.JyAddr),
  49. Port: util.IntAll(config.Conf.Udp.JyPort),
  50. }
  51. log.Info("init success")
  52. }
  53. func main() {
  54. go checkMapJob()
  55. go task_index()
  56. go UpdateBidding()
  57. go SaveEsMethod()
  58. go SaveAllEsMethod()
  59. go SaveProjectEs()
  60. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  61. UdpClient.Listen(processUdpMsg)
  62. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  63. ch := make(chan bool, 1)
  64. <-ch
  65. }
  66. var pool = make(chan bool, 20)
  67. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  68. switch act {
  69. case udp.OP_TYPE_DATA:
  70. var mapInfo map[string]interface{}
  71. err := json.Unmarshal(data, &mapInfo)
  72. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  73. if err != nil {
  74. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  75. } else if mapInfo != nil {
  76. key, _ := mapInfo["key"].(string)
  77. if key == "" {
  78. key = "udpok"
  79. }
  80. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  81. tasktype, _ := mapInfo["stype"].(string)
  82. switch tasktype {
  83. case "index-by-id":
  84. pool <- true
  85. go func() {
  86. defer func() {
  87. <-pool
  88. }()
  89. biddingTaskById(mapInfo)
  90. }()
  91. case "bidding":
  92. pool <- true
  93. go func() {
  94. defer func() {
  95. <-pool
  96. }()
  97. biddingTask(mapInfo)
  98. }()
  99. case "project":
  100. pool <- true
  101. go func() {
  102. defer func() {
  103. <-pool
  104. }()
  105. projectTask(data, mapInfo)
  106. }()
  107. case "biddingdata": //bidding全量数据
  108. pool <- true
  109. go func() {
  110. defer func() {
  111. <-pool
  112. }()
  113. biddingDataTask(data, mapInfo)
  114. }()
  115. case "biddingdelbyextracttype": //根据extracttype删除es
  116. pool <- true
  117. go func() {
  118. defer func() {
  119. <-pool
  120. }()
  121. biddingDelByExtracttype(data, mapInfo)
  122. }()
  123. default:
  124. pool <- true
  125. go func() {
  126. defer func() {
  127. <-pool
  128. }()
  129. log.Info("err", zap.Any("mapInfo", mapInfo))
  130. }()
  131. }
  132. }
  133. case udp.OP_NOOP: //下个节点回应
  134. ok := string(data)
  135. if ok != "" {
  136. log.Info("udp re", zap.String("data:", ok))
  137. UdpTaskMap.Delete(ok)
  138. }
  139. }
  140. }
  141. func task_index() {
  142. c := cron.New()
  143. _ = c.AddFunc("0 0 0 * * ?", func() { task_winneres() }) //每天凌晨执行一次winner生索引
  144. _ = c.AddFunc("0 0 1 * * ?", func() { task_buyeres() }) //每天1点执行一次buyer生索引
  145. c.Start()
  146. }
  147. func task_winneres() {
  148. log.Info("定时任务,winneres")
  149. winnerEsTaskOnce()
  150. }
  151. func task_buyeres() {
  152. log.Info("定时任务,buyeres")
  153. buyerEsTaskOnce()
  154. }
  155. type UdpNode struct {
  156. data []byte
  157. addr *net.UDPAddr
  158. timestamp int64
  159. retry int
  160. }
  161. func UpdateBidding() {
  162. arru := make([][]map[string]interface{}, 200)
  163. indexu := 0
  164. for {
  165. select {
  166. case v := <-updateBiddingPool:
  167. arru[indexu] = v
  168. indexu++
  169. if indexu == 200 {
  170. updateBiddingSp <- true
  171. go func(arru [][]map[string]interface{}) {
  172. defer func() {
  173. <-updateBiddingSp
  174. }()
  175. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  176. }(arru)
  177. arru = make([][]map[string]interface{}, 200)
  178. indexu = 0
  179. }
  180. case <-time.After(1000 * time.Millisecond):
  181. if indexu > 0 {
  182. updateBiddingSp <- true
  183. go func(arru [][]map[string]interface{}) {
  184. defer func() {
  185. <-updateBiddingSp
  186. }()
  187. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  188. }(arru[:indexu])
  189. arru = make([][]map[string]interface{}, 200)
  190. indexu = 0
  191. }
  192. }
  193. }
  194. }
  195. func SaveEsMethod() {
  196. arru := make([]map[string]interface{}, EsBulkSize)
  197. indexu := 0
  198. for {
  199. select {
  200. case v := <-saveEsPool:
  201. arru[indexu] = v
  202. indexu++
  203. if indexu == EsBulkSize {
  204. saveEsSp <- true
  205. go func(arru []map[string]interface{}) {
  206. defer func() {
  207. <-saveEsSp
  208. }()
  209. Es.BulkSave(config.Conf.DB.Es.IndexB, config.Conf.DB.Es.TypeB, &arru, true)
  210. }(arru)
  211. arru = make([]map[string]interface{}, EsBulkSize)
  212. indexu = 0
  213. }
  214. case <-time.After(1000 * time.Millisecond):
  215. if indexu > 0 {
  216. saveEsSp <- true
  217. go func(arru []map[string]interface{}) {
  218. defer func() {
  219. <-saveEsSp
  220. }()
  221. Es.BulkSave(config.Conf.DB.Es.IndexB, config.Conf.DB.Es.TypeB, &arru, true)
  222. }(arru[:indexu])
  223. arru = make([]map[string]interface{}, EsBulkSize)
  224. indexu = 0
  225. }
  226. }
  227. }
  228. }
  229. func SaveAllEsMethod() {
  230. arru := make([]map[string]interface{}, EsBulkSize)
  231. indexu := 0
  232. for {
  233. select {
  234. case v := <-saveEsAllPool:
  235. arru[indexu] = v
  236. indexu++
  237. if indexu == EsBulkSize {
  238. saveEsAllSp <- true
  239. go func(arru []map[string]interface{}) {
  240. defer func() {
  241. <-saveEsAllSp
  242. }()
  243. Es.BulkSave("biddingall", "bidding", &arru, true)
  244. }(arru)
  245. arru = make([]map[string]interface{}, EsBulkSize)
  246. indexu = 0
  247. }
  248. case <-time.After(1000 * time.Millisecond):
  249. if indexu > 0 {
  250. saveEsAllSp <- true
  251. go func(arru []map[string]interface{}) {
  252. defer func() {
  253. <-saveEsAllSp
  254. }()
  255. Es.BulkSave("biddingall", "bidding", &arru, true)
  256. }(arru[:indexu])
  257. arru = make([]map[string]interface{}, EsBulkSize)
  258. indexu = 0
  259. }
  260. }
  261. }
  262. }
  263. func SaveProjectEs() {
  264. arru := make([]map[string]interface{}, EsBulkSize)
  265. indexu := 0
  266. for {
  267. select {
  268. case v := <-saveProjectEsPool:
  269. arru[indexu] = v
  270. indexu++
  271. if indexu == EsBulkSize {
  272. saveProjectSp <- true
  273. go func(arru []map[string]interface{}) {
  274. defer func() {
  275. <-saveProjectSp
  276. }()
  277. Es.BulkSave(config.Conf.DB.Es.IndexP, config.Conf.DB.Es.TypeP, &arru, true)
  278. }(arru)
  279. arru = make([]map[string]interface{}, EsBulkSize)
  280. indexu = 0
  281. }
  282. case <-time.After(1000 * time.Millisecond):
  283. if indexu > 0 {
  284. saveProjectSp <- true
  285. go func(arru []map[string]interface{}) {
  286. defer func() {
  287. <-saveProjectSp
  288. }()
  289. Es.BulkSave(config.Conf.DB.Es.IndexP, config.Conf.DB.Es.TypeP, &arru, true)
  290. }(arru[:indexu])
  291. arru = make([]map[string]interface{}, EsBulkSize)
  292. indexu = 0
  293. }
  294. }
  295. }
  296. }
  297. func checkMapJob() {
  298. if config.Conf.Mail.Send {
  299. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  300. for {
  301. UdpTaskMap.Range(func(k, v interface{}) bool {
  302. now := time.Now().Unix()
  303. node, _ := v.(*UdpNode)
  304. if now-node.timestamp > 120 {
  305. node.retry++
  306. if node.retry > 5 {
  307. UdpTaskMap.Delete(k)
  308. 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)))
  309. if err == nil {
  310. defer res.Body.Close()
  311. read, err := ioutil.ReadAll(res.Body)
  312. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  313. }
  314. } else {
  315. log.Info("udp重发", zap.Any("k:", k))
  316. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  317. }
  318. } else if now-node.timestamp > 10 {
  319. log.Info("udp任务超时中..", zap.Any("k:", k))
  320. }
  321. return true
  322. })
  323. time.Sleep(60 * time.Second)
  324. }
  325. }
  326. }