Skip to content

Commit

Permalink
Create file system based BlobSidecar data archive (#8674)
Browse files Browse the repository at this point in the history
Add the option --Xdata-storage-blobs-archive-path to allow pruned BlobSidecars to be written to disk. Each block/slot has all associated BlobSidecars written to disk in a filename of the block root hash and written out in JSON format.
  • Loading branch information
david-ry4n authored Oct 16, 2024
1 parent b50adc4 commit 5be6509
Show file tree
Hide file tree
Showing 20 changed files with 738 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ public String toLogString() {
getKZGCommitment().toAbbreviatedString(),
getKZGProof().toAbbreviatedString());
}

@Override
public BlobSidecarSchema getSchema() {
return (BlobSidecarSchema) super.getSchema();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static tech.pegasys.teku.infrastructure.async.AsyncRunnerFactory.DEFAULT_MAX_QUEUE_SIZE;
import static tech.pegasys.teku.spec.config.Constants.STORAGE_QUERY_CHANNEL_PARALLELISM;

import java.nio.file.Path;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -33,6 +34,9 @@
import tech.pegasys.teku.storage.api.CombinedStorageChannel;
import tech.pegasys.teku.storage.api.Eth1DepositStorageChannel;
import tech.pegasys.teku.storage.api.VoteUpdateChannel;
import tech.pegasys.teku.storage.archive.DataArchive;
import tech.pegasys.teku.storage.archive.fsarchive.FileSystemArchive;
import tech.pegasys.teku.storage.archive.nooparchive.NoopDataArchive;
import tech.pegasys.teku.storage.server.BatchingVoteUpdateChannel;
import tech.pegasys.teku.storage.server.ChainStorage;
import tech.pegasys.teku.storage.server.CombinedStorageChannelSplitter;
Expand Down Expand Up @@ -149,12 +153,20 @@ protected SafeFuture<?> doStart() {
pruningActiveLabelledGauge));
}
}

final DataArchive dataArchive =
config
.getBlobsArchivePath()
.<DataArchive>map(path -> new FileSystemArchive(Path.of(path)))
.orElse(new NoopDataArchive());

if (config.getSpec().isMilestoneSupported(SpecMilestone.DENEB)) {
blobsPruner =
Optional.of(
new BlobSidecarPruner(
config.getSpec(),
database,
dataArchive,
serviceConfig.getMetricsSystem(),
storagePrunerAsyncRunner,
serviceConfig.getTimeProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
import tech.pegasys.teku.storage.api.OnDiskStoreData;
import tech.pegasys.teku.storage.api.StorageUpdate;
import tech.pegasys.teku.storage.api.WeakSubjectivityUpdate;
import tech.pegasys.teku.storage.archive.fsarchive.FileSystemArchive;
import tech.pegasys.teku.storage.client.RecentChainData;
import tech.pegasys.teku.storage.server.Database;
import tech.pegasys.teku.storage.server.DatabaseArchiveNoopWriter;
import tech.pegasys.teku.storage.server.DatabaseContext;
import tech.pegasys.teku.storage.server.ShuttingDownException;
import tech.pegasys.teku.storage.server.StateStorageMode;
Expand Down Expand Up @@ -122,20 +122,24 @@ public class DatabaseTest {
private StateStorageMode storageMode;
private StorageSystem storageSystem;
private Database database;
private FileSystemArchive fileSystemDataArchive;
private RecentChainData recentChainData;
private UpdatableStore store;
private final List<StorageSystem> storageSystems = new ArrayList<>();

@BeforeEach
public void setup() {
public void setup() throws IOException {
setupWithSpec(TestSpecFactory.createMinimalDeneb());
}

private void setupWithSpec(final Spec spec) {
private void setupWithSpec(final Spec spec) throws IOException {
this.spec = spec;
this.dataStructureUtil = new DataStructureUtil(spec);
this.chainBuilder = ChainBuilder.create(spec, VALIDATOR_KEYS);
this.chainProperties = new ChainProperties(spec);
final Path blobsArchive = Files.createTempDirectory("blobs");
tmpDirectories.add(blobsArchive.toFile());
this.fileSystemDataArchive = new FileSystemArchive(blobsArchive);
genesisBlockAndState = chainBuilder.generateGenesis(genesisTime, true);
genesisCheckpoint = getCheckpointForBlock(genesisBlockAndState.getBlock());
genesisAnchor = AnchorPoint.fromGenesisState(spec, genesisBlockAndState.getState());
Expand Down Expand Up @@ -296,7 +300,7 @@ public void verifyBlobsLifecycle(final DatabaseContext context) throws IOExcepti
// let's prune with limit to 1
assertThat(
database.pruneOldestBlobSidecars(
UInt64.MAX_VALUE, 1, DatabaseArchiveNoopWriter.NOOP_BLOBSIDECAR_STORE))
UInt64.MAX_VALUE, 1, fileSystemDataArchive.getBlobSidecarWriter()))
.isTrue();
assertBlobSidecarKeys(
blobSidecar2_0.getSlot(),
Expand All @@ -313,10 +317,13 @@ public void verifyBlobsLifecycle(final DatabaseContext context) throws IOExcepti
assertThat(database.getEarliestBlobSidecarSlot()).contains(UInt64.valueOf(2));
assertThat(database.getBlobSidecarColumnCount()).isEqualTo(4L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar1_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar2_0)).doesNotExist();

// let's prune up to slot 1 (nothing will be pruned)
assertThat(
database.pruneOldestBlobSidecars(
ONE, 10, DatabaseArchiveNoopWriter.NOOP_BLOBSIDECAR_STORE))
database.pruneOldestBlobSidecars(ONE, 10, fileSystemDataArchive.getBlobSidecarWriter()))
.isFalse();
assertBlobSidecarKeys(
blobSidecar2_0.getSlot(),
Expand All @@ -336,23 +343,35 @@ public void verifyBlobsLifecycle(final DatabaseContext context) throws IOExcepti
// let's prune all from slot 4 excluded
assertThat(
database.pruneOldestBlobSidecars(
UInt64.valueOf(3), 10, DatabaseArchiveNoopWriter.NOOP_BLOBSIDECAR_STORE))
UInt64.valueOf(3), 10, fileSystemDataArchive.getBlobSidecarWriter()))
.isFalse();
assertBlobSidecarKeys(
blobSidecar1_0.getSlot(), blobSidecar5_0.getSlot(), blobSidecarToKey(blobSidecar5_0));
assertBlobSidecars(Map.of(blobSidecar5_0.getSlot(), List.of(blobSidecar5_0)));
assertThat(database.getEarliestBlobSidecarSlot()).contains(UInt64.valueOf(4));
assertThat(database.getBlobSidecarColumnCount()).isEqualTo(1L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar2_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar3_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar5_0)).doesNotExist();

// let's prune all
assertThat(
database.pruneOldestBlobSidecars(
UInt64.valueOf(5), 1, DatabaseArchiveNoopWriter.NOOP_BLOBSIDECAR_STORE))
UInt64.valueOf(5), 1, fileSystemDataArchive.getBlobSidecarWriter()))
.isTrue();
// all empty now
assertBlobSidecarKeys(ZERO, UInt64.valueOf(10));
assertThat(database.getEarliestBlobSidecarSlot()).contains(UInt64.valueOf(6));
assertThat(database.getBlobSidecarColumnCount()).isEqualTo(0L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar5_0)).exists();
}

private File getSlotBlobsArchiveFile(final BlobSidecar blobSidecar) {
return fileSystemDataArchive.resolve(blobSidecar.getSlotAndBlockRoot());
}

@TestTemplate
Expand Down Expand Up @@ -449,7 +468,10 @@ public void verifyNonCanonicalBlobsLifecycle(final DatabaseContext context) thro
List.of(blobSidecar5_0)));

// Pruning with a prune limit set to 1: Only blobSidecar1 will be pruned
assertThat(database.pruneOldestNonCanonicalBlobSidecars(UInt64.MAX_VALUE, 1)).isTrue();
assertThat(
database.pruneOldestNonCanonicalBlobSidecars(
UInt64.MAX_VALUE, 1, fileSystemDataArchive.getBlobSidecarWriter()))
.isTrue();
assertNonCanonicalBlobSidecarKeys(
blobSidecar2_0.getSlot(),
blobSidecar5_0.getSlot(),
Expand All @@ -464,8 +486,15 @@ public void verifyNonCanonicalBlobsLifecycle(final DatabaseContext context) thro
blobSidecar5_0.getSlot(), List.of(blobSidecar5_0)));
assertThat(database.getNonCanonicalBlobSidecarColumnCount()).isEqualTo(4L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar1_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar2_0)).doesNotExist();

// Pruning up to slot 1: No blobs pruned
assertThat(database.pruneOldestNonCanonicalBlobSidecars(ONE, 10)).isFalse();
assertThat(
database.pruneOldestNonCanonicalBlobSidecars(
ONE, 10, fileSystemDataArchive.getBlobSidecarWriter()))
.isFalse();
assertNonCanonicalBlobSidecarKeys(
blobSidecar2_0.getSlot(),
blobSidecar5_0.getSlot(),
Expand All @@ -480,18 +509,36 @@ public void verifyNonCanonicalBlobsLifecycle(final DatabaseContext context) thro
blobSidecar5_0.getSlot(), List.of(blobSidecar5_0)));
assertThat(database.getNonCanonicalBlobSidecarColumnCount()).isEqualTo(4L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar1_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar2_0)).doesNotExist();

// Prune blobs up to slot 3
assertThat(database.pruneOldestNonCanonicalBlobSidecars(UInt64.valueOf(3), 10)).isFalse();
assertThat(
database.pruneOldestNonCanonicalBlobSidecars(
UInt64.valueOf(3), 10, fileSystemDataArchive.getBlobSidecarWriter()))
.isFalse();
assertNonCanonicalBlobSidecarKeys(
blobSidecar1_0.getSlot(), blobSidecar5_0.getSlot(), blobSidecarToKey(blobSidecar5_0));
assertNonCanonicalBlobSidecars(Map.of(blobSidecar5_0.getSlot(), List.of(blobSidecar5_0)));
assertThat(database.getNonCanonicalBlobSidecarColumnCount()).isEqualTo(1L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar2_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar3_0)).exists();
assertThat(getSlotBlobsArchiveFile(blobSidecar5_0)).doesNotExist();

// Pruning all blobs
assertThat(database.pruneOldestNonCanonicalBlobSidecars(UInt64.valueOf(5), 1)).isTrue();
assertThat(
database.pruneOldestNonCanonicalBlobSidecars(
UInt64.valueOf(5), 1, fileSystemDataArchive.getBlobSidecarWriter()))
.isTrue();
// No blobs should be left
assertNonCanonicalBlobSidecarKeys(ZERO, UInt64.valueOf(10));
assertThat(database.getNonCanonicalBlobSidecarColumnCount()).isEqualTo(0L);

// check if the pruned blob was written to disk. Not validating contents here.
assertThat(getSlotBlobsArchiveFile(blobSidecar5_0)).exists();
}

@TestTemplate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.
*/

package tech.pegasys.teku.storage.archive;

import java.io.IOException;
import java.util.List;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;

/**
* Interface for a data archive which stores prunable BlobSidecars outside the data availability
* window and could be extended later to include other data types. It is expected that the
* DataArchive is on disk or externally stored with slow write and recovery times. Initial interface
* is write only, but may be expanded to include read operations later.
*/
public interface DataArchive {

/**
* Returns the archive writer capable of storing BlobSidecars.
*
* @return a closeable DataArchiveWriter for writing BlobSidecars
* @throws IOException throw exception if it fails to get a writer.
*/
DataArchiveWriter<List<BlobSidecar>> getBlobSidecarWriter() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.storage.server;
package tech.pegasys.teku.storage.archive;

import java.io.Closeable;

/**
* A functional interface to allow storing data that is to be pruned from the Database. If the store
* function is successful it returns true, signalling the data can be pruned. If the store function
* fails, the data was not stored and the data should not be pruned.
* An interface to allow storing data that is to be pruned from the Database. If the store function
* is successful it returns true, signalling the data can be pruned. If the store function fails,
* the data was not stored and the data should not be pruned.
*
* @param <T> the data to be stored.
*/
@FunctionalInterface
public interface DatabaseArchiveWriter<T> {
public interface DataArchiveWriter<T> extends Closeable {
boolean archive(final T data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.
*/

package tech.pegasys.teku.storage.archive.fsarchive;

import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition.listOf;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Objects;
import tech.pegasys.teku.infrastructure.json.JsonUtil;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;

public class BlobSidecarJsonWriter {

public void writeSlotBlobSidecars(final OutputStream out, final List<BlobSidecar> blobSidecars)
throws IOException {
Objects.requireNonNull(out);
Objects.requireNonNull(blobSidecars);

// Technically not possible as pruner prunes sidecars and not slots.
if (blobSidecars.isEmpty()) {
out.write("[]".getBytes(UTF_8));
return;
}

final SerializableTypeDefinition<List<BlobSidecar>> type =
listOf(blobSidecars.getFirst().getSchema().getJsonTypeDefinition());
JsonUtil.serializeToBytes(blobSidecars, type, out);
}
}
Loading

0 comments on commit 5be6509

Please sign in to comment.