| | |
| | | ) |
| | | |
| | | 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 { |
| | |
| | | return |
| | | } |
| | | var rowsReturn []Rows |
| | | for _,r := range rows { |
| | | for _, r := range rows { |
| | | rowsReturn = append(rowsReturn, *r) |
| | | } |
| | | |
| | |
| | | //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 |
| | |
| | | } |
| | | |
| | | //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 |
| | | } |
| | | |
| | | |
| | | |
| | | |