Skip to content

Commit

Permalink
Move data-format-exception at a common place
Browse files Browse the repository at this point in the history
This CR moves data format exception at a common place so that it can be
used both in leveldb and couchdb

FAB-15491 #done

Change-Id: I355cbc7d851511c39a100b7f098f884623431104
Signed-off-by: manish <[email protected]>
  • Loading branch information
manish-sethi committed Oct 19, 2019
1 parent 11bb0ac commit 365cd26
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 79 deletions.
15 changes: 0 additions & 15 deletions common/ledger/dataformat/constants.go

This file was deleted.

37 changes: 37 additions & 0 deletions common/ledger/dataformat/dataformats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package dataformat

import "fmt"

const (
// Version1x specifies the data format in version 1.x
Version1x = ""

// Version20 specifies the data format in version 2.0
Version20 = "2.0"
)

// ErrVersionMismatch is returned if it is detected that the version of the format recorded in
// the internal database is different from what is specified in the `Conf` that is used for opening the db
type ErrVersionMismatch struct {
DBInfo string
ExpectedVersion string
Version string
}

func (e *ErrVersionMismatch) Error() string {
return fmt.Sprintf("unexpected format. db info = [%s], data format = [%s], expected format = [%s]",
e.DBInfo, e.Version, e.ExpectedVersion,
)
}

// IsVersionMismatch returns true if err is an ErrVersionMismatch
func IsVersionMismatch(err error) bool {
_, ok := err.(*ErrVersionMismatch)
return ok
}
28 changes: 6 additions & 22 deletions common/ledger/util/leveldbhelper/leveldb_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"sync"

"github.com/hyperledger/fabric/common/ledger/dataformat"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
)
Expand All @@ -37,26 +38,6 @@ type Conf struct {
ExpectedFormatVersion string
}

// ErrFormatVersionMismatch is returned if it is detected that the version of the format recorded in
// the internal database is different from what is specified in the `Conf` that is used for opening the db
type ErrFormatVersionMismatch struct {
DBPath string
ExpectedFormatVersion string
DataFormatVersion string
}

func (e *ErrFormatVersionMismatch) Error() string {
return fmt.Sprintf("unexpected format. db path = [%s], data format = [%s], expected format = [%s]",
e.DBPath, e.DataFormatVersion, e.ExpectedFormatVersion,
)
}

// IsFormatVersionMismatch returns true if err is an errFormatMismatch
func IsFormatVersionMismatch(err error) bool {
_, ok := err.(*ErrFormatVersionMismatch)
return ok
}

