// signalCatcher blocks until a signal is recieved and then takes appropriate action func signalCatcher(server *server.Server, endpoint *http.HttpEndpoint, writeCPUprof bool, f *os.File) { sig_chan := make(chan os.Signal, 4) signal.Notify(sig_chan, os.Interrupt, syscall.SIGTERM) var s os.Signal select { case s = <-sig_chan: } if writeCPUprof { logging.Infop("Stopping CPU profiling...") pprof.StopCPUProfile() } if f != nil { logging.Infop("Stopping Memory profiling...") pprof.WriteHeapProfile(f) f.Close() } if s == os.Interrupt { // Interrupt (ctrl-C) => Immediate (ungraceful) exit logging.Infop("cbq-engine shutting down immediately...") os.Exit(0) } logging.Infop("cbq-engine attempting graceful...") // Stop accepting new requests endpoint.Close() // TODO: wait until server requests have all completed }
func (this *HttpEndpoint) Close() { if this.listener != nil { this.listener.Close() logging.Infop("HttpEndpoint.Close()", logging.Pair{"Address", this.listener.Addr()}) } if this.listenerTLS != nil { this.listenerTLS.Close() logging.Infop("HttpEndpoint.Close()", logging.Pair{"Address", this.listener.Addr()}) } }
func TestStub(t *testing.T) { logger := NewLogger(os.Stdout, logging.Debug, false) logging.SetLogger(logger) logger.Infof("This is a message from %s", "test") logging.Infof("This is a message from %s", "test") logger.Infop("This is a message from ", logging.Pair{"name", "test"}, logging.Pair{"Queue Size", 10}, logging.Pair{"Debug Mode", false}) logging.Infop("This is a message from ", logging.Pair{"name", "test"}) logger.Infom("This is a message from ", logging.Map{"name": "test", "Queue Size": 10, "Debug Mode": false}) logging.Infom("This is a message from ", logging.Map{"name": "test"}) logger.Requestf(logging.Warn, "This is a Request from %s", "test") logging.Requestf(logging.Info, "This is a Request from %s", "test") logger.Requestp(logging.Debug, "This is a Request from ", logging.Pair{"name", "test"}) logging.Requestp(logging.Error, "This is a Request from ", logging.Pair{"name", "test"}) logger.SetLevel(logging.Warn) fmt.Printf("Log level is %s\n", logger.Level()) logger.Requestf(logging.Warn, "This is a Request from %s", "test") logging.Requestf(logging.Info, "This is a Request from %s", "test") logger.Requestp(logging.Debug, "This is a Request from ", logging.Pair{"name", "test"}) logging.Requestp(logging.Error, "This is a Request from ", logging.Pair{"name", "test"}) logger.Warnf("This is a message from %s", "test") logging.Infof("This is a message from %s", "test") logger.Debugp("This is a message from ", logging.Pair{"name", "test"}) logging.Errorp("This is a message from ", logging.Pair{"name", "test"}) fmt.Printf("Changing to json formatter\n") logger.entryFormatter = &jsonFormatter{} logger.SetLevel(logging.Debug) logger.Infof("This is a message from %s", "test") logging.Infof("This is a message from %s", "test") logger.Infop("This is a message from ", logging.Pair{"name", "test"}, logging.Pair{"Queue Size", 10}, logging.Pair{"Debug Mode", false}) logging.Infop("This is a message from ", logging.Pair{"name", "test"}) logger.Infom("This is a message from ", logging.Map{"name": "test", "Queue Size": 10, "Debug Mode": false}) logging.Infom("This is a message from ", logging.Map{"name": "test"}) logger.Requestf(logging.Warn, "This is a Request from %s", "test") logging.Requestf(logging.Info, "This is a Request from %s", "test") logger.Requestp(logging.Debug, "This is a Request from ", logging.Pair{"name", "test"}) logging.Requestp(logging.Error, "This is a Request from ", logging.Pair{"name", "test"}) }
func pollStdin() { reader := bufio.NewReader(os.Stdin) logging.Infop("pollEOF: About to start stdin polling") for { ch, err := reader.ReadByte() if err == io.EOF { logging.Infop("Received EOF; Exiting...") os.Exit(0) } if err != nil { logging.Errorp("Unexpected error polling stdin", logging.Pair{"error", err}) os.Exit(1) } if ch == '\n' || ch == '\r' { logging.Infop("Received EOL; Exiting...") // TODO: "graceful" shutdown should be placed here os.Exit(0) } } }
func (this *Fetch) UnmarshalJSON(body []byte) error { var _unmarshalled struct { _ string `json:"#operator"` Proj string `json:"projection"` Names string `json:"namespace"` Keys string `json:"keyspace"` As string `json:"as"` } var proj_expr expression.Path err := json.Unmarshal(body, &_unmarshalled) if err != nil { return err } if _unmarshalled.Proj != "" { expr, err := parser.Parse(_unmarshalled.Proj) logging.Infop("Fetch", logging.Pair{"_unmarshalled.Proj", _unmarshalled.Proj}, logging.Pair{"err", err}, logging.Pair{"expr", expr}, ) if err != nil { return err } _proj_expr, is_path := expr.(expression.Path) if !is_path { return fmt.Errorf("Fetch.UnmarshalJSON: cannot resolve path expression from %s", _unmarshalled.Proj) } proj_expr = _proj_expr } this.term = algebra.NewKeyspaceTerm(_unmarshalled.Names, _unmarshalled.Keys, proj_expr, _unmarshalled.As, nil) this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys) return err }
func main() { flag.Parse() var f *os.File if *MEM_PROFILE != "" { var err error f, err = os.Create(*MEM_PROFILE) if err != nil { fmt.Printf("Cannot start mem profiler %v\n", err) } } if *CPU_PROFILE != "" { f, err := os.Create(*CPU_PROFILE) if err != nil { fmt.Printf("Cannot start cpu profiler %v\n", err) } else { pprof.StartCPUProfile(f) } } if *LOGGER != "" { logger, _ := log_resolver.NewLogger(*LOGGER) if logger == nil { fmt.Printf("Invalid logger: %s\n", *LOGGER) os.Exit(1) } logging.SetLogger(logger) } if *DEBUG { logging.SetLevel(logging.Debug) logging.Debugp("Debug mode enabled") } else { logging.SetLevel(logging.Info) } datastore, err := resolver.NewDatastore(*DATASTORE) if err != nil { logging.Errorp(err.Error()) os.Exit(1) } datastore_package.SetDatastore(datastore) configstore, err := config_resolver.NewConfigstore(*CONFIGSTORE) if err != nil { logging.Errorp("Could not connect to configstore", logging.Pair{"error", err}, ) } acctstore, err := acct_resolver.NewAcctstore(*ACCTSTORE) if err != nil { logging.Errorp("Could not connect to acctstore", logging.Pair{"error", err}, ) } else { // Create the metrics we are interested in accounting.RegisterMetrics(acctstore) // Make metrics available acctstore.MetricReporter().Start(1, 1) } keep_alive_length, e := util.ParseQuantity(*KEEP_ALIVE_LENGTH) if e != nil { logging.Errorp("Error parsing keep alive length; reverting to default", logging.Pair{"keep alive length", *KEEP_ALIVE_LENGTH}, logging.Pair{"error", e}, logging.Pair{"default", server.KEEP_ALIVE_DEFAULT}, ) } if e == nil && keep_alive_length < 1 { logging.Infop("Negative or zero keep alive length; reverting to default", logging.Pair{"keep alive length", *KEEP_ALIVE_LENGTH}, logging.Pair{"default", server.KEEP_ALIVE_DEFAULT}, ) } channel := make(server.RequestChannel, *REQUEST_CAP) server, err := server.NewServer(datastore, configstore, acctstore, *NAMESPACE, *READONLY, channel, *THREAD_COUNT, *TIMEOUT, *SIGNATURE, *METRICS, keep_alive_length) if err != nil { logging.Errorp(err.Error()) os.Exit(1) } go server.Serve() logging.Infop("cbq-engine started", logging.Pair{"version", VERSION}, logging.Pair{"datastore", *DATASTORE}, ) // Create http endpoint endpoint := http.NewServiceEndpoint(server, *STATIC_PATH, *METRICS) er := endpoint.Listen(*HTTP_ADDR) if er != nil { logging.Errorp("cbq-engine exiting with error", logging.Pair{"error", er}, logging.Pair{"HTTP_ADDR", *HTTP_ADDR}, ) os.Exit(1) } if *CERT_FILE != "" && *KEY_FILE != "" { er := endpoint.ListenTLS(*HTTPS_ADDR, *CERT_FILE, *KEY_FILE) if er != nil { logging.Errorp("cbq-engine exiting with error", logging.Pair{"error", er}, logging.Pair{"HTTP_ADDR", *HTTP_ADDR}, ) os.Exit(1) } } signalCatcher(server, endpoint, *CPU_PROFILE != "", f) }