github.com/matrixorigin/matrixone@v1.2.0/pkg/bootstrap/types.go (about)

     1  // Copyright 2023 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bootstrap
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/bootstrap/versions"
    22  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    23  	"github.com/matrixorigin/matrixone/pkg/util/executor"
    24  )
    25  
    26  // Service is used to bootstrap and upgrade mo cluster.
    27  //
    28  // In bootstrap, it will create some internal databases and tables at the time of MO
    29  // initialization according to a specific logic. It provides the necessary dependencies
    30  // for other components to be launched later.
    31  //
    32  // In upgrade, it will upgrade the metadata between two MO versions. When there is a
    33  // need to modify the original data (e.g. create a system table, modify the system table
    34  // structure, etc.) between two MO versions, then an upgrade process is required. This
    35  // process is used to ensure that each tenant's data is properly upgraded and updated.
    36  //
    37  // Note that, this service is not used to bootstrap logservice and dn. The internal
    38  // databases and tables as below:
    39  // 1. mo_indexes in mo_catalog
    40  // 2. task infrastructure database
    41  type Service interface {
    42  	// Bootstrap try to bootstrap mo cluster
    43  	Bootstrap(ctx context.Context) error
    44  	// BootstrapUpgrade bootstrap upgrade framework
    45  	BootstrapUpgrade(ctx context.Context) error
    46  	// MaybeUpgradeTenant used to upgrade tenant metadata if the tenant is old version.
    47  	// Return true, nil means tenant upgraded, the call need to load tenant again to get
    48  	// latest tenant info.
    49  	MaybeUpgradeTenant(
    50  		ctx context.Context,
    51  		tenantFetchFunc func() (int32, string, error),
    52  		txnOp client.TxnOperator) (bool, error)
    53  	// UpgradeTenant used to manual upgrade tenant metadata
    54  	UpgradeTenant(ctx context.Context, tenantName string, retryCount uint32, isALLAccount bool) (bool, error)
    55  	// GetFinalVersion Get mo final version, which is based on the current code
    56  	GetFinalVersion() string
    57  	// GetFinalVersionOffset Get mo final version offset, which is based on the current code
    58  	GetFinalVersionOffset() int32
    59  	// Close close bootstrap service
    60  	Close() error
    61  }
    62  
    63  // Locker locker is used to get lock to bootstrap. Only one cn can get lock to bootstrap.
    64  // Other cns need to wait bootstrap completed.
    65  type Locker interface {
    66  	// Get return true means get lock
    67  	Get(ctx context.Context, key string) (bool, error)
    68  }
    69  
    70  // VersionHandle every version that needs to be upgraded with cluster metadata needs to
    71  // have an VersionHandle implementation!
    72  type VersionHandle interface {
    73  	// Metadata version metadata
    74  	Metadata() versions.Version
    75  	// Prepare prepare upgrade. This upgrade will be executed before cluster and tenant upgrade.
    76  	Prepare(ctx context.Context, txn executor.TxnExecutor, final bool) error
    77  	// ClusterNeedUpgrade handle upgrade cluster metadata. This upgrade will be executed before
    78  	// tenant upgrade.
    79  	HandleClusterUpgrade(ctx context.Context, txn executor.TxnExecutor) error
    80  	// HandleTenantUpgrade handle upgrade a special tenant.
    81  	HandleTenantUpgrade(ctx context.Context, tenantID int32, txn executor.TxnExecutor) error
    82  	// HandleCreateFrameworkDeps Used to execute pre dependencies when creating a new upgrade framework
    83  	// based on the current version for the first time.
    84  	HandleCreateFrameworkDeps(txn executor.TxnExecutor) error
    85  }
    86  
    87  // Option option for bootstrap service
    88  type Option func(s *service)
    89  
    90  // WithUpgradeHandles reset upgrade handles
    91  func WithUpgradeHandles(handles []VersionHandle) Option {
    92  	return func(s *service) {
    93  		s.handles = handles
    94  	}
    95  }
    96  
    97  // WithUpgradeTenantBatch setup upgrade tenant batch
    98  func WithUpgradeTenantBatch(value int) Option {
    99  	return func(s *service) {
   100  		s.upgrade.upgradeTenantBatch = value
   101  	}
   102  }
   103  
   104  // WithCheckUpgradeDuration setup check upgrade duration
   105  func WithCheckUpgradeDuration(value time.Duration) Option {
   106  	return func(s *service) {
   107  		s.upgrade.checkUpgradeDuration = value
   108  	}
   109  }
   110  
   111  // WithCheckUpgradeDuration setup check upgrade duration
   112  func WithCheckUpgradeTenantDuration(value time.Duration) Option {
   113  	return func(s *service) {
   114  		s.upgrade.checkUpgradeTenantDuration = value
   115  	}
   116  }
   117  
   118  // WithCheckUpgradeTenantWorkers setup upgrade tenant workers
   119  func WithCheckUpgradeTenantWorkers(value int) Option {
   120  	return func(s *service) {
   121  		s.upgrade.upgradeTenantTasks = value
   122  	}
   123  }