github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/ngaut/go-zookeeper/zk/server_java.go (about) 1 package zk 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "os/exec" 8 "path/filepath" 9 ) 10 11 type ErrMissingServerConfigField string 12 13 func (e ErrMissingServerConfigField) Error() string { 14 return fmt.Sprintf("zk: missing server config field '%s'", string(e)) 15 } 16 17 const ( 18 DefaultServerTickTime = 2000 19 DefaultServerInitLimit = 10 20 DefaultServerSyncLimit = 5 21 DefaultServerAutoPurgeSnapRetainCount = 3 22 DefaultPeerPort = 2888 23 DefaultLeaderElectionPort = 3888 24 ) 25 26 type ServerConfigServer struct { 27 ID int 28 Host string 29 PeerPort int 30 LeaderElectionPort int 31 } 32 33 type ServerConfig struct { 34 TickTime int // Number of milliseconds of each tick 35 InitLimit int // Number of ticks that the initial synchronization phase can take 36 SyncLimit int // Number of ticks that can pass between sending a request and getting an acknowledgement 37 DataDir string // Direcrory where the snapshot is stored 38 ClientPort int // Port at which clients will connect 39 AutoPurgeSnapRetainCount int // Number of snapshots to retain in dataDir 40 AutoPurgePurgeInterval int // Purge task internal in hours (0 to disable auto purge) 41 Servers []ServerConfigServer 42 } 43 44 func (sc ServerConfig) Marshall(w io.Writer) error { 45 if sc.DataDir == "" { 46 return ErrMissingServerConfigField("dataDir") 47 } 48 fmt.Fprintf(w, "dataDir=%s\n", sc.DataDir) 49 if sc.TickTime <= 0 { 50 sc.TickTime = DefaultServerTickTime 51 } 52 fmt.Fprintf(w, "tickTime=%d\n", sc.TickTime) 53 if sc.InitLimit <= 0 { 54 sc.InitLimit = DefaultServerInitLimit 55 } 56 fmt.Fprintf(w, "initLimit=%d\n", sc.InitLimit) 57 if sc.SyncLimit <= 0 { 58 sc.SyncLimit = DefaultServerSyncLimit 59 } 60 fmt.Fprintf(w, "syncLimit=%d\n", sc.SyncLimit) 61 if sc.ClientPort <= 0 { 62 sc.ClientPort = DefaultPort 63 } 64 fmt.Fprintf(w, "clientPort=%d\n", sc.ClientPort) 65 if sc.AutoPurgePurgeInterval > 0 { 66 if sc.AutoPurgeSnapRetainCount <= 0 { 67 sc.AutoPurgeSnapRetainCount = DefaultServerAutoPurgeSnapRetainCount 68 } 69 fmt.Fprintf(w, "autopurge.snapRetainCount=%d\n", sc.AutoPurgeSnapRetainCount) 70 fmt.Fprintf(w, "autopurge.purgeInterval=%d\n", sc.AutoPurgePurgeInterval) 71 } 72 if len(sc.Servers) > 0 { 73 for _, srv := range sc.Servers { 74 if srv.PeerPort <= 0 { 75 srv.PeerPort = DefaultPeerPort 76 } 77 if srv.LeaderElectionPort <= 0 { 78 srv.LeaderElectionPort = DefaultLeaderElectionPort 79 } 80 fmt.Fprintf(w, "server.%d=%s:%d:%d\n", srv.ID, srv.Host, srv.PeerPort, srv.LeaderElectionPort) 81 } 82 } 83 return nil 84 } 85 86 var jarSearchPaths = []string{ 87 "zookeeper-*/contrib/fatjar/zookeeper-*-fatjar.jar", 88 "../zookeeper-*/contrib/fatjar/zookeeper-*-fatjar.jar", 89 "/usr/share/java/zookeeper-*.jar", 90 "/usr/local/zookeeper-*/contrib/fatjar/zookeeper-*-fatjar.jar", 91 "/usr/local/Cellar/zookeeper/*/libexec/contrib/fatjar/zookeeper-*-fatjar.jar", 92 } 93 94 func findZookeeperFatJar() string { 95 var paths []string 96 zkPath := os.Getenv("ZOOKEEPER_PATH") 97 if zkPath == "" { 98 paths = jarSearchPaths 99 } else { 100 paths = []string{filepath.Join(zkPath, "contrib/fatjar/zookeeper-*-fatjar.jar")} 101 } 102 for _, path := range paths { 103 matches, _ := filepath.Glob(path) 104 // TODO: could sort by version and pick latest 105 if len(matches) > 0 { 106 return matches[0] 107 } 108 } 109 return "" 110 } 111 112 type Server struct { 113 JarPath string 114 ConfigPath string 115 116 cmd *exec.Cmd 117 } 118 119 func (srv *Server) Start() error { 120 if srv.JarPath == "" { 121 srv.JarPath = findZookeeperFatJar() 122 if srv.JarPath == "" { 123 return fmt.Errorf("zk: unable to find server jar") 124 } 125 } 126 srv.cmd = exec.Command("java", "-jar", srv.JarPath, "server", srv.ConfigPath) 127 // srv.cmd.Stdout = os.Stdout 128 // srv.cmd.Stderr = os.Stderr 129 err := srv.cmd.Start() 130 if err != nil { 131 fmt.Println("start failed", err) 132 } 133 134 fmt.Println("start zookeeper ok") 135 136 return err 137 } 138 139 func (srv *Server) Stop() error { 140 srv.cmd.Process.Signal(os.Kill) 141 return srv.cmd.Wait() 142 }