From 2cd1af13bc4e7aec4c85b9fe88db2d294af6468f Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期日, 08 十月 2023 11:24:37 +0800 Subject: [PATCH] 修复集群同步功能 --- system-service/service/clusterService.go | 145 ++++++++++++++++++------------------------------ 1 files changed, 54 insertions(+), 91 deletions(-) diff --git a/system-service/service/clusterService.go b/system-service/service/clusterService.go index 07a9928..2f978ef 100644 --- a/system-service/service/clusterService.go +++ b/system-service/service/clusterService.go @@ -98,12 +98,8 @@ return m } -//鏍规嵁闆嗙兢鍚嶇О鍜屽瘑鐮佸垱寤洪泦缇� +// 鏍规嵁闆嗙兢鍚嶇О鍜屽瘑鐮佸垱寤洪泦缇� func (s ClusterService) Create(clusterName string, pwd string, virtualIp string) (bool, string) { - devTyp := GetDevType(config.Server.DeviceType) - if devTyp != DevType_Storage && devTyp != DevType_Analysis_Storage { - return false, "鍙湁鍒嗘瀽鍜屽瓨鍌ㄨ妭鐐规墠鑳藉垱寤洪泦缇�" - } clusterId := uuid.NewV4().String() pwd = config.ClusterSet.PwdPre + pwd var clusterE = models.Cluster{ @@ -116,32 +112,21 @@ arr, err := clusterE.FindAll() if err == nil && (arr == nil || len(arr) == 0) { - b := clusterE.Create() - if b { + err = clusterE.Create() + if err == nil { serf.InitAgent(context.Background()) - - //鍒涘缓es闆嗙兢 - if _, err := CreateOriginalCluster("/opt/elasticsearch/config", "/opt/vasystem/script", "/opt/vasystem/indexInit"); err != nil { - //鍒涘缓澶辫触锛屽垯serf闆嗙兢涔熻鎺ㄥ嚭 - s.Leave(false) - return false, err.Error() - } - - //鍒涘缓weedfs闆嗙兢 - if _, err = CreateWeedfsServer("/opt/vasystem/script"); err != nil { - s.Leave(false) - return false, err.Error() - } chMsg := protomsg.DbChangeMessage{ Id: clusterE.ClusterId, Table: protomsg.TableChanged_T_Cluster, Action: protomsg.DbAction_Insert, - Info: "", + Info: "create", } s.AddDbMessage(&chMsg) return true, clusterId + } else { + logger.Error("鍒濆鍖栭泦缇ゆ暟鎹簱淇℃伅澶辫触. ", err.Error()) } } else { if s.UpdateClusterName(clusterName, virtualIp) { @@ -186,7 +171,7 @@ } } -//鍔犲叆闆嗙兢 +// 鍔犲叆闆嗙兢 func (s ClusterService) JoinCluster(joinArg *vo.ClusterJoinVo) (bool, error) { start := time.Now() if config.Server.AnalyServerId == "" { @@ -218,50 +203,33 @@ agent, err := dbSync.Init(joinArg.ClusterId, joinArg.Password, config.Server.AnalyServerId, joinIps, config.ClusterSet.SerfSnapShotPath, conf) if err == nil && agent != nil { //鍔犲叆鎴愬姛 logger.Debug("AddCluster dbSync.Init success") + agent.RegisterHandleEventFunc(serf.HandleSerfEvent) serf.Agent = agent t := time.Now() - syncB := syncTableDataFromCluster(joinArg) - logger.Debugf("AddCluster syncTableDataFromCluster time=%v, syncB=%v", time.Since(t), syncB) - if syncB { - devTyp := GetDevType(config.Server.DeviceType) - if devTyp == DevType_Analysis_Storage || devTyp == DevType_Storage { //鍙湁鑳藉惎鍔‥S鐨勮妭鐐规墠鑳藉姞鍏S - go func() { - //娣诲姞ES鑺傜偣 - if _, err := AddCluster("/opt/elasticsearch/config", "/opt/vasystem/script", targetIp, "9200", config.EsConfig.StorePath); err != nil { - s.Leave(false) - return - } - - //娣诲姞weedfs鑺傜偣 - if _, err := AddWeedfsServer("/opt/vasystem/script", targetIp, config.StorageConf.VolumePath); err != nil { - s.Leave(false) - return - } - }() - } - - //闇�瑕侀噸鏂板垵濮嬪寲鏈湴姣斿杩涚▼ - go serf.ReInitDbPersonCompareData() - chMsg := protomsg.DbChangeMessage{ - Id: joinArg.ClusterId, - Table: protomsg.TableChanged_T_Cluster, - Action: protomsg.DbAction_Insert, - Info: "", - } - s.AddDbMessage(&chMsg) - logger.Debugf("AddCluster 鍔犲叆闆嗙兢鎴愬姛 time=%v", time.Since(start)) - return true, nil - } else { - logger.Debug("AddCluster syncTableDataFromCluster fail") - if agent != nil { - agent.Leave() - err = agent.Shutdown() - if err != nil { - logger.Debug("AddCluster agent shutdown err:", err) - } - } + syncTableDataFromCluster(joinArg) + logger.Debugf("AddCluster time=%v", time.Since(t)) + //if syncB { + chMsg := protomsg.DbChangeMessage{ + Id: joinArg.ClusterId, + Table: protomsg.TableChanged_T_Cluster, + Action: protomsg.DbAction_Insert, + Info: "join", } + + s.AddDbMessage(&chMsg) + logger.Debugf("AddCluster 鍔犲叆闆嗙兢鎴愬姛 time=%v", time.Since(start)) + return true, nil + //} else { + // logger.Debug("AddCluster syncTableDataFromCluster fail") + // if agent != nil { + // agent.Leave() + // err = agent.Shutdown() + // if err != nil { + // logger.Debug("AddCluster agent shutdown err:", err) + // } + // } + //} } else { logger.Debug("AddCluster dbSync.Init err:", err) if agent != nil { @@ -282,7 +250,7 @@ DevType_Other = "other" //鍏朵粬璁惧绫诲瀷锛宔g锛氬簲鐢� ) -//鑾峰彇鏈満绫诲瀷锛屽彧杩涜鍒嗘瀽銆佸垎鏋愬瓨鍌ㄤ竴浣撱�佸彧瀛樺偍銆佸叾浠栥�傘�� +// 鑾峰彇鏈満绫诲瀷锛屽彧杩涜鍒嗘瀽銆佸垎鏋愬瓨鍌ㄤ竴浣撱�佸彧瀛樺偍銆佸叾浠栥�傘�� func GetDevType(dt string) string { if dt != "" { if len(dt) >= 4 { @@ -336,23 +304,6 @@ func (s ClusterService) Leave(isDel bool) (bool, error) { start := time.Now() - devType := GetDevType(config.Server.DeviceType) - logger.Debugf("Leave GetDevType=%v, isDel=%v", devType, isDel) - - if devType == DevType_Analysis_Storage || devType == DevType_Storage { //鍙湁鍒嗘瀽鍜屽瓨鍌ㄦ墠鏈塃S闆嗙兢 - go func() { - //閫�鍑簑eedfs鑺傜偣 - if _, err := ExitWeedfsServer("/opt/vasystem/script", isDel, config.StorageConf.VolumePath); err != nil { - logger.Error("Leave ExitWeedfsServer err:", err) - return - } - //閫�鍑篹s鑺傜偣 - if _, err := ExitCluster("/opt/elasticsearch/config", "/opt/vasystem/script"); err != nil { - logger.Error("Leave ExitCluster es cluster err:", err) - return - } - }() - } var err error tx := models.GetDB().Begin() @@ -382,7 +333,7 @@ Id: "", Table: protomsg.TableChanged_T_Cluster, Action: protomsg.DbAction_Delete, - Info: "", + Info: "leave", } logger.Debugf("Leave delete db time=%v", time.Since(t)) tm := time.Now() @@ -391,6 +342,7 @@ } logger.Debugf("Leave success time=%v", time.Since(start)) + return true, nil } @@ -411,7 +363,7 @@ return false } -//鍔犲叆闆嗙兢鍚庢竻绌烘湰鍦扮殑鍚屾搴撴暟鎹� +// 鍔犲叆闆嗙兢鍚庢竻绌烘湰鍦扮殑鍚屾搴撴暟鎹� func syncTableDataFromCluster(joinArg *vo.ClusterJoinVo) bool { var lc models.LocalConfig e := lc.Select() @@ -424,36 +376,36 @@ db := models.GetDB() db.LogMode(false) defer db.LogMode(true) + tx := db.Begin() defer func() { if err != nil && tx != nil { tx.Rollback() } }() + //0.鍏抽棴reference tx.Exec("PRAGMA foreign_keys=OFF") //1.鍒犻櫎鏈湴鐨勫悓姝ュ簱鏁版嵁 for _, t := range serf.SyncTables { - delSql := "" - if t == "dbtables" { - delSql = "delete from " + t + " where (analyServerId='' or analyServerId=NULL)" - } else if t == "dbtablepersons" { - delSql = "delete from " + t + " where tableId in (select id from dbtables where (analyServerId='' or analyServerId=NULL))" - } else { - delSql = "delete from " + t + "" - } + delSql := "delete from " + t + "" + err = tx.Exec(delSql).Error if err != nil { + logger.Error("鍒犻櫎鏈湴鐨勫悓姝ュ簱鏁版嵁澶辫触,", err.Error()) + logger.Error("sql:", delSql) return false } } + //2.鎷夊彇闆嗙兢鍐呯殑鍚屾搴撴暟鎹埌鏈湴鏁版嵁搴撹〃涓� var dumpSqls *[]string dumpSqls, err = serf.GetTableDataFromCluster(serf.Agent, joinArg.ClusterId, serf.SyncTables, 20*time.Second) if dumpSqls != nil && len(*dumpSqls) > 0 { for _, sqlStr := range *dumpSqls { - logger.Debug("gorm exec dumpSql:", sqlStr) + //logger.Debug("gorm exec dumpSql:", sqlStr) if err = tx.Exec(sqlStr).Error; err != nil { + logger.Error("gorm exec dumpSql:", sqlStr, " error:", err.Error()) return false } } @@ -505,3 +457,14 @@ var lc models.Node return lc.FindIpByNode(nodeId) } + +func ClusterSyncProcMessage(payload []byte) { + if serf.Agent == nil { + logger.Error("鏈姞鍏ラ泦缇�") + } + + err := serf.Agent.UserEvent(serf.UserEventSyncMessage, payload, false) + if err != nil { + logger.Error("UserEventSyncMessage err:", err) + } +} -- Gitblit v1.8.0