github.com/matrixorigin/matrixone@v1.2.0/pkg/backup/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 backup
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"strings"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    24  	"github.com/matrixorigin/matrixone/pkg/logservice"
    25  )
    26  
    27  const (
    28  	Version = "0823"
    29  )
    30  
    31  const (
    32  	moMeta       = "mo_meta"
    33  	configDir    = "config"
    34  	taeDir       = "tae"
    35  	taeList      = "tae_list"
    36  	taeSum       = "tae_sum"
    37  	hakeeperDir  = "hakeeper"
    38  	HakeeperFile = "hk_data"
    39  )
    40  
    41  // Format :type,subtype,filename or dirname
    42  const (
    43  	TypePos              = 0
    44  	SubTypePos           = 1
    45  	FileNameOrDirNamePos = 2
    46  )
    47  
    48  type MetaType int
    49  
    50  const (
    51  	/**
    52  	  backup_meta | mo_meta
    53  	    ID        |
    54  	    Version   | Version
    55  	    Buildinfo | Buildinfo
    56  	              | Launchconfig
    57  	              | Tae
    58  	              | Hakeeper
    59  	*/
    60  	TypeVersion MetaType = iota
    61  	TypeBuildinfo
    62  	TypeLaunchconfig
    63  )
    64  
    65  func (t MetaType) String() string {
    66  	switch t {
    67  	case TypeVersion:
    68  		return "version"
    69  	case TypeBuildinfo:
    70  		return "buildinfo"
    71  	case TypeLaunchconfig:
    72  		return "launchconfig"
    73  	default:
    74  		return fmt.Sprintf("invalid type %d", t)
    75  	}
    76  }
    77  
    78  // Meta of mo_meta same as the mo_br
    79  type Meta struct {
    80  	Typ               MetaType
    81  	SubTyp            string
    82  	FileNameOrDirName string
    83  
    84  	//version
    85  	Version string
    86  
    87  	//build info
    88  	Buildinfo string
    89  
    90  	//launch config
    91  	LaunchConfigFile string
    92  }
    93  
    94  func (m *Meta) String() string {
    95  	line := m.CsvString()
    96  	return strings.Join(line, ",")
    97  }
    98  
    99  func (m *Meta) CsvString() []string {
   100  	format := make([]string, 0, 3)
   101  	format = append(format, m.Typ.String(), m.SubTyp, m.FileNameOrDirName)
   102  	switch m.Typ {
   103  	case TypeVersion:
   104  		format[SubTypePos] = m.Version
   105  	case TypeBuildinfo:
   106  		format[SubTypePos] = m.Buildinfo
   107  	case TypeLaunchconfig:
   108  		format[FileNameOrDirNamePos] = m.LaunchConfigFile
   109  	}
   110  	return format
   111  }
   112  
   113  type Metas struct {
   114  	metas []*Meta
   115  }
   116  
   117  func NewMetas() *Metas {
   118  	return &Metas{}
   119  }
   120  
   121  func (m *Metas) Append(meta *Meta) {
   122  	m.metas = append(m.metas, meta)
   123  }
   124  
   125  func (m *Metas) AppendVersion(version string) {
   126  	m.Append(&Meta{
   127  		Typ:     TypeVersion,
   128  		Version: version,
   129  	})
   130  }
   131  
   132  func (m *Metas) AppendBuildinfo(info string) {
   133  	m.Append(&Meta{
   134  		Typ:       TypeBuildinfo,
   135  		Buildinfo: info,
   136  	})
   137  }
   138  
   139  func (m *Metas) AppendLaunchconfig(subTyp, file string) {
   140  	if !subTypeIsValid(subTyp) {
   141  		return
   142  	}
   143  	m.Append(&Meta{
   144  		Typ:              TypeLaunchconfig,
   145  		SubTyp:           subTyp,
   146  		LaunchConfigFile: file,
   147  	})
   148  }
   149  
   150  func (m *Metas) orderTypes() []int {
   151  	idx := make([]int, 0, len(m.metas))
   152  	for i := range m.metas {
   153  		idx = append(idx, i)
   154  	}
   155  	sort.Slice(idx, func(i, j int) bool {
   156  		return m.metas[idx[i]].Typ < m.metas[idx[j]].Typ
   157  	})
   158  	return idx
   159  }
   160  
   161  func (m *Metas) CsvString() [][]string {
   162  	lines := make([][]string, 0, len(m.metas))
   163  	idx := m.orderTypes()
   164  	for _, s := range idx {
   165  		t := m.metas[s]
   166  		lines = append(lines, t.CsvString())
   167  	}
   168  	return lines
   169  }
   170  
   171  func (m *Metas) String() string {
   172  	lines := make([]string, 0, len(m.metas))
   173  	idx := m.orderTypes()
   174  	for _, s := range idx {
   175  		t := m.metas[s]
   176  		lines = append(lines, t.String())
   177  	}
   178  	return strings.Join(lines, "\n")
   179  }
   180  
   181  var (
   182  	launchConfigPaths = make(map[string][]string)
   183  	subTypes          = map[string]int8{
   184  		CnConfig:     1,
   185  		DnConfig:     1,
   186  		LogConfig:    1,
   187  		ProxyConfig:  1,
   188  		LaunchConfig: 1,
   189  	}
   190  )
   191  
   192  func subTypeIsValid(subType string) bool {
   193  	if _, ok := subTypes[subType]; ok {
   194  		return ok
   195  	}
   196  	return false
   197  }
   198  
   199  const (
   200  	CnConfig     = "cn"
   201  	DnConfig     = "dn"
   202  	LogConfig    = "log"
   203  	ProxyConfig  = "proxy"
   204  	LaunchConfig = "launch"
   205  )
   206  
   207  type Config struct {
   208  	// Timestamp
   209  	Timestamp types.TS
   210  
   211  	// For General usage
   212  	GeneralDir fileservice.FileService
   213  
   214  	// For locating tae's storage fs
   215  	SharedFs fileservice.FileService
   216  
   217  	// For tae and hakeeper
   218  	TaeDir fileservice.FileService
   219  
   220  	// hakeeper client
   221  	HAkeeper logservice.CNHAKeeperClient
   222  
   223  	Metas *Metas
   224  
   225  	// For parallel backup
   226  	Parallelism uint16
   227  
   228  	BackupType string
   229  	BackupTs   types.TS
   230  }
   231  
   232  // metasGeneralFsMustBeSet denotes metas and generalFs must be ready
   233  func (c *Config) metasGeneralFsMustBeSet() bool {
   234  	return !(c == nil || c.Metas == nil || c.GeneralDir == nil)
   235  }
   236  
   237  // metasMustBeSet denotes metas must be ready
   238  func (c *Config) metasMustBeSet() bool {
   239  	return !(c == nil || c.Metas == nil)
   240  }
   241  
   242  type s3Config struct {
   243  	endpoint        string
   244  	accessKeyId     string
   245  	secretAccessKey string
   246  	bucket          string
   247  	filepath        string
   248  	region          string
   249  	compression     string
   250  	roleArn         string
   251  	provider        string
   252  	externalId      string
   253  	format          string
   254  	jsonData        string
   255  	isMinio         bool
   256  	parallelism     uint16
   257  }
   258  
   259  type filesystemConfig struct {
   260  	path string
   261  }
   262  
   263  type pathConfig struct {
   264  	isS3   bool
   265  	forETL bool
   266  	s3Config
   267  	filesystemConfig
   268  }
   269  
   270  type taeFile struct {
   271  	path     string
   272  	size     int64
   273  	checksum []byte
   274  	needCopy bool
   275  	ts       types.TS
   276  }
   277  
   278  func (tfs *taeFile) String() string {
   279  	line := tfs.CsvString()
   280  	return strings.Join(line, ",")
   281  }
   282  
   283  func (tfs *taeFile) CsvString() []string {
   284  	return []string{tfs.path, fmt.Sprintf("%d", tfs.size),
   285  		fmt.Sprintf("%x", tfs.checksum), fmt.Sprintf("%t", tfs.needCopy),
   286  		tfs.ts.ToString()}
   287  }
   288  
   289  func taeFileListToCsv(files []*taeFile) ([][]string, int64) {
   290  	lines := make([][]string, 0, len(files))
   291  	ret := int64(0)
   292  	for _, file := range files {
   293  		lines = append(lines, file.CsvString())
   294  		ret += file.size
   295  	}
   296  	return lines, ret
   297  }
   298  
   299  func taeBackupTimeAndSizeToCsv(backupTime, backupTS, typ string, size int64) []string {
   300  	return []string{backupTime, fmt.Sprintf("%d", size), backupTS, typ}
   301  }