• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang raft.NewHTTPTransporter函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/goraft/raft.NewHTTPTransporter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHTTPTransporter函数的具体用法?Golang NewHTTPTransporter怎么用?Golang NewHTTPTransporter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewHTTPTransporter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: startRaft

func (s *RaftServer) startRaft() error {
	log.Info("Initializing Raft Server: %s", s.config.RaftConnectionString())

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	var err error
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, s.clusterConfig, s.clusterConfig, "")
	if err != nil {
		return err
	}

	s.raftServer.SetElectionTimeout(s.config.RaftTimeout.Duration)
	s.raftServer.LoadSnapshot() // ignore errors

	s.raftServer.AddEventListener(raft.StateChangeEventType, s.raftEventHandler)

	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	go s.CompactLog()

	if !s.raftServer.IsLogEmpty() {
		log.Info("Recovered from log")
		return nil
	}

	potentialLeaders := s.config.SeedServers

	if len(potentialLeaders) == 0 {
		log.Info("Starting as new Raft leader...")
		name := s.raftServer.Name()
		_, err := s.raftServer.Do(&InfluxJoinCommand{
			Name:                     name,
			ConnectionString:         s.config.RaftConnectionString(),
			ProtobufConnectionString: s.config.ProtobufConnectionString(),
		})

		if err != nil {
			log.Error(err)
		}
		err = s.CreateRootUser()
		return err
	}

	for {
		for _, leader := range potentialLeaders {
			log.Info("(raft:%s) Attempting to join leader: %s", s.raftServer.Name(), leader)

			if err := s.Join(leader); err == nil {
				log.Info("Joined: %s", leader)
				return nil
			}
		}

		log.Warn("Couldn't join any of the seeds, sleeping and retrying...")
		time.Sleep(100 * time.Millisecond)
	}

	return nil
}
开发者ID:hyc,项目名称:influxdb,代码行数:60,代码来源:raft_server.go


示例2: main

func main() {
	hostname, _ := os.Hostname()

	// Initialize the internal Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	server, _ = raft.NewServer(hostname, ".", transporter, nil, nil, "")

	// Attach the Raft server to the HTTP server.
	transporter.Install(server, http.DefaultServeMux)

	// Create a /add endpoint.
	http.HandleFunc("/add", addHandler)

	// Start both servers.
	server.Start()

	// Initialize to a cluster of one for this example.
	if server.IsLogEmpty() {
		_, err := server.Do(&raft.DefaultJoinCommand{Name: hostname})
		if err != nil {
			log.Fatal(err)
		}
	}

	// Initialize HTTP server.
	log.Fatal(http.ListenAndServe(":8000", nil))
}
开发者ID:nporsche,项目名称:writing-a-distributed-systems-library,代码行数:27,代码来源:main.go


示例3: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(leader string) error {
	var err error

	log.Printf("Initializing Raft Server: %s", s.path)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.db, "")
	if err != nil {
		log.Fatal(err)
	}
	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	if leader != "" {
		// Join to leader if specified.

		log.Println("Attempting to join leader:", leader)

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}
		if err := s.Join(leader); err != nil {
			log.Fatal(err)
		}

	} else if s.raftServer.IsLogEmpty() {
		// Initialize the server by joining itself.

		log.Println("Initializing new cluster")

		_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             s.raftServer.Name(),
			ConnectionString: s.connectionString(),
		})
		if err != nil {
			log.Fatal(err)
		}

	} else {
		log.Println("Recovered from log")
	}

	log.Println("Initializing HTTP server")

	// Initialize and start HTTP server.
	s.httpServer = &http.Server{
		Addr:    fmt.Sprintf(":%d", s.port),
		Handler: s.router,
	}

	s.router.HandleFunc("/db/{key}", s.readHandler).Methods("GET")
	s.router.HandleFunc("/db/{key}", s.writeHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")

	log.Println("Listening at:", s.connectionString())

	return s.httpServer.ListenAndServe()
}
开发者ID:rboyd,项目名称:raftd,代码行数:60,代码来源:server.go


示例4: ListenAndServe

