forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restart zookeeper server for every test.
Also, make it easier to start test clusters. This will allow testing of more scenarios.
- Loading branch information
Showing
7 changed files
with
289 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
const ( | ||
protocolVersion = 0 | ||
|
||
defaultPort = 2181 | ||
DefaultPort = 2181 | ||
) | ||
|
||
const ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package zk | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
type ErrMissingServerConfigField string | ||
|
||
func (e ErrMissingServerConfigField) Error() string { | ||
return fmt.Sprintf("zk: missing server config field '%s'", string(e)) | ||
} | ||
|
||
const ( | ||
DefaultServerTickTime = 2000 | ||
DefaultServerInitLimit = 10 | ||
DefaultServerSyncLimit = 5 | ||
DefaultServerAutoPurgeSnapRetainCount = 3 | ||
DefaultPeerPort = 2888 | ||
DefaultLeaderElectionPort = 3888 | ||
) | ||
|
||
type ServerConfigServer struct { | ||
Id int | ||
Host string | ||
PeerPort int | ||
LeaderElectionPort int | ||
} | ||
|
||
type ServerConfig struct { | ||
TickTime int // Number of milliseconds of each tick | ||
InitLimit int // Number of ticks that the initial synchronization phase can take | ||
SyncLimit int // Number of ticks that can pass between sending a request and getting an acknowledgement | ||
DataDir string // Direcrory where the snapshot is stored | ||
ClientPort int // Port at which clients will connect | ||
AutoPurgeSnapRetainCount int // Number of snapshots to retain in dataDir | ||
AutoPurgePurgeInterval int // Purge task internal in hours (0 to disable auto purge) | ||
Servers []ServerConfigServer | ||
} | ||
|
||
func (sc ServerConfig) Marshall(w io.Writer) error { | ||
if sc.DataDir == "" { | ||
return ErrMissingServerConfigField("dataDir") | ||
} | ||
fmt.Fprintf(w, "dataDir=%s\n", sc.DataDir) | ||
if sc.TickTime <= 0 { | ||
sc.TickTime = DefaultServerTickTime | ||
} | ||
fmt.Fprintf(w, "tickTime=%d\n", sc.TickTime) | ||
if sc.InitLimit <= 0 { | ||
sc.InitLimit = DefaultServerInitLimit | ||
} | ||
fmt.Fprintf(w, "initLimit=%d\n", sc.InitLimit) | ||
if sc.SyncLimit <= 0 { | ||
sc.SyncLimit = DefaultServerSyncLimit | ||
} | ||
fmt.Fprintf(w, "syncLimit=%d\n", sc.SyncLimit) | ||
if sc.ClientPort <= 0 { | ||
sc.ClientPort = DefaultPort | ||
} | ||
fmt.Fprintf(w, "clientPort=%d\n", sc.ClientPort) | ||
if sc.AutoPurgePurgeInterval > 0 { | ||
if sc.AutoPurgeSnapRetainCount <= 0 { | ||
sc.AutoPurgeSnapRetainCount = DefaultServerAutoPurgeSnapRetainCount | ||
} | ||
fmt.Fprintf(w, "autopurge.snapRetainCount=%d\n", sc.AutoPurgeSnapRetainCount) | ||
fmt.Fprintf(w, "autopurge.purgeInterval=%d\n", sc.AutoPurgePurgeInterval) | ||
} | ||
if len(sc.Servers) > 0 { | ||
for _, srv := range sc.Servers { | ||
if srv.PeerPort <= 0 { | ||
srv.PeerPort = DefaultPeerPort | ||
} | ||
if srv.LeaderElectionPort <= 0 { | ||
srv.LeaderElectionPort = DefaultLeaderElectionPort | ||
} | ||
fmt.Fprintf(w, "server.%d=%s:%d:%d\n", srv.Id, srv.Host, srv.PeerPort, srv.LeaderElectionPort) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
var jarSearchPaths = []string{ | ||
"zookeeper-*/contrib/fatjar/zookeeper-*-fatjar.jar", | ||
"/usr/local/zookeeper-*/contrib/fatjar/zookeeper-*-fatjar.jar", | ||
"/usr/local/Cellar/zookeeper/*/libexec/contrib/fatjar/zookeeper-*-fatjar.jar", | ||
} | ||
|
||
func findZookeeperFatJar() string { | ||
for _, path := range jarSearchPaths { | ||
matches, _ := filepath.Glob(path) | ||
// TODO: could sort by version and pick latest | ||
if len(matches) > 0 { | ||
return matches[0] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
type Server struct { | ||
JarPath string | ||
ConfigPath string | ||
|
||
cmd *exec.Cmd | ||
} | ||
|
||
func (srv *Server) Start() error { | ||
if srv.JarPath == "" { | ||
srv.JarPath = findZookeeperFatJar() | ||
if srv.JarPath == "" { | ||
return fmt.Errorf("zk: unable to find server jar") | ||
} | ||
} | ||
srv.cmd = exec.Command("java", "-jar", srv.JarPath, "server", srv.ConfigPath) | ||
// srv.cmd.Stdout = os.Stdout | ||
// srv.cmd.Stderr = os.Stderr | ||
return srv.cmd.Start() | ||
} | ||
|
||
func (srv *Server) Stop() error { | ||
srv.cmd.Process.Signal(os.Kill) | ||
return srv.cmd.Wait() | ||
} |
Oops, something went wrong.