-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
reset.go
244 lines (229 loc) · 6.76 KB
/
reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fsblkstorage
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"github.com/hyperledger/fabric/common/ledger/util"
)
func ResetBlockStore(blockStorageDir string) error {
conf := &Conf{blockStorageDir: blockStorageDir}
indexDir := conf.getIndexDir()
logger.Infof("Dropping the index dir [%s]... if present", indexDir)
if err := os.RemoveAll(indexDir); err != nil {
return err
}
chainsDir := conf.getChainsDir()
chainsDirExists, err := pathExists(chainsDir)
if err != nil {
return err
}
if !chainsDirExists {
logger.Infof("Dir [%s] missing... exiting", chainsDir)
return nil
}
ledgerIDs, err := util.ListSubdirs(chainsDir)
if err != nil {
return err
}
if len(ledgerIDs) == 0 {
logger.Info("No ledgers found.. exiting")
return nil
}
logger.Infof("Found ledgers - %s", ledgerIDs)
for _, ledgerID := range ledgerIDs {
ledgerDir := conf.getLedgerBlockDir(ledgerID)
if err := recordHeightIfGreaterThanPreviousRecording(ledgerDir); err != nil {
return err
}
if err := resetToGenesisBlk(ledgerDir); err != nil {
return err
}
}
return nil
}
func resetToGenesisBlk(ledgerDir string) error {
logger.Infof("Resetting ledger [%s] to genesis block", ledgerDir)
lastFileNum, err := retrieveLastFileSuffix(ledgerDir)
logger.Infof("lastFileNum = [%d]", lastFileNum)
if err != nil {
return err
}
if lastFileNum < 0 {
return nil
}
zeroFilePath, genesisBlkEndOffset, err := retrieveGenesisBlkOffsetAndMakeACopy(ledgerDir)
if err != nil {
return err
}
for lastFileNum > 0 {
filePath := deriveBlockfilePath(ledgerDir, lastFileNum)
logger.Infof("Deleting file number = [%d]", lastFileNum)
if err := os.Remove(filePath); err != nil {
return err
}
lastFileNum--
}
logger.Infof("Truncating file [%s] to offset [%d]", zeroFilePath, genesisBlkEndOffset)
return os.Truncate(zeroFilePath, genesisBlkEndOffset)
}
func retrieveGenesisBlkOffsetAndMakeACopy(ledgerDir string) (string, int64, error) {
blockfilePath := deriveBlockfilePath(ledgerDir, 0)
blockfileStream, err := newBlockfileStream(ledgerDir, 0, 0)
if err != nil {
return "", -1, err
}
genesisBlockBytes, _, err := blockfileStream.nextBlockBytesAndPlacementInfo()
if err != nil {
return "", -1, err
}
endOffsetGenesisBlock := blockfileStream.currentOffset
blockfileStream.close()
if err := assertIsGenesisBlock(genesisBlockBytes); err != nil {
return "", -1, err
}
// just for an extra safety make a backup of genesis block
if err := ioutil.WriteFile(path.Join(ledgerDir, "__backupGenesisBlockBytes"), genesisBlockBytes, 0640); err != nil {
return "", -1, err
}
logger.Infof("Genesis block backed up. Genesis block info file [%s], offset [%d]", blockfilePath, endOffsetGenesisBlock)
return blockfilePath, endOffsetGenesisBlock, nil
}
func assertIsGenesisBlock(blockBytes []byte) error {
block, err := deserializeBlock(blockBytes)
if err != nil {
return err
}
if block.Header.Number != 0 || block.Header.Hash() == nil {
return fmt.Errorf("The supplied bytes are not of genesis block. blockNum=%d, blockHash=%x", block.Header.Number, block.Header.Hash())
}
return nil
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
const (
fileNamePreRestHt = "__preResetHeight"
)
// recordHeightIfGreaterThanPreviousRecording creates a file "__preResetHeight" in the ledger's
// directory. This file contains human readable string for the current block height. This function
// only overwrites this information if the current block height is higher than the one recorded in
// the existing file (if present). This helps in achieving fail-safe behviour of reset utility
func recordHeightIfGreaterThanPreviousRecording(ledgerDir string) error {
logger.Infof("Preparing to record current height for ledger at [%s]", ledgerDir)
checkpointInfo, err := constructCheckpointInfoFromBlockFiles(ledgerDir)
if err != nil {
return err
}
logger.Infof("Loaded current info from blockfiles %#v", checkpointInfo)
preResetHtFile := path.Join(ledgerDir, fileNamePreRestHt)
exists, err := pathExists(preResetHtFile)
logger.Infof("preResetHtFile already exists? = %t", exists)
if err != nil {
return err
}
previuoslyRecordedHt := uint64(0)
if exists {
htBytes, err := ioutil.ReadFile(preResetHtFile)
if err != nil {
return err
}
if previuoslyRecordedHt, err = strconv.ParseUint(string(htBytes), 10, 64); err != nil {
return err
}
logger.Infof("preResetHtFile contains height = %d", previuoslyRecordedHt)
}
currentHt := checkpointInfo.lastBlockNumber + 1
if currentHt > previuoslyRecordedHt {
logger.Infof("Recording current height [%d]", currentHt)
return ioutil.WriteFile(preResetHtFile,
[]byte(strconv.FormatUint(currentHt, 10)),
0640,
)
}
logger.Infof("Not recording current height [%d] since this is less than previously recorded height [%d]",
currentHt, previuoslyRecordedHt)
return nil
}
func LoadPreResetHeight(blockStorageDir string) (map[string]uint64, error) {
logger.Info("Loading Pre-reset heights")
preResetFilesMap, err := preRestHtFiles(blockStorageDir)
if err != nil {
return nil, err
}
m := map[string]uint64{}
for ledgerID, filePath := range preResetFilesMap {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
previuoslyRecordedHt, err := strconv.ParseUint(string(bytes), 10, 64)
if err != nil {
return nil, err
}
m[ledgerID] = previuoslyRecordedHt
}
logger.Info("Pre-reset heights loaded")
return m, nil
}
func ClearPreResetHeight(blockStorageDir string) error {
logger.Info("Clearing Pre-reset heights")
preResetFilesMap, err := preRestHtFiles(blockStorageDir)
if err != nil {
return err
}
for _, filePath := range preResetFilesMap {
if err := os.Remove(filePath); err != nil {
return err
}
}
logger.Info("Cleared off Pre-reset heights")
return nil
}
func preRestHtFiles(blockStorageDir string) (map[string]string, error) {
conf := &Conf{blockStorageDir: blockStorageDir}
chainsDir := conf.getChainsDir()
chainsDirExists, err := pathExists(chainsDir)
if err != nil {
return nil, err
}
if !chainsDirExists {
logger.Infof("Dir [%s] missing... exiting", chainsDir)
return nil, err
}
ledgerIDs, err := util.ListSubdirs(chainsDir)
if err != nil {
return nil, err
}
if len(ledgerIDs) == 0 {
logger.Info("No ledgers found.. exiting")
return nil, nil
}
logger.Infof("Found ledgers - %s", ledgerIDs)
m := map[string]string{}
for _, ledgerID := range ledgerIDs {
ledgerDir := conf.getLedgerBlockDir(ledgerID)
file := path.Join(ledgerDir, fileNamePreRestHt)
exists, err := pathExists(file)
if err != nil {
return nil, err
}
if !exists {
continue
}
m[ledgerID] = file
}
return m, nil
}