Move generic server initializations into a separate package.

This commit is contained in:
Sugu Sougoumarane 2012-07-03 14:32:10 -07:00
Родитель f1843c18c5
Коммит 2fdd372e17
2 изменённых файлов: 107 добавлений и 69 удалений

Просмотреть файл

@ -5,28 +5,21 @@
package main
import (
"code.google.com/p/vitess/go/logfile"
"errors"
"flag"
"fmt"
_ "net/http/pprof"
"net/rpc"
"syscall"
"code.google.com/p/vitess/go/relog"
"code.google.com/p/vitess/go/rpcwrap/bsonrpc"
"code.google.com/p/vitess/go/rpcwrap/jsonrpc"
"code.google.com/p/vitess/go/sighandler"
_ "code.google.com/p/vitess/go/snitch"
"code.google.com/p/vitess/go/umgmt"
"code.google.com/p/vitess/go/vt/servenv"
ts "code.google.com/p/vitess/go/vt/tabletserver"
"crypto/md5"
"encoding/hex"
"errors"
"expvar"
"flag"
"fmt"
"io"
"log"
_ "net/http/pprof"
"net/rpc"
"os"
"runtime"
"syscall"
"time"
)
const (
@ -34,65 +27,24 @@ const (
DefaultRebindDelay = 0.0
)
func exportBinaryVersion() {
hasher := md5.New()
exeFile, err := os.Open("/proc/self/exe")
if err != nil {
panic(err)
}
if _, err = io.Copy(hasher, exeFile); err != nil {
panic(err)
}
md5sum := hex.EncodeToString(hasher.Sum(nil))
fileInfo, err := exeFile.Stat()
if err != nil {
panic(err)
}
mtime := fileInfo.ModTime().Format(time.RFC3339)
version := mtime + " " + md5sum
expvar.NewString("binary-version").Set(version)
// rexport this value for varz scraper
expvar.NewString("Version").Set(version)
}
var (
port = flag.Int("port", 6510, "tcp port to serve on")
lameDuckPeriod = flag.Float64("lame-duck-period", DefaultLameDuckPeriod,
"how long to give in-flight transactions to finish")
rebindDelay = flag.Float64("rebind-delay", DefaultRebindDelay,
"artificial delay before rebinding a hijacked listener")
)
func main() {
port := flag.Int("port", 6510, "tcp port to serve on")
memProfileRate := flag.Int("mem-profile-rate", 512*1024, "profile every n bytes allocated")
maxOpenFds := flag.Uint64("max-open-fds", 32768, "max open file descriptors")
lameDuckPeriod := flag.Float64("lame-duck-period", DefaultLameDuckPeriod,
"how long to give in-flight transactions to finish")
rebindDelay := flag.Float64("rebind-delay", DefaultRebindDelay,
"artificial delay before rebinding a hijacked listener")
logfileName := flag.String("logfile", "/dev/stderr", "base log file name")
logFrequency := flag.Int64("logfile.frequency", 0, "rotation frequency in seconds")
logMaxSize := flag.Int64("logfile.maxsize", 0, "max file size in bytes")
logMaxFiles := flag.Int64("logfile.maxfiles", 0, "max number of log files")
flag.Parse()
exportBinaryVersion()
runtime.MemProfileRate = *memProfileRate
f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles)
if err != nil {
panic(fmt.Sprintf("unable to open logfile %s", *logfileName))
}
logger := relog.New(f, "vtocc ",
log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.DEBUG)
relog.SetLogger(logger)
fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds}
if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil {
relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err)
} else {
relog.Info("set max-open-fds = %v", *maxOpenFds)
}
env.Init("vtocc")
config, dbconfig := ts.Init()
qm := &OccManager{config, dbconfig}
rpc.Register(qm)
ts.StartQueryService(config)
ts.AllowQueries(dbconfig)
rpc.HandleHTTP()
jsonrpc.ServeHTTP()
jsonrpc.ServeRPC()
@ -102,7 +54,6 @@ func main() {
// we delegate out startup to the micromanagement server so these actions
// will occur after we have obtained our socket.
usefulLameDuckPeriod := float64(config.QueryTimeout + 1)
if usefulLameDuckPeriod > *lameDuckPeriod {
*lameDuckPeriod = usefulLameDuckPeriod
@ -114,8 +65,7 @@ func main() {
umgmt.StartHttpServer(fmt.Sprintf(":%v", *port))
})
umgmt.AddStartupCallback(func() {
sighandler.SetSignalHandler(syscall.SIGTERM,
umgmt.SigTermHandler)
sighandler.SetSignalHandler(syscall.SIGTERM, umgmt.SigTermHandler)
})
umgmt.AddCloseCallback(func() {
ts.DisallowQueries()
@ -128,6 +78,7 @@ func main() {
relog.Info("done")
}
// OccManager is deprecated. Use SqlQuery.GetSessionId instead.
type OccManager struct {
config ts.Config
dbconfig map[string]interface{}
@ -135,7 +86,8 @@ type OccManager struct {
func (self *OccManager) GetSessionId(dbname *string, sessionId *int64) error {
if *dbname != self.dbconfig["dbname"].(string) {
return errors.New(fmt.Sprintf("db name mismatch, expecting %v, received %v", self.dbconfig["dbname"].(string), *dbname))
return errors.New(fmt.Sprintf("db name mismatch, expecting %v, received %v",
self.dbconfig["dbname"].(string), *dbname))
}
*sessionId = ts.GetSessionId()
return nil

86
go/vt/servenv/servenv.go Normal file
Просмотреть файл

@ -0,0 +1,86 @@
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package env defines and initializes command line flags that control
the runtime environment.
After the main program has called flag.Parse, it needs to call env.Init
to make env use the command line variables to initialize the environment.
*/
package env
import (
"code.google.com/p/vitess/go/logfile"
"code.google.com/p/vitess/go/relog"
"crypto/md5"
"encoding/hex"
"expvar"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"syscall"
"time"
)
var (
logfileName = flag.String("logfile", "/dev/stderr", "base log file name")
logFrequency = flag.Int64("logfile.frequency", 0,
"rotation frequency in seconds")
logMaxSize = flag.Int64("logfile.maxsize", 0, "max file size in bytes")
logMaxFiles = flag.Int64("logfile.maxfiles", 0, "max number of log files")
memProfileRate = flag.Int("mem-profile-rate", 512*1024,
"profile every n bytes allocated")
maxOpenFds = flag.Uint64("max-open-fds", 32768, "max open file descriptors")
gomaxprocs = flag.Int("gomaxprocs", 0, "Sets GOMAXPROCS")
)
func Init(logPrefix string) {
f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles)
if err != nil {
panic(fmt.Sprintf("unable to open logfile %s", *logfileName))
}
logger := relog.New(f, logPrefix + " ",
log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.DEBUG)
relog.SetLogger(logger)
runtime.MemProfileRate = *memProfileRate
if *gomaxprocs != 0 {
runtime.GOMAXPROCS(*gomaxprocs)
relog.Info("set GOMAXPROCS = %v", *gomaxprocs)
}
fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds}
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil {
relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err)
} else {
relog.Info("set max-open-fds = %v", *maxOpenFds)
}
exportBinaryVersion()
}
func exportBinaryVersion() {
hasher := md5.New()
exeFile, err := os.Open("/proc/self/exe")
if err != nil {
panic(err)
}
if _, err = io.Copy(hasher, exeFile); err != nil {
panic(err)
}
md5sum := hex.EncodeToString(hasher.Sum(nil))
fileInfo, err := exeFile.Stat()
if err != nil {
panic(err)
}
mtime := fileInfo.ModTime().Format(time.RFC3339)
version := mtime + " " + md5sum
expvar.NewString("binary-version").Set(version)
// rexport this value for varz scraper
expvar.NewString("Version").Set(version)
}