Ver Fonte

优化url mapping更新逻辑

路佳明 há 3 meses atrás
pai
commit
71070ae3cf
1 ficheiros alterados com 32 adições e 10 exclusões
  1. 32 10
      cmd/consumer/traceurl/traceurl.go

+ 32 - 10
cmd/consumer/traceurl/traceurl.go

@@ -265,26 +265,48 @@ func (tg *taskGroup) upUrlMappings(wg *sync.WaitGroup) {
 		urlMappings := []tinyUrlMapping{}
 		fields := "Method, Route, ServiceName, AppAlias"
 		if err := chdb.Model(&models.TracesURL{}).Distinct(fields).
-			Where(fmt.Sprintf("Timestamp>=now()-INTERVAL %d SECOND", seconds*2)).Find(&urlMappings).Error; err != nil {
+			// 由于Timestamp不是入库时间,这里需要统计时间长一些
+			Where(fmt.Sprintf("Timestamp>=now()-INTERVAL %d SECOND", seconds*10)).Find(&urlMappings).Error; err != nil {
 			log.Errorf("获取route失败: %s", err)
 			continue
 		}
 		log.Infof("获取 traces url 基础数据成功,共计%d条数据", len(urlMappings))
+		svcNameMap := map[string]struct{}{}
+		for _, um := range urlMappings {
+			svcNameMap[um.ServiceName] = struct{}{}
+		}
+		serviceNames := make([]string, 0, len(svcNameMap))
+		for svcName := range svcNameMap {
+			serviceNames = append(serviceNames, svcName)
+		}
+		existsUrlMappings := []tinyUrlMapping{}
+		err := mydb.Model(&models.UrlMapping{}).Where("service_name in ? and is_perfect_match=1", serviceNames).
+			Distinct("app_alias, service_name, method, url as route").Scan(&existsUrlMappings).Error
+		if err != nil {
+			log.Errorf("获取已经存在的url mapping失败")
+		}
+		existsUrlMappingsMap := map[string]struct{}{}
+		for _, um := range existsUrlMappings {
+			existsUrlMappingsMap[fmt.Sprintf("%s-%s-%s-%s", um.AppAlias, um.ServiceName, um.Method, um.Route)] = struct{}{}
+		}
 		newUrlMappings := map[string]models.UrlMapping{}
 		for _, um := range urlMappings {
 			// 检测当前route是否存在
-			exists := false
-			if config.ApplicationConfig.Mode == "dev" {
-				exists, _ = query.NewUrlMapping().Exists(um.AppAlias, um.ServiceName, um.Method, um.Route)
-			} else {
-				exists, _ = query.NewUrlMapping().ExistsCache(um.AppAlias, um.ServiceName, um.Method, um.Route)
-			}
+			_, exists := existsUrlMappingsMap[fmt.Sprintf("%s-%s-%s-%s", um.AppAlias, um.ServiceName, um.Method, um.Route)]
 			if exists {
-				mydb.Model(&models.UrlMapping{}).
-					Where("app_alias=? and service_name=? and method=? and url=?", um.AppAlias, um.ServiceName, um.Method, um.Route).
-					Update("is_perfect_match", 1)
 				continue
 			}
+			// if config.ApplicationConfig.Mode == "dev" {
+			// 	exists, _ = query.NewUrlMapping().Exists(um.AppAlias, um.ServiceName, um.Method, um.Route)
+			// } else {
+			// 	exists, _ = query.NewUrlMapping().ExistsCache(um.AppAlias, um.ServiceName, um.Method, um.Route)
+			// }
+			// if exists {
+			// 	mydb.Model(&models.UrlMapping{}).
+			// 		Where("app_alias=? and service_name=? and method=? and url=?", um.AppAlias, um.ServiceName, um.Method, um.Route).
+			// 		Update("is_perfect_match", 1)
+			// 	continue
+			// }
 			log.Infof("AppAlias: %s, ServiceName: %s, Method: %s, Route: %s 在url mapping中不存在", um.AppAlias, um.ServiceName, um.Method, um.Route)
 			// 如果route不存在
 			arr := strings.Split(um.Route, "/")