Пример #1
0
func unmarshalFile(name string, val interface{}) {
	if name != "" {
		data, err := ioutil.ReadFile(name)
		if err != nil {
			relog.Fatal("could not read %v: %v", val, err)
		}
		if err = json.Unmarshal(data, val); err != nil {
			relog.Fatal("could not read %s: %v", val, err)
		}
	}
	data, _ := json.MarshalIndent(val, "", "  ")
	relog.Info("config: %s\n", data)
}
Пример #2
0
func Init(logPrefix string) {
	if logPrefix != "" {
		logPrefix += " "
	}

	logPrefix += fmt.Sprintf("[%v]", os.Getpid())
	f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles)
	if err != nil {
		panic(fmt.Sprintf("unable to open logfile %s: %v", *logfileName, err))
	}
	logger := relog.New(f, logPrefix+" ",
		log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.LogNameToLogLevel(*logLevel))
	relog.SetLogger(logger)

	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)
	}
}
Пример #3
0
func main() {
	flag.Usage = Usage
	flag.Parse()

	if *showVersion {
		fmt.Printf("agent_leveldb version is %.2f\n", agent_server.Version)
		return
	}

	agent_server.Init("agent_leveldb")
	if *configFile != "" {
		agent_server.Parse_config(*configFile)
	}

	if *laddr == "" {
		fmt.Fprintln(os.Stderr, "require a listen address")
		flag.Usage()
		os.Exit(1)
	}

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			relog.Fatal("%s", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	signal_init()

	agent_server.Run_server(*laddr)
}
Пример #4
0
func serve() {
	AddShutdownCallback(ShutdownCallback(func() error { relog.Error("testserver GracefulShutdown callback"); return nil }))
	err := ListenAndServe("/tmp/test-sock")
	if err != nil {
		relog.Fatal("listen err:%v", err)
	}
	relog.Info("test server finished")
}
Пример #5
0
// this is a callback to bind and startup an http server.
// usually it is called like:
//   umgmt.AddStartupCallback(func () { umgmt.StartHttpServer(addr) })
func StartHttpServer(addr string) {
	httpListener, httpErr := net.Listen("tcp", addr)

	go func() {
		if httpErr == nil {
			httpListener = newHttpListener(httpListener)
			AddListener(httpListener)
			httpErr = http.Serve(httpListener, nil)
			httpListener.Close()
		}
		if httpErr != nil {
			switch e := httpErr.(type) {
			case *net.OpError:
				switch e.Err {
				case syscall.EADDRINUSE:
					relog.Fatal("StartHttpServer failed: %v", e)
				}
			case error:
				// NOTE(msolomon) even though these are Errno objects, the constants
				// are typed as os.Error.
				switch e {
				// FIXME(msolomon) this needs to be migrated into the system library
				// because this needs to be properly handled in the accept loop.
				case syscall.EMFILE, syscall.ENFILE:
					relog.Error("non-fatal error serving HTTP: %s", e.Error())
				case syscall.EINVAL:
					// nothing - listener was probably closed
				default:
					relog.Error("http.ListenAndServe: " + httpErr.Error())
				}
			default:
				relog.Error("http.ListenAndServe: " + httpErr.Error())
			}
		}
	}()
}
Пример #6
0
func main() {
	memProfileRate := flag.Int("mem-profile-rate", 512*1024, "profile every n bytes allocated")
	maxOpenFds := flag.Uint64("max-open-fds", 32768, "max open file descriptors")
	configFile := flag.String("config", "", "config file name")
	dbConfigFile := flag.String("dbconfig", "", "db config file name")
	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")
	queryLog := flag.String("querylog", "", "for testing: log all queries to this file")
	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)
	if *queryLog != "" {
		if f, err = os.OpenFile(*queryLog, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644); err == nil {
			ts.QueryLogger = relog.New(f, "", log.Ldate|log.Lmicroseconds, relog.DEBUG)
		}
	}
	unmarshalFile(*configFile, &config)
	unmarshalFile(*dbConfigFile, &dbconfig)
	// work-around for jsonism
	if v, ok := dbconfig["port"].(float64); ok {
		dbconfig["port"] = int(v)
	}

	fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds}
	if err = syscall.Setrlimit(RLIMIT_NOFILE, fdLimit); err != nil {
		relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err)
	} else {
		relog.Info("set max-open-fds = %v", *maxOpenFds)
	}

	snitch.RegisterCommand("reload_schema", "Rescan the schema for new tables", ReloadHandler)
	snitch.Register()

	qm := &OccManager{config, dbconfig}
	rpc.Register(qm)

	ts.StartQueryService(
		config.PoolSize,
		config.TransactionCap,
		config.TransactionTimeout,
		config.MaxResultSize,
		config.QueryCacheSize,
		config.SchemaReloadTime,
		config.QueryTimeout,
		config.IdleTimeout,
	)
	ts.AllowQueries(ts.GenericConnectionCreator(dbconfig), nil)

	rpc.HandleHTTP()
	jsonrpc.ServeHTTP()
	jsonrpc.ServeRPC()
	bsonrpc.ServeHTTP()
	bsonrpc.ServeRPC()

	relog.Info("started vtocc %v", config.Port)

	// 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
		relog.Info("readjusted -lame-duck-period to %f", *lameDuckPeriod)
	}
	umgmt.SetLameDuckPeriod(float32(*lameDuckPeriod))
	umgmt.SetRebindDelay(float32(*rebindDelay))
	umgmt.AddStartupCallback(func() {
		umgmt.StartHttpServer(fmt.Sprintf(":%v", config.Port))
	})
	umgmt.AddStartupCallback(func() {
		sighandler.SetSignalHandler(syscall.SIGTERM,
			umgmt.SigTermHandler)
	})
	umgmt.AddCloseCallback(func() {
		ts.DisallowQueries()
	})
	umgmt.AddShutdownCallback(func() error {
		HandleGracefulShutdown()
		return nil
	})

	umgmtSocket := fmt.Sprintf(config.UmgmtSocket, config.Port)
	if umgmtErr := umgmt.ListenAndServe(umgmtSocket); umgmtErr != nil {
		relog.Error("umgmt.ListenAndServe err: %v", umgmtErr)
	}
	relog.Info("done")
}