OLD | NEW |
1 package zookeeper | 1 package zookeeper |
2 | 2 |
3 import ( | 3 import ( |
4 "bufio" | 4 "bufio" |
5 "bytes" | 5 "bytes" |
6 "errors" | 6 "errors" |
7 "fmt" | 7 "fmt" |
8 "io/ioutil" | 8 "io/ioutil" |
9 "net" | 9 "net" |
10 "os" | 10 "os" |
11 "path/filepath" | 11 "path/filepath" |
12 "strings" | 12 "strings" |
13 ) | 13 ) |
14 | 14 |
15 // Server represents a ZooKeeper server, its data and configuration files. | 15 // Server represents a ZooKeeper server, its data and configuration files. |
16 type Server struct { | 16 type Server struct { |
17 runDir string | 17 runDir string |
18 zkDir string | 18 zkDir string |
19 } | 19 } |
20 | 20 |
21 // CreateServer creates the directory runDir and sets up a ZooKeeper server | 21 // CreateServer creates the directory runDir and sets up a ZooKeeper |
22 // environment inside it. It is an error if runDir already exists. | 22 // server environment inside it. It is an error if runDir already |
23 // The server will listen on the specified TCP port. | 23 // exists and is not empty. The server will listen on the specified TCP |
| 24 // port. |
24 //· | 25 //· |
25 // The ZooKeeper installation directory is specified by zkDir. | 26 // The ZooKeeper installation directory is specified by zkDir. |
26 // If this is empty, a system default will be used. | 27 // If this is empty, a system default will be used. |
27 // | 28 // |
28 // CreateServer does not start the server. | 29 // CreateServer does not start the server. |
29 func CreateServer(port int, runDir, zkDir string) (*Server, error) { | 30 func CreateServer(port int, runDir, zkDir string) (*Server, error) { |
30 if err := os.Mkdir(runDir, 0777); err != nil { | 31 if err := os.Mkdir(runDir, 0777); err != nil { |
31 » » return nil, err | 32 » » if !os.IsExist(err) { |
| 33 » » » return nil, err |
| 34 » » } |
| 35 » » info, err := ioutil.ReadDir(runDir) |
| 36 » » if err != nil { |
| 37 » » » return nil, err |
| 38 » » } |
| 39 » » if len(info) != 0 { |
| 40 » » » return nil, fmt.Errorf("server directory %q is not empty
") |
| 41 » » } |
32 } | 42 } |
33 srv := &Server{runDir: runDir, zkDir: zkDir} | 43 srv := &Server{runDir: runDir, zkDir: zkDir} |
34 if err := srv.writeLog4JConfig(); err != nil { | 44 if err := srv.writeLog4JConfig(); err != nil { |
35 return nil, err | 45 return nil, err |
36 } | 46 } |
37 if err := srv.writeZooKeeperConfig(port); err != nil { | 47 if err := srv.writeZooKeeperConfig(port); err != nil { |
38 return nil, err | 48 return nil, err |
39 } | 49 } |
40 if err := srv.writeZkDir(); err != nil { | 50 if err := srv.writeZkDir(); err != nil { |
41 return nil, err | 51 return nil, err |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return 0, fmt.Errorf("cannot get port from %q", srv.path
("zoo.cfg")) | 91 return 0, fmt.Errorf("cannot get port from %q", srv.path
("zoo.cfg")) |
82 } | 92 } |
83 var port int | 93 var port int |
84 if n, _ := fmt.Sscanf(string(line), "clientPort=%d\n", &port); n
== 1 { | 94 if n, _ := fmt.Sscanf(string(line), "clientPort=%d\n", &port); n
== 1 { |
85 return port, nil | 95 return port, nil |
86 } | 96 } |
87 } | 97 } |
88 panic("not reached") | 98 panic("not reached") |
89 } | 99 } |
90 | 100 |
| 101 // Addr returns a local host address that can be used |
| 102 // to contact the server when it is running. |
| 103 func (srv *Server) Addr() (string, error) { |
| 104 port, err := srv.networkPort() |
| 105 if err != nil { |
| 106 return "", err |
| 107 } |
| 108 return fmt.Sprintf("127.0.0.1:%d", port), nil |
| 109 } |
| 110 |
91 // command returns the command used to start the | 111 // command returns the command used to start the |
92 // ZooKeeper server. | 112 // ZooKeeper server. |
93 func (srv *Server) command() ([]string, error) { | 113 func (srv *Server) command() ([]string, error) { |
94 cp, err := srv.classPath() | 114 cp, err := srv.classPath() |
95 if err != nil { | 115 if err != nil { |
96 return nil, fmt.Errorf("cannot get class path: %v", err) | 116 return nil, fmt.Errorf("cannot get class path: %v", err) |
97 } | 117 } |
98 return []string{ | 118 return []string{ |
99 "java", | 119 "java", |
100 "-cp", strings.Join(cp, ":"), | 120 "-cp", strings.Join(cp, ":"), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 return err | 241 return err |
222 } | 242 } |
223 return &os.PathError{Op: "stat", Path: path, Err: errors.New("is
not a directory")} | 243 return &os.PathError{Op: "stat", Path: path, Err: errors.New("is
not a directory")} |
224 } | 244 } |
225 return nil | 245 return nil |
226 } | 246 } |
227 | 247 |
228 func (srv *Server) path(name string) string { | 248 func (srv *Server) path(name string) string { |
229 return filepath.Join(srv.runDir, name) | 249 return filepath.Join(srv.runDir, name) |
230 } | 250 } |
OLD | NEW |