// !!!!NOTE!!!! only used in UT func _clearModelData() error { return models.ClearModeData(nil) }
func slaveCheckMaster() error { confWriteMux.Lock() defer confWriteMux.Unlock() localNode := *memConfNodes[conf.ClientAddr] nodeString, _ := json.Marshal(localNode) reqData := nodeRequestDataT{ Auth: nodeAuthString, Data: string(nodeString), } data, err := nodeRequest(conf.MasterAddr, NODE_REQUEST_TYPE_CHECKMASTER, reqData) if err != nil { return err } masterVersion := &models.DataVersion{} if err = json.Unmarshal([]byte(data.(string)), masterVersion); err != nil { return fmt.Errorf("bad response data format: %s < %s >", err.Error(), data.(string)) } if masterVersion.Version == memConfDataVersion.Version && masterVersion.Sign == memConfDataVersion.Sign { localNode.LastCheckUTC = utils.GetNowSecond() if err = models.UpdateDBModel(nil, &localNode); err != nil { return err } memConfMux.Lock() memConfNodes[conf.ClientAddr] = &localNode memConfMux.Unlock() return nil } reqData = nodeRequestDataT{ Auth: nodeAuthString, Data: "", } // slave's data_version not equals master's data_version, slave sync all data from master data, err = nodeRequest(conf.MasterAddr, NODE_REQUEST_TYPE_SYNCMASTER, reqData) if err != nil { return err } resData := &syncAllDataT{} if err = json.Unmarshal([]byte(data.(string)), resData); err != nil { return fmt.Errorf("bad response data format: %s < %s >", err.Error(), data.(string)) } var users []*models.User var apps []*models.App var configs []*models.Config var nodes []*models.Node bs, _ := json.Marshal(resData.DataVersion) localNode.DataVersion = resData.DataVersion localNode.DataVersionStr = string(bs) localNode.LastCheckUTC = utils.GetNowSecond() // let sqlite use new db file models.UpdateSqliteDBEngine() s := models.NewSession() defer s.Close() if err = s.Begin(); err != nil { s.Rollback() return err } if err = models.ClearModeData(s); err != nil { s.Rollback() return err } toInsertModels := make([]interface{}, 0) for _, node := range resData.Nodes { if node.URL == conf.ClientAddr { node.DataVersion = localNode.DataVersion node.DataVersionStr = localNode.DataVersionStr node.LastCheckUTC = localNode.LastCheckUTC } toInsertModels = append(toInsertModels, node) nodes = append(nodes, node) } if err := models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } toInsertModels = make([]interface{}, 0) for _, user := range resData.Users { users = append(users, user) toInsertModels = append(toInsertModels, user) } if err = models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } toInsertModels = make([]interface{}, 0) for _, app := range resData.Apps { toInsertModels = append(toInsertModels, app) apps = append(apps, app) } if err = models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } toInsertModels = make([]interface{}, len(resData.WebHooks)) for ix, hook := range resData.WebHooks { toInsertModels[ix] = hook } if err = models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } toInsertModels = make([]interface{}, 0) for _, config := range resData.Configs { toInsertModels = append(toInsertModels, config) configs = append(configs, config) } if err = models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } toInsertModels = make([]interface{}, len(resData.ConfHistory)) for ix, history := range resData.ConfHistory { toInsertModels[ix] = history } if err = models.InsertMultiRows(s, toInsertModels); err != nil { s.Rollback() return err } if err = models.UpdateDataVersion(s, resData.DataVersion); err != nil { s.Rollback() return err } if err = s.Commit(); err != nil { s.Rollback() return err } fillMemConfData(users, apps, resData.WebHooks, configs, nodes, resData.DataVersion) nodeString, _ = json.Marshal(&localNode) reqData = nodeRequestDataT{ Auth: nodeAuthString, Data: string(nodeString), } nodeRequest(conf.MasterAddr, NODE_REQUEST_TYPE_CHECKMASTER, reqData) return nil }