func (raftPeer *RaftPeer) ListenAndServe(router *mux.Router, httpServer raft.HTTPMuxer) error {
	var err error
	rand.Seed(time.Now().UnixNano())

	// Setup commands.
	raft.RegisterCommand(&TransactionBatchCommand{})
	raft.RegisterCommand(&SetCommand{})

	if err := os.MkdirAll(raftPeer.path, 0744); err != nil {
		log.Fatalf("Unable to create path: %v", err)
	}

	log.Printf("Initializing Raft Server: %s", raftPeer.path)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	//NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, context interface{}, connectionString string) (*Server, error) {
	raftPeer.raftServer, err = raft.NewServer(raftPeer.name, raftPeer.path, transporter, nil, raftPeer.db, "")
	if err != nil {
		log.Fatal(err)
	}
	transporter.Install(raftPeer.raftServer, httpServer)
	raftPeer.raftServer.Start()

	// Join to leader if specified.
	if raftPeer.leaderAddress != "" {
		log.Println("Attempting to join leader:", raftPeer.leaderAddress)

		if !raftPeer.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}
		if err := raftPeer.Join(raftPeer.leaderAddress); err != nil {
			log.Fatal(err)
		}

		// Initialize the server by joining itself.
	} else if raftPeer.raftServer.IsLogEmpty() {
		log.Println("Initializing new cluster")

		_, err := raftPeer.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             raftPeer.raftServer.Name(),
			ConnectionString: raftPeer.connectionString(),
		})
		if err != nil {
			log.Fatal(err)
		}

	} else {
		log.Println("Recovered from log")
	}

	router.HandleFunc("/join", raftPeer.joinHandler).Methods("POST")

	fmt.Printf("Raft listening\n")
	return nil // TODO return errors.
}
开发者ID:kellabyte,项目名称:dazzle,代码行数:56,代码来源:raft_peer.go


示例5: InitRaftServer

func (this *electServer) InitRaftServer() (err error) {
	// raft.SetLogLevel(raft.Trace)
	transporter := raft.NewHTTPTransporter("/raft", 200*time.Millisecond)
	this.raftServer, err = raft.NewServer(this.Name, this.GetDataDir(), transporter, nil, nil, "")
	if err != nil {
		return
	}
	transporter.Install(this.raftServer, this.mux)
	err = this.raftServer.Start()
	return
}
开发者ID:QLeelulu,项目名称:goatherd,代码行数:11,代码来源:raft.go


示例6: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(primary string) error {
	var err error = nil

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	transporter.Transport.Dial = transport.UnixDialer
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.sql, "")
	if err != nil {
		log.Fatal(err)
	}
	s.raftServer.SetElectionTimeout(750 * time.Millisecond)
	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	// Initialize and start HTTP server.
	s.httpServer = &http.Server{
		Handler: s.router,
	}

	s.router.HandleFunc("/sql", s.sqlHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")

	if primary == "" {
		if s.raftServer.IsLogEmpty() {
			_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
				Name:             s.raftServer.Name(),
				ConnectionString: s.connectionString(),
			})
			if err != nil {
				log.Fatal(err)
			}
		}

	} else {

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}
		//	   		namePath, _ := filepath.Abs()
		if err := s.Join(primary); err != nil {
			log.Fatal(err)
		}
		log.Println("Joined leader:", s.raftServer.Leader())
	}

	// Start Unix transport
	l, err := transport.Listen(s.listen)
	if err != nil {
		log.Fatal(err)
	}

	return s.httpServer.Serve(l)
}
开发者ID:rhtyd,项目名称:hacklab,代码行数:54,代码来源:server.go


示例7: StartRaftServer

func StartRaftServer(me string, path string, lead bool, peers []string) {
	log.Println("start raft")

	transporter := raft.NewHTTPTransporter("raft")

	raftserver, err := raft.NewServer(me, path, transporter, &StateMachine{}, nil)
	if err != nil {
		log.Fatal(err)
	}

	mux := http.NewServeMux()

	httpserver := &http.Server{
		Handler: mux,
		Addr:    me,
	}

	transporter.Install(raftserver, mux)

	//raftserver.Start()
	log.Println("listen and serve")
	httpserver.ListenAndServe()

}
开发者ID:ryanbressler,项目名称:RiverFS,代码行数:24,代码来源:raft.go


示例8: Start

