github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/ledger/blkstorage/fsblkstorage/blockfile_helper.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package fsblkstorage
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"strconv"
    14  	"strings"
    15  
    16  	"github.com/davecgh/go-spew/spew"
    17  	"github.com/hyperledger/fabric/protos/common"
    18  )
    19  
    20  // constructCheckpointInfoFromBlockFiles scans the last blockfile (if any) and construct the checkpoint info
    21  // if the last file contains no block or only a partially written block (potentially because of a crash while writing block to the file),
    22  // this scans the second last file (if any)
    23  func constructCheckpointInfoFromBlockFiles(rootDir string) (*checkpointInfo, error) {
    24  	logger.Debugf("Retrieving checkpoint info from block files")
    25  	var lastFileNum int
    26  	var numBlocksInFile int
    27  	var endOffsetLastBlock int64
    28  	var lastBlockNumber uint64
    29  
    30  	var lastBlockBytes []byte
    31  	var lastBlock *common.Block
    32  	var err error
    33  
    34  	if lastFileNum, err = retrieveLastFileSuffix(rootDir); err != nil {
    35  		return nil, err
    36  	}
    37  	logger.Debugf("Last file number found = %d", lastFileNum)
    38  
    39  	if lastFileNum == -1 {
    40  		cpInfo := &checkpointInfo{0, 0, true, 0}
    41  		logger.Debugf("No block file found")
    42  		return cpInfo, nil
    43  	}
    44  
    45  	fileInfo := getFileInfoOrPanic(rootDir, lastFileNum)
    46  	logger.Debugf("Last Block file info: FileName=[%s], FileSize=[%d]", fileInfo.Name(), fileInfo.Size())
    47  	if lastBlockBytes, endOffsetLastBlock, numBlocksInFile, err = scanForLastCompleteBlock(rootDir, lastFileNum, 0); err != nil {
    48  		logger.Errorf("Error while scanning last file [file num=%d]: %s", lastFileNum, err)
    49  		return nil, err
    50  	}
    51  
    52  	if numBlocksInFile == 0 && lastFileNum > 0 {
    53  		secondLastFileNum := lastFileNum - 1
    54  		fileInfo := getFileInfoOrPanic(rootDir, secondLastFileNum)
    55  		logger.Debugf("Second last Block file info: FileName=[%s], FileSize=[%d]", fileInfo.Name(), fileInfo.Size())
    56  		if lastBlockBytes, _, _, err = scanForLastCompleteBlock(rootDir, secondLastFileNum, 0); err != nil {
    57  			logger.Errorf("Error while scanning second last file [file num=%d]: %s", secondLastFileNum, err)
    58  			return nil, err
    59  		}
    60  	}
    61  
    62  	if lastBlockBytes != nil {
    63  		if lastBlock, err = deserializeBlock(lastBlockBytes); err != nil {
    64  			logger.Errorf("Error deserializing last block: %s. Block bytes length = %d", err, len(lastBlockBytes))
    65  			return nil, err
    66  		}
    67  		lastBlockNumber = lastBlock.Header.Number
    68  	}
    69  
    70  	cpInfo := &checkpointInfo{
    71  		lastBlockNumber:          lastBlockNumber,
    72  		latestFileChunksize:      int(endOffsetLastBlock),
    73  		latestFileChunkSuffixNum: lastFileNum,
    74  		isChainEmpty:             lastFileNum == 0 && numBlocksInFile == 0,
    75  	}
    76  	logger.Debugf("Checkpoint info constructed from file system = %s", spew.Sdump(cpInfo))
    77  	return cpInfo, nil
    78  }
    79  
    80  func retrieveLastFileSuffix(rootDir string) (int, error) {
    81  	logger.Debugf("retrieveLastFileSuffix()")
    82  	biggestFileNum := -1
    83  	filesInfo, err := ioutil.ReadDir(rootDir)
    84  	if err != nil {
    85  		return -1, err
    86  	}
    87  	for _, fileInfo := range filesInfo {
    88  		name := fileInfo.Name()
    89  		if fileInfo.IsDir() || !isBlockFileName(name) {
    90  			logger.Debugf("Skipping File name = %s", name)
    91  			continue
    92  		}
    93  		fileSuffix := strings.TrimPrefix(name, blockfilePrefix)
    94  		fileNum, err := strconv.Atoi(fileSuffix)
    95  		if err != nil {
    96  			return -1, err
    97  		}
    98  		if fileNum > biggestFileNum {
    99  			biggestFileNum = fileNum
   100  		}
   101  	}
   102  	logger.Debugf("retrieveLastFileSuffix() - biggestFileNum = %d", biggestFileNum)
   103  	return biggestFileNum, err
   104  }
   105  
   106  func isBlockFileName(name string) bool {
   107  	return strings.HasPrefix(name, blockfilePrefix)
   108  }
   109  
   110  func getFileInfoOrPanic(rootDir string, fileNum int) os.FileInfo {
   111  	filePath := deriveBlockfilePath(rootDir, fileNum)
   112  	fileInfo, err := os.Lstat(filePath)
   113  	if err != nil {
   114  		panic(fmt.Errorf("Error in retrieving file info for file num = %d", fileNum))
   115  	}
   116  	return fileInfo
   117  }