-
Notifications
You must be signed in to change notification settings - Fork 499
/
tikv_member_manager.go
1112 lines (999 loc) · 35.5 KB
/
tikv_member_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 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 member
import (
"context"
"fmt"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/pingcap/tidb-operator/pkg/apis/label"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/manager"
"github.com/pingcap/tidb-operator/pkg/manager/member/constants"
"github.com/pingcap/tidb-operator/pkg/manager/suspender"
mngerutils "github.com/pingcap/tidb-operator/pkg/manager/utils"
"github.com/pingcap/tidb-operator/pkg/manager/volumes"
"github.com/pingcap/tidb-operator/pkg/pdapi"
"github.com/pingcap/tidb-operator/pkg/util"
"github.com/pingcap/advanced-statefulset/client/apis/apps/v1/helper"
"github.com/pingcap/kvproto/pkg/metapb"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
)
const (
// tikvClusterCertPath is where the cert for inter-cluster communication stored (if any)
tikvClusterCertPath = "/var/lib/tikv-tls"
// find a better way to manage store only managed by tikv in Operator
tikvStoreLimitPattern = `%s-tikv-\d+\.%s-tikv-peer\.%s\.svc%s\:\d+`
)
// tikvMemberManager implements manager.Manager.
type tikvMemberManager struct {
deps *controller.Dependencies
failover Failover
scaler Scaler
upgrader TiKVUpgrader
suspender suspender.Suspender
podVolumeModifier volumes.PodVolumeModifier
statefulSetIsUpgradingFn func(corelisters.PodLister, pdapi.PDControlInterface, *apps.StatefulSet, *v1alpha1.TidbCluster) (bool, error)
}
// NewTiKVMemberManager returns a *tikvMemberManager
func NewTiKVMemberManager(deps *controller.Dependencies, failover Failover, scaler Scaler, upgrader TiKVUpgrader, spder suspender.Suspender, pvm volumes.PodVolumeModifier) manager.Manager {
m := &tikvMemberManager{
deps: deps,
failover: failover,
scaler: scaler,
upgrader: upgrader,
suspender: spder,
podVolumeModifier: pvm,
}
m.statefulSetIsUpgradingFn = tikvStatefulSetIsUpgrading
return m
}
// SvcConfig corresponds to a K8s service
type SvcConfig struct {
Name string
Port int32
SvcLabel func(label.Label) label.Label
MemberName func(clusterName string) string
Headless bool
}
// Sync fulfills the manager.Manager interface
func (m *tikvMemberManager) Sync(tc *v1alpha1.TidbCluster) error {
// If tikv is not specified return
if tc.Spec.TiKV == nil {
return nil
}
ns := tc.GetNamespace()
tcName := tc.GetName()
// skip sync if tikv is suspended
component := v1alpha1.TiKVMemberType
needSuspend, err := m.suspender.SuspendComponent(tc, component)
if err != nil {
return fmt.Errorf("suspend %s failed: %v", component, err)
}
if needSuspend {
klog.Infof("component %s for cluster %s/%s is suspended, skip syncing", component, ns, tcName)
return nil
}
if tc.Spec.PD != nil && !tc.PDIsAvailable() {
return controller.RequeueErrorf("TidbCluster: [%s/%s], waiting for PD cluster running", ns, tcName)
}
// Check if all PD Micro Services are available
if tc.Spec.PDMS != nil && (tc.Spec.PD != nil && tc.Spec.PD.Mode == "ms") {
for _, pdms := range tc.Spec.PDMS {
if cli := controller.GetPDMSClient(m.deps.PDControl, tc, pdms.Name); cli == nil {
return controller.RequeueErrorf("PDMS component %s for TidbCluster: [%s/%s], "+
"waiting for PD micro service cluster running", pdms.Name, ns, tcName)
}
}
}
// Check TidbCluster Recovery
if err := m.checkRecoveryForTidbCluster(tc); err != nil {
return err
}
svcList := []SvcConfig{
{
Name: "peer",
Port: v1alpha1.DefaultTiKVServerPort,
Headless: true,
SvcLabel: func(l label.Label) label.Label { return l.TiKV() },
MemberName: controller.TiKVPeerMemberName,
},
}
for _, svc := range svcList {
if err := m.syncServiceForTidbCluster(tc, svc); err != nil {
return err
}
}
return m.syncStatefulSetForTidbCluster(tc)
}
func (m *tikvMemberManager) checkRecoveryForTidbCluster(tc *v1alpha1.TidbCluster) error {
// Check whether the cluster is in recovery mode
// and whether the volumes have been restored for TiKV
if !tc.Spec.RecoveryMode {
return nil
}
ns := tc.GetNamespace()
tcName := tc.GetName()
anns := tc.GetAnnotations()
if _, ok := anns[label.AnnTiKVVolumesReadyKey]; !ok {
return controller.RequeueErrorf("TidbCluster: [%s/%s], waiting for TiKV volumes ready", ns, tcName)
}
return nil
}
func (m *tikvMemberManager) syncServiceForTidbCluster(tc *v1alpha1.TidbCluster, svcConfig SvcConfig) error {
if tc.Spec.Paused {
klog.V(4).Infof("tikv cluster %s/%s is paused, skip syncing for tikv service", tc.GetNamespace(), tc.GetName())
return nil
}
ns := tc.GetNamespace()
tcName := tc.GetName()
newSvc := getNewServiceForTidbCluster(tc, svcConfig)
oldSvcTmp, err := m.deps.ServiceLister.Services(ns).Get(svcConfig.MemberName(tcName))
if errors.IsNotFound(err) {
err = controller.SetServiceLastAppliedConfigAnnotation(newSvc)
if err != nil {
return err
}
return m.deps.ServiceControl.CreateService(tc, newSvc)
}
if err != nil {
return fmt.Errorf("syncServiceForTidbCluster: failed to get svc %s for cluster %s/%s, error: %s", svcConfig.MemberName(tcName), ns, tcName, err)
}
oldSvc := oldSvcTmp.DeepCopy()
_, err = m.deps.ServiceControl.SyncComponentService(
tc,
newSvc,
oldSvc,
true)
if err != nil {
return err
}
return nil
}
func (m *tikvMemberManager) syncStatefulSetForTidbCluster(tc *v1alpha1.TidbCluster) error {
ns := tc.GetNamespace()
tcName := tc.GetName()
oldSetTmp, err := m.deps.StatefulSetLister.StatefulSets(ns).Get(controller.TiKVMemberName(tcName))
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("syncStatefulSetForTidbCluster: failed to get sts %s for cluster %s/%s, error: %s", controller.TiKVMemberName(tcName), ns, tcName, err)
}
setNotExist := errors.IsNotFound(err)
oldSet := oldSetTmp.DeepCopy()
if err := m.syncTiKVClusterStatus(tc, oldSet); err != nil {
return err
}
if tc.Spec.Paused {
klog.V(4).Infof("tikv cluster %s/%s is paused, skip syncing for tikv statefulset", tc.GetNamespace(), tc.GetName())
return nil
}
cm, err := m.syncTiKVConfigMap(tc, oldSet)
if err != nil {
return err
}
// Recover failed stores if any before generating desired statefulset
if len(tc.Status.TiKV.FailureStores) > 0 {
m.failover.RemoveUndesiredFailures(tc)
}
if len(tc.Status.TiKV.FailureStores) > 0 &&
(tc.Spec.TiKV.RecoverFailover || tc.Status.TiKV.FailoverUID == tc.Spec.TiKV.GetRecoverByUID()) &&
shouldRecover(tc, label.TiKVLabelVal, m.deps.PodLister) {
m.failover.Recover(tc)
}
newSet, err := getNewTiKVSetForTidbCluster(tc, cm)
if err != nil {
return err
}
if setNotExist {
err = mngerutils.SetStatefulSetLastAppliedConfigAnnotation(newSet)
if err != nil {
return err
}
err = m.deps.StatefulSetControl.CreateStatefulSet(tc, newSet)
if err != nil {
return err
}
tc.Status.TiKV.StatefulSet = &apps.StatefulSetStatus{}
return nil
}
if _, err := m.setStoreLabelsForTiKV(tc); err != nil {
return err
}
// Scaling takes precedence over upgrading because:
// - if a store fails in the upgrading, users may want to delete it or add
// new replicas
// - it's ok to scale in the middle of upgrading (in statefulset controller
// scaling takes precedence over upgrading too)
if err := m.scaler.Scale(tc, oldSet, newSet); err != nil {
return err
}
// Perform failover logic if necessary. Note that this will only update
// TidbCluster status. The actual scaling performs in next sync loop (if a
// new replica needs to be added).
if m.deps.CLIConfig.AutoFailover && tc.Spec.TiKV.MaxFailoverCount != nil {
if tc.TiKVAllPodsStarted() && !tc.TiKVAllStoresReady() {
if err := m.failover.Failover(tc); err != nil {
return err
}
}
}
if tc.Status.TiKV.VolReplaceInProgress {
// Volume Replace in Progress, so do not make any changes to Sts spec, overwrite with old pod spec
// config as we are not ready to upgrade yet.
_, podSpec, err := GetLastAppliedConfig(oldSet)
if err != nil {
return err
}
newSet.Spec.Template.Spec = *podSpec
}
if !templateEqual(newSet, oldSet) || tc.Status.TiKV.Phase == v1alpha1.UpgradePhase {
if err := m.upgrader.Upgrade(tc, oldSet, newSet); err != nil {
return err
}
}
return mngerutils.UpdateStatefulSetWithPrecheck(m.deps, tc, "FailedUpdateTiKVSTS", newSet, oldSet)
}
func (m *tikvMemberManager) syncTiKVConfigMap(tc *v1alpha1.TidbCluster, set *apps.StatefulSet) (*corev1.ConfigMap, error) {
// For backward compatibility, only sync tidb configmap when .tikv.config is non-nil
if tc.Spec.TiKV.Config == nil {
return nil, nil
}
newCm, err := getTikVConfigMap(tc)
if err != nil {
return nil, err
}
var inUseName string
if set != nil {
inUseName = mngerutils.FindConfigMapVolume(&set.Spec.Template.Spec, func(name string) bool {
return strings.HasPrefix(name, controller.TiKVMemberName(tc.Name))
})
} else {
inUseName, err = mngerutils.FindConfigMapNameFromTCAnno(context.Background(), m.deps.ConfigMapLister, tc, v1alpha1.TiKVMemberType, newCm)
if err != nil {
return nil, err
}
}
err = mngerutils.UpdateConfigMapIfNeed(m.deps.ConfigMapLister, tc.BaseTiKVSpec().ConfigUpdateStrategy(), inUseName, newCm)
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
}
func getNewServiceForTidbCluster(tc *v1alpha1.TidbCluster, svcConfig SvcConfig) *corev1.Service {
ns := tc.Namespace
tcName := tc.Name
instanceName := tc.GetInstanceName()
svcName := svcConfig.MemberName(tcName)
svcSelector := svcConfig.SvcLabel(label.New().Instance(instanceName))
svcLabel := svcSelector.Copy()
if svcConfig.Headless {
svcLabel = svcLabel.UsedByPeer()
} else {
svcLabel = svcLabel.UsedByEndUser()
}
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Namespace: ns,
Labels: svcLabel.Labels(),
OwnerReferences: []metav1.OwnerReference{controller.GetOwnerRef(tc)},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: svcConfig.Name,
Port: svcConfig.Port,
TargetPort: intstr.FromInt(int(svcConfig.Port)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: svcSelector.Labels(),
PublishNotReadyAddresses: true,
},
}
if svcConfig.Headless {
svc.Spec.ClusterIP = "None"
} else {
svc.Spec.Type = controller.GetServiceType(tc.Spec.Services, v1alpha1.TiKVMemberType.String())
}
if tc.Spec.PreferIPv6 {
SetServiceWhenPreferIPv6(&svc)
}
return &svc
}
func getNewTiKVSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (*apps.StatefulSet, error) {
ns := tc.GetNamespace()
tcName := tc.GetName()
baseTiKVSpec := tc.BaseTiKVSpec()
tikvConfigMap := controller.MemberConfigMapName(tc, v1alpha1.TiKVMemberType)
if cm != nil {
tikvConfigMap = cm.Name
}
annoMount, annoVolume := annotationsMountVolume()
dataVolumeName := string(v1alpha1.GetStorageVolumeName("", v1alpha1.TiKVMemberType))
tikvDataVol := corev1.VolumeMount{
Name: dataVolumeName,
MountPath: constants.TiKVDataVolumeMountPath}
volMounts := []corev1.VolumeMount{
annoMount,
tikvDataVol,
{Name: "config", ReadOnly: true, MountPath: "/etc/tikv"},
{Name: "startup-script", ReadOnly: true, MountPath: "/usr/local/bin"},
}
volMounts = append(volMounts, tc.Spec.TiKV.AdditionalVolumeMounts...)
if tc.IsTLSClusterEnabled() {
volMounts = append(volMounts, corev1.VolumeMount{
Name: "tikv-tls", ReadOnly: true, MountPath: "/var/lib/tikv-tls",
})
if tc.Spec.TiKV.MountClusterClientSecret != nil && *tc.Spec.TiKV.MountClusterClientSecret {
volMounts = append(volMounts, corev1.VolumeMount{
Name: util.ClusterClientVolName, ReadOnly: true, MountPath: util.ClusterClientTLSPath,
})
}
}
vols := []corev1.Volume{
annoVolume,
{Name: "config", VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: tikvConfigMap,
},
Items: []corev1.KeyToPath{{Key: "config-file", Path: "tikv.toml"}},
}},
},
{Name: "startup-script", VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: tikvConfigMap,
},
Items: []corev1.KeyToPath{{Key: "startup-script", Path: "tikv_start_script.sh"}},
}},
},
}
if tc.IsTLSClusterEnabled() {
vols = append(vols, corev1.Volume{
Name: "tikv-tls", VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: util.ClusterTLSSecretName(tc.Name, label.TiKVLabelVal),
},
},
})
if tc.Spec.TiKV.MountClusterClientSecret != nil && *tc.Spec.TiKV.MountClusterClientSecret {
vols = append(vols, corev1.Volume{
Name: util.ClusterClientVolName, VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: util.ClusterClientTLSSecretName(tc.Name),
},
},
})
}
}
// handle StorageVolumes and AdditionalVolumeMounts in ComponentSpec
storageVolMounts, additionalPVCs := util.BuildStorageVolumeAndVolumeMount(tc.Spec.TiKV.StorageVolumes, tc.Spec.TiKV.StorageClassName, v1alpha1.TiKVMemberType)
volMounts = append(volMounts, storageVolMounts...)
sysctls := "sysctl -w"
var initContainers []corev1.Container
if baseTiKVSpec.Annotations() != nil {
init, ok := baseTiKVSpec.Annotations()[label.AnnSysctlInit]
if ok && (init == label.AnnSysctlInitVal) {
if baseTiKVSpec.PodSecurityContext() != nil && len(baseTiKVSpec.PodSecurityContext().Sysctls) > 0 {
for _, sysctl := range baseTiKVSpec.PodSecurityContext().Sysctls {
sysctls = sysctls + fmt.Sprintf(" %s=%s", sysctl.Name, sysctl.Value)
}
privileged := true
initContainers = append(initContainers, corev1.Container{
Name: "init",
Image: tc.HelperImage(),
Command: []string{
"sh",
"-c",
sysctls,
},
SecurityContext: &corev1.SecurityContext{
Privileged: &privileged,
},
// Init container resourceRequirements should be equal to app container.
// Scheduling is done based on effective requests/limits,
// which means init containers can reserve resources for
// initialization that are not used during the life of the Pod.
// ref:https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#resources
Resources: controller.ContainerResource(tc.Spec.TiKV.ResourceRequirements),
})
}
}
}
// Init container is only used for the case where allowed-unsafe-sysctls
// cannot be enabled for kubelet, so clean the sysctl in statefulset
// SecurityContext if init container is enabled
podSecurityContext := baseTiKVSpec.PodSecurityContext().DeepCopy()
if len(initContainers) > 0 {
podSecurityContext.Sysctls = []corev1.Sysctl{}
}
storageRequest, err := controller.ParseStorageRequest(tc.Spec.TiKV.Requests)
if err != nil {
return nil, fmt.Errorf("cannot parse storage request for tikv, tidbcluster %s/%s, error: %v", tc.Namespace, tc.Name, err)
}
stsLabels := labelTiKV(tc)
podLabels := util.CombineStringMap(stsLabels.Labels(), baseTiKVSpec.Labels())
setName := controller.TiKVMemberName(tcName)
podAnnotations := util.CombineStringMap(baseTiKVSpec.Annotations(), controller.AnnProm(v1alpha1.DefaultTiKVStatusPort, "/metrics"))
stsAnnotations := getStsAnnotations(tc.Annotations, label.TiKVLabelVal)
capacity := controller.TiKVCapacity(tc.Spec.TiKV.Limits)
headlessSvcName := controller.TiKVPeerMemberName(tcName)
deleteSlotsNumber, err := util.GetDeleteSlotsNumber(stsAnnotations)
if err != nil {
return nil, fmt.Errorf("get delete slots number of statefulset %s/%s failed, err:%v", ns, setName, err)
}
var containers []corev1.Container
if tc.Spec.TiKV.ShouldSeparateRocksDBLog() {
logFile := "rocksdb.info"
if TiKVLessThanV50(tc.TiKVImage()) {
logFile = "db/LOG"
}
var rocksDBLogVolumeMount corev1.VolumeMount
rocksDBLogFilePath := ""
rocksDBLogVolumeName := tc.Spec.TiKV.RocksDBLogVolumeName
if rocksDBLogVolumeName == "" {
rocksDBLogVolumeMount = tikvDataVol
rocksDBLogFilePath = path.Join(tikvDataVol.MountPath, logFile)
} else {
existVolume := false
volMountName := fmt.Sprintf("%s-%s", v1alpha1.TiKVMemberType.String(), rocksDBLogVolumeName)
for _, volMount := range storageVolMounts {
if volMount.Name == volMountName {
rocksDBLogVolumeMount = volMount
existVolume = true
break
}
}
if !existVolume {
for _, volMount := range tc.Spec.TiKV.AdditionalVolumeMounts {
if volMount.Name == rocksDBLogVolumeName {
rocksDBLogVolumeMount = volMount
existVolume = true
break
}
}
}
if !existVolume {
return nil, fmt.Errorf("failed to get rocksDBLogVolumeName %s for cluster %s/%s", rocksDBLogVolumeName, ns, tcName)
}
rocksDBLogFilePath = path.Join(rocksDBLogVolumeMount.MountPath, logFile)
}
// mount a shared volume and tail the RocksDB log to STDOUT using a sidecar.
containers = append(containers, corev1.Container{
Name: v1alpha1.ContainerRocksDBLogTailer.String(),
Image: tc.HelperImage(),
ImagePullPolicy: tc.HelperImagePullPolicy(),
Resources: controller.ContainerResource(tc.Spec.TiKV.GetLogTailerSpec().ResourceRequirements),
VolumeMounts: []corev1.VolumeMount{rocksDBLogVolumeMount},
Command: []string{
"sh",
"-c",
fmt.Sprintf("touch %s; tail -n0 -F %s;", rocksDBLogFilePath, rocksDBLogFilePath),
},
})
}
if tc.Spec.TiKV.ShouldSeparateRaftLog() {
raftdbLogFile := "raftdb.info"
if TiKVLessThanV50(tc.TiKVImage()) {
raftdbLogFile = "raft/LOG"
}
var raftLogVolumeMount corev1.VolumeMount
raftLogFilePath := ""
raftLogVolumeName := tc.Spec.TiKV.RaftLogVolumeName
if raftLogVolumeName == "" {
raftLogVolumeMount = tikvDataVol
raftLogFilePath = path.Join(tikvDataVol.MountPath, raftdbLogFile)
} else {
existVolume := false
volMountName := fmt.Sprintf("%s-%s", v1alpha1.TiKVMemberType.String(), raftLogVolumeName)
for _, volMount := range storageVolMounts {
if volMount.Name == volMountName {
raftLogVolumeMount = volMount
existVolume = true
break
}
}
if !existVolume {
for _, volMount := range tc.Spec.TiKV.AdditionalVolumeMounts {
if volMount.Name == raftLogVolumeName {
raftLogVolumeMount = volMount
existVolume = true
break
}
}
}
if !existVolume {
return nil, fmt.Errorf("failed to get raftLogVolume %s for cluster %s/%s", raftLogVolumeName, ns, tcName)
}
raftLogFilePath = path.Join(raftLogVolumeMount.MountPath, raftdbLogFile)
}
// mount a shared volume and tail the Raft log to STDOUT using a sidecar.
containers = append(containers, corev1.Container{
Name: v1alpha1.ContainerRaftLogTailer.String(),
Image: tc.HelperImage(),
ImagePullPolicy: tc.HelperImagePullPolicy(),
Resources: controller.ContainerResource(tc.Spec.TiKV.GetLogTailerSpec().ResourceRequirements),
VolumeMounts: []corev1.VolumeMount{raftLogVolumeMount},
Command: []string{
"sh",
"-c",
fmt.Sprintf("touch %s; tail -n0 -F %s;", raftLogFilePath, raftLogFilePath),
},
})
}
env := []corev1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "CLUSTER_NAME",
Value: tcName,
},
{
Name: "HEADLESS_SERVICE_NAME",
Value: headlessSvcName,
},
{
Name: "CAPACITY",
Value: capacity,
},
{
Name: "TZ",
Value: tc.Spec.Timezone,
},
}
tikvContainer := corev1.Container{
Name: v1alpha1.TiKVMemberType.String(),
Image: tc.TiKVImage(),
ImagePullPolicy: baseTiKVSpec.ImagePullPolicy(),
Command: []string{"/bin/sh", "/usr/local/bin/tikv_start_script.sh"},
SecurityContext: &corev1.SecurityContext{
Privileged: tc.TiKVContainerPrivilege(),
},
Ports: []corev1.ContainerPort{
{
Name: "server",
ContainerPort: v1alpha1.DefaultTiKVServerPort,
Protocol: corev1.ProtocolTCP,
},
},
VolumeMounts: volMounts,
Resources: controller.ContainerResource(tc.Spec.TiKV.ResourceRequirements),
}
if tc.Spec.TiKV.ReadinessProbe != nil {
tikvContainer.ReadinessProbe = &corev1.Probe{
ProbeHandler: buildTiKVReadinessProbHandler(tc),
InitialDelaySeconds: int32(10),
}
}
if tc.Spec.TiKV.ReadinessProbe != nil {
if tc.Spec.TiKV.ReadinessProbe.InitialDelaySeconds != nil {
tikvContainer.ReadinessProbe.InitialDelaySeconds = *tc.Spec.TiKV.ReadinessProbe.InitialDelaySeconds
}
if tc.Spec.TiKV.ReadinessProbe.PeriodSeconds != nil {
tikvContainer.ReadinessProbe.PeriodSeconds = *tc.Spec.TiKV.ReadinessProbe.PeriodSeconds
}
}
if tc.Spec.TiKV.EnableNamedStatusPort {
kvStatusPort := corev1.ContainerPort{
Name: "status",
ContainerPort: v1alpha1.DefaultTiKVStatusPort,
Protocol: corev1.ProtocolTCP,
}
tikvContainer.Ports = append(tikvContainer.Ports, kvStatusPort)
}
podSpec := baseTiKVSpec.BuildPodSpec()
if baseTiKVSpec.HostNetwork() {
env = append(env, corev1.EnvVar{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
})
}
tikvContainer.Env = util.AppendEnv(env, baseTiKVSpec.Env())
tikvContainer.EnvFrom = baseTiKVSpec.EnvFrom()
containers = append(containers, tikvContainer)
podSpec.Volumes = append(vols, baseTiKVSpec.AdditionalVolumes()...)
podSpec.SecurityContext = podSecurityContext
podSpec.InitContainers = append(initContainers, baseTiKVSpec.InitContainers()...)
podSpec.Containers, err = MergePatchContainers(containers, baseTiKVSpec.AdditionalContainers())
if err != nil {
return nil, fmt.Errorf("failed to merge containers spec for TiKV of [%s/%s], error: %v", ns, tcName, err)
}
podSpec.ServiceAccountName = tc.Spec.TiKV.ServiceAccount
if podSpec.ServiceAccountName == "" {
podSpec.ServiceAccountName = tc.Spec.ServiceAccount
}
updateStrategy := apps.StatefulSetUpdateStrategy{}
if tc.Status.TiKV.VolReplaceInProgress {
updateStrategy.Type = apps.OnDeleteStatefulSetStrategyType
} else if baseTiKVSpec.StatefulSetUpdateStrategy() == apps.OnDeleteStatefulSetStrategyType {
updateStrategy.Type = apps.OnDeleteStatefulSetStrategyType
} else {
updateStrategy.Type = apps.RollingUpdateStatefulSetStrategyType
updateStrategy.RollingUpdate = &apps.RollingUpdateStatefulSetStrategy{
Partition: pointer.Int32Ptr(tc.TiKVStsDesiredReplicas() + deleteSlotsNumber),
}
}
tikvset := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: setName,
Namespace: ns,
Labels: stsLabels.Labels(),
Annotations: stsAnnotations,
OwnerReferences: []metav1.OwnerReference{controller.GetOwnerRef(tc)},
},
Spec: apps.StatefulSetSpec{
Replicas: pointer.Int32Ptr(tc.TiKVStsDesiredReplicas()),
Selector: stsLabels.LabelSelector(),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: podLabels,
Annotations: podAnnotations,
},
Spec: podSpec,
},
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
util.VolumeClaimTemplate(storageRequest, dataVolumeName, tc.Spec.TiKV.StorageClassName),
},
ServiceName: headlessSvcName,
PodManagementPolicy: baseTiKVSpec.PodManagementPolicy(),
UpdateStrategy: updateStrategy,
},
}
tikvset.Spec.VolumeClaimTemplates = append(tikvset.Spec.VolumeClaimTemplates, additionalPVCs...)
return tikvset, nil
}
// 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 v := config.Get("pessimistic-txn.wait-for-lock-timeout"); v != nil {
if str, err := v.AsString(); err == nil {
_, err := strconv.ParseInt(str, 10, 64)
if err == nil {
waitForLockTimeOutKey := "wait-for-lock-timeout"
old := fmt.Sprintf(`%s = "%s"`, waitForLockTimeOutKey, str)
newString := fmt.Sprintf(`%s = %s`, waitForLockTimeOutKey, str)
srcStr = strings.ReplaceAll(srcStr, old, newString)
}
} else {
klog.Warningf("pessimistic-txn.wait-for-lock-timeout is not string type: %v", err)
}
}
if v := config.Get("pessimistic-txn.wake-up-delay-duration"); v != nil {
if str, err := v.AsString(); err == nil {
_, err := strconv.ParseInt(str, 10, 64)
if err == nil {
wakeUpDelayDuration := "wake-up-delay-duration"
old := fmt.Sprintf(`%s = "%s"`, wakeUpDelayDuration, str)
newString := fmt.Sprintf(`%s = %s`, wakeUpDelayDuration, str)
srcStr = strings.ReplaceAll(srcStr, old, newString)
}
} else {
klog.Warningf("pessimistic-txn.wake-up-delay-duration is not string type: %v", err)
}
}
return srcStr
}
func getTikVConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
if tc.Spec.TiKV.Config == nil {
return nil, nil
}
cm, err := getTikVConfigMapForTiKVSpec(tc.Spec.TiKV, tc)
if err != nil {
return nil, err
}
instanceName := tc.GetInstanceName()
tikvLabel := label.New().Instance(instanceName).TiKV().Labels()
cm.ObjectMeta = metav1.ObjectMeta{
Name: controller.TiKVMemberName(tc.Name),
Namespace: tc.Namespace,
Labels: tikvLabel,
OwnerReferences: []metav1.OwnerReference{controller.GetOwnerRef(tc)},
}
return cm, nil
}
func labelTiKV(tc *v1alpha1.TidbCluster) label.Label {
instanceName := tc.GetInstanceName()
return label.New().Instance(instanceName).TiKV()
}
func (m *tikvMemberManager) syncTiKVClusterStatus(tc *v1alpha1.TidbCluster, set *apps.StatefulSet) error {
if set == nil {
// skip if not created yet
return nil
}
tc.Status.TiKV.StatefulSet = &set.Status
upgrading, err := m.statefulSetIsUpgradingFn(m.deps.PodLister, m.deps.PDControl, set, tc)
if err != nil {
return err
}
// If phase changes from UpgradePhase to NormalPhase, try to endEvictLeader for the last store.
if !upgrading && tc.Status.TiKV.Phase == v1alpha1.UpgradePhase {
if err = endEvictLeader(m.deps, tc, helper.GetMinPodOrdinal(*set.Spec.Replicas, set)); err != nil {
return err
}
// end evict leader for all stores when upgrade is done
if err = endEvictLeaderForAllStore(m.deps, tc); err != nil {
return err
}
}
// Scaling takes precedence over upgrading.
if tc.TiKVStsDesiredReplicas() != *set.Spec.Replicas {
tc.Status.TiKV.Phase = v1alpha1.ScalePhase
} else if upgrading && tc.Status.PD.Phase != v1alpha1.UpgradePhase {
if !tc.IsComponentLeaderEvicting(v1alpha1.TiKVMemberType) { // skip upgrade if someone is evicting leader
tc.Status.TiKV.Phase = v1alpha1.UpgradePhase
}
} else {
tc.Status.TiKV.Phase = v1alpha1.NormalPhase
}
previousStores := tc.Status.TiKV.Stores
previousPeerStores := tc.Status.TiKV.PeerStores
previousTombstoneStores := tc.Status.TiKV.TombstoneStores
stores := map[string]v1alpha1.TiKVStore{}
peerStores := map[string]v1alpha1.TiKVStore{}
tombstoneStores := map[string]v1alpha1.TiKVStore{}
pdCli := controller.GetPDClient(m.deps.PDControl, tc)
// This only returns Up/Down/Offline stores
storesInfo, err := pdCli.GetStores()
if err != nil {
if pdapi.IsTiKVNotBootstrappedError(err) {
klog.Infof("TiKV of Cluster %s/%s not bootstrapped yet", tc.Namespace, tc.Name)
tc.Status.TiKV.Synced = true
tc.Status.TiKV.BootStrapped = false
return nil
}
tc.Status.TiKV.Synced = false
return err
}
pattern, err := regexp.Compile(fmt.Sprintf(tikvStoreLimitPattern, tc.Name, tc.Name, tc.Namespace, controller.FormatClusterDomainForRegex(tc.Spec.ClusterDomain)))
if err != nil {
return err
}
for _, store := range storesInfo.Stores {
status := getTiKVStore(store)
if status == nil {
continue
}
oldStore, exist := previousStores[status.ID]
if !exist {
oldStore, exist = previousPeerStores[status.ID]
}
status.LastTransitionTime = metav1.Now()
if exist && status.State == oldStore.State {
status.LastTransitionTime = oldStore.LastTransitionTime
}
if oldStore.LeaderCountBeforeUpgrade != nil {
status.LeaderCountBeforeUpgrade = oldStore.LeaderCountBeforeUpgrade
}
// In theory, the external tikv can join the cluster, and the operator would only manage the internal tikv.
// So we check the store owner to make sure it.
if store.Store != nil {
if pattern.Match([]byte(store.Store.Address)) {
stores[status.ID] = *status
} else if util.MatchLabelFromStoreLabels(store.Store.Labels, label.TiKVLabelVal) {
peerStores[status.ID] = *status
}
}
}
// this returns all tombstone stores
tombstoneStoresInfo, err := pdCli.GetTombStoneStores()
if err != nil {
tc.Status.TiKV.Synced = false
return err
}
for _, store := range tombstoneStoresInfo.Stores {
if store.Store != nil && !pattern.Match([]byte(store.Store.Address)) {
continue
}
status := getTiKVStore(store)
if status == nil {
continue
}
oldStore, exist := previousTombstoneStores[status.ID]
status.LastTransitionTime = metav1.Now()
if exist && status.State == oldStore.State {
status.LastTransitionTime = oldStore.LastTransitionTime
}
tombstoneStores[status.ID] = *status
}
tc.Status.TiKV.Synced = true
tc.Status.TiKV.Stores = stores
tc.Status.TiKV.PeerStores = peerStores
tc.Status.TiKV.TombstoneStores = tombstoneStores
tc.Status.TiKV.BootStrapped = true
tc.Status.TiKV.Image = ""
c := findContainerByName(set, "tikv")
if c != nil {
tc.Status.TiKV.Image = c.Image
}
err = volumes.SyncVolumeStatus(m.podVolumeModifier, m.deps.PodLister, tc, v1alpha1.TiKVMemberType)
if err != nil {
return fmt.Errorf("failed to sync volume status for tikv: %v", err)
}
return nil
}
func getTiKVStore(store *pdapi.StoreInfo) *v1alpha1.TiKVStore {
if store.Store == nil || store.Status == nil {
return nil
}
storeID := fmt.Sprintf("%d", store.Store.GetId())
ip := strings.Split(store.Store.GetAddress(), ":")[0]
podName := strings.Split(ip, ".")[0]
return &v1alpha1.TiKVStore{
ID: storeID,
PodName: podName,
IP: ip,
LeaderCount: int32(store.Status.LeaderCount),
State: store.Store.StateName,
}
}
func (m *tikvMemberManager) setStoreLabelsForTiKV(tc *v1alpha1.TidbCluster) (int, error) {
if m.deps.NodeLister == nil {
klog.V(4).Infof("Node lister is unavailable, skip setting store labels for TiKV of TiDB cluster %s/%s. This may be caused by no relevant permissions", tc.Namespace, tc.Name)
return 0, nil
}
ns := tc.GetNamespace()
// for unit test
setCount := 0
if !tc.TiKVBootStrapped() {
klog.Infof("TiKV of Cluster %s/%s is not bootstrapped yet, no need to set store labels", tc.Namespace, tc.Name)
return setCount, nil
}
pdCli := controller.GetPDClient(m.deps.PDControl, tc)
storesInfo, err := pdCli.GetStores()
if err != nil {
return setCount, err
}
config, err := pdCli.GetConfig()
if err != nil {
return setCount, err
}
storeLabels := append(config.Replication.LocationLabels, tc.Spec.TiKV.StoreLabels...)
if storeLabels == nil {
return setCount, nil
}
pattern, err := regexp.Compile(fmt.Sprintf(tikvStoreLimitPattern, tc.Name, tc.Name, tc.Namespace, controller.FormatClusterDomainForRegex(tc.Spec.ClusterDomain)))
if err != nil {
return -1, err
}
for _, store := range storesInfo.Stores {
// In theory, the external tikv can join the cluster, and the operator would only manage the internal tikv.
// So we check the store owner to make sure it.