Skip to content

Commit

Permalink
Merge pull request #17 from doyoubi/ClusterConfig
Browse files Browse the repository at this point in the history
Support changing cluster config
  • Loading branch information
doyoubi authored Nov 28, 2020
2 parents 6cc8463 + 66bd046 commit 3794f94
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Makefile.utils
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ install-helm-package:
--set "image.undermoonImageTag=$(UNDERMOON_IMG_VERSION)" \
example-undermoon "undermoon-cluster-$(OPERATOR_HELM_VERSION).tgz"

upgrade-helm-package:
$(MAKE) build-helm
helm upgrade \
--set "image.undermoonImage=$(UNDERMOON_IMG_NAME)" \
--set "image.undermoonImageTag=$(UNDERMOON_IMG_VERSION)" \
example-undermoon "undermoon-cluster-$(OPERATOR_HELM_VERSION).tgz"

uninstall-helm-package:
helm uninstall example-undermoon || true
helm uninstall example-operator || true
Expand Down Expand Up @@ -95,6 +102,9 @@ debug-build:
debug-install:
$(MAKE) install-helm-package UM_OP_DEBUG=1

debug-upgrade:
$(MAKE) upgrade-helm-package UM_OP_DEBUG=1

debug-run:
$(MAKE) install UM_OP_DEBUG=1
$(MAKE) deploy UM_OP_DEBUG=1
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/undermoon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ type UndermoonSpec struct {
ActiveRedirection bool `json:"activeRedirection"`
// +kubebuilder:validation:Minimum=1
ProxyThreads uint32 `json:"proxyThreads"`
// Interval to trigger SCAN command during migration. This is in microseconds.
MigrationScanInterval uint32 `json:"migrationScanInterval"`
// COUNT arguments for SCAN command during migration.
// +kubebuilder:validation:Minimum=1
MigrationScanCount uint32 `json:"migrationScanCount"`

// +kubebuilder:validation:MinLength=1
UndermoonImage string `json:"undermoonImage"`
Expand Down
6 changes: 6 additions & 0 deletions config/cr/overlays/helm/replace_values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
- op: replace
path: /spec/proxyThreads
value: "{{ .Values.cluster.proxyThreads }}"
- op: replace
path: /spec/migrationScanInterval
value: "{{ .Values.cluster.migrationScanInterval }}"
- op: replace
path: /spec/migrationScanCount
value: "{{ .Values.cluster.migrationScanCount }}"
- op: replace
path: /spec/undermoonImage
value: "{{ .Values.image.undermoonImage }}:{{ .Values.image.undermoonImageTag }}"
Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/undermoon.doyoubi.mydomain_undermoons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@ spec:
format: int32
minimum: 1
type: integer
migrationScanCount:
description: COUNT arguments for SCAN command during migration.
format: int32
minimum: 1
type: integer
migrationScanInterval:
description: Interval to trigger SCAN command during migration. This
is in microseconds.
format: int32
type: integer
port:
description: Port for the redis service.
format: int32
Expand Down Expand Up @@ -756,6 +766,8 @@ spec:
- chunkNumber
- clusterName
- maxMemory
- migrationScanCount
- migrationScanInterval
- port
- proxyThreads
- redisImage
Expand Down
51 changes: 51 additions & 0 deletions controllers/broker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,54 @@ func (client *brokerClient) getClusterInfo(address, clusterName string) (*cluste
content := res.Body()
return nil, errors.Errorf("Failed to get cluster info: invalid status code %d: %s", res.StatusCode(), string(content))
}

type clusterConfigPayload struct {
MigrationScanInterval string `json:"migration_scan_interval"`
MigrationScanCount string `json:"migration_scan_count"`
}

func (client *brokerClient) changeClusterConfig(address, clusterName string, scanInterval, scanCount uint32) error {
url := fmt.Sprintf("http://%s/api/v2/clusters/config/%s", address, clusterName)
payload := &clusterConfigPayload{
MigrationScanInterval: fmt.Sprintf("%d", scanInterval),
MigrationScanCount: fmt.Sprintf("%d", scanCount),
}
res, err := client.httpClient.R().
SetBody(payload).
SetError(&errorResponse{}).
Patch(url)
if err != nil {
return err
}

if res.StatusCode() == 200 {
return nil
}

if res.StatusCode() == 504 {
response, ok := res.Error().(*errorResponse)
if ok && response.Error == errStrExternalTimeout {
return errExternalTimeout
}
}

if res.StatusCode() == 404 {
response, ok := res.Error().(*errorResponse)
if ok {
return errors.Errorf("cluster not found, error code %s", response.Error)
}
}

if res.StatusCode() == 409 {
response, ok := res.Error().(*errorResponse)
if ok && response.Error == errStrRetry {
return errRetryReconciliation
}
if ok && response.Error == errStrMigrationRunning {
return errMigrationRunning
}
}

content := res.Body()
return errors.Errorf("Failed to update cluster config: invalid status code %d: %s", res.StatusCode(), string(content))
}
28 changes: 28 additions & 0 deletions controllers/meta_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (con *metaController) reconcileMeta(
return nil, err
}

err = con.changeClusterConfig(reqLogger, masterBrokerAddress, cr)
if err != nil {
return nil, err
}

info, err := con.getClusterInfo(reqLogger, masterBrokerAddress, cr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -199,6 +204,29 @@ func (con *metaController) createCluster(reqLogger logr.Logger, masterBrokerAddr
return nil
}

func (con *metaController) changeClusterConfig(reqLogger logr.Logger, masterBrokerAddress string, cr *undermoonv1alpha1.Undermoon) error {
err := con.client.changeClusterConfig(
masterBrokerAddress,
cr.Spec.ClusterName,
cr.Spec.MigrationScanInterval,
cr.Spec.MigrationScanCount,
)
if err != nil {
if err == errMigrationRunning {
// Undermoon will implement this restriction later.
return errRetryReconciliation
}
if err == errRetryReconciliation {
return err
}
reqLogger.Error(err, "failed to change cluster config",
"Name", cr.ObjectMeta.Name,
"ClusterName", cr.Spec.ClusterName)
return err
}
return nil
}

func (con *metaController) changeNodeNumber(reqLogger logr.Logger, masterBrokerAddress string, cr *undermoonv1alpha1.Undermoon) error {
chunkNumber := int(cr.Spec.ChunkNumber)
clusterName := cr.Spec.ClusterName
Expand Down
2 changes: 2 additions & 0 deletions helm/undermoon-cluster/templates/undermoon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spec:
clusterName: {{ .Values.cluster.clusterName }}
coordinatorResources: {{- toYaml .Values.resources.coordinatorResources | nindent 4 }}
maxMemory: {{ .Values.cluster.maxMemory }}
migrationScanCount: {{ .Values.cluster.migrationScanCount }}
migrationScanInterval: {{ .Values.cluster.migrationScanInterval }}
port: {{ .Values.cluster.port }}
proxyResources: {{- toYaml .Values.resources.proxyResources | nindent 4 }}
proxyThreads: {{ .Values.cluster.proxyThreads }}
Expand Down
2 changes: 2 additions & 0 deletions helm/undermoon-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ cluster:
port: 5299
activeRedirection: false
proxyThreads: 2
migrationScanInterval: 1000
migrationScanCount: 16

image:
undermoonImage: doyoubi/undermoon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ spec:
format: int32
minimum: 1
type: integer
migrationScanCount:
description: COUNT arguments for SCAN command during migration.
format: int32
minimum: 1
type: integer
migrationScanInterval:
description: Interval to trigger SCAN command during migration. This is in microseconds.
format: int32
type: integer
port:
description: Port for the redis service.
format: int32
Expand Down Expand Up @@ -500,6 +509,8 @@ spec:
- chunkNumber
- clusterName
- maxMemory
- migrationScanCount
- migrationScanInterval
- port
- proxyThreads
- redisImage
Expand Down

0 comments on commit 3794f94

Please sign in to comment.