github.com/pingcap/tiup@v1.15.1/components/dm/task/update_dm_meta.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package task
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"strings"
    20  
    21  	dmspec "github.com/pingcap/tiup/components/dm/spec"
    22  
    23  	"github.com/pingcap/tiup/pkg/cluster/spec"
    24  	"github.com/pingcap/tiup/pkg/set"
    25  )
    26  
    27  // UpdateDMMeta is used to maintain the DM meta information
    28  type UpdateDMMeta struct {
    29  	cluster        string
    30  	metadata       *dmspec.Metadata
    31  	deletedNodesID []string
    32  }
    33  
    34  // NewUpdateDMMeta create i update dm meta task.
    35  func NewUpdateDMMeta(cluster string, metadata *dmspec.Metadata, deletedNodesID []string) *UpdateDMMeta {
    36  	return &UpdateDMMeta{
    37  		cluster:        cluster,
    38  		metadata:       metadata,
    39  		deletedNodesID: deletedNodesID,
    40  	}
    41  }
    42  
    43  // Execute implements the Task interface
    44  // the metadata especially the topology is in wide use,
    45  // the other callers point to this field by a pointer,
    46  // so we should update the original topology directly, and don't make a copy
    47  func (u *UpdateDMMeta) Execute(ctx context.Context) error {
    48  	deleted := set.NewStringSet(u.deletedNodesID...)
    49  	topo := u.metadata.Topology
    50  	masters := make([]*dmspec.MasterSpec, 0)
    51  	for i, instance := range (&dmspec.DMMasterComponent{Topology: topo}).Instances() {
    52  		if deleted.Exist(instance.ID()) {
    53  			continue
    54  		}
    55  		masters = append(masters, topo.Masters[i])
    56  	}
    57  	topo.Masters = masters
    58  
    59  	workers := make([]*dmspec.WorkerSpec, 0)
    60  	for i, instance := range (&dmspec.DMWorkerComponent{Topology: topo}).Instances() {
    61  		if deleted.Exist(instance.ID()) {
    62  			continue
    63  		}
    64  		workers = append(workers, topo.Workers[i])
    65  	}
    66  	topo.Workers = workers
    67  
    68  	monitors := make([]*spec.PrometheusSpec, 0)
    69  	for i, instance := range (&spec.MonitorComponent{Topology: topo}).Instances() {
    70  		if deleted.Exist(instance.ID()) {
    71  			continue
    72  		}
    73  		monitors = append(monitors, topo.Monitors[i])
    74  	}
    75  	topo.Monitors = monitors
    76  
    77  	grafanas := make([]*spec.GrafanaSpec, 0)
    78  	for i, instance := range (&spec.GrafanaComponent{Topology: topo}).Instances() {
    79  		if deleted.Exist(instance.ID()) {
    80  			continue
    81  		}
    82  		grafanas = append(grafanas, topo.Grafanas[i])
    83  	}
    84  	topo.Grafanas = grafanas
    85  
    86  	alertmanagers := make([]*spec.AlertmanagerSpec, 0)
    87  	for i, instance := range (&spec.AlertManagerComponent{Topology: topo}).Instances() {
    88  		if deleted.Exist(instance.ID()) {
    89  			continue
    90  		}
    91  		alertmanagers = append(alertmanagers, topo.Alertmanagers[i])
    92  	}
    93  	topo.Alertmanagers = alertmanagers
    94  
    95  	return dmspec.GetSpecManager().SaveMeta(u.cluster, u.metadata)
    96  }
    97  
    98  // Rollback implements the Task interface
    99  func (u *UpdateDMMeta) Rollback(ctx context.Context) error {
   100  	return dmspec.GetSpecManager().SaveMeta(u.cluster, u.metadata)
   101  }
   102  
   103  // String implements the fmt.Stringer interface
   104  func (u *UpdateDMMeta) String() string {
   105  	return fmt.Sprintf("UpdateMeta: cluster=%s, deleted=`'%s'`", u.cluster, strings.Join(u.deletedNodesID, "','"))
   106  }