github.com/matrixorigin/matrixone@v1.2.0/pkg/cnservice/server_metadata.go (about)

     1  // Copyright 2021 - 2022 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 cnservice
    16  
    17  import (
    18  	"path/filepath"
    19  
    20  	"github.com/fagongzi/util/protoc"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    22  	"github.com/matrixorigin/matrixone/pkg/util/file"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  const (
    27  	metadataDir = "cnservice"
    28  )
    29  
    30  func getMetadataFile(uuid string) string {
    31  	return filepath.Join(metadataDir, uuid)
    32  }
    33  
    34  func (s *service) initMetadata() error {
    35  	data, err := file.ReadFile(s.metadataFS, getMetadataFile(s.cfg.UUID))
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if len(data) == 0 {
    41  		s.mustUpdateMetadata()
    42  		return nil
    43  	}
    44  
    45  	v := &metadata.CNStore{}
    46  	protoc.MustUnmarshal(v, data)
    47  	if v.UUID != s.metadata.UUID {
    48  		s.logger.Fatal("BUG: disk CNStore and start CNStore not match",
    49  			zap.String("disk-store", v.UUID))
    50  	}
    51  	s.metadata = *v
    52  	s.logger.Info("local CNStore loaded",
    53  		zap.String("metadata", s.metadata.DebugString()))
    54  	return nil
    55  }
    56  
    57  func (s *service) mustUpdateMetadata() {
    58  	if err := file.WriteFile(s.metadataFS,
    59  		getMetadataFile(s.cfg.UUID),
    60  		protoc.MustMarshal(&s.metadata)); err != nil {
    61  		s.logger.Fatal("update metadata to local file failed",
    62  			zap.Error(err))
    63  	}
    64  }