コード例 #1
0
ファイル: raft_server.go プロジェクト: tnextday/seaweedfs
func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
	s := &RaftServer{
		peers:    peers,
		httpAddr: httpAddr,
		dataDir:  dataDir,
		router:   r,
		topo:     topo,
	}

	if glog.V(4) {
		raft.SetLogLevel(2)
	}

	raft.RegisterCommand(&topology.MaxVolumeIdCommand{})

	var err error
	transporter := raft.NewHTTPTransporter("/cluster", 0)
	transporter.Transport.MaxIdleConnsPerHost = 1024
	glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr)

	// Clear old cluster configurations if peers are set
	if len(s.peers) > 0 {
		os.RemoveAll(path.Join(s.dataDir, "conf"))
		os.RemoveAll(path.Join(s.dataDir, "log"))
		os.RemoveAll(path.Join(s.dataDir, "snapshot"))
	}

	s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
	if err != nil {
		glog.V(0).Infoln(err)
		return nil
	}
	transporter.Install(s.raftServer, s)
	s.raftServer.SetHeartbeatInterval(1 * time.Second)
	s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 3450 * time.Millisecond)
	s.raftServer.Start()

	s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
	s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")

	if len(s.peers) > 0 {
		// Join to leader if specified.
		glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
		time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
		firstJoinError := s.Join(s.peers)
		if firstJoinError != nil {
			glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.")
			_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
				Name:             s.raftServer.Name(),
				ConnectionString: "http://" + s.httpAddr,
			})
			if err != nil {
				glog.V(0).Infoln(err)
				return nil
			}
		}
	} else if s.raftServer.IsLogEmpty() {
		// Initialize the server by joining itself.
		glog.V(0).Infoln("Initializing new cluster")

		_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             s.raftServer.Name(),
			ConnectionString: "http://" + s.httpAddr,
		})

		if err != nil {
			glog.V(0).Infoln(err)
			return nil
		}

	} else {
		glog.V(0).Infoln("Old conf,log,snapshot should have been removed.")
	}

	return s
}
コード例 #2
0
ファイル: raft_server.go プロジェクト: se77en/rabbitfs
// NewRaftServer returns a new RaftServer and an error
func NewRaftServer(
	directoryServer *Directory,
	peers []string,
	dir string,
	Addr string,
	router *mux.Router,
	transporterPrefix string,
	transporterTimeout time.Duration,
	pulse time.Duration,
) (rs *RaftServer, err error) {
	for i := range peers {
		if peers[i][:7] != "http://" {
			peers[i] = "http://" + peers[i]
		}
	}
	if Addr[:7] != "http://" {
		Addr = "http://" + Addr
	}
	rs = &RaftServer{
		directoryServer: directoryServer,
		peers:           peers,
		dataDir:         dir,
		router:          router,
		httpAddr:        Addr,
	}
	if err = os.MkdirAll(dir, 0700); err != nil {
		return nil, err
	}
	// Clear old cluster's configuration
	if len(rs.peers) > 0 {
		if err = os.RemoveAll(path.Join(rs.dataDir, "conf")); err != nil {
			return nil, err
		}
		if err = os.RemoveAll(path.Join(rs.dataDir, "log")); err != nil {
			return nil, err
		}
		if err = os.RemoveAll(path.Join(rs.dataDir, "snapshot")); err != nil {
			return nil, err
		}
	}

	transporter := raft.NewHTTPTransporter(transporterPrefix, transporterTimeout)
	rs.Server, err = raft.NewServer(Addr, dir, transporter, nil, directoryServer, Addr)
	transporter.Install(rs.Server, rs)
	if err = rs.Server.Start(); err != nil {
		return nil, err
	}

	if pulse > 0 {
		rs.Server.SetHeartbeatInterval(pulse)
		rs.Server.SetElectionTimeout(pulse * 5)
	}

	rs.router.HandleFunc("/raft_server/join", rs.joinHandler)
	if len(rs.peers) > 1 {
		// fmt.Println(peers)
		err := rs.Join(rs.peers)
		if err != nil {
			// if cannot join clusters, joins itself
			log4go.Info("%s join itself\n", rs.Server.Name())
			_, err = rs.Server.Do(&raft.DefaultJoinCommand{
				Name:             rs.Server.Name(),
				ConnectionString: Addr,
			})
			if err != nil {
				return nil, err
			}
		}
	} else {
		// server joining itself
		_, err = rs.Server.Do(&raft.DefaultJoinCommand{
			Name:             rs.Server.Name(),
			ConnectionString: Addr,
		})
		if err != nil {
			return nil, err
		}
	}

	return rs, nil
}