// Start starts a DNS server and blocks waiting to be killed.
func (s *Server) Start() (*sync.WaitGroup, error) {
	var err error
	log.Printf("Initializing Server. DNS Addr: %q, HTTP Addr: %q, Data Dir: %q", s.dnsAddr, s.httpAddr, s.dataDir)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	s.raftServer, err = raft.NewServer(s.HTTPAddr(), s.dataDir, transporter, nil, s.registry, "")
	if err != nil {
		log.Fatal(err)
	}
	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	// Join to leader if specified.
	if len(s.members) > 0 {
		log.Println("Joining cluster:", strings.Join(s.members, ","))

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}

		if err := s.Join(s.members); err != nil {
			return nil, err
		}

		log.Println("Joined cluster")

		// Initialize the server by joining itself.
	} else if s.raftServer.IsLogEmpty() {
		log.Println("Initializing new cluster")

		_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             s.raftServer.Name(),
			ConnectionString: s.connectionString(),
		})

		if err != nil {
			log.Fatal(err)
			return nil, err
		}

	} else {
		log.Println("Recovered from log")
	}

	s.dnsTCPServer = &dns.Server{
		Addr:         s.DNSAddr(),
		Net:          "tcp",
		Handler:      s.dnsHandler,
		ReadTimeout:  s.readTimeout,
		WriteTimeout: s.writeTimeout,
	}

	s.dnsUDPServer = &dns.Server{
		Addr:         s.DNSAddr(),
		Net:          "udp",
		Handler:      s.dnsHandler,
		UDPSize:      65535,
		ReadTimeout:  s.readTimeout,
		WriteTimeout: s.writeTimeout,
	}

	s.httpServer = &http.Server{
		Addr:           s.HTTPAddr(),
		Handler:        s.router,
		ReadTimeout:    s.readTimeout,
		WriteTimeout:   s.writeTimeout,
		MaxHeaderBytes: 1 << 20,
	}

	go s.listenAndServe()

	s.waiter.Add(1)
	go s.run()

	return s.waiter, nil
}
开发者ID:velebak,项目名称:skydns,代码行数:78,代码来源:server.go


示例9: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(leader string, tcpIp string) error {
	var err error

	log.Printf("Initializing Raft Server: %s", s.path)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft", 200*time.Millisecond)
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, NewFsStateMachine(s.fs), s.fs, "")
	if err != nil {
		log.Fatal(err)
	}
	//s.Server.raftServer = s.raftServer

	s.raftServer.LoadSnapshot()
	s.snapshotIndex = s.raftServer.CommitIndex()

	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	leaderChangeFun := func(e raft.Event) {
		server := e.Source().(raft.Server)
		log.Printf("change: [%s] %s %v -> %v\n", server.Name(), e.Type(), e.PrevValue(), e.Value())

		if e.Type() == raft.LeaderChangeEventType {
			go func() {
				s.facade.DispatchEvent(NewEvent(kEventLeaderChanged, server, nil))
				s.facade.DispatchEvent(NewEvent(kEventUpdataLibClients, nil, nil))
			}()
		}
	}

	s.raftServer.AddEventListener(raft.StateChangeEventType, leaderChangeFun)
	s.raftServer.AddEventListener(raft.LeaderChangeEventType, leaderChangeFun)
	s.raftServer.AddEventListener(raft.TermChangeEventType, leaderChangeFun)
	s.raftServer.AddEventListener(raft.AddPeerEventType, leaderChangeFun)
	//s.raftServer.AddEventListener(raft.CommitEventType, leaderChangeFun)
	s.raftServer.AddEventListener(raft.RemovePeerEventType, leaderChangeFun)
	//s.raftServer.AddEventListener(raft.HeartbeatIntervalEventType, leaderChangeFun)

	if leader != "" {
		// Join to leader if specified.

		log.Println("Attempting to join leader:", leader)

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}
		if err := s.Join(leader); err != nil {
			log.Fatal(err)
		}
	} else if s.raftServer.IsLogEmpty() {
		// Initialize the server by joining itself.

		log.Println("Initializing new cluster")
		join := raft.DefaultJoinCommand{Name: s.raftServer.Name(),
			ConnectionString: s.connectionString()}
		_, err := s.raftServer.Do(&join)
		if err != nil {
			log.Fatal(err)
		}

	} else {
		log.Println("Recovered from log")
	}

	log.Println("Initializing HTTP server")

	// Initialize and start HTTP server.
	s.httpServer = &http.Server{
		Addr:    fmt.Sprintf(":%d", s.port),
		Handler: s.router,
	}

	s.router.HandleFunc("/db/{key}", s.readHandler).Methods("GET")
	s.router.HandleFunc("/db/{key}", s.writeHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")

	log.Println("Listening at:", s.connectionString())

	s.startTcpSer(tcpIp)

	GoDone(1, func() {
		s.httpServer.ListenAndServe()
	})
	//	go func() {
	//		s.httpServer.ListenAndServe()
	//	}()
	//	return s.httpServer.ListenAndServe()
	return nil
}
开发者ID:izouxv,项目名称:raftX,代码行数:91,代码来源:server.go


