Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose isUpgradeable method on mutable tree and unit test #17

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
expose isUpgradeable method on mutable tree and unit test
  • Loading branch information
p0mvn committed Feb 8, 2022
commit 364e68ba5418a930a1c288228d9241f63d44d4dc
11 changes: 9 additions & 2 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,21 @@ func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64,
return latestVersion, nil
}

// Returns true if the tree may be auto-upgraded, false otherwise
// An example of when an upgrade may be performed is when we are enaling fast storage for the first time or
// need to overwrite fast nodes due to mismatch with live state.
func (tree *MutableTree) IsUpgradeable() bool {
return !tree.ndb.hasUpgradedToFastStorage() || tree.ndb.shouldForceFastStorageUpgrade()
}

// enableFastStorageAndCommitIfNotEnabled if nodeDB doesn't mark fast storage as enabled, enable it, and commit the update.
// Checks whether the fast cache on disk matches latest live state. If not, deletes all existing fast nodes and repopulates them
// from latest tree.
func (tree *MutableTree) enableFastStorageAndCommitIfNotEnabled() (bool, error) {
shouldForceUpdate := tree.ndb.shouldForceFastStorageUpdate()
shouldForceUpdate := tree.ndb.shouldForceFastStorageUpgrade()
isFastStorageEnabled := tree.ndb.hasUpgradedToFastStorage()

if isFastStorageEnabled && !shouldForceUpdate {
if !tree.IsUpgradeable() {
return false, nil
}

Expand Down
14 changes: 12 additions & 2 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,11 @@ func TestUpgradeStorageToFast_LatestVersion_Success(t *testing.T) {
randomizeTreeAndMirror(t, tree, mirror)

// Enable fast storage
require.True(t, tree.IsUpgradeable())
enabled, err := tree.enableFastStorageAndCommitIfNotEnabled()
require.NoError(t, err)
require.True(t, enabled)
require.False(t, tree.IsUpgradeable())

require.True(t, tree.IsFastCacheEnabled())
}
Expand All @@ -699,10 +701,12 @@ func TestUpgradeStorageToFast_AlreadyUpgraded_Success(t *testing.T) {
randomizeTreeAndMirror(t, tree, mirror)

// Enable fast storage
require.True(t, tree.IsUpgradeable())
enabled, err := tree.enableFastStorageAndCommitIfNotEnabled()
require.NoError(t, err)
require.True(t, enabled)
require.True(t, tree.IsFastCacheEnabled())
require.False(t, tree.IsUpgradeable())

// Test enabling fast storage when already enabled
enabled, err = tree.enableFastStorageAndCommitIfNotEnabled()
Expand Down Expand Up @@ -799,7 +803,7 @@ func TestFastStorageReUpgradeProtection_NoForceUpgrade_Success(t *testing.T) {

// Ensure that the right branch of enableFastStorageAndCommitIfNotEnabled will be triggered
require.True(t, tree.IsFastCacheEnabled())
require.False(t, tree.ndb.shouldForceFastStorageUpdate())
require.False(t, tree.ndb.shouldForceFastStorageUpgrade())

enabled, err := tree.enableFastStorageAndCommitIfNotEnabled()
require.NoError(t, err)
Expand Down Expand Up @@ -886,7 +890,7 @@ func TestFastStorageReUpgradeProtection_ForceUpgradeFirstTime_NoForceSecondTime_

// Ensure that the right branch of enableFastStorageAndCommitIfNotEnabled will be triggered
require.True(t, tree.IsFastCacheEnabled())
require.True(t, tree.ndb.shouldForceFastStorageUpdate())
require.True(t, tree.ndb.shouldForceFastStorageUpgrade())

// Actual method under test
enabled, err := tree.enableFastStorageAndCommitIfNotEnabled()
Expand All @@ -904,16 +908,19 @@ func TestUpgradeStorageToFast_Integration_Upgraded_FastIterator_Success(t *testi
tree, mirror := setupTreeAndMirrorForUpgrade(t)

require.False(t, tree.IsFastCacheEnabled())
require.True(t, tree.IsUpgradeable())

// Should auto enable in save version
_, _, err := tree.SaveVersion()
require.NoError(t, err)

require.True(t, tree.IsFastCacheEnabled())
require.False(t, tree.IsUpgradeable())

sut, _ := NewMutableTree(tree.ndb.db, 1000)

require.False(t, sut.IsFastCacheEnabled())
require.False(t, sut.IsUpgradeable()) // upgraded in save version

// Load version - should auto enable fast storage
version, err := sut.Load()
Expand Down Expand Up @@ -954,16 +961,19 @@ func TestUpgradeStorageToFast_Integration_Upgraded_GetFast_Success(t *testing.T)
tree, mirror := setupTreeAndMirrorForUpgrade(t)

require.False(t, tree.IsFastCacheEnabled())
require.True(t, tree.IsUpgradeable())

// Should auto enable in save version
_, _, err := tree.SaveVersion()
require.NoError(t, err)

require.True(t, tree.IsFastCacheEnabled())
require.False(t, tree.IsUpgradeable())

sut, _ := NewMutableTree(tree.ndb.db, 1000)

require.False(t, sut.IsFastCacheEnabled())
require.False(t, sut.IsUpgradeable()) // upgraded in save version

// LazyLoadVersion - should auto enable fast storage
version, err := sut.LazyLoadVersion(1)
Expand Down
8 changes: 4 additions & 4 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,16 @@ func (ndb *nodeDB) getStorageVersion() string {
return ndb.storageVersion
}

// Returns true if the upgrade to fast storage has occurred, false otherwise.
// Returns true if the upgrade to latest storage version has been performed, false otherwise.
func (ndb *nodeDB) hasUpgradedToFastStorage() bool {
return ndb.getStorageVersion() >= fastStorageVersionValue
}

// Returns true if the upgrade to fast storage has occurred but it does not match the live state, false otherwise.
// When the live state is not matched, we must force reupgrade.
// We determine this by checking the version of the live state and the version of the live state wheb
// fast storage was updated on disk the last time.
func (ndb *nodeDB) shouldForceFastStorageUpdate() bool {
// We determine this by checking the version of the live state and the version of the live state when
// latest storage was updated on disk the last time.
func (ndb *nodeDB) shouldForceFastStorageUpgrade() bool {
versions := strings.Split(ndb.storageVersion, fastStorageVersionDelimiter)

if len(versions) == 2 {
Expand Down
10 changes: 5 additions & 5 deletions nodedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestShouldForceFastStorageUpdate_DefaultVersion_True(t *testing.T) {
ndb.storageVersion = defaultStorageVersionValue
ndb.latestVersion = 100

require.False(t, ndb.shouldForceFastStorageUpdate())
require.False(t, ndb.shouldForceFastStorageUpgrade())
}

func TestShouldForceFastStorageUpdate_FastVersion_Greater_True(t *testing.T) {
Expand All @@ -194,7 +194,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Greater_True(t *testing.T) {
ndb.latestVersion = 100
ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion + 1))

require.True(t, ndb.shouldForceFastStorageUpdate())
require.True(t, ndb.shouldForceFastStorageUpgrade())
}

func TestShouldForceFastStorageUpdate_FastVersion_Smaller_True(t *testing.T) {
Expand All @@ -203,7 +203,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Smaller_True(t *testing.T) {
ndb.latestVersion = 100
ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion - 1))

require.True(t, ndb.shouldForceFastStorageUpdate())
require.True(t, ndb.shouldForceFastStorageUpgrade())
}

func TestShouldForceFastStorageUpdate_FastVersion_Match_False(t *testing.T) {
Expand All @@ -212,7 +212,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Match_False(t *testing.T) {
ndb.latestVersion = 100
ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion))

require.False(t, ndb.shouldForceFastStorageUpdate())
require.False(t, ndb.shouldForceFastStorageUpgrade())
}

func TestIsFastStorageEnabled_True(t *testing.T) {
Expand All @@ -230,7 +230,7 @@ func TestIsFastStorageEnabled_False(t *testing.T) {
ndb.latestVersion = 100
ndb.storageVersion = defaultStorageVersionValue

require.False(t, ndb.shouldForceFastStorageUpdate())
require.False(t, ndb.shouldForceFastStorageUpgrade())
}

func makeHashes(b *testing.B, seed int64) [][]byte {
Expand Down