Skip to content

Commit

Permalink
*: Save Xenon's metadata to config map.
Browse files Browse the repository at this point in the history
subpath -> configmap
subpath may not work in some conditions.
1. Add a Xenon Configmap which is used to save xenon data.
2. Build peers.json according to the replicas.
3. Make peers.json in init-sidecar.
4. Xenon boot using existing peers.json.
  • Loading branch information
runkecheng committed Apr 13, 2022
1 parent 42e595f commit 7892d33
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 6 deletions.
1 change: 1 addition & 0 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
clustersyncer.NewFollowerSVCSyncer(r.Client, instance),
clustersyncer.NewStatefulSetSyncer(r.Client, instance, cmRev, sctRev, r.SQLRunnerFactory, r.XenonExecutor),
clustersyncer.NewPDBSyncer(r.Client, instance),
clustersyncer.NewXenonCMSyncer(r.Client, instance),
}

if instance.Spec.MetricsOpts.Enabled {
Expand Down
8 changes: 8 additions & 0 deletions mysqlcluster/container/init_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ func (c *initSidecar) getVolumeMounts() []corev1.VolumeMount {
Name: utils.MysqlCMVolumeName,
MountPath: utils.MysqlCMVolumeMountPath,
},
{
Name: utils.XenonCMVolumeName,
MountPath: utils.XenonCMVolumeMountPath,
},
{
Name: utils.XenonMetaVolumeName,
MountPath: utils.XenonMetaVolumeMountPath,
},
{
Name: utils.ScriptsVolumeName,
MountPath: utils.ScriptsVolumeMountPath,
Expand Down
12 changes: 10 additions & 2 deletions mysqlcluster/container/init_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ var (
Name: utils.MysqlCMVolumeName,
MountPath: utils.MysqlCMVolumeMountPath,
},
{
Name: utils.XenonCMVolumeName,
MountPath: utils.XenonCMVolumeMountPath,
},
{
Name: utils.XenonMetaVolumeName,
MountPath: utils.XenonMetaVolumeMountPath,
},
{
Name: utils.ScriptsVolumeName,
MountPath: utils.ScriptsVolumeMountPath,
Expand Down Expand Up @@ -437,7 +445,7 @@ func TestGetInitSidecarVolumeMounts(t *testing.T) {
MysqlCluster: &testToKuDBMysqlCluster,
}
tokudbCase := EnsureContainer("init-sidecar", &testTokuDBCluster)
tokuDBVolumeMounts := make([]corev1.VolumeMount, 6, 7)
tokuDBVolumeMounts := make([]corev1.VolumeMount, 8, 9)
copy(tokuDBVolumeMounts, defaultInitsidecarVolumeMounts)
tokuDBVolumeMounts = append(tokuDBVolumeMounts, corev1.VolumeMount{
Name: utils.SysVolumeName,
Expand All @@ -453,7 +461,7 @@ func TestGetInitSidecarVolumeMounts(t *testing.T) {
MysqlCluster: &testPersistenceMysqlCluster,
}
persistenceCase := EnsureContainer("init-sidecar", &testPersistenceCluster)
persistenceVolumeMounts := make([]corev1.VolumeMount, 6, 7)
persistenceVolumeMounts := make([]corev1.VolumeMount, 8, 9)
copy(persistenceVolumeMounts, defaultInitsidecarVolumeMounts)
persistenceVolumeMounts = append(persistenceVolumeMounts, corev1.VolumeMount{
Name: utils.DataVolumeName,
Expand Down
4 changes: 4 additions & 0 deletions mysqlcluster/container/xenon.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (c *xenon) getVolumeMounts() []corev1.VolumeMount {
Name: utils.XenonVolumeName,
MountPath: utils.XenonVolumeMountPath,
},
{
Name: utils.XenonMetaVolumeName,
MountPath: utils.XenonMetaVolumeMountPath,
},
{
Name: utils.SysLocalTimeZone,
MountPath: utils.SysLocalTimeZoneMountPath,
Expand Down
6 changes: 5 additions & 1 deletion mysqlcluster/container/xenon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ func TestGetXenonVolumeMounts(t *testing.T) {
MountPath: "/scripts",
},
{
Name: "xenon",
Name: "xenon-conf",
MountPath: "/etc/xenon",
},
{
Name: "xenon-meta",
MountPath: "/var/lib/xenon",
},
{
Name: utils.SysLocalTimeZone,
MountPath: "/etc/localtime",
Expand Down
18 changes: 18 additions & 0 deletions mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ func (c *MysqlCluster) EnsureVolumes() []corev1.Volume {
},
},
},
corev1.Volume{
Name: utils.XenonCMVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: c.GetNameForResource(utils.XenonMetaData),
},
},
},
},
corev1.Volume{
Name: utils.XenonMetaVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
corev1.Volume{
Name: utils.ScriptsVolumeName,
VolumeSource: corev1.VolumeSource{
Expand Down Expand Up @@ -294,6 +310,8 @@ func (c *MysqlCluster) GetNameForResource(name utils.ResourceName) string {
return fmt.Sprintf("%s-metrics", c.Name)
case utils.Secret:
return fmt.Sprintf("%s-secret", c.Name)
case utils.XenonMetaData:
return fmt.Sprintf("%s-xenon", c.Name)
default:
return c.Name
}
Expand Down
16 changes: 16 additions & 0 deletions mysqlcluster/mysqlcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ func TestEnsureVolumes(t *testing.T) {
},
},
},
{
Name: utils.XenonCMVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "sample-xenon",
},
},
},
},
{
Name: utils.XenonMetaVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: utils.ScriptsVolumeName,
VolumeSource: corev1.VolumeSource{
Expand Down
88 changes: 88 additions & 0 deletions mysqlcluster/syncer/xenon_cm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2021 RadonDB.
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 syncer

import (
"encoding/json"
"fmt"

"github.com/presslabs/controller-util/syncer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)

// NewXenonCMSyncer returns xenon configmap syncer.
func NewXenonCMSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Interface {
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: c.GetNameForResource(utils.XenonMetaData),
Namespace: c.Namespace,
Labels: c.GetLabels(),
},
}

return syncer.NewObjectSyncer("ConfigMap", c.Unwrap(), cm, cli, func() error {
if int(*c.Spec.Replicas) == 0 {
return nil
}

metaData, err := buildXenonMetaData(c)
if err != nil {
return fmt.Errorf("failed to build xenon metadata: %s", err)
}

cm.Data = map[string]string{
"peers.json": metaData,
}

return nil
})
}

type XenonMetaData struct {
Idlepeers []string `json:"idlepeers"`
Peers []string `json:"peers"`
}

// buildXenonMetaData build the default metadata of xenon.
func buildXenonMetaData(c *mysqlcluster.MysqlCluster) (string, error) {
replicas := c.Spec.Replicas
xenonMetaData := XenonMetaData{}
for i := 0; i < int(*replicas); i++ {
xenonMetaData.Peers = append(xenonMetaData.Peers,
fmt.Sprintf("%s-%d.%s.%s:%d",
c.GetNameForResource(utils.StatefulSet),
i,
c.GetNameForResource(utils.HeadlessSVC),
c.Namespace,
utils.XenonPort,
))
}
metaJson, err := json.Marshal(xenonMetaData)
if err != nil {
return "", err
}
return string(metaJson), nil
}
4 changes: 2 additions & 2 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (cfg *Config) buildXenonConf() []byte {
"election-timeout": %d,
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "/var/lib/xenon/",
"meta-datadir": "%s",
"semi-sync-degrade": true,
"purge-binlog-disabled": true,
"super-idle": false
Expand All @@ -353,7 +353,7 @@ func (cfg *Config) buildXenonConf() []byte {
`, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser,
cfg.GtidPurged, requestTimeout,
pingTimeout, cfg.RootPassword, version, srcSysVars, replicaSysVars, cfg.ElectionTimeout,
cfg.AdmitDefeatHearbeatCount, heartbeatTimeout)
cfg.AdmitDefeatHearbeatCount, heartbeatTimeout, xenonConfigPath)

return utils.StringToBytes(str)
}
Expand Down
20 changes: 20 additions & 0 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func runInitCommand(cfg *Config) error {
return fmt.Errorf("failed to copy my.cnf: %s", err)
}

buildDefaultXenonMeta(uid, gid)

// build client.conf.
clientConfig, err := cfg.buildClientConfig()
if err != nil {
Expand Down Expand Up @@ -290,3 +292,21 @@ chown -R mysql.mysql %s`,
// chown -R mysql.mysql extra.cnf
path.Join(extraConfPath, "extra.cnf"))
}

func buildDefaultXenonMeta(uid, gid int) error {
metaFile := fmt.Sprintf("%s/peers.json", xenonConfigPath)
// mkdir var/lib/xenon.
// https://github.com/radondb/xenon/blob/master/src/raft/raft.go#L118
if err := os.MkdirAll(xenonConfigPath, 0777); err != nil {
return fmt.Errorf("failed to mkdir %s: %s", xenonConfigPath, err)
}
// copy appropriate peers.json from config-map to config mount.
if err := copyFile(path.Join(xenonCMPath, "peers.json"), path.Join(xenonConfigPath, "peers.json")); err != nil {
return fmt.Errorf("failed to copy peers.json: %s", err)
}
// chown -R mysql:mysql /var/lib/xenon/peers.json.
if err := os.Chown(metaFile, uid, gid); err != nil {
return fmt.Errorf("failed to chown %s: %s", metaFile, err)
}
return nil
}
6 changes: 6 additions & 0 deletions sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var (
// mysqlConfigPath is the mysql configs path.
mysqlConfigPath = utils.MysqlConfVolumeMountPath

// xenonConfigPath is the xenon meta path.
xenonConfigPath = utils.XenonMetaVolumeMountPath

// clientConfPath is the client.cnf path.
clientConfPath = utils.ConfClientPath

Expand All @@ -43,6 +46,9 @@ var (
// mysqlCMPath is the mounted configmap.
mysqlCMPath = utils.MysqlCMVolumeMountPath

// xenonCMPath is the mounted configmap.
xenonCMPath = utils.XenonCMVolumeMountPath

// dataPath is the mysql data path.
dataPath = utils.DataVolumeMountPath

Expand Down
7 changes: 6 additions & 1 deletion utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,20 @@ const (
// volumes names.
ConfVolumeName = "mysql-conf"
MysqlCMVolumeName = "mysql-cm"
XenonMetaVolumeName = "xenon-meta"
XenonCMVolumeName = "xenon-cm"
LogsVolumeName = "logs"
DataVolumeName = "data"
SysVolumeName = "host-sys"
ScriptsVolumeName = "scripts"
XenonVolumeName = "xenon"
XenonVolumeName = "xenon-conf"
InitFileVolumeName = "init-mysql"

// volumes mount path.
MysqlConfVolumeMountPath = "/etc/mysql"
MysqlCMVolumeMountPath = "/mnt/mysql-cm"
XenonMetaVolumeMountPath = "/var/lib/xenon"
XenonCMVolumeMountPath = "/mnt/xenon-cm"
LogsVolumeMountPath = "/var/log/mysql"
DataVolumeMountPath = "/var/lib/mysql"
SysVolumeMountPath = "/host-sys"
Expand Down Expand Up @@ -160,6 +163,8 @@ const (
ServiceAccount ResourceName = "service-account"
// PodDisruptionBudget is the name of pod disruption budget for the statefulset.
PodDisruptionBudget ResourceName = "pdb"
// XenonMetaData is the name of the configmap that contains xenon metadata.
XenonMetaData ResourceName = "xenon-metadata"
)

// JobType
Expand Down

0 comments on commit 7892d33

Please sign in to comment.