示例10: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(leader string) error {
	var err error

    log.Printf("Initializing Raft Server: %s", s.path)
             
    // Initialize and start Raft server.
    transporter := raft.NewHTTPTransporter("/raft")
    transporter.Transport.Dial = transport.UnixDialer
    s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.sql, "")
    if err != nil {
            log.Fatal(err)
    }
    transporter.Install(s.raftServer, s)


    s.raftServer.Start()
	s.raftServer.SetHeartbeatTimeout(1 * time.Millisecond)
	s.raftServer.SetElectionTimeout(500 * time.Millisecond)
	fn := func(e raft.Event) {
		log.Printf("%s %v -> %v\n", e.Type(), e.PrevValue(), e.Value())
	}
    s.raftServer.AddEventListener(raft.StateChangeEventType, fn)
    s.raftServer.AddEventListener(raft.LeaderChangeEventType, fn)
    s.raftServer.AddEventListener(raft.TermChangeEventType, fn)


    if leader != "" {
            // Join to leader if specified.

            log.Println("Attempting to join leader:", leader)

            if !s.raftServer.IsLogEmpty() {
                    log.Fatal("Cannot join with an existing log")
            }
             //time.Sleep(1 * time.Second)

            if err := s.Join(leader); err != nil {
            	log.Println("Join failed")
                log.Fatal(err)
            }
            log.Printf("Node %s joined leader %s" , s.connectionString(), leader)

    } else if s.raftServer.IsLogEmpty() {
            // Initialize the server by joining itself.

            log.Println("Initializing new cluster")

			cs, err := transport.Encode(s.listen)
			if err != nil {
				return err
			}

            _, err = s.raftServer.Do(&raft.DefaultJoinCommand{
                    Name:             s.raftServer.Name(),
                    ConnectionString: cs,
            })
            if err != nil {
                    log.Fatal(err)
            }

    } else {
            log.Println("Recovered from log")
    }

    log.Println("Initializing HTTP server")

	// Initialize and start HTTP server.
	s.httpServer = &http.Server{
		Handler: s.router,
	}

	s.router.HandleFunc("/sql", s.sqlHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")

	// Start Unix transport
	l, err := transport.Listen(s.listen)
	if err != nil {
		log.Fatal(err)
	}
	return s.httpServer.Serve(l)
}
开发者ID:henrik-muehe,项目名称:level4,代码行数:82,代码来源:server.go


示例11: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(leader string) error {
	var err error

	// Initialize and start HTTP server.
	log.Println("Initializing HTTP server")
	s.httpServer = &http.Server{
		Handler: s.router,
	}

	s.router.HandleFunc("/sql", s.sqlHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")

	// Start Unix transport
	log.Println(s.name, "listening at", s.listen)
	l, err := transport.Listen(s.listen)
	if err != nil {
		log.Fatal(err)
	}

	// initialize raft
	transporter := raft.NewHTTPTransporter("/raft")

	// swap the dialer with the unix dialer that also allows unix-sockets to
	// be passed around
	transporter.Transport.Dial = transport.UnixDialer

	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.sql, "")
	if err != nil {
		log.Fatal(err)
	}
	transporter.Install(s.raftServer, s)

	// this seems to yield good results, but it's definitely not the most
	// empirical of the measurements
	s.raftServer.SetElectionTimeout(800 * time.Millisecond)
	s.raftServer.SetHeartbeatTimeout(150 * time.Millisecond)
	s.raftServer.Start()

	if leader != "" {
		// Join the leader if specified.
		log.Println("Attempting to join the leader:", leader)

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
			return nil
		}

		// retry the join until we actually join the cluster
		// (it may take a while, until octopus sets the sockets up)
		for {
			if err := s.Join(leader); err == nil {
				break
			}
			log.Fatal(err)
			time.Sleep(10 * time.Millisecond)
		}
		log.Println("joined.")
	} else if s.raftServer.IsLogEmpty() {
		// Initialize the server by joining itself.
		log.Println("Initializing new cluster")

		_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             s.raftServer.Name(),
			ConnectionString: s.connectionString(),
		})
		if err != nil {
			log.Fatal(err)
		}
	} else {
		log.Println("Recovered from log")
	}

	log.Println(s.name, "IS READY TO ACCEPT REQUESTS")

	return s.httpServer.Serve(l)
}
开发者ID:kratorius,项目名称:stripe-ctf3,代码行数:77,代码来源:server.go


