From 645acecdb7811436797ede13ceb4448e02e3e880 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Fri, 27 Mar 2020 13:30:53 +0800 Subject: [PATCH 1/3] default tidb file log config --- docs/api-references/docs.html | 5 + .../v1alpha1/defaulting/tidbcluster.go | 31 ++++++ .../v1alpha1/defaulting/tidbcluster_test.go | 100 ++++++++++++++++++ pkg/apis/pingcap/v1alpha1/pd_config.go | 15 ++- .../pingcap/v1alpha1/zz_generated.deepcopy.go | 29 ++++- 5 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go diff --git a/docs/api-references/docs.html b/docs/api-references/docs.html index 44560452e7a..c04d4e0be73 100644 --- a/docs/api-references/docs.html +++ b/docs/api-references/docs.html @@ -2920,6 +2920,7 @@

FileLogConfig +(Optional)

Log filename, leave empty to disable file log.

@@ -2931,6 +2932,7 @@

FileLogConfig +(Optional)

Is log rotate enabled.

@@ -2942,6 +2944,7 @@

FileLogConfig +(Optional)

Max size for a single file, in MB.

@@ -2953,6 +2956,7 @@

FileLogConfig +(Optional)

Max log keep days, default is never deleting.

@@ -2964,6 +2968,7 @@

FileLogConfig +(Optional)

Maximum number of old log files to retain.

