Skip to content

Commit

Permalink
[FAB-17862] Allow upgrade-dbs to drop all dbs in multiple runs (#1236)
Browse files Browse the repository at this point in the history
Administrators may need to rerun upgrade-dbs in some cases, e.g.,
if they forgot to pass the CouchDB variable CORE_LEDGER_STATE_STATEDATABASE,
it will not drop the CouchDB databases as part of upgrade-dbs.
Therefore, upgrade-dbs should be able to drop all dbs when
running multiple times.

Signed-off-by: Wenjian Qiao <wenjianq@gmail.com>
  • Loading branch information
wenjianqiao committed May 11, 2020
1 parent 299e170 commit 14e5f8a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/ledger/kvledger/kv_ledger_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,15 @@ func (s *idStore) checkUpgradeEligibility() (bool, error) {
return false, err
}
if emptydb {
logger.Warnf("Ledger database %s is empty, nothing to upgrade.", s.dbPath)
logger.Warnf("Ledger database %s is empty, nothing to upgrade", s.dbPath)
return false, nil
}
format, err := s.db.Get(formatKey)
if err != nil {
return false, err
}
if bytes.Equal(format, []byte(dataformat.CurrentFormat)) {
logger.Info("Ledger data format is current, nothing to upgrade.")
logger.Debugf("Ledger database %s has current data format, nothing to upgrade", s.dbPath)
return false, nil
}
if !bytes.Equal(format, []byte(dataformat.PreviousFormat)) {
Expand Down
6 changes: 0 additions & 6 deletions core/ledger/kvledger/tests/v20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ func TestV20SampleLedger(t *testing.T) {
ledgerFSRoot := env.initializer.Config.RootFSPath
require.NoError(t, testutil.Unzip("testdata/v20/sample_ledgers/ledgersData.zip", ledgerFSRoot, false))

// The UpgradeDBs call is not really needed. It is added to test that dbs are not deleted if someone calls UpgradeDBs against a 2.0 ledger.
require.NoError(t, kvledger.UpgradeDBs(env.initializer.Config))
rebuildable := rebuildableStatedb | rebuildableBookkeeper | rebuildableConfigHistory | rebuildableHistoryDB | rebuildableBlockIndex
env.verifyRebuilablesExist(rebuildable)

env.initLedgerMgmt()

h1 := env.newTestHelperOpenLgr("testchannel", t)
dataHelper.verify(h1)

Expand Down
24 changes: 8 additions & 16 deletions core/ledger/kvledger/upgrade_dbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,23 @@ func UpgradeDBs(config *ledger.Config) error {
defer fileLock.Unlock()

logger.Infof("Ledger data folder from config = [%s]", rootFSPath)
dbPath := LedgerProviderPath(rootFSPath)
db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath})
db.Open()
defer db.Close()

idStore := &idStore{db, dbPath}
eligible, err := idStore.checkUpgradeEligibility()
if err != nil {
return err
}
if !eligible {
return nil
}

if config.StateDBConfig.StateDatabase == "CouchDB" {
if err = statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
return err
}
}
if err = dropDBs(rootFSPath); err != nil {
if err := dropDBs(rootFSPath); err != nil {
return err
}
if err = blkstorage.DeleteBlockStoreIndex(BlockStorePath(rootFSPath)); err != nil {
if err := blkstorage.DeleteBlockStoreIndex(BlockStorePath(rootFSPath)); err != nil {
return err
}

dbPath := LedgerProviderPath(rootFSPath)
db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath})
db.Open()
defer db.Close()
idStore := &idStore{db, dbPath}
return idStore.upgradeFormat()
}

0 comments on commit 14e5f8a

Please sign in to comment.