本文整理汇总了Golang中github.com/goraft/raft.RegisterCommand函数的典型用法代码示例。如果您正苦于以下问题:Golang RegisterCommand函数的具体用法?Golang RegisterCommand怎么用?Golang RegisterCommand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RegisterCommand函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: init
func init() {
// Register Raft Commands
raft.RegisterCommand(&AddServiceCommand{})
raft.RegisterCommand(&UpdateTTLCommand{})
raft.RegisterCommand(&RemoveServiceCommand{})
raft.RegisterCommand(&AddCallbackCommand{})
}
示例2: init
func init() {
// Register Raft Commands
raft.RegisterCommand(&AddServiceCommand{})
raft.RegisterCommand(&UpdateTTLCommand{})
raft.RegisterCommand(&RemoveServiceCommand{})
expiredCount = metrics.NewCounter()
metrics.Register("skydns-expired-entries", expiredCount)
requestCount = metrics.NewCounter()
metrics.Register("skydns-requests", requestCount)
addServiceCount = metrics.NewCounter()
metrics.Register("skydns-add-service-requests", addServiceCount)
updateTTLCount = metrics.NewCounter()
metrics.Register("skydns-update-ttl-requests", updateTTLCount)
getServiceCount = metrics.NewCounter()
metrics.Register("skydns-get-service-requests", getServiceCount)
removeServiceCount = metrics.NewCounter()
metrics.Register("skydns-remove-service-requests", removeServiceCount)
}
示例3: 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.
}
示例4: main
func main() {
go func() {
//http://localhost:6060/debug/pprof/
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
log.SetFlags(0)
flag.Parse()
if verbose {
log.Print("Verbose logging enabled.")
}
if trace {
raft.SetLogLevel(raft.Trace)
log.Print("Raft trace debugging enabled.")
} else if debug {
raft.SetLogLevel(raft.Debug)
log.Print("Raft debugging enabled.")
}
rand.Seed(time.Now().UnixNano())
// Setup commands.
raft.RegisterCommand(&command.SetCommand{})
raft.RegisterCommand(&command.RemoveTmpDataCommand{})
// Set the data directory.
if flag.NArg() == 0 {
flag.Usage()
log.Fatal("Data path argument required")
}
path := flag.Arg(0)
if err := os.MkdirAll(path, 0744); err != nil {
log.Fatalf("Unable to create path: %v", err)
}
// log.Fatalf("join: %v", join)
// log.Fatalf(" path: %v", path)
log.SetFlags(log.LstdFlags)
s := server.New(path, host, port)
if true {
runtime.GOMAXPROCS(runtime.NumCPU())
}
addr := fmt.Sprintf("%s:%d", host, tcpSerPort)
//log.Fatal(s.ListenAndServe(join, addr))
s.ListenAndServe(join, addr)
select {}
}
示例5: main
func main() {
log.SetFlags(0)
flag.Parse()
if verbose {
log.Print("Verbose logging enabled.")
}
if trace {
raft.SetLogLevel(raft.Trace)
log.Print("Raft trace debugging enabled.")
} else if debug {
raft.SetLogLevel(raft.Debug)
log.Print("Raft debugging enabled.")
}
rand.Seed(time.Now().UnixNano())
// Setup commands.
raft.RegisterCommand(&command.WriteCommand{})
// Set the data directory.
if flag.NArg() == 0 {
log.Fatal("Data path argument required")
}
path := flag.Arg(0)
if err := os.MkdirAll(path, 0744); err != nil {
log.Fatalf("Unable to create path: %v", err)
}
log.SetFlags(log.LstdFlags)
s := server.New(path, host, port)
log.Fatal(s.ListenAndServe(join))
}
示例6: NewRaftServer
// Creates a new server.
func NewRaftServer(config *configuration.Configuration, clusterConfig *ClusterConfiguration) *RaftServer {
if !registeredCommands {
registeredCommands = true
for _, command := range internalRaftCommands {
raft.RegisterCommand(command)
}
}
s := &RaftServer{
host: config.HostnameOrDetect(),
port: config.RaftServerPort,
path: config.RaftDir,
clusterConfig: clusterConfig,
router: mux.NewRouter(),
config: config,
}
rand.Seed(time.Now().Unix())
// Read existing name or generate a new one.
if b, err := ioutil.ReadFile(filepath.Join(s.path, "name")); err == nil {
s.name = string(b)
} else {
s.name = fmt.Sprintf("%07x", rand.Int())[0:7]
if err = ioutil.WriteFile(filepath.Join(s.path, "name"), []byte(s.name), 0644); err != nil {
panic(err)
}
}
return s
}
示例7: NewRaftServer
// Creates a new server.
func NewRaftServer(config *configuration.Configuration, clusterConfig *cluster.ClusterConfiguration) *RaftServer {
if !registeredCommands {
registeredCommands = true
for _, command := range internalRaftCommands {
raft.RegisterCommand(command)
}
}
s := &RaftServer{
host: config.HostnameOrDetect(),
port: config.RaftServerPort,
path: config.RaftDir,
bind_address: config.BindAddress,
clusterConfig: clusterConfig,
notLeader: make(chan bool, 1),
router: mux.NewRouter(),
config: config,
}
// Read existing name or generate a new one.
if b, err := ioutil.ReadFile(filepath.Join(s.path, "name")); err == nil {
s.name = string(b)
} else {
var i uint64
if _, err := os.Stat("/dev/random"); err == nil {
log.Info("Using /dev/random to initialize the raft server name")
f, err := os.Open("/dev/random")
if err != nil {
panic(err)
}
defer f.Close()
readBytes := 0
b := make([]byte, 8)
for readBytes < 8 {
n, err := f.Read(b[readBytes:])
if err != nil {
panic(err)
}
readBytes += n
}
err = binary.Read(bytes.NewBuffer(b), binary.BigEndian, &i)
if err != nil {
panic(err)
}
} else {
log.Info("Using rand package to generate raft server name")
rand.Seed(time.Now().UnixNano())
i = uint64(rand.Int())
}
s.name = fmt.Sprintf("%07x", i)[0:7]
log.Info("Setting raft name to %s", s.name)
if err = ioutil.WriteFile(filepath.Join(s.path, "name"), []byte(s.name), 0644); err != nil {
panic(err)
}
}
return s
}
示例8: startSer
func startSer() {
// Setup commands.
raft.RegisterCommand(&command.SetCommand{})
raft.RegisterCommand(&command.RemoveTmpDataCommand{})
path := kPath
if err := os.MkdirAll(path, 0744); err != nil {
log.Fatalf("Unable to create path: %v", err)
}
log.SetFlags(log.LstdFlags)
s := server.New(path, host, port)
addr := fmt.Sprintf("%s:%d", host, tcpSerPort)
s.ListenAndServe(join, addr)
}
示例9: main
func main() {
log.SetFlags(0)
flag.Parse()
if verbose {
log.Print("Verbose logging enabled.")
}
if trace {
raft.SetLogLevel(raft.Trace)
log.Print("Raft trace debugging enabled.")
} else if debug {
raft.SetLogLevel(raft.Debug)
log.Print("Raft debugging enabled.")
}
rand.Seed(time.Now().UnixNano())
raft.RegisterCommand(&WriteCommand{})
if flag.NArg() == 0 {
flag.Usage()
log.Fatal("Data path argument required")
}
path := flag.Arg(0)
if err := os.MkdirAll(path, 0744); err != nil {
log.Fatalf("Unable to creater path: %s", err.Error())
}
log.SetFlags(log.LstdFlags)
s := NewServer(path, host, port)
log.Fatal(s.ListenAndServer(join))
}
示例10: main
func main() {
//defer profile.Start(profile.CPUProfile).Stop()
var raftdebug, verbose bool
var listen, join, directory string
flag.BoolVar(&verbose, "v", false, "Enable debug output")
flag.BoolVar(&raftdebug, "r", false, "Enable raft debugging")
flag.StringVar(&listen, "l", "127.0.0.1:4000", "Socket to listen on (Unix or TCP)")
flag.StringVar(&join, "join", "", "Cluster to join")
flag.StringVar(&directory, "d", "", "Storage directory")
dir := filepath.Dir(os.Args[0])
base := "./" + filepath.Base(os.Args[0])
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s [options]
SQLCluster is a highly-available SQL store. It accepts commands over
HTTP, and returns the output of commands together with a
SequenceNumber which indicates the ordering in which requests are
being applied. (SequenceNumber is useful for validation, and can
otherwise be ignored.)
Run a cluster as follows:
cd %s
%s -d /tmp/sqlcluster/node0 &
%s -d /tmp/sqlcluster/node1 -l 127.0.0.1:4001 --join 127.0.0.1:4000 &
%s -d /tmp/sqlcluster/node2 -l 127.0.0.1:4002 --join 127.0.0.1:4000
You can then issue queries using your favorite HTTP client.
curl 127.0.0.1:4000/sql -d 'CREATE TABLE hello (world int)'
curl 127.0.0.1:4000/sql -d 'INSERT INTO hello (world) VALUES (1), (2)'
curl 127.0.0.1:4000/sql -d 'SELECT * FROM hello'
This should return the following sequence of outputs:
- SequenceNumber: 0
- SequenceNumber: 1
- SequenceNumber: 2
1
2
By default, SQLCluster will listen on a TCP port. However, if you
specify a listen address that begins with a / or ., that will be
interpeted as Unix path for SQLCluster to listen on. (Note that
Octopus runs using Unix sockets only, but it will probably be more
convient for you to develop using TCP.)
OPTIONS:
`, os.Args[0], dir, base, base, base)
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 0 {
flag.Usage()
os.Exit(1)
}
log.SetVerbose(verbose)
if directory == "" {
var err error
directory, err = ioutil.TempDir("/tmp", "node")
if err != nil {
log.Fatalf("Could not create temporary base directory: %s", err)
}
defer os.RemoveAll(directory)
log.Printf("Storing state in tmpdir %s", directory)
} else {
if err := os.MkdirAll(directory, os.ModeDir|0755); err != nil {
log.Fatalf("Error while creating storage directory: %s", err)
}
}
log.Printf("Changing directory to %s", directory)
if err := os.Chdir(directory); err != nil {
log.Fatalf("Error while changing to storage directory: %s", err)
}
// Make sure we don't leave stranded sqlclusters lying around
go func() {
for {
time.Sleep(2 * time.Second)
if os.Getppid() == 1 {
log.Fatal("Parent process exited; terminating")
}
}
}()
runtime.GOMAXPROCS(4)
rand.Seed(time.Now().UnixNano())
raft.RegisterCommand(&server.WriteCommand{})
if raftdebug {
//.........这里部分代码省略.........
示例11: init
func init() {
raft.RegisterCommand(&AddCommand{})
}
示例12: ListenAndServe
// Starts the server.
func (s *Server) ListenAndServe(primary string) error {
var err error
rand.Seed(int64(time.Now().Nanosecond()))
s.primary = primary
s.name = "name-" + s.listen
raft.RegisterCommand(&BatchCommand{})
// Initialize and start HTTP server.
s.httpServer = &http.Server{
Handler: s.router,
}
httpTransport := transport.NewClient().GetHTTPClient()
//log.Printf(("Initializing Raft Server")
transporter := NewHTTPTransporter("/raft", *httpTransport)
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()
s.raftServer.SetElectionTimeout(400 * time.Millisecond)
s.raftServer.AddEventListener("addPeer", func(e raft.Event) {
//log.Printf("Joined!")
s.joined = true
})
s.raftServer.AddEventListener("leaderChange", func(e raft.Event) {
leader := e.Value().(string)
if leader == s.name {
//log.Printf("Leader Changed to %v", leader)
s.leaderNotify <- leader
}
})
if primary == "" {
cs, err := transport.Encode(s.listen)
if err != nil {
log.Fatal(err)
}
//log.Printf(("Starting as Leader")
_, err = s.raftServer.Do(&raft.DefaultJoinCommand{
Name: s.raftServer.Name(),
ConnectionString: cs,
})
//log.Printf(("I am Leader")
if err != nil {
log.Fatal(err)
}
} else {
//log.Printf("Waiting 100 milliseconds to join Primary")
time.AfterFunc(10*time.Millisecond, func() {
maxTries := 25
tries := 0
for !s.joined {
//log.Printf("Trying to Join")
tries++
//log.Printf("Attempting to Join")
s.Join(primary)
// if err != nil {
// //log.Printf("Failed to join")
// } else {
// //log.Printf("Joined!")
// break
// }
if tries > maxTries {
log.Fatal("Could not join!")
}
time.Sleep(JOIN_TIMEOUT)
}
})
}
s.router.HandleFunc("/sql", s.sqlHandler).Methods("POST")
s.router.HandleFunc("/healthcheck", s.healthcheckHandler).Methods("GET")
s.router.HandleFunc("/join", s.joinHandler).Methods("POST")
s.router.HandleFunc("/forward", s.forwardHandler).Methods("POST")
// Start Unix transport
l, err := transport.Listen(s.listen)
if err != nil {
log.Fatal(err)
}
//log.Printf(("Serving?")
//.........这里部分代码省略.........
示例13: 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
}
示例14: init
func init() {
raft.RegisterCommand(&putCommand{})
raft.RegisterCommand(&delCommand{})
raft.RegisterCommand(&getCommand{})
}