github.com/true-sqn/fabric@v2.1.1+incompatible/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  	"io/ioutil"
    11  	"os"
    12  	"strconv"
    13  	"strings"
    14  
    15  	"github.com/davecgh/go-spew/spew"
    16  	"github.com/hyperledger/fabric-protos-go/common"
    17  	"github.com/pkg/errors"
    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 scanning last 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 scanning second last 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  // binarySearchFileNumForBlock locates the file number that contains the given block number.
    81  // This function assumes that the caller invokes this function with a block number that has been commited
    82  // For any uncommitted block, this function returns the last file present
    83  func binarySearchFileNumForBlock(rootDir string, blockNum uint64) (int, error) {
    84  	cpInfo, err := constructCheckpointInfoFromBlockFiles(rootDir)
    85  	if err != nil {
    86  		return -1, err
    87  	}
    88  
    89  	beginFile := 0
    90  	endFile := cpInfo.latestFileChunkSuffixNum
    91  
    92  	for endFile != beginFile {
    93  		searchFile := beginFile + (endFile-beginFile)/2 + 1
    94  		n, err := retriveFirstBlockNumFromFile(rootDir, searchFile)
    95  		if err != nil {
    96  			return -1, err
    97  		}
    98  		switch {
    99  		case n == blockNum:
   100  			return searchFile, nil
   101  		case n > blockNum:
   102  			endFile = searchFile - 1
   103  		case n < blockNum:
   104  			beginFile = searchFile
   105  		}
   106  	}
   107  	return beginFile, nil
   108  }
   109  
   110  func retriveFirstBlockNumFromFile(rootDir string, fileNum int) (uint64, error) {
   111  	s, err := newBlockfileStream(rootDir, fileNum, 0)
   112  	if err != nil {
   113  		return 0, err
   114  	}
   115  	defer s.close()
   116  	bb, err := s.nextBlockBytes()
   117  	if err != nil {
   118  		return 0, err
   119  	}
   120  	blockInfo, err := extractSerializedBlockInfo(bb)
   121  	if err != nil {
   122  		return 0, err
   123  	}
   124  	return blockInfo.blockHeader.Number, nil
   125  }
   126  
   127  func retrieveLastFileSuffix(rootDir string) (int, error) {
   128  	logger.Debugf("retrieveLastFileSuffix()")
   129  	biggestFileNum := -1
   130  	filesInfo, err := ioutil.ReadDir(rootDir)
   131  	if err != nil {
   132  		return -1, errors.Wrapf(err, "error reading dir %s", rootDir)
   133  	}
   134  	for _, fileInfo := range filesInfo {
   135  		name := fileInfo.Name()
   136  		if fileInfo.IsDir() || !isBlockFileName(name) {
   137  			logger.Debugf("Skipping File name = %s", name)
   138  			continue
   139  		}
   140  		fileSuffix := strings.TrimPrefix(name, blockfilePrefix)
   141  		fileNum, err := strconv.Atoi(fileSuffix)
   142  		if err != nil {
   143  			return -1, err
   144  		}
   145  		if fileNum > biggestFileNum {
   146  			biggestFileNum = fileNum
   147  		}
   148  	}
   149  	logger.Debugf("retrieveLastFileSuffix() - biggestFileNum = %d", biggestFileNum)
   150  	return biggestFileNum, err
   151  }
   152  
   153  func isBlockFileName(name string) bool {
   154  	return strings.HasPrefix(name, blockfilePrefix)
   155  }
   156  
   157  func getFileInfoOrPanic(rootDir string, fileNum int) os.FileInfo {
   158  	filePath := deriveBlockfilePath(rootDir, fileNum)
   159  	fileInfo, err := os.Lstat(filePath)
   160  	if err != nil {
   161  		panic(errors.Wrapf(err, "error retrieving file info for file number %d", fileNum))
   162  	}
   163  	return fileInfo
   164  }