Skip to content

Commit

Permalink
Clean config, log initialization and refactor version info (#6007)
Browse files Browse the repository at this point in the history
ref #5836

Clean config, log initialization and refactor version info.
1. Refactor version info print and log applied to all service modes.
2. Clean config/log initialization in the CreateServerWrapper for PD and TSO

Signed-off-by: Bin Shi <binshi.bing@gmail.com>
  • Loading branch information
binshi-bing authored Feb 17, 2023
1 parent f03c8f3 commit d203d79
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 81 deletions.
35 changes: 11 additions & 24 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ package main

import (
"context"
"flag"
"os"
"os/signal"
"syscall"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/spf13/cobra"
"github.com/tikv/pd/pkg/autoscaling"
"github.com/tikv/pd/pkg/dashboard"
"github.com/tikv/pd/pkg/errs"
tso "github.com/tikv/pd/pkg/mcs/tso/server"
"github.com/tikv/pd/pkg/swaggerserver"
"github.com/tikv/pd/pkg/utils/configutil"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/metricutil"
"github.com/tikv/pd/pkg/versioninfo"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/api"
"github.com/tikv/pd/server/apiv2"
Expand Down Expand Up @@ -108,39 +108,26 @@ func createServerWrapper(cmd *cobra.Command, args []string) {
flagSet := cmd.Flags()
flagSet.Parse(args)
err := cfg.Parse(flagSet)
defer logutil.LogPanic()

if err != nil {
cmd.Println(err)
return
}

printVersion, err := flagSet.GetBool("version")
if err != nil {
if printVersion, err := flagSet.GetBool("version"); err != nil {
cmd.Println(err)
return
}
if printVersion {
server.PrintPDInfo()
} else if printVersion {
versioninfo.Print()
exit(0)
}

defer logutil.LogPanic()

switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
exit(0)
default:
log.Fatal("parse cmd flags error", errs.ZapError(err))
}

configCheck, err := flagSet.GetBool("config-check")
if err != nil {
if configCheck, err := flagSet.GetBool("config-check"); err != nil {
cmd.Println(err)
return
}

if configCheck {
server.PrintConfigCheckMsg(cfg)
} else if configCheck {
configutil.PrintConfigCheckMsg(os.Stdout, cfg.WarningMsgs)
exit(0)
}

Expand All @@ -154,7 +141,7 @@ func createServerWrapper(cmd *cobra.Command, args []string) {
// Flushing any buffered log entries
defer log.Sync()

server.LogPDInfo()
versioninfo.Log("PD")

for _, msg := range cfg.WarningMsgs {
log.Warn(msg)
Expand Down
29 changes: 8 additions & 21 deletions pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package server

import (
"context"
"flag"
"net/http"
"os"
"os/signal"
Expand All @@ -25,7 +24,6 @@ import (
"time"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
Expand All @@ -38,6 +36,7 @@ import (
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/metricutil"
"github.com/tikv/pd/pkg/utils/tsoutil"
"github.com/tikv/pd/pkg/versioninfo"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand Down Expand Up @@ -262,30 +261,19 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
cfg := tso.NewConfig()
flagSet := cmd.Flags()
err := cfg.Parse(flagSet)
defer logutil.LogPanic()

if err != nil {
cmd.Println(err)
return
}

printVersion, err := flagSet.GetBool("version")
if err != nil {
if printVersion, err := flagSet.GetBool("version"); err != nil {
cmd.Println(err)
return
}
if printVersion {
// TODO: support printing TSO server info
// server.PrintTSOInfo()
exit(0)
}

defer logutil.LogPanic()

switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
} else if printVersion {
versioninfo.Print()
exit(0)
default:
log.Fatal("parse cmd flags error", errs.ZapError(err))
}

// New zap logger
Expand All @@ -298,10 +286,9 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
// Flushing any buffered log entries
defer log.Sync()

// TODO: support printing TSO server info
// LogTSOInfo()
versioninfo.Log("TSO")
log.Info("TSO Config", zap.Reflect("config", cfg))

// TODO: Make it configurable if it has big impact on performance.
grpcprometheus.EnableHandlingTimeHistogram()

metricutil.Push(&cfg.Metric)
Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/configutil/configutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package configutil

import (
"errors"
"fmt"
"io"

"github.com/BurntSushi/toml"
)
Expand Down Expand Up @@ -66,3 +68,15 @@ func (m *ConfigMetaData) CheckUndecoded() error {
}
return errors.New(errInfo[:len(errInfo)-2])
}

// PrintConfigCheckMsg prints the message about configuration checks.
func PrintConfigCheckMsg(w io.Writer, warningMsgs []string) {
if len(warningMsgs) == 0 {
fmt.Fprintln(w, "config check successful")
return
}

for _, msg := range warningMsgs {
fmt.Fprintln(w, msg)
}
}
54 changes: 54 additions & 0 deletions pkg/utils/configutil/configutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package configutil

import (
"bytes"
"testing"
)

func TestPrintConfigCheckMsg(t *testing.T) {
// Define test cases
tests := []struct {
name string
warningMsgs []string
want string
}{
{
name: "no warnings",
warningMsgs: []string{},
want: "config check successful\n",
},
{
name: "with warnings",
warningMsgs: []string{"warning message 1", "warning message 2"},
want: "warning message 1\nwarning message 2\n",
},
}

// Run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Redirect output to a buffer to capture the output
var buf bytes.Buffer
PrintConfigCheckMsg(&buf, tt.warningMsgs)

// Compare the output to the expected value
if got := buf.String(); got != tt.want {
t.Errorf("PrintConfigCheckMsg() = %q, want %q", got, tt.want)
}
})
}
}
24 changes: 24 additions & 0 deletions pkg/versioninfo/versioninfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package versioninfo

import (
"fmt"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -78,3 +82,23 @@ func IsFeatureSupported(clusterVersion *semver.Version, f Feature) bool {
}
return IsCompatible(minSupportVersion, *clusterVersion)
}

// Log prints the version information of the PD with the specific service mode.
func Log(serviceMode string) {
mode := strings.ToUpper(serviceMode)
log.Info(fmt.Sprintf("Welcome to Placement Driver (%s)", mode))
log.Info(mode, zap.String("release-version", PDReleaseVersion))
log.Info(mode, zap.String("edition", PDEdition))
log.Info(mode, zap.String("git-hash", PDGitHash))
log.Info(mode, zap.String("git-branch", PDGitBranch))
log.Info(mode, zap.String("utc-build-time", PDBuildTS))
}

// Print prints the version information, without log info, of the PD with the specific service mode.
func Print() {
fmt.Println("Release Version:", PDReleaseVersion)
fmt.Println("Edition:", PDEdition)
fmt.Println("Git Commit Hash:", PDGitHash)
fmt.Println("Git Branch:", PDGitBranch)
fmt.Println("UTC Build Time: ", PDBuildTS)
}
32 changes: 0 additions & 32 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package server

import (
"context"
"fmt"
"net/http"
"strings"

Expand All @@ -32,37 +31,6 @@ import (
"go.uber.org/zap"
)

// LogPDInfo prints the PD version information.
func LogPDInfo() {
log.Info("Welcome to Placement Driver (PD)")
log.Info("PD", zap.String("release-version", versioninfo.PDReleaseVersion))
log.Info("PD", zap.String("edition", versioninfo.PDEdition))
log.Info("PD", zap.String("git-hash", versioninfo.PDGitHash))
log.Info("PD", zap.String("git-branch", versioninfo.PDGitBranch))
log.Info("PD", zap.String("utc-build-time", versioninfo.PDBuildTS))
}

// PrintPDInfo prints the PD version information without log info.
func PrintPDInfo() {
fmt.Println("Release Version:", versioninfo.PDReleaseVersion)
fmt.Println("Edition:", versioninfo.PDEdition)
fmt.Println("Git Commit Hash:", versioninfo.PDGitHash)
fmt.Println("Git Branch:", versioninfo.PDGitBranch)
fmt.Println("UTC Build Time: ", versioninfo.PDBuildTS)
}

// PrintConfigCheckMsg prints the message about configuration checks.
func PrintConfigCheckMsg(cfg *config.Config) {
if len(cfg.WarningMsgs) == 0 {
fmt.Println("config check successful")
return
}

for _, msg := range cfg.WarningMsgs {
fmt.Println(msg)
}
}

// CheckPDVersion checks if PD needs to be upgraded.
func CheckPDVersion(opt *config.PersistOptions) {
pdVersion := versioninfo.MinSupportedVersion(versioninfo.Base)
Expand Down
4 changes: 2 additions & 2 deletions tools/pd-ctl/pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/mattn/go-shellwords"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tikv/pd/server"
"github.com/tikv/pd/pkg/versioninfo"
"github.com/tikv/pd/tools/pd-ctl/pdctl/command"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func MainStart(args []string) {

rootCmd.Run = func(cmd *cobra.Command, args []string) {
if v, err := cmd.Flags().GetBool("version"); err == nil && v {
server.PrintPDInfo()
versioninfo.Print()
return
}
if v, err := cmd.Flags().GetBool("interact"); err == nil && v {
Expand Down
4 changes: 2 additions & 2 deletions tools/pd-recover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/pkg/versioninfo"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/pkg/transport"
)
Expand Down Expand Up @@ -76,7 +76,7 @@ func main() {
exitErr(err)
}
if v {
server.PrintPDInfo()
versioninfo.Print()
return
}

Expand Down

0 comments on commit d203d79

Please sign in to comment.