| | |
| | | "io/ioutil" |
| | | "net" |
| | | "os" |
| | | "sync" |
| | | |
| | | //"os" |
| | | "strings" |
| | |
| | | ) |
| | | |
| | | const ( |
| | | QueryEventGetDB = "GetDatabase" |
| | | QueryEventGetDB = "GetDatabase" |
| | | QueryEventUpdateDBData = "UpdateDBData" |
| | | UserEventSyncSql = "SyncSql" |
| | | UserEventSyncDbTablePersonCache = "SyncCache" |
| | | ) |
| | | |
| | | // 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 |
| | |
| | | go a.BroadcastMemberlist(BroadcastInterval * time.Second) |
| | | } |
| | | |
| | | var SyncDbTablePersonCacheChan = make(chan []byte,0) |
| | | |
| | | // HandleEvent Handles serf.EventMemberJoin events, |
| | | // which will wait for members to join until the number of group members is equal to "groupExpect" |
| | | // when the startup mode is "ModeCluster", |
| | |
| | | |
| | | switch ev := event.(type) { |
| | | case serf.UserEvent: |
| | | fmt.Println(string(ev.Payload)) |
| | | var tmpstringslice []string |
| | | tmpstringslice = append(tmpstringslice, string(ev.Payload)) |
| | | fmt.Println(tmpstringslice) |
| | | results, err := ExecuteWriteSql(tmpstringslice) |
| | | |
| | | for _, result := range results { |
| | | fmt.Println(result, "results err: ", err) |
| | | if ev.Name == UserEventSyncSql { |
| | | var sqlUe SqlUserEvent |
| | | err := json.Unmarshal(ev.Payload, &sqlUe) |
| | | if err !=nil { |
| | | fmt.Println("sqlUe unmarshal err:",err) |
| | | return |
| | | } |
| | | if sqlUe.Owner != a.conf.NodeName { |
| | | //results, err := ExecuteWriteSql(sqlArr) |
| | | flag, _ := ExecuteSqlByGorm(sqlUe.Sql) |
| | | fmt.Println("userEvent exec ",sqlUe.Sql,",Result:",flag) |
| | | } |
| | | } else if ev.Name == UserEventSyncDbTablePersonCache { |
| | | SyncDbTablePersonCacheChan <- ev.Payload |
| | | } |
| | | |
| | | |
| | | 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) |
| | | fmt.Println("Query tableNames:",tableNames) |
| | | datas, err := ExecuteQueryByGorm(tableNames) |
| | | if err !=nil { |
| | | fmt.Println("queryByGorm err") |
| | | return |
| | | } |
| | | |
| | | bytesReturn, err := json.Marshal(rowsReturn) |
| | | fmt.Println("results: ", bytesReturn) |
| | | bytesReturn, err := json.Marshal(datas) |
| | | fmt.Println("results.len: ", len(bytesReturn)) |
| | | if query, ok := event.(*serf.Query); ok { |
| | | if err := query.Respond(bytesReturn); err != nil { |
| | | fmt.Printf("err: %s\n", err) |
| | |
| | | |
| | | //var res []*Rows |
| | | //json.Unmarshal(bytesReturn, &res) |
| | | } |
| | | case serf.MemberEvent: |
| | | if event.EventType() == serf.EventMemberLeave { |
| | | if ev.Members !=nil && len(ev.Members) ==1 { |
| | | leaveMember := ev.Members[0] |
| | | leaveSql := "delete from cluster_node where node_id='"+leaveMember.Name+"'" |
| | | ExecuteSqlByGorm([]string{ leaveSql }) |
| | | |
| | | fmt.Println("EventMemberLeave,current Members:",ev.Members) |
| | | } |
| | | return |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | //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 |
| | |
| | | }() |
| | | } |
| | | |
| | | //SyncSql boardcast sql to cluster |
| | | func (a *Agent)SyncSql(sqlOp string) { |
| | | // event : use to send command to operate db. |
| | | err := a.UserEvent("SyncSql", []byte(sqlOp), false) |
| | | //GetDbFromCluster get the newest database after join cluster |
| | | //dbPathWrite the path where to write after got a database, |
| | | func (a *Agent) GetTableDataFromCluster(tableNames []string) (*[]string,error) { |
| | | //members: get name of first member |
| | | mbs := a.GroupMembers(a.conf.ClusterID) |
| | | var specmembername string |
| | | for _, m := range mbs { |
| | | fmt.Println("m",m) |
| | | if m.Name != a.conf.NodeName { |
| | | specmembername = m.Name |
| | | break |
| | | } |
| | | } |
| | | fmt.Println("mbs:",mbs,"a.conf.BindAddr:",a.conf.BindAddr,"specmembername:",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) |
| | | } |
| | | fmt.Println("Query.resp.err:",err,"resp:",resp) |
| | | |
| | | var wg sync.WaitGroup |
| | | wg.Add(1) |
| | | var dumpSqls []string |
| | | go func() { |
| | | defer wg.Done() |
| | | respCh := resp.ResponseCh() |
| | | for { |
| | | select { |
| | | case r := <-respCh: |
| | | fmt.Println("Query response's len:", len(r.Payload)) |
| | | err := json.Unmarshal(r.Payload, &dumpSqls) |
| | | if err ==nil { |
| | | fmt.Println("dumpSql:",dumpSqls) |
| | | fmt.Println("data dump success") |
| | | } |
| | | return |
| | | } |
| | | } |
| | | }() |
| | | wg.Wait() |
| | | return &dumpSqls,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 |
| | | // } |
| | | //} |
| | | |
| | | } |
| | | |
| | | type SqlUserEvent struct { |
| | | Owner string `json:"owner"` |
| | | Sql []string `json:"sql"` |
| | | } |
| | | |
| | | //SyncSql boardcast sql to cluster |
| | | func (a *Agent) SyncSql(sqlOp []string) { |
| | | // event : use to send command to operate db. |
| | | var sqlUe = SqlUserEvent{ |
| | | Owner: a.conf.NodeName, |
| | | Sql: sqlOp, |
| | | } |
| | | ueB, err := json.Marshal(sqlUe) |
| | | if err !=nil { |
| | | fmt.Println("sqlUE marshal err:",err) |
| | | return |
| | | } |
| | | err = a.UserEvent(UserEventSyncSql, ueB, false) |
| | | if err == nil || !strings.Contains(err.Error(), "cannot contain") { |
| | | fmt.Println("err: ", err) |
| | | } |
| | | } |
| | | |
| | | //更新同步库的比对缓存 |
| | | func (a *Agent) SyncDbTablePersonCache(b []byte) { |
| | | err := a.UserEvent(UserEventSyncDbTablePersonCache, b, false) |
| | | if err !=nil{ |
| | | fmt.Println("UserEventSyncDbTablePersonCache err:",err) |
| | | } |
| | | } |
| | | |
| | |
| | | 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{ |
| | | a.Stop() |
| | | fmt.Println("Stop node") |
| | | 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 |
| | | } |
| | | |
| | | |
| | | |
| | | |