From 747d5e974957607043846ee872467a542d284ecf Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期三, 13 五月 2020 18:49:45 +0800
Subject: [PATCH] ch to Forward
---
agent.go | 56 ++++++++++++++++++++++++++++++++------------------------
1 files changed, 32 insertions(+), 24 deletions(-)
diff --git a/agent.go b/agent.go
index 45f9ab7..f8f34c8 100644
--- a/agent.go
+++ b/agent.go
@@ -62,9 +62,9 @@
}
// Create create serf agent with config
-func Create(conf *Config) (*Agent, error) {
+func Create(conf *Config, snapshotPath string) (*Agent, error) {
// config cover to serf config
- serfConf, err := conf.convertToSerf()
+ serfConf, err := conf.convertToSerf(snapshotPath)
if err != nil {
return nil, err
}
@@ -130,6 +130,26 @@
receiveSqlInterface = c
}
+type DbHandler interface {
+ Forward(sql string) bool
+}
+
+type DbDumpHandler interface {
+ Forward(tables string) string
+}
+
+//鏁版嵁搴揺xecute鍙ユ焺
+var DbHandle DbHandler
+//鏁版嵁搴撳浠藉彞鏌�,鍙湪syncdb涓敤鍒�
+var dbDumpH DbDumpHandler
+
+func RegisterDbHandler(h DbHandler) {
+ DbHandle = h
+}
+
+func RegisterDbDumpHandler(h DbDumpHandler) {
+ dbDumpH = h
+}
// HandleEvent Handles serf.EventMemberJoin events,
// which will wait for members to join until the number of group members is equal to "groupExpect"
@@ -148,7 +168,7 @@
return
}
if sqlUe.Owner != a.conf.NodeName {
- ExecuteWriteSql(sqlUe.Sql, true)
+ //ExecuteWriteSql(sqlUe.Sql, true)
//flag, _ := ExecuteSqlByGorm(sqlUe.Sql)
//fmt.Println("userEvent exec 1 ", sqlUe.Sql, ",Result:", results)
if receiveSqlInterface != nil && sqlUe.Sql !=nil && len(sqlUe.Sql) > 0 {
@@ -191,23 +211,11 @@
} else if ev.Name == QueryEventUpdateDBData {
//fmt.Println(string(ev.Payload))
var tableNames []string
- err := json.Unmarshal(ev.Payload, &tableNames)
- if err !=nil {
- //fmt.Println("Query tableNames unmarshal err")
- return
- }
+ json.Unmarshal(ev.Payload, &tableNames)
- rows, err := ExecuteQuerySql(tableNames)
- if err != nil {
- //fmt.Println("err: ", err)
- return
- }
- var rowsReturn []Rows
- for _, r := range rows {
- rowsReturn = append(rowsReturn, *r)
- }
+ dumpData := dbDumpH.Forward(strings.Join(tableNames, ","))
- bytesReturn, err := json.Marshal(rowsReturn)
+ bytesReturn, _ := json.Marshal(dumpData)
//fmt.Println("results: ", bytesReturn)
if query, ok := event.(*serf.Query); ok {
if err := query.Respond(bytesReturn); err != nil {
@@ -446,8 +454,8 @@
for _, m := range mbs {
//fmt.Println("m", m)
if m.Name != a.conf.NodeName { //鍓嶇紑锛欴SVAD:鍒嗘瀽鏈嶅姟鍣� DSPAD:杩涘嚭鍏ad
- if strings.HasPrefix(a.conf.NodeName, "DSVAD"){
- if strings.HasPrefix(m.Name, "DSVAD") {
+ if strings.HasPrefix(a.conf.NodeName, "PSPAD"){
+ if strings.HasPrefix(m.Name, "PSPAD") {
specmembername = m.Name
break
}
@@ -520,8 +528,8 @@
}
//Init serf Init
-func Init(clusterID string, password string, nodeID string, addrs []string) (*Agent, error) {
- agent, err := InitNode(clusterID, password, nodeID)
+func Init(clusterID string, password string, nodeID string, addrs []string, snapshotPath string) (*Agent, error) {
+ agent, err := InitNode(clusterID, password, nodeID, snapshotPath)
if err != nil {
//fmt.Printf("InitNode failed, error: %s", err)
return agent, err
@@ -537,7 +545,7 @@
}
//InitNode web鍚庡彴鏀跺埌鍒涘缓闆嗙兢鐨勮姹傦紝
-func InitNode(clusterID string, password string, nodeID string) (*Agent, error) {
+func InitNode(clusterID string, password string, nodeID string, snapshotPath string) (*Agent, error) {
conf := DefaultConfig()
//fmt.Println("clusterID:", clusterID, "password:", password, "nodeID:", nodeID)
conf.ClusterID = clusterID
@@ -553,7 +561,7 @@
}
conf.EncryptKey = password
}
- agent, err := Create(conf)
+ agent, err := Create(conf, snapshotPath)
if err != nil {
//fmt.Printf("create agent failed, error: %s", err)
return agent, err
--
Gitblit v1.8.0