diff --git a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go index eac2b434ee0..35719f03701 100644 --- a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go +++ b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go @@ -26,6 +26,11 @@ const ( defaultBinlogImage = "pingcap/tidb-binlog" ) +var ( + tidbLogMaxSize = 300 + tidbLogMaxBackups = 3 +) + func SetTidbClusterDefault(tc *v1alpha1.TidbCluster) { setTidbClusterSpecDefault(tc) setPdSpecDefault(tc) @@ -59,6 +64,32 @@ func setTidbSpecDefault(tc *v1alpha1.TidbCluster) { if tc.Spec.TiDB.MaxFailoverCount == nil { tc.Spec.TiDB.MaxFailoverCount = pointer.Int32Ptr(3) } + + // we only set default log + if tc.Spec.TiDB.Config != nil { + if tc.Spec.TiDB.Config.Log == nil { + tc.Spec.TiDB.Config.Log = &v1alpha1.Log{ + File: &v1alpha1.FileLogConfig{ + MaxSize: &tidbLogMaxSize, + MaxBackups: &tidbLogMaxBackups, + }, + } + } else { + if tc.Spec.TiDB.Config.Log.File == nil { + tc.Spec.TiDB.Config.Log.File = &v1alpha1.FileLogConfig{ + MaxSize: &tidbLogMaxSize, + MaxBackups: &tidbLogMaxBackups, + } + } else { + if tc.Spec.TiDB.Config.Log.File.MaxSize == nil { + tc.Spec.TiDB.Config.Log.File.MaxSize = &tidbLogMaxSize + } + if tc.Spec.TiDB.Config.Log.File.MaxBackups == nil { + tc.Spec.TiDB.Config.Log.File.MaxBackups = &tidbLogMaxBackups + } + } + } + } } func setTikvSpecDefault(tc *v1alpha1.TidbCluster) { diff --git a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go new file mode 100644 index 00000000000..a49287dae28 --- /dev/null +++ b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go @@ -0,0 +1,100 @@ +// Copyright 2020 PingCAP, Inc. +// +// 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package defaulting + +import ( + . "github.com/onsi/gomega" + "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" + "testing" +) + +func TestSetTidbSpecDefault(t *testing.T) { + g := NewGomegaWithT(t) + + tc := newTidbCluster() + setTidbSpecDefault(tc) + g.Expect(tc.Spec.TiDB.Config).Should(BeNil()) + + tc = newTidbCluster() + tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{} + setTidbSpecDefault(tc) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) + + tc = newTidbCluster() + oomAction := "cancel" + tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{ + OOMAction: &oomAction, + } + setTidbSpecDefault(tc) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) + g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) + + tc = newTidbCluster() + infoLevel := "info" + tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{ + OOMAction: &oomAction, + Log: &v1alpha1.Log{ + Level: &infoLevel, + }, + } + setTidbSpecDefault(tc) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) + g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) + g.Expect(*tc.Spec.TiDB.Config.Log.Level).Should(Equal(infoLevel)) + + tc = newTidbCluster() + fileName := "slowlog.log" + tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{ + OOMAction: &oomAction, + Log: &v1alpha1.Log{ + Level: &infoLevel, + File: &v1alpha1.FileLogConfig{ + Filename: &fileName, + }, + }, + } + setTidbSpecDefault(tc) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) + g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) + g.Expect(*tc.Spec.TiDB.Config.Log.Level).Should(Equal(infoLevel)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.Filename).Should(Equal(fileName)) + + tc = newTidbCluster() + maxSize := 600 + tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{ + OOMAction: &oomAction, + Log: &v1alpha1.Log{ + Level: &infoLevel, + File: &v1alpha1.FileLogConfig{ + Filename: &fileName, + MaxSize: &maxSize, + }, + }, + } + setTidbSpecDefault(tc) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(maxSize)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) + g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) + g.Expect(*tc.Spec.TiDB.Config.Log.Level).Should(Equal(infoLevel)) + g.Expect(*tc.Spec.TiDB.Config.Log.File.Filename).Should(Equal(fileName)) + +} + +func newTidbCluster() *v1alpha1.TidbCluster { + return &v1alpha1.TidbCluster{} +} diff --git a/pkg/apis/pingcap/v1alpha1/pd_config.go b/pkg/apis/pingcap/v1alpha1/pd_config.go index e7ed6429c3e..49490476b46 100644 --- a/pkg/apis/pingcap/v1alpha1/pd_config.go +++ b/pkg/apis/pingcap/v1alpha1/pd_config.go @@ -428,15 +428,20 @@ type PDMetricConfig struct { // +k8s:openapi-gen=true type FileLogConfig struct { // Log filename, leave empty to disable file log. - Filename string `toml:"filename,omitempty" json:"filename,omitempty"` + // +optional + Filename *string `toml:"filename,omitempty" json:"filename,omitempty"` // Is log rotate enabled. - LogRotate bool `toml:"log-rotate,omitempty" json:"log-rotate,omitempty"` + // +optional + LogRotate *bool `toml:"log-rotate,omitempty" json:"log-rotate,omitempty"` // Max size for a single file, in MB. - MaxSize int `toml:"max-size,omitempty" json:"max-size,omitempty"` + // +optional + MaxSize *int `toml:"max-size,omitempty" json:"max-size,omitempty"` // Max log keep days, default is never deleting. - MaxDays int `toml:"max-days,omitempty" json:"max-days,omitempty"` + // +optional + MaxDays *int `toml:"max-days,omitempty" json:"max-days,omitempty"` // Maximum number of old log files to retain. - MaxBackups int `toml:"max-backups,omitempty" json:"max-backups,omitempty"` + // +optional + MaxBackups *int `toml:"max-backups,omitempty" json:"max-backups,omitempty"` } //StringSlice is more friendly to json encode/decode diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 72756adb2dd..72948c7022b 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -680,6 +680,31 @@ func (in *Experimental) DeepCopy() *Experimental { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *FileLogConfig) DeepCopyInto(out *FileLogConfig) { *out = *in + if in.Filename != nil { + in, out := &in.Filename, &out.Filename + *out = new(string) + **out = **in + } + if in.LogRotate != nil { + in, out := &in.LogRotate, &out.LogRotate + *out = new(bool) + **out = **in + } + if in.MaxSize != nil { + in, out := &in.MaxSize, &out.MaxSize + *out = new(int) + **out = **in + } + if in.MaxDays != nil { + in, out := &in.MaxDays, &out.MaxDays + *out = new(int) + **out = **in + } + if in.MaxBackups != nil { + in, out := &in.MaxBackups, &out.MaxBackups + *out = new(int) + **out = **in + } return } @@ -836,7 +861,7 @@ func (in *Log) DeepCopyInto(out *Log) { if in.File != nil { in, out := &in.File, &out.File *out = new(FileLogConfig) - **out = **in + (*in).DeepCopyInto(*out) } if in.EnableSlowLog != nil { in, out := &in.EnableSlowLog, &out.EnableSlowLog @@ -1143,7 +1168,7 @@ func (in *PDLogConfig) DeepCopyInto(out *PDLogConfig) { if in.File != nil { in, out := &in.File, &out.File *out = new(FileLogConfig) - **out = **in + (*in).DeepCopyInto(*out) } if in.Development != nil { in, out := &in.Development, &out.Development From 71b37999dee09fec183d72a46bf45b97db7fcff2 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Fri, 27 Mar 2020 16:07:32 +0800 Subject: [PATCH 2/3] fix log error --- pkg/apis/pingcap/v1alpha1/tidb_config.go | 2 +- pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/apis/pingcap/v1alpha1/tidb_config.go b/pkg/apis/pingcap/v1alpha1/tidb_config.go index 771ee2ddf8f..2abab5f7848 100644 --- a/pkg/apis/pingcap/v1alpha1/tidb_config.go +++ b/pkg/apis/pingcap/v1alpha1/tidb_config.go @@ -172,7 +172,7 @@ type Log struct { QueryLogMaxLen *uint64 `toml:"query-log-max-len,omitempty" json:"query-log-max-len,omitempty"` // Optional: Defaults to 1 // +optional - RecordPlanInSlowLog uint32 `toml:"record-plan-in-slow-log,omitempty" json:"record-plan-in-slow-log,omitempty"` + RecordPlanInSlowLog *uint32 `toml:"record-plan-in-slow-log,omitempty" json:"record-plan-in-slow-log,omitempty"` } // Security is the security section of the config. diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 72948c7022b..27076dd3a7e 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -888,6 +888,11 @@ func (in *Log) DeepCopyInto(out *Log) { *out = new(uint64) **out = **in } + if in.RecordPlanInSlowLog != nil { + in, out := &in.RecordPlanInSlowLog, &out.RecordPlanInSlowLog + *out = new(uint32) + **out = **in + } return } From 3ba82834dbb7382bb856c9fbb091f04a934849e3 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Fri, 27 Mar 2020 20:39:46 +0800 Subject: [PATCH 3/3] remove max-size --- pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go | 6 ------ pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go | 4 ---- 2 files changed, 10 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go index 35719f03701..b7e6ace3414 100644 --- a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go +++ b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go @@ -27,7 +27,6 @@ const ( ) var ( - tidbLogMaxSize = 300 tidbLogMaxBackups = 3 ) @@ -70,20 +69,15 @@ func setTidbSpecDefault(tc *v1alpha1.TidbCluster) { if tc.Spec.TiDB.Config.Log == nil { tc.Spec.TiDB.Config.Log = &v1alpha1.Log{ File: &v1alpha1.FileLogConfig{ - MaxSize: &tidbLogMaxSize, MaxBackups: &tidbLogMaxBackups, }, } } else { if tc.Spec.TiDB.Config.Log.File == nil { tc.Spec.TiDB.Config.Log.File = &v1alpha1.FileLogConfig{ - MaxSize: &tidbLogMaxSize, MaxBackups: &tidbLogMaxBackups, } } else { - if tc.Spec.TiDB.Config.Log.File.MaxSize == nil { - tc.Spec.TiDB.Config.Log.File.MaxSize = &tidbLogMaxSize - } if tc.Spec.TiDB.Config.Log.File.MaxBackups == nil { tc.Spec.TiDB.Config.Log.File.MaxBackups = &tidbLogMaxBackups } diff --git a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go index a49287dae28..fa57445328a 100644 --- a/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go +++ b/pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go @@ -29,7 +29,6 @@ func TestSetTidbSpecDefault(t *testing.T) { tc = newTidbCluster() tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{} setTidbSpecDefault(tc) - g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) tc = newTidbCluster() @@ -38,7 +37,6 @@ func TestSetTidbSpecDefault(t *testing.T) { OOMAction: &oomAction, } setTidbSpecDefault(tc) - g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) @@ -51,7 +49,6 @@ func TestSetTidbSpecDefault(t *testing.T) { }, } setTidbSpecDefault(tc) - g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) g.Expect(*tc.Spec.TiDB.Config.Log.Level).Should(Equal(infoLevel)) @@ -68,7 +65,6 @@ func TestSetTidbSpecDefault(t *testing.T) { }, } setTidbSpecDefault(tc) - g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxSize).Should(Equal(tidbLogMaxSize)) g.Expect(*tc.Spec.TiDB.Config.Log.File.MaxBackups).Should(Equal(tidbLogMaxBackups)) g.Expect(*tc.Spec.TiDB.Config.OOMAction).Should(Equal(oomAction)) g.Expect(*tc.Spec.TiDB.Config.Log.Level).Should(Equal(infoLevel))