Skip to content

Commit

Permalink
Fix TiKV Config Incompatibility error (pingcap#2593) (pingcap#2614)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Jun 3, 2020
1 parent c49e412 commit c296b57
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"path"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -511,6 +512,38 @@ func volumeClaimTemplate(r corev1.ResourceRequirements, metaName string, storage
}
}

// transformTiKVConfigMap change the `wait-for-lock-timeout` and `wake-up-delay-duration` due to their content type.
// If either of their content is numeric, it would be rendered as numeric in toml in the tikv configmap.
// In https://github.com/tikv/tikv/pull/7197 , these 2 configurations become string type from int32 type, so we add
// this transforming steps to make tikv config compatible with both 4.0.0 version or under 4.0.0 version
func transformTiKVConfigMap(srcStr string, tc *v1alpha1.TidbCluster) string {
config := tc.Spec.TiKV.Config
if config == nil {
return srcStr
}
if config.TiKVPessimisticTxn != nil {
if config.TiKVPessimisticTxn.WaitForLockTimeout != nil {
_, err := strconv.ParseInt(*config.TiKVPessimisticTxn.WaitForLockTimeout, 10, 64)
if err == nil {
waitForLockTimeOutKey := "wait-for-lock-timeout"
old := fmt.Sprintf(`%s = "%s"`, waitForLockTimeOutKey, *config.TiKVPessimisticTxn.WaitForLockTimeout)
newString := fmt.Sprintf(`%s = %s`, waitForLockTimeOutKey, *config.TiKVPessimisticTxn.WaitForLockTimeout)
srcStr = strings.ReplaceAll(srcStr, old, newString)
}
}
if config.TiKVPessimisticTxn.WakeUpDelayDuration != nil {
_, err := strconv.ParseInt(*config.TiKVPessimisticTxn.WakeUpDelayDuration, 10, 64)
if err == nil {
wakeUpDelayDuration := "wake-up-delay-duration"
old := fmt.Sprintf(`%s = "%s"`, wakeUpDelayDuration, *config.TiKVPessimisticTxn.WakeUpDelayDuration)
newString := fmt.Sprintf(`%s = %s`, wakeUpDelayDuration, *config.TiKVPessimisticTxn.WakeUpDelayDuration)
srcStr = strings.ReplaceAll(srcStr, old, newString)
}
}
}
return srcStr
}

func getTikVConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {

config := tc.Spec.TiKV.Config
Expand Down Expand Up @@ -548,7 +581,7 @@ func getTikVConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
OwnerReferences: []metav1.OwnerReference{controller.GetOwnerRef(tc)},
},
Data: map[string]string{
"config-file": string(confText),
"config-file": transformTiKVConfigMap(string(confText), tc),
"startup-script": startScript,
},
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/manager/member/tikv_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2074,3 +2074,72 @@ func TestGetTiKVConfigMap(t *testing.T) {
})
}
}

func TestTransformTiKVConfigMap(t *testing.T) {
g := NewGomegaWithT(t)
type testcase struct {
name string
waitForLockTimeout string
wakeUpDelayDuration string
result string
}
tests := []testcase{
{
name: "under 4.0",
waitForLockTimeout: "1000",
wakeUpDelayDuration: "20",
result: `[pessimistic-txn]
wait-for-lock-timeout = 1000
wake-up-delay-duration = 20
`,
},
{
name: "4.0.0",
waitForLockTimeout: "1s",
wakeUpDelayDuration: "20ms",
result: `[pessimistic-txn]
wait-for-lock-timeout = "1s"
wake-up-delay-duration = "20ms"
`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tc := newTidbClusterForTiKV()
tc.Spec.TiKV.Config.TiKVPessimisticTxn = &v1alpha1.TiKVPessimisticTxn{
WaitForLockTimeout: pointer.StringPtr(test.waitForLockTimeout),
WakeUpDelayDuration: pointer.StringPtr(test.wakeUpDelayDuration),
}
confText, err := MarshalTOML(tc.Spec.TiKV.Config)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(test.result).Should(Equal(transformTiKVConfigMap(string(confText), tc)))
})
}
}

func newTidbClusterForTiKV() *v1alpha1.TidbCluster {
return &v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: corev1.NamespaceDefault,
},
Spec: v1alpha1.TidbClusterSpec{
TiKV: v1alpha1.TiKVSpec{
ComponentSpec: v1alpha1.ComponentSpec{
Image: "tikv-test-image",
},
ResourceRequirements: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("2Gi"),
corev1.ResourceStorage: resource.MustParse("100Gi"),
},
},
Replicas: 3,
StorageClassName: pointer.StringPtr("my-storage-class"),
Config: &v1alpha1.TiKVConfig{},
},
},
}
}

0 comments on commit c296b57

Please sign in to comment.