// Provider enables to use a single leveldb as multiple logical leveldbs
type Provider struct {
db *DB
Expand Down Expand Up @@ -114,9 +95,12 @@ func openDBAndCheckFormat(conf *Conf) (d *DB, e error) {
if !bytes.Equal(formatVersion, []byte(conf.ExpectedFormatVersion)) {
logger.Errorf("The db at path [%s] contains data in unexpected format. expected data format = [%s] (%#v), data format = [%s] (%#v).",
conf.DBPath, conf.ExpectedFormatVersion, []byte(conf.ExpectedFormatVersion), formatVersion, formatVersion)
return nil, &ErrFormatVersionMismatch{ExpectedFormatVersion: conf.ExpectedFormatVersion, DataFormatVersion: string(formatVersion), DBPath: conf.DBPath}
return nil, &dataformat.ErrVersionMismatch{
ExpectedVersion: conf.ExpectedFormatVersion,
Version: string(formatVersion),
DBInfo: fmt.Sprintf("leveldb at [%s]", conf.DBPath),
}
}

logger.Debug("format is latest, nothing to do")
return db, nil
}
Expand Down
12 changes: 7 additions & 5 deletions common/ledger/util/leveldbhelper/leveldb_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"testing"

"github.com/hyperledger/fabric/common/ledger/dataformat"

"github.com/hyperledger/fabric/common/flogging"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -92,7 +94,7 @@ func TestFormatCheck(t *testing.T) {
dataFormat string
dataExists bool
expectedFormat string
expectedErr *ErrFormatVersionMismatch
expectedErr *dataformat.ErrVersionMismatch
}{
{
dataFormat: "",
Expand All @@ -116,7 +118,7 @@ func TestFormatCheck(t *testing.T) {
dataFormat: "",
dataExists: true,
expectedFormat: "2.0",
expectedErr: &ErrFormatVersionMismatch{DataFormatVersion: "", ExpectedFormatVersion: "2.0"},
expectedErr: &dataformat.ErrVersionMismatch{Version: "", ExpectedVersion: "2.0"},
},
{
dataFormat: "2.0",
Expand All @@ -128,7 +130,7 @@ func TestFormatCheck(t *testing.T) {
dataFormat: "2.0",
dataExists: true,
expectedFormat: "3.0",
expectedErr: &ErrFormatVersionMismatch{DataFormatVersion: "2.0", ExpectedFormatVersion: "3.0"},
expectedErr: &dataformat.ErrVersionMismatch{Version: "2.0", ExpectedVersion: "3.0"},
},
}

Expand All @@ -141,7 +143,7 @@ func TestFormatCheck(t *testing.T) {
}
}

func testFormatCheck(t *testing.T, dataFormat, expectedFormat string, dataExists bool, expectedErr *ErrFormatVersionMismatch) {
func testFormatCheck(t *testing.T, dataFormat, expectedFormat string, dataExists bool, expectedErr *dataformat.ErrVersionMismatch) {
assert.NoError(t, os.RemoveAll(testDBPath))
defer func() {
assert.NoError(t, os.RemoveAll(testDBPath))
Expand All @@ -161,7 +163,7 @@ func testFormatCheck(t *testing.T, dataFormat, expectedFormat string, dataExists
p.Close()
p, err = NewProvider(&Conf{DBPath: testDBPath, ExpectedFormatVersion: expectedFormat})
if expectedErr != nil {
expectedErr.DBPath = testDBPath
expectedErr.DBInfo = fmt.Sprintf("leveldb at [%s]", testDBPath)
assert.Equal(t, err, expectedErr)
return
}
Expand Down
16 changes: 12 additions & 4 deletions core/ledger/kvledger/kv_ledger_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func NewProvider(initializer *ledger.Initializer) (pr *Provider, e error) {
defer func() {
if e != nil {
p.Close()
if errFormatMismatch, ok := e.(*leveldbhelper.ErrFormatVersionMismatch); ok {
if errFormatMismatch.DataFormatVersion == dataformat.Version1x && errFormatMismatch.ExpectedFormatVersion == dataformat.Version20 {
if errFormatMismatch, ok := e.(*dataformat.ErrVersionMismatch); ok {
if errFormatMismatch.Version == dataformat.Version1x && errFormatMismatch.ExpectedVersion == dataformat.Version20 {
logger.Errorf("Please execute the 'peer node upgrade-dbs' command to upgrade the database format: %s", errFormatMismatch)
} else {
logger.Errorf("Please check the Fabric version matches the ledger data format: %s", errFormatMismatch)
Expand Down Expand Up @@ -402,7 +402,11 @@ func openIDStore(path string) (s *idStore, e error) {
if !bytes.Equal(formatVersion, expectedFormatBytes) {
logger.Errorf("The db at path [%s] contains data in unexpected format. expected data format = [%s] (%#v), data format = [%s] (%#v).",
path, dataformat.Version20, expectedFormatBytes, formatVersion, formatVersion)
return nil, &leveldbhelper.ErrFormatVersionMismatch{ExpectedFormatVersion: dataformat.Version20, DataFormatVersion: string(formatVersion), DBPath: path}
return nil, &dataformat.ErrVersionMismatch{
ExpectedVersion: dataformat.Version20,
Version: string(formatVersion),
DBInfo: fmt.Sprintf("leveldb for channel-IDs at [%s]", path),
}
}
return &idStore{db, path}, nil
}
Expand All @@ -418,7 +422,11 @@ func (s *idStore) upgradeFormat() error {
return nil
}
if format != nil {
err = &leveldbhelper.ErrFormatVersionMismatch{ExpectedFormatVersion: "", DataFormatVersion: string(format), DBPath: s.dbPath}
err = &dataformat.ErrVersionMismatch{
ExpectedVersion: "",
Version: string(format),
DBInfo: fmt.Sprintf("leveldb for channel-IDs at [%s]", s.dbPath),
}
logger.Errorf("Failed to upgrade format [%#v] to new format [%#v]: %s", format, idStoreFormatBytes, err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/kvledger/kv_ledger_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestNewProviderIdStoreFormatError(t *testing.T) {
Config: conf,
},
)
require.EqualError(t, err, fmt.Sprintf("unexpected format. db path = [%s], data format = [], expected format = [2.0]", LedgerProviderPath(conf.RootFSPath)))
require.EqualError(t, err, fmt.Sprintf("unexpected format. db info = [leveldb for channel-IDs at [%s]], data format = [], expected format = [2.0]", LedgerProviderPath(conf.RootFSPath)))
}

func TestUpgradeIDStoreFormatDBError(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions core/ledger/kvledger/tests/v11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestV11(t *testing.T) {
idStorePath := kvledger.LedgerProviderPath(ledgerFSRoot)
assert.PanicsWithValue(
t,
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db path = [%s], data format = [], expected format = [2.0]",
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db info = [leveldb for channel-IDs at [%s]], data format = [], expected format = [2.0]",
idStorePath),
func() { env.initLedgerMgmt() },
"A panic should occur because idstore is in format 1.x",
Expand All @@ -43,7 +43,7 @@ func TestV11(t *testing.T) {
blkIndexPath := path.Join(kvledger.BlockStorePath(ledgerFSRoot), "index")
require.PanicsWithValue(
t,
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db path = [%s], data format = [], expected format = [2.0]",
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db info = [leveldb at [%s]], data format = [], expected format = [2.0]",
blkIndexPath),
func() { env.initLedgerMgmt() },
"A panic should occur because block store index is in format 1.x",
Expand All @@ -54,7 +54,7 @@ func TestV11(t *testing.T) {
historyDBPath := kvledger.HistoryDBPath(ledgerFSRoot)
require.PanicsWithValue(
t,
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db path = [%s], data format = [], expected format = [2.0]",
fmt.Sprintf("Error in instantiating ledger provider: unexpected format. db info = [leveldb at [%s]], data format = [], expected format = [2.0]",
historyDBPath),
func() { env.initLedgerMgmt() },
"A panic should occur because history is in format 1.x",
Expand All @@ -66,7 +66,7 @@ func TestV11(t *testing.T) {
require.PanicsWithValue(
t,
fmt.Sprintf(
"Error in instantiating ledger provider: unexpected format. db path = [%s], data format = [], expected format = [2.0]",
"Error in instantiating ledger provider: unexpected format. db info = [leveldb at [%s]], data format = [], expected format = [2.0]",
stateLevelDBPath,
),
func() { env.initLedgerMgmt() },
Expand Down
20 changes: 4 additions & 16 deletions core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ const (
dataformatVersionDocID = "dataformatVersion"
)

// ErrFormatVersionMismatch is returned if it is detected that the version of the format recorded in
// the internal database is not "2.0"
type ErrFormatVersionMismatch struct {
ExpectedFormatVersion string
DataFormatVersion string
}

func (e *ErrFormatVersionMismatch) Error() string {
return fmt.Sprintf("unexpected format. data format = [%s], expected format = [%s]",
e.DataFormatVersion, e.ExpectedFormatVersion,
)
}

// VersionedDBProvider implements interface VersionedDBProvider
type VersionedDBProvider struct {
couchInstance *couchdb.CouchInstance
Expand Down Expand Up @@ -98,9 +85,10 @@ func checkExpectedDataformatVersion(couchInstance *couchdb.CouchInstance) error
return err
}
if dataformatVersion != dataformat.Version20 {
return &ErrFormatVersionMismatch{
ExpectedFormatVersion: dataformat.Version20,
DataFormatVersion: dataformatVersion,
return &dataformat.ErrVersionMismatch{
DBInfo: "CouchDB for state database",
ExpectedVersion: dataformat.Version20,
Version: dataformatVersion,
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,15 +808,19 @@ func assertQueryResults(t *testing.T, results []*couchdb.QueryResult, expectedId

func TestFormatCheck(t *testing.T) {
testCases := []struct {
dataFormat string // precondition
dataExists bool // precondition
expectedFormat string // postcondition
expectedErr *ErrFormatVersionMismatch // postcondition
dataFormat string // precondition
dataExists bool // precondition
expectedFormat string // postcondition
expectedErr *dataformat.ErrVersionMismatch // postcondition
}{
{
dataFormat: "",
dataExists: true,
expectedErr: &ErrFormatVersionMismatch{DataFormatVersion: "", ExpectedFormatVersion: "2.0"},
dataFormat: "",
dataExists: true,
expectedErr: &dataformat.ErrVersionMismatch{
DBInfo: "CouchDB for state database",
Version: "",
ExpectedVersion: "2.0",
},
expectedFormat: "does not matter as the test should not reach to check this",
},

Expand All @@ -842,9 +846,13 @@ func TestFormatCheck(t *testing.T) {
},

{
dataFormat: "3.0",
dataExists: true,
expectedErr: &ErrFormatVersionMismatch{DataFormatVersion: "3.0", ExpectedFormatVersion: dataformat.Version20},
dataFormat: "3.0",
dataExists: true,
expectedErr: &dataformat.ErrVersionMismatch{
DBInfo: "CouchDB for state database",
Version: "3.0",
ExpectedVersion: dataformat.Version20,
},
expectedFormat: "does not matter as the test should not reach to check this",
},
}
Expand All @@ -858,7 +866,7 @@ func TestFormatCheck(t *testing.T) {
}
}

func testFormatCheck(t *testing.T, dataFormat string, dataExists bool, expectedErr *ErrFormatVersionMismatch, expectedFormat string) {
func testFormatCheck(t *testing.T, dataFormat string, dataExists bool, expectedErr *dataformat.ErrVersionMismatch, expectedFormat string) {
redoPath, err := ioutil.TempDir("", "redoPath")
require.NoError(t, err)
defer os.RemoveAll(redoPath)
Expand Down
7 changes: 6 additions & 1 deletion core/ledger/kvledger/upgrade_dbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package kvledger

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -76,7 +77,11 @@ func TestUpgradeIDStoreWrongFormat(t *testing.T) {
require.NoError(t, err)

err = UpgradeIDStoreFormat(conf.RootFSPath)
expectedErr := &leveldbhelper.ErrFormatVersionMismatch{ExpectedFormatVersion: "", DataFormatVersion: "x.0", DBPath: LedgerProviderPath(conf.RootFSPath)}
expectedErr := &dataformat.ErrVersionMismatch{
ExpectedVersion: "",
Version: "x.0",
DBInfo: fmt.Sprintf("leveldb for channel-IDs at [%s]", LedgerProviderPath(conf.RootFSPath)),
}
require.EqualError(t, err, expectedErr.Error())
}

Expand Down

0 comments on commit 365cd26

Please sign in to comment.