Skip to content

Commit

Permalink
Default Tidb Log File Configuration (#2045)
Browse files Browse the repository at this point in the history
* default tidb file log config
  • Loading branch information
Yisaer authored Mar 30, 2020
1 parent 23977bd commit c66a92b
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/api-references/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,7 @@ <h3 id="pingcap.com/v1alpha1.FileLogConfig">FileLogConfig
</em>
</td>
<td>
<em>(Optional)</em>
<p>Log filename, leave empty to disable file log.</p>
</td>
</tr>
Expand All @@ -2972,6 +2973,7 @@ <h3 id="pingcap.com/v1alpha1.FileLogConfig">FileLogConfig
</em>
</td>
<td>
<em>(Optional)</em>
<p>Is log rotate enabled.</p>
</td>
</tr>
Expand All @@ -2983,6 +2985,7 @@ <h3 id="pingcap.com/v1alpha1.FileLogConfig">FileLogConfig
</em>
</td>
<td>
<em>(Optional)</em>
<p>Max size for a single file, in MB.</p>
</td>
</tr>
Expand All @@ -2994,6 +2997,7 @@ <h3 id="pingcap.com/v1alpha1.FileLogConfig">FileLogConfig
</em>
</td>
<td>
<em>(Optional)</em>
<p>Max log keep days, default is never deleting.</p>
</td>
</tr>
Expand All @@ -3005,6 +3009,7 @@ <h3 id="pingcap.com/v1alpha1.FileLogConfig">FileLogConfig
</em>
</td>
<td>
<em>(Optional)</em>
<p>Maximum number of old log files to retain.</p>
</td>
</tr>
Expand Down
25 changes: 25 additions & 0 deletions pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const (
defaultBinlogImage = "pingcap/tidb-binlog"
)

var (
tidbLogMaxBackups = 3
)

func SetTidbClusterDefault(tc *v1alpha1.TidbCluster) {
setTidbClusterSpecDefault(tc)
setPdSpecDefault(tc)
Expand Down Expand Up @@ -59,6 +63,27 @@ 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{
MaxBackups: &tidbLogMaxBackups,
},
}
} else {
if tc.Spec.TiDB.Config.Log.File == nil {
tc.Spec.TiDB.Config.Log.File = &v1alpha1.FileLogConfig{
MaxBackups: &tidbLogMaxBackups,
}
} else {
if tc.Spec.TiDB.Config.Log.File.MaxBackups == nil {
tc.Spec.TiDB.Config.Log.File.MaxBackups = &tidbLogMaxBackups
}
}
}
}
}

func setTikvSpecDefault(tc *v1alpha1.TidbCluster) {
Expand Down
96 changes: 96 additions & 0 deletions pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.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.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.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.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{}
}
15 changes: 10 additions & 5 deletions pkg/apis/pingcap/v1alpha1/pd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pingcap/v1alpha1/tidb_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 32 additions & 2 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c66a92b

Please sign in to comment.