InfluxDB server startup process
Operating System: CentOS7.3.1611 _ x64
Go language version: 1.8.3 linux/amd64
InfluxDB version: 1.1.0
Source code path: github.com/influxdata/influxdb/#/influxd
Program entry (main. go ):
Func main (){
Rand. Seed (time. Now (). UnixNano ())
M: = NewMain ()
If err: = m. Run (OS. Args [1:]...); err! = Nil {
Fmt. Fprintln (OS. Stderr, err)
OS. Exit (1)
}
}
In the main function, call the Main. Run function.
The command line parameter is null or "run"
If the command line parameter is null or "run", execute the following process:
Case "", "run ":
Cmd: = run. NewCommand ()
// Tell the server the build details.
Cmd. Version = version
Cmd. Commit = commit
Cmd. Branch = branch
If err: = cmd. Run (args...); err! = Nil {
Return fmt. Errorf ("run: % s", err)
}
SignalCh: = make (chan OS. Signal, 1)
Signal. Y (signalCh, OS. Interrupt, syscall. SIGTERM)
M. Logger. Println ("Listening for signals ")
// Block until one of the signals above is already ed
Select {
Case <-signalCh:
M. Logger. Println ("Signal received, initializing clean shutdown ...")
Go func (){
Cmd. Close ()
}()
}
// Block again until another signal is wrongly ed, a shutdown timeout elapses,
// Or the Command is gracefully closed
M. Logger. Println ("Waiting for clean shutdown ...")
Select {
Case <-signalCh:
M. Logger. Println ("second signal received, initializing hard shutdown ")
Case <-time. After (time. Second * 30 ):
M. Logger. Println ("time limit reached, initializing hard shutdown ")
Case <-cmd. Closed:
M. Logger. Println ("server shutdown completed ")
}
// Goodbye.
Run the Command. Run function, followed by the code that processes System signals.
The Command. Run function contains the following content:
Func (cmd * Command) Run (args... string) error {
// Parse the command line flags.
Options, err: = cmd. ParseFlags (args ...)
If err! = Nil {
Return err
}
// Print sweet InfluxDB logo.
Fmt. Print (logo)
// Configure default logging.
Log. SetPrefix ("[run]")
Log. SetFlags (log. LstdFlags)
// Set parallelism.
Runtime. GOMAXPROCS (runtime. NumCPU ())
// Mark start-up in log.
Log. Printf ("InfluxDB starting, version % s, branch % s, commit % s ",
Cmd. Version, cmd. Branch, cmd. Commit)
Log. Printf ("Go version % s, GOMAXPROCS set to % d", runtime. Version (), runtime. GOMAXPROCS (0 ))
// Write the PID file.
If err: = cmd. writePIDFile (options. PIDFile); err! = Nil {
Return fmt. Errorf ("write pid file: % s", err)
}
// Parse config
Config, err: = cmd. ParseConfig (options. GetConfigPath ())
If err! = Nil {
Return fmt. Errorf ("parse config: % s", err)
}
// Apply any environment variables on top of the parsed config
If err: = config. ApplyEnvOverrides (); err! = Nil {
Return fmt. Errorf ("apply env config: % v", err)
}
// Validate the configuration.
If err: = config. Validate (); err! = Nil {
Return fmt. Errorf ("% s. To generate a valid configuration file run 'influxd config> influxdb. generated. conf'", err)
}
If config. HTTPD. PprofEnabled {
// Turn on block profiling to debug stuck databases
Runtime. SetBlockProfileRate (int (1 * time. Second ))
}
// Create server from config and start it.
BuildInfo: = & BuildInfo {
Version: cmd. Version,
Commit: cmd. Commit,
Branch: cmd. Branch,
Time: cmd. BuildTime,
}
S, err: = NewServer (config, buildInfo)
If err! = Nil {
Return fmt. Errorf ("create server: % s", err)
}
S. CPUProfile = options. CPUProfile
S. MemProfile = options. MemProfile
If err: = s. Open (); err! = Nil {
Return fmt. Errorf ("open server: % s", err)
}
Cmd. Server = s
// Begin monitoring the server's error channel.
Go cmd. monitorServerErrors ()
Return nil
}
Explanation:
1. parse the command line parameters and put them in the options variable.
2. Print the InfluxDB string logo
3. Set the log prefix
4. Set the maximum number of CPUs used by the program
By default, all the CPUs on the server are used. In the worst case, 100% of the cpu usage occurs.
5. Add startup logs
6. Record the pid File
This function requires you to specify the pid file path in the command line parameters.
7. Load the configuration file
Parse and verify the configuration file. If no problem exists, the configuration file takes effect.
8. Set profile information and start the server
9. Start various services
Run the Server. Open function (run/server. go) to start various services. You can view the specific content in the Server. Open function.
10. Run monitorServerErrors to monitor server errors
The command line parameter is "backup"
If the command line parameter is "backup", execute the following process:
1 case "backup":2 name := backup.NewCommand()3 if err := name.Run(args...); err != nil {4 return fmt.Errorf("backup: %s", err)5 }
Data backup process.
The command line parameter is "restore"
If the command line parameter is "restore", execute the following process:
case "restore": name := restore.NewCommand() if err := name.Run(args...); err != nil { return fmt.Errorf("restore: %s", err) }
Data recovery process.
The command line parameter is "config"
If the command line parameter is "config", execute the following process:
case "config": if err := run.NewPrintConfigCommand().Run(args...); err != nil { return fmt.Errorf("config: %s", err) }
Output the default configuration information.
The command line parameter is "help"
If the command line parameter is "help", execute the following process:
case "help": if err := help.NewCommand().Run(args...); err != nil { return fmt.Errorf("help: %s", err) }
Output help information.
Okay, that's all. I hope it will help you.