示例12: NewRaftServer

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)
	}

	//注册命令以便在Do中回调
	raft.RegisterCommand(&topology.MaxVolumeIdCommand{})

	var err error

	//Creates a new HTTP transporter with the given path prefix.
	transporter := raft.NewHTTPTransporter("/cluster", 0)
	transporter.Transport.MaxIdleConnsPerHost = 1024

	//http://godoc.org/github.com/goraft/raft#RegisterCommand
	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) * 1150 * time.Millisecond)
	s.raftServer.Start()

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

	// Join to leader if specified.
	if len(s.peers) > 0 {
		if !s.raftServer.IsLogEmpty() {
			glog.V(0).Infoln("Starting cluster with existing logs.")
		} else {
			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
				}
			}
		}

		// Initialize the server by joining itself.
	} else if s.raftServer.IsLogEmpty() {
		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("Recovered from log")
	}

	return s
}
开发者ID:shenlanzifa,项目名称:weed-fs,代码行数:77,代码来源:raft_server.go


示例13: ListenAndServe

// Starts the server.
func (s *Server) ListenAndServe(leader string) error {
	var err error

	leader = strings.Replace(leader, "/", "-", -1)
	log.Printf("Initializing Raft Server: %s", s.path)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	transporter.Transport.Dial = transport.UnixDialer
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.sql, "")
	if err != nil {
		log.Fatal(err)
	}
	s.raftServer.SetElectionTimeout(200 * time.Millisecond) // default 150ms
	s.raftServer.SetHeartbeatTimeout(80 * time.Millisecond) // default 50ms

	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	if leader != "" {
		// Join to leader if specified.

		log.Println("Attempting to join leader:", leader)

		if !s.raftServer.IsLogEmpty() {
			log.Fatal("Cannot join with an existing log")
		}

		for tries := 0; tries < 10; tries += 1 {
			err := s.Join(leader)
			if err == nil {
				break
			}
			log.Printf("Join attempt %d failed; sleeping", tries)
			time.Sleep(200 * time.Millisecond)
		}
		if err != nil {
			log.Fatal(err)
		}

	} else if s.raftServer.IsLogEmpty() {
		// Initialize the server by joining itself.

		log.Println("Initializing new cluster")

		_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
			Name:             s.raftServer.Name(),
			ConnectionString: s.connection_string,
		})
		if err != nil {
			log.Fatal(err)
		}

	} else {
		log.Println("Recovered from log")
	}

	// Initialize and start HTTP server.
	s.httpServer = &http.Server{
		Handler: s.router,
	}

	s.router.HandleFunc("/sql", s.sqlHandler).Methods("POST")
	s.router.HandleFunc("/join", s.joinHandler).Methods("POST")
	s.router.HandleFunc("/forward", s.forwardHandler).Methods("GET")

	// Start Unix transport
	l, err := transport.Listen(s.listen)
	if err != nil {
		log.Fatal(err)
	}
	return s.httpServer.Serve(l)
}
开发者ID:jstanley0,项目名称:stripe-ctf-3,代码行数:74,代码来源:server.go


示例14: startRaft

func (s *RaftServer) startRaft(potentialLeaders []string, retryUntilJoin bool) {
	log.Info("Initializing Raft Server: %s %d", s.path, s.port)

	// Initialize and start Raft server.
	transporter := raft.NewHTTPTransporter("/raft")
	var err error
	s.raftServer, err = raft.NewServer(s.name, s.path, transporter, nil, s.clusterConfig, "")
	if err != nil {
		log.Error(err)
	}

	transporter.Install(s.raftServer, s)
	s.raftServer.Start()

	if !s.raftServer.IsLogEmpty() {
		log.Info("Recovered from log")
		return
	}

	for {
		joined := false
		for _, leader := range potentialLeaders {
			log.Info("(raft:%s) Attempting to join leader: %s", s.raftServer.Name(), leader)

			if err := s.Join(leader); err == nil {
				joined = true
				log.Info("Joined: %s", leader)
				break
			}
		}

		if joined {
			break
		} else if retryUntilJoin {
			log.Warn("Couldn't join any of the seeds, sleeping and retrying...")
			time.Sleep(100 * time.Millisecond)
			continue
		}

		// couldn't join a leader so we must be the first one up
		log.Warn("Couldn't contact a leader so initializing new cluster for server on port %d", s.port)

		name := s.raftServer.Name()
		connectionString := s.connectionString()
		_, err := s.raftServer.Do(&InfluxJoinCommand{
			Name:                     name,
			ConnectionString:         connectionString,
			ProtobufConnectionString: s.config.ProtobufConnectionString(),
		})

		if err != nil {
			log.Error(err)
		}

		command := NewAddPotentialServerCommand(&ClusterServer{
			RaftName:                 name,
			RaftConnectionString:     connectionString,
			ProtobufConnectionString: s.config.ProtobufConnectionString(),
		})
		s.doOrProxyCommand(command, "add_server")
		s.CreateRootUser()
		break
	}
}
开发者ID:ronaldevers,项目名称:influxdb,代码行数:64,代码来源:raft_server.go


示例15: NewRaftkv

// NewRaftkv returns a new Raftkv and an error
func NewRaftkv(
	peers []string,
	kvs KVstore,
	dir string,
	Addr string,
	port int,
	transporterPrefix string,
	transporterTimeout time.Duration,
	pulse time.Duration,
) (rkv *Raftkv, err error) {
	connectionString := fmt.Sprintf("%s:%d", Addr, port)
	rkv = &Raftkv{
		peers:    peers,
		kvs:      kvs,
		dataDir:  dir,
		router:   mux.NewRouter(),
		httpAddr: connectionString,
		port:     port,
	}
	if err = os.MkdirAll(dir, 0700); err != nil {
		return nil, err
	}
	// Clear old cluster's configuration
	if len(rkv.peers) > 0 {
		if err = os.RemoveAll(path.Join(rkv.dataDir, "conf")); err != nil {
			return nil, err
		}
		if err = os.RemoveAll(path.Join(rkv.dataDir, "log")); err != nil {
			return nil, err
		}
		if err = os.RemoveAll(path.Join(rkv.dataDir, "snapshot")); err != nil {
			return nil, err
		}
	}

	transporter := raft.NewHTTPTransporter(transporterPrefix, transporterTimeout)
	rkv.server, err = raft.NewServer(connectionString, dir, transporter, nil, rkv.kvs, connectionString)
	transporter.Install(rkv.server, rkv)
	if err = rkv.server.Start(); err != nil {
		return nil, err
	}

	if pulse > 0 {
		rkv.server.SetHeartbeatInterval(pulse)
		rkv.server.SetElectionTimeout(pulse * 5)
	}

	rkv.router.HandleFunc("/raftkv_join", rkv.joinHandler)
	rkv.router.HandleFunc("/raftkv_leave", rkv.leaveHandler)
	rkv.router.HandleFunc("/raftkv_put", rkv.redirectedPut)
	rkv.router.HandleFunc("/raftkv_del", rkv.redirectedDel)
	rkv.router.HandleFunc("/raftkv_get", rkv.redirectedGet)

	if len(rkv.peers) > 0 {
		// fmt.Println(peers)
		err := rkv.Join(rkv.peers)
		if err != nil {
			logger.Println(err)
			// if cannot join clusters, joins itself
			logger.Printf("i am %s, i join myself\n", rkv.server.Name())
			_, err = rkv.server.Do(&raft.DefaultJoinCommand{
				Name:             rkv.server.Name(),
				ConnectionString: connectionString,
			})
			if err != nil {
				return nil, err
			}
		}
	} else if rkv.server.IsLogEmpty() {
		// Initialize the server by joining itself
		_, err = rkv.server.Do(&raft.DefaultJoinCommand{
			Name:             rkv.server.Name(),
			ConnectionString: connectionString,
		})
		if err != nil {
			return nil, err
		}
	}

	return rkv, nil
}
开发者ID:lilwulin,项目名称:raftkv,代码行数:82,代码来源:raft_server.go



注:本文中的github.com/goraft/raft.NewHTTPTransporter函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang raft.NewServer函数代码示例发布时间:2022-05-23
下一篇:
Golang models.Campaign类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap