mirror of https://github.com/hyperledger/besu
Bonsai keyvalue refactor (#6404)
Refactor BonsaiWorldStateKeyValueStorage to use a FlatDbStrategyProvider Signed-off-by: Jason Frame <jason.frame@consensys.net>pull/6431/head
parent
f81d5445f1
commit
94d86afb40
@ -0,0 +1,105 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.ethereum.trie.bonsai.storage.flat; |
||||
|
||||
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE; |
||||
|
||||
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; |
||||
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode; |
||||
import org.hyperledger.besu.plugin.services.MetricsSystem; |
||||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage; |
||||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction; |
||||
|
||||
import java.nio.charset.StandardCharsets; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
public class FlatDbStrategyProvider { |
||||
private static final Logger LOG = LoggerFactory.getLogger(FlatDbStrategyProvider.class); |
||||
|
||||
// 0x666C61744462537461747573
|
||||
public static final byte[] FLAT_DB_MODE = "flatDbStatus".getBytes(StandardCharsets.UTF_8); |
||||
private final MetricsSystem metricsSystem; |
||||
protected FlatDbMode flatDbMode; |
||||
protected FlatDbStrategy flatDbStrategy; |
||||
|
||||
public FlatDbStrategyProvider( |
||||
final MetricsSystem metricsSystem, final DataStorageConfiguration dataStorageConfiguration) { |
||||
this.metricsSystem = metricsSystem; |
||||
} |
||||
|
||||
public void loadFlatDbStrategy(final SegmentedKeyValueStorage composedWorldStateStorage) { |
||||
// derive our flatdb strategy from db or default:
|
||||
var newFlatDbMode = deriveFlatDbStrategy(composedWorldStateStorage); |
||||
|
||||
// if flatDbMode is not loaded or has changed, reload flatDbStrategy
|
||||
if (this.flatDbMode == null || !this.flatDbMode.equals(newFlatDbMode)) { |
||||
this.flatDbMode = newFlatDbMode; |
||||
if (flatDbMode == FlatDbMode.FULL) { |
||||
this.flatDbStrategy = new FullFlatDbStrategy(metricsSystem); |
||||
} else { |
||||
this.flatDbStrategy = new PartialFlatDbStrategy(metricsSystem); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private FlatDbMode deriveFlatDbStrategy( |
||||
final SegmentedKeyValueStorage composedWorldStateStorage) { |
||||
var flatDbMode = |
||||
FlatDbMode.fromVersion( |
||||
composedWorldStateStorage |
||||
.get(TRIE_BRANCH_STORAGE, FLAT_DB_MODE) |
||||
.map(Bytes::wrap) |
||||
.orElse(FlatDbMode.PARTIAL.getVersion())); |
||||
LOG.info("Bonsai flat db mode found {}", flatDbMode); |
||||
|
||||
return flatDbMode; |
||||
} |
||||
|
||||
public FlatDbStrategy getFlatDbStrategy( |
||||
final SegmentedKeyValueStorage composedWorldStateStorage) { |
||||
if (flatDbStrategy == null) { |
||||
loadFlatDbStrategy(composedWorldStateStorage); |
||||
} |
||||
return flatDbStrategy; |
||||
} |
||||
|
||||
public void upgradeToFullFlatDbMode(final SegmentedKeyValueStorage composedWorldStateStorage) { |
||||
final SegmentedKeyValueStorageTransaction transaction = |
||||
composedWorldStateStorage.startTransaction(); |
||||
// TODO: consider ARCHIVE mode
|
||||
transaction.put( |
||||
TRIE_BRANCH_STORAGE, FLAT_DB_MODE, FlatDbMode.FULL.getVersion().toArrayUnsafe()); |
||||
transaction.commit(); |
||||
loadFlatDbStrategy(composedWorldStateStorage); // force reload of flat db reader strategy
|
||||
} |
||||
|
||||
public void downgradeToPartialFlatDbMode( |
||||
final SegmentedKeyValueStorage composedWorldStateStorage) { |
||||
final SegmentedKeyValueStorageTransaction transaction = |
||||
composedWorldStateStorage.startTransaction(); |
||||
transaction.put( |
||||
TRIE_BRANCH_STORAGE, FLAT_DB_MODE, FlatDbMode.PARTIAL.getVersion().toArrayUnsafe()); |
||||
transaction.commit(); |
||||
loadFlatDbStrategy(composedWorldStateStorage); // force reload of flat db reader strategy
|
||||
} |
||||
|
||||
public FlatDbMode getFlatDbMode() { |
||||
return flatDbMode; |
||||
} |
||||
} |
@ -0,0 +1,89 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.ethereum.trie.bonsai.storage.flat; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier; |
||||
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; |
||||
import org.hyperledger.besu.ethereum.worldstate.FlatDbMode; |
||||
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; |
||||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage; |
||||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction; |
||||
import org.hyperledger.besu.services.kvstore.SegmentedInMemoryKeyValueStorage; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.junit.jupiter.params.ParameterizedTest; |
||||
import org.junit.jupiter.params.provider.EnumSource; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class FlatDbStrategyProviderTest { |
||||
private final FlatDbStrategyProvider flatDbStrategyProvider = |
||||
new FlatDbStrategyProvider(new NoOpMetricsSystem(), DataStorageConfiguration.DEFAULT_CONFIG); |
||||
private final SegmentedKeyValueStorage composedWorldStateStorage = |
||||
new SegmentedInMemoryKeyValueStorage(List.of(KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE)); |
||||
|
||||
@ParameterizedTest |
||||
@EnumSource(FlatDbMode.class) |
||||
void loadsFlatDbStrategyForStoredFlatDbMode(final FlatDbMode flatDbMode) { |
||||
updateFlatDbMode(flatDbMode); |
||||
|
||||
flatDbStrategyProvider.loadFlatDbStrategy(composedWorldStateStorage); |
||||
assertThat(flatDbStrategyProvider.getFlatDbMode()).isEqualTo(flatDbMode); |
||||
} |
||||
|
||||
@Test |
||||
void loadsPartialFlatDbStrategyWhenNoFlatDbModeStored() { |
||||
flatDbStrategyProvider.loadFlatDbStrategy(composedWorldStateStorage); |
||||
assertThat(flatDbStrategyProvider.getFlatDbMode()).isEqualTo(FlatDbMode.PARTIAL); |
||||
} |
||||
|
||||
@Test |
||||
void upgradesFlatDbStrategyToFullFlatDbMode() { |
||||
updateFlatDbMode(FlatDbMode.PARTIAL); |
||||
|
||||
flatDbStrategyProvider.upgradeToFullFlatDbMode(composedWorldStateStorage); |
||||
assertThat(flatDbStrategyProvider.flatDbMode).isEqualTo(FlatDbMode.FULL); |
||||
assertThat(flatDbStrategyProvider.flatDbStrategy).isNotNull(); |
||||
assertThat(flatDbStrategyProvider.getFlatDbStrategy(composedWorldStateStorage)) |
||||
.isInstanceOf(FullFlatDbStrategy.class); |
||||
} |
||||
|
||||
@Test |
||||
void downgradesFlatDbStrategyToPartiallyFlatDbMode() { |
||||
updateFlatDbMode(FlatDbMode.FULL); |
||||
|
||||
flatDbStrategyProvider.downgradeToPartialFlatDbMode(composedWorldStateStorage); |
||||
assertThat(flatDbStrategyProvider.flatDbMode).isEqualTo(FlatDbMode.PARTIAL); |
||||
assertThat(flatDbStrategyProvider.flatDbStrategy).isNotNull(); |
||||
assertThat(flatDbStrategyProvider.getFlatDbStrategy(composedWorldStateStorage)) |
||||
.isInstanceOf(PartialFlatDbStrategy.class); |
||||
} |
||||
|
||||
private void updateFlatDbMode(final FlatDbMode flatDbMode) { |
||||
final SegmentedKeyValueStorageTransaction transaction = |
||||
composedWorldStateStorage.startTransaction(); |
||||
transaction.put( |
||||
KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE, |
||||
FlatDbStrategyProvider.FLAT_DB_MODE, |
||||
flatDbMode.getVersion().toArrayUnsafe()); |
||||
transaction.commit(); |
||||
} |
||||
} |
Loading…
Reference in new issue