func stopMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] fmt.Printf("stopNode Request, node_id:%v\n", node_id) node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } n := getNode(node_id_int) if n == nil { fmt.Printf("get Node failure, node not exist\n") util.Response(nil, http.StatusBadRequest, resp) return } /*n.stop() if n.state.Commit != 10006 { fmt.Printf("[error]commit = %d, want = 10006\n", n.state.Commit) util.Response(nil, http.StatusBadRequest, resp) return } */ resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func resumeMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] fmt.Printf("restartNode Request, node_id:%v\n", node_id) node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } if len(nodes) < node_id_int { fmt.Printf("node_id out of bound\n") util.Response(nil, http.StatusBadRequest, resp) return } fmt.Println(node_id_int) /* n := getNode(node_id_int) if n == nil { fmt.Printf("get Node failure, node not exist\n") util.Response(nil, http.StatusBadRequest, resp) return } n.resume() */ resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func startNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] fmt.Printf("startNode Request, node_id:%v\n", node_id) node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } if !hasPeer(uint64(node_id_int)) { fmt.Printf("the peer not exist") util.Response(nil, http.StatusBadRequest, resp) return } ok := getNode(node_id_int) if ok != nil { fmt.Printf("node_id already exist\n") util.Response(nil, http.StatusBadRequest, resp) return } n := startNode(uint64(node_id_int), peers, nt.nodeNetwork(uint64(node_id_int))) n.Node.Campaign(context.TODO()) nodes = append(nodes, n) resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func addMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] fmt.Printf("addNode Request, node_id:%v\n", node_id) node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } fmt.Println(node_id_int) /* //添加peer if flag := addPeer(raft.Peer{uint64(node_id_int), nil}); !flag { fmt.Printf("add peer failure\n") util.Response(nil, http.StatusBadRequest, resp) return } //启动节点 node := startNode(uint64(node_id_int), peers, nt.nodeNetwork(uint64(node_id_int))) nodes = append(nodes, node) node.Node.Campaign(context.TODO()) cc := raftpb.ConfChange{ Type: raftpb.ConfChangeAddNode, NodeID: uint64(node_id_int), Context: []byte(node_id), } //遍历所有节点 for _, n := range nodes { if n.id == uint64(node_id_int) { continue } err = n.ProposeConfChange(context.TODO(), cc) if err != nil { fmt.Printf("node ProposeConfChange failure, err:%v\n", err) util.Response(nil, http.StatusBadRequest, resp) return } break //st := n.ApplyConfChange(cc) //fmt.Printf("state: %v\n", st.String()) } */ resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func proposeMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] data := vars["data"] fmt.Printf("proposeNode Request, node_id:%v data:%v\n", node_id, data) m := make(map[string]string) m["data"] = data m["time"] = fmt.Sprintf("%v", time.Now()) json_data, err := json.Marshal(m) if err != nil { fmt.Printf("data json Marshal error\n") util.Response(nil, http.StatusBadRequest, resp) return } node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } fmt.Println(node_id_int) fmt.Println(json_data) /* n := getNode(node_id_int) if n == nil { fmt.Printf("get Node failure, node not exist\n") util.Response(nil, http.StatusBadRequest, resp) return } err = n.Propose(context.TODO(), json_data) if err != nil { fmt.Printf("err:%v\n", err) util.Response(nil, http.StatusBadRequest, resp) return } */ resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func removeMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node_id := vars["node_id"] fmt.Printf("removeNode Request, node_id:%v\n", node_id) node_id_int, err := strconv.Atoi(node_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } fmt.Println(node_id_int) /* //删除peer if flag := removePeer(uint64(node_id_int)); !flag { fmt.Printf("remove peer failure\n") util.Response(nil, http.StatusBadRequest, resp) return } cc := raftpb.ConfChange{ Type: raftpb.ConfChangeRemoveNode, NodeID: uint64(node_id_int), Context: []byte(node_id), } //遍历所有节点 for _, n := range nodes { err = n.ProposeConfChange(context.TODO(), cc) if err != nil { fmt.Printf("node ProposeConfChange failure, err:%v\n", err) util.Response(nil, http.StatusBadRequest, resp) return } break //st := n.ApplyConfChange(cc) //fmt.Printf("state: %v\n", st.String()) } */ resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func startMultiNodeHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) id := vars["id"] id_int, err := strconv.Atoi(id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } n := startMultiNode(uint64(id_int), MultiNt) MultiNodes[uint64(id_int)] = n resp.Header().Set("Content-Type", "Application/json") util.Response(nil, http.StatusOK, resp) }
func createGroupHandler(resp http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) id := vars["id"] group_id := vars["group_id"] peer_id := vars["peer_id"] id_int, err := strconv.Atoi(id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } group_id_int, err := strconv.Atoi(group_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } peer_id_int, err := strconv.Atoi(peer_id) if err != nil { fmt.Printf("字符串转换成整数失败") util.Response(nil, http.StatusBadRequest, resp) return } n := MultiNodes[uint64(id_int)] c := &raft.Config{ ID: uint64(peer_id_int), ElectionTick: 10, HeartbeatTick: 1, Storage: n.storage, MaxSizePerMsg: 1024 * 1024, MaxInflightMsgs: 256, } fmt.Println("node_id:", n.nodeid) n.createGroup(uint64(group_id_int), c, MultiPeers) }