| | |
| | | ) |
| | | |
| | | const ( |
| | | QueryEventGetDB = "GetDatabase" |
| | | QueryEventGetDB = "GetDatabase" |
| | | QueryEventUpdateDBData = "UpdateDBData" |
| | | ) |
| | | |
| | | // Agent warps the serf agent |
| | | type Agent struct { |
| | | *agent.Agent |
| | |
| | | } |
| | | |
| | | type NodeInfo struct { |
| | | ClusterID string `json:"clusterID"` |
| | | NodeID string `json:"nodeID"` |
| | | NodeAddress string `json:"nodeAddress"` |
| | | IsAlive int `json:"isAlive"` |
| | | ClusterID string `json:"clusterID"` |
| | | NodeID string `json:"nodeID"` |
| | | NodeAddress string `json:"nodeAddress"` |
| | | IsAlive int `json:"isAlive"` |
| | | } |
| | | |
| | | // Create create serf agent with config |
| | |
| | | } |
| | | |
| | | // create serf agent with serf config |
| | | fmt.Println("conf.Config.EncryptKey:",conf.EncryptKey) |
| | | fmt.Println("conf.Config.EncryptKey:", conf.EncryptKey) |
| | | serfAgent, err := agent.Create(conf.Config, serfConf, nil) |
| | | if err != nil { |
| | | return nil, err |
| | |
| | | |
| | | case *serf.Query: |
| | | |
| | | if ev.Name == QueryEventGetDB{ |
| | | if ev.Name == QueryEventGetDB { |
| | | //bak file and send resp |
| | | filename, err := BakDbFile() |
| | | if err != nil { |
| | |
| | | } |
| | | } |
| | | } else if ev.Name == QueryEventUpdateDBData { |
| | | fmt.Println(string(ev.Payload)) |
| | | var tmpstringslice []string |
| | | tmpstringslice = append(tmpstringslice, string(ev.Payload)) |
| | | fmt.Println(tmpstringslice) |
| | | rows, err := ExecuteQuerySql(tmpstringslice) |
| | | if err != nil { |
| | | fmt.Println("err: ", err) |
| | | //fmt.Println(string(ev.Payload)) |
| | | //var tmpstringslice []string |
| | | //tmpstringslice = append(tmpstringslice, string(ev.Payload)) |
| | | //fmt.Println(tmpstringslice) |
| | | //rows, err := ExecuteQuerySql(tmpstringslice) |
| | | //if err != nil { |
| | | // fmt.Println("err: ", err) |
| | | // return |
| | | //} |
| | | //var rowsReturn []Rows |
| | | //for _, r := range rows { |
| | | // rowsReturn = append(rowsReturn, *r) |
| | | //} |
| | | var tableNames []string |
| | | err := json.Unmarshal(ev.Payload, &tableNames) |
| | | if err !=nil { |
| | | fmt.Println("Query tableNames unmarshal err") |
| | | return |
| | | } |
| | | var rowsReturn []Rows |
| | | for _,r := range rows { |
| | | rowsReturn = append(rowsReturn, *r) |
| | | datas, err := ExecuteQueryByGorm(tableNames) |
| | | if err !=nil { |
| | | fmt.Println("queryByGorm err") |
| | | return |
| | | } |
| | | |
| | | bytesReturn, err := json.Marshal(rowsReturn) |
| | | bytesReturn, err := json.Marshal(datas) |
| | | fmt.Println("results: ", bytesReturn) |
| | | if query, ok := event.(*serf.Query); ok { |
| | | if err := query.Respond(bytesReturn); err != nil { |
| | |
| | | //var res []*Rows |
| | | //json.Unmarshal(bytesReturn, &res) |
| | | } |
| | | |
| | | |
| | | default: |
| | | fmt.Printf("Unknown event type: %s\n", ev.EventType().String()) |
| | |
| | | |
| | | //GetDbFromCluster get the newest database after join cluster |
| | | //dbPathWrite the path where to write after got a database, |
| | | func (a *Agent)GetDbFromCluster(dbPathWrite string) { |
| | | func (a *Agent) GetDbFromCluster(dbPathWrite string) { |
| | | //members: get name of first member |
| | | mbs := a.GroupMembers(a.conf.ClusterID) |
| | | var specmembername string |
| | |
| | | }() |
| | | } |
| | | |
| | | //GetDbFromCluster get the newest database after join cluster |
| | | //dbPathWrite the path where to write after got a database, |
| | | func (a *Agent) GetTableDataFromCluster(tableNames []string) error { |
| | | //members: get name of first member |
| | | mbs := a.GroupMembers(a.conf.ClusterID) |
| | | var specmembername string |
| | | for _, m := range mbs { |
| | | if m.Addr.String() != a.conf.BindAddr { |
| | | specmembername = m.Name |
| | | break |
| | | } |
| | | } |
| | | fmt.Println(specmembername) |
| | | |
| | | //query: get db file. |
| | | params := serf.QueryParam{ |
| | | FilterNodes: strings.Fields(specmembername), |
| | | } |
| | | |
| | | //SQL |
| | | tBytes, _ := json.Marshal(tableNames) |
| | | |
| | | resp, err := a.Query(QueryEventUpdateDBData, tBytes, ¶ms) |
| | | if err == nil || !strings.Contains(err.Error(), "cannot contain") { |
| | | fmt.Println("err: ", err) |
| | | } |
| | | |
| | | |
| | | go func() { |
| | | respCh := resp.ResponseCh() |
| | | for { |
| | | select { |
| | | case r := <-respCh: |
| | | fmt.Println("x length is: ", len(r.Payload)) |
| | | // // byte to file. |
| | | var dumpSqls []string |
| | | err := json.Unmarshal(r.Payload, &dumpSqls) |
| | | if err ==nil { |
| | | if flag,_ := ExecuteSqlByGorm(dumpSqls);flag{ |
| | | fmt.Println("data dump success") |
| | | } |
| | | } |
| | | return |
| | | } |
| | | } |
| | | }() |
| | | return nil |
| | | |
| | | //r, err = c.Query([]string{query}, false, false) |
| | | //if err != nil { |
| | | // return err |
| | | //} |
| | | //for _, x := range r[0].Values { |
| | | // y := fmt.Sprintf("%s;\n", x[0].(string)) |
| | | // if _, err := w.Write([]byte(y)); err != nil { |
| | | // return err |
| | | // } |
| | | //} |
| | | |
| | | } |
| | | |
| | | //SyncSql boardcast sql to cluster |
| | | func (a *Agent)SyncSql(sqlOp string) { |
| | | func (a *Agent) SyncSql(sqlOp string) { |
| | | // event : use to send command to operate db. |
| | | err := a.UserEvent("SyncSql", []byte(sqlOp), false) |
| | | if err == nil || !strings.Contains(err.Error(), "cannot contain") { |
| | |
| | | conf.NodeName = nodeID |
| | | if password == "" { |
| | | conf.EncryptKey = DefaultEncryptKey |
| | | }else{ |
| | | } else { |
| | | if len(password) >= 16 { |
| | | password = password[:16] |
| | | }else{ |
| | | } else { |
| | | password = fmt.Sprintf("%016s", password)[:16] |
| | | //return nil, fmt.Errorf("error password") |
| | | } |
| | |
| | | agent.ShutdownCh() |
| | | }() |
| | | time.Sleep(time.Second) |
| | | fmt.Println("Stats:",agent.Agent.Serf().Stats()) |
| | | fmt.Println("EncryptionEnabled:",agent.Agent.Serf().EncryptionEnabled()) |
| | | fmt.Printf("create agent sucess!!") |
| | | fmt.Println("Stats:", agent.Agent.Serf().Stats()) |
| | | fmt.Println("EncryptionEnabled:", agent.Agent.Serf().EncryptionEnabled()) |
| | | fmt.Println("create agent sucess!!") |
| | | |
| | | return agent, nil |
| | | } |
| | |
| | | } |
| | | |
| | | n, err := a.Agent.Join(nodes, true) |
| | | if err != nil || n == 0{ |
| | | if err != nil || n == 0 { |
| | | a.Stop() |
| | | fmt.Println("Stop node") |
| | | return fmt.Errorf("Error Encrypt Key!") |
| | |
| | | |
| | | type Node struct { |
| | | clusterID string |
| | | NodeID string |
| | | IP string |
| | | isAlive int //StatusNone:0, StatusAlive:1, StatusLeaving:2, StatusLeft:3, StatusFailed:4 |
| | | NodeID string |
| | | IP string |
| | | isAlive int //StatusNone:0, StatusAlive:1, StatusLeaving:2, StatusLeft:3, StatusFailed:4 |
| | | } |
| | | |
| | | func (a *Agent) GetNodes() (nodes []Node) { |
| | | var node Node |
| | | fmt.Println("a.conf.ClusterID:",a.conf.ClusterID) |
| | | fmt.Println("a.conf.ClusterID:", a.conf.ClusterID) |
| | | mbs := a.GroupMembers(a.conf.ClusterID) |
| | | for _, mb := range mbs { |
| | | node.NodeID = mb.Name |
| | |
| | | |
| | | return nodes |
| | | } |
| | | |
| | | |
| | | |
| | | |