Browse Source

Add control+c or INT handling

pull/2/head
Chris Lu 11 years ago
parent
commit
982aaa41b9
  1. 2
      go/util/constants.go
  2. 59
      go/weed/server.go
  3. 6
      go/weed/weed_server/volume_server.go

2
go/util/constants.go

@ -3,5 +3,5 @@ package util
import () import ()
const ( const (
VERSION = "0.57"
VERSION = "0.57 beta"
) )

59
go/weed/server.go

@ -21,8 +21,8 @@ type ServerOptions struct {
} }
var ( var (
filer FilerOptions
serverOptions ServerOptions serverOptions ServerOptions
filerOptions FilerOptions
) )
func init() { func init() {
@ -70,12 +70,12 @@ var (
) )
func init() { func init() {
filer.master = cmdServer.Flag.String("filer.master", "", "default to current master server")
filer.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
filer.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
filer.dir = cmdServer.Flag.String("filer.dir", "", "directory to store meta data, default to a 'filer' sub directory of what -mdir is specified")
filer.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "write cpu profile to file") serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "write cpu profile to file")
filerOptions.master = cmdServer.Flag.String("filer.master", "", "default to current master server")
filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
filerOptions.dir = cmdServer.Flag.String("filer.dir", "", "directory to store meta data, default to a 'filer' sub directory of what -mdir is specified")
filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
} }
func runServer(cmd *Command, args []string) bool { func runServer(cmd *Command, args []string) bool {
@ -86,17 +86,6 @@ func runServer(cmd *Command, args []string) bool {
} }
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
// sig is a ^C, handle it
pprof.StopCPUProfile()
os.Exit(0)
}
}()
} }
if *serverPublicIp == "" { if *serverPublicIp == "" {
@ -107,10 +96,10 @@ func runServer(cmd *Command, args []string) bool {
} }
} }
*filer.master = *serverPublicIp + ":" + strconv.Itoa(*masterPort)
*filerOptions.master = *serverPublicIp + ":" + strconv.Itoa(*masterPort)
if *filer.defaultReplicaPlacement == "" {
*filer.defaultReplicaPlacement = *masterDefaultReplicaPlacement
if *filerOptions.defaultReplicaPlacement == "" {
*filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
} }
if *serverMaxCpu < 1 { if *serverMaxCpu < 1 {
@ -140,15 +129,15 @@ func runServer(cmd *Command, args []string) bool {
if *masterMetaFolder == "" { if *masterMetaFolder == "" {
*masterMetaFolder = folders[0] *masterMetaFolder = folders[0]
} }
if *filer.dir == "" {
*filer.dir = *masterMetaFolder + "/filer"
os.MkdirAll(*filer.dir, 0700)
if *filerOptions.dir == "" {
*filerOptions.dir = *masterMetaFolder + "/filer"
os.MkdirAll(*filerOptions.dir, 0700)
} }
if err := util.TestFolderWritable(*masterMetaFolder); err != nil { if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err) glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
} }
if err := util.TestFolderWritable(*filer.dir); err != nil {
glog.Fatalf("Check Mapping Meta Folder (-filer.dir=\"%s\") Writable: %s", *filer.dir, err)
if err := util.TestFolderWritable(*filerOptions.dir); err != nil {
glog.Fatalf("Check Mapping Meta Folder (-filer.dir=\"%s\") Writable: %s", *filerOptions.dir, err)
} }
if *serverWhiteListOption != "" { if *serverWhiteListOption != "" {
@ -158,13 +147,13 @@ func runServer(cmd *Command, args []string) bool {
if *isStartingFiler { if *isStartingFiler {
go func() { go func() {
r := http.NewServeMux() r := http.NewServeMux()
_, nfs_err := weed_server.NewFilerServer(r, *filer.port, *filer.master, *filer.dir, *filer.collection)
_, nfs_err := weed_server.NewFilerServer(r, *filerOptions.port, *filerOptions.master, *filerOptions.dir, *filerOptions.collection)
if nfs_err != nil { if nfs_err != nil {
glog.Fatalf(nfs_err.Error()) glog.Fatalf(nfs_err.Error())
} }
glog.V(0).Infoln("Start Weed Filer", util.VERSION, "at port", strconv.Itoa(*filer.port))
glog.V(0).Infoln("Start Weed Filer", util.VERSION, "at port", strconv.Itoa(*filerOptions.port))
filerListener, e := util.NewListener( filerListener, e := util.NewListener(
":"+strconv.Itoa(*filer.port),
":"+strconv.Itoa(*filerOptions.port),
time.Duration(10)*time.Second, time.Duration(10)*time.Second,
) )
if e != nil { if e != nil {
@ -216,7 +205,7 @@ func runServer(cmd *Command, args []string) bool {
volumeWait.Wait() volumeWait.Wait()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
r := http.NewServeMux() r := http.NewServeMux()
weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts,
volumeServer := weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts,
*serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList, *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList,
) )
@ -228,6 +217,18 @@ func runServer(cmd *Command, args []string) bool {
if e != nil { if e != nil {
glog.Fatalf(e.Error()) glog.Fatalf(e.Error())
} }
// deal with control+c
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
volumeServer.Shutdown()
pprof.StopCPUProfile()
os.Exit(0)
}
}()
if e := http.Serve(volumeListener, r); e != nil { if e := http.Serve(volumeListener, r); e != nil {
glog.Fatalf("Fail to serve:%s", e.Error()) glog.Fatalf("Fail to serve:%s", e.Error())
} }

6
go/weed/weed_server/volume_server.go

@ -74,3 +74,9 @@ func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, fol
return vs return vs
} }
func (vs *VolumeServer) Shutdown() {
glog.V(0).Infoln("Shutting down volume server...")
vs.store.Close()
glog.V(0).Infoln("Shut down successfully!")
}
Loading…
Cancel
Save