-
Notifications
You must be signed in to change notification settings - Fork 4
/
zookeeper.go
99 lines (83 loc) · 2.06 KB
/
zookeeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package test
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/fsouza/go-dockerclient"
)
const (
// config file name and template
zkCfgFileName = "zoo_test.cfg"
zkCfgTpl = `
tickTime=100
initLimit=20
syncLimit=20
dataDir={{.ZK_DATA_DIR}}
clientPort={{.ZK_PORT}}
`
)
func init() {
RegisterService(ZooKeeper, func() Service {
return &zkService{}
})
}
type zkService struct {
port int
workDir string
}
func (s *zkService) Start() (string, error) {
// perform default check
if err := CheckExecutable("java", "zkServer.sh"); err != nil {
return "", err
}
// booking 4 ports
ports, err := BookPorts(1)
if err != nil {
return "", fmt.Errorf("fail to book ports, err:%v", err)
}
s.port = ports[0]
// prepare tmp dir
s.workDir, err = ioutil.TempDir("", "zk-test")
if err != nil {
return "", fmt.Errorf("fail to prepare tmp dir, err:%v", err)
}
// prepare cfg
if err = ApplyTemplate(
s.cfgFile(),
zkCfgTpl,
map[string]interface{}{
"ZK_PORT": s.port,
"ZK_DATA_DIR": s.workDir,
}); err != nil {
return "", fmt.Errorf("fail to prepare cfg file, err:%v", err)
}
// leverage zkServer.sh to start zk with config file
if err := Exec(
s.workDir, nil, nil,
"zkServer.sh", "start", s.cfgFile()); err != nil {
return "", fmt.Errorf("fail to start hbase master, err:%v", err)
}
// Make sure zk really starts
if err := WaitPortAvail(s.port, 20*time.Second); err != nil {
return "", fmt.Errorf("port %v isn't available after 20 sec", s.port)
}
// only need region server thrift port
return fmt.Sprintf("localhost:%d", s.port), nil
}
func (s *zkService) Stop() error {
return Exec(
s.workDir, nil, nil,
"zkServer.sh", "stop", s.cfgFile())
}
// StartDocker start the service via docker
func (s *zkService) StartDocker(cl *docker.Client) (string, error) {
return "", fmt.Errorf("implmenet this")
}
// StopDocker stops the service via docker
func (s *zkService) StopDocker(cl *docker.Client) error {
return fmt.Errorf("implmenet this")
}
func (s *zkService) cfgFile() string {
return filepath.Join(s.workDir, zkCfgFileName)
}