github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/metadata/cluster_info.go (about)

     1  // Copyright 2022 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 metadata
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/coreos/go-semver/semver"
    20  	"github.com/pingcap/tiflow/engine/pkg/adapter"
    21  	metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model"
    22  	"github.com/pingcap/tiflow/pkg/errors"
    23  )
    24  
    25  // ClusterInfo represents the cluster info.
    26  type ClusterInfo struct {
    27  	Version semver.Version
    28  }
    29  
    30  // NewClusterInfo creates a new ClusterInfo instance.
    31  func NewClusterInfo(version semver.Version) *ClusterInfo {
    32  	return &ClusterInfo{
    33  		Version: version,
    34  	}
    35  }
    36  
    37  // ClusterInfoStore manages the state of ClusterInfo.
    38  type ClusterInfoStore struct {
    39  	*frameworkMetaStore
    40  }
    41  
    42  // NewClusterInfoStore returns a new ClusterInfoStore instance
    43  func NewClusterInfoStore(kvClient metaModel.KVClient) *ClusterInfoStore {
    44  	clusterInfoStore := &ClusterInfoStore{
    45  		frameworkMetaStore: newTOMLFrameworkMetaStore(kvClient),
    46  	}
    47  	clusterInfoStore.frameworkMetaStore.stateFactory = clusterInfoStore
    48  	return clusterInfoStore
    49  }
    50  
    51  // CreateState creates an empty ClusterInfo object
    52  func (clusterInfoStore *ClusterInfoStore) createState() state {
    53  	return &ClusterInfo{}
    54  }
    55  
    56  // Key returns encoded key of ClusterInfo state store
    57  func (clusterInfoStore *ClusterInfoStore) key() string {
    58  	return adapter.DMInfoKeyAdapter.Encode()
    59  }
    60  
    61  // UpdateVersion updates the version of ClusterInfo.
    62  func (clusterInfoStore *ClusterInfoStore) UpdateVersion(ctx context.Context, newVer semver.Version) error {
    63  	state, err := clusterInfoStore.Get(ctx)
    64  	if err != nil {
    65  		return errors.Trace(err)
    66  	}
    67  	clusterInfo := state.(*ClusterInfo)
    68  	clusterInfo.Version = newVer
    69  	return clusterInfoStore.Put(ctx, clusterInfo)
    70  }