This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PAN-2818] Database versioning and enable multi-column database (#1830)
* Database Versioning: The behavior is to load the database at the existing version if it already exists or create the newest version if it doesn't * multi-column by default: This makes the separated world state storage column required by mark sweep on by default
- Loading branch information
Showing
6 changed files
with
221 additions
and
39 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
.../core/src/main/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/DatabaseMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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.pantheon.ethereum.storage.keyvalue; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class DatabaseMetadata { | ||
static final String METADATA_FILENAME = "DATABASE_METADATA.json"; | ||
private static ObjectMapper MAPPER = new ObjectMapper(); | ||
private final int version; | ||
|
||
@JsonCreator | ||
DatabaseMetadata(@JsonProperty("version") final int version) { | ||
this.version = version; | ||
} | ||
|
||
public int getVersion() { | ||
return version; | ||
} | ||
|
||
static DatabaseMetadata fromDirectory(final Path databaseDir) throws IOException { | ||
final File metadataFile = getDefaultMetadataFile(databaseDir); | ||
try { | ||
return MAPPER.readValue(metadataFile, DatabaseMetadata.class); | ||
} catch (FileNotFoundException fnfe) { | ||
return new DatabaseMetadata(0); | ||
} catch (JsonProcessingException jpe) { | ||
throw new IllegalStateException( | ||
String.format("Invalid metadata file %s", metadataFile.getAbsolutePath()), jpe); | ||
} | ||
} | ||
|
||
void writeToDirectory(final Path databaseDir) throws IOException { | ||
MAPPER.writeValue(getDefaultMetadataFile(databaseDir), this); | ||
} | ||
|
||
private static File getDefaultMetadataFile(final Path databaseDir) { | ||
return databaseDir.resolve(METADATA_FILENAME).toFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...test/java/tech/pegasys/pantheon/ethereum/storage/keyvalue/RocksDbStorageProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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.pantheon.ethereum.storage.keyvalue; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
import tech.pegasys.pantheon.metrics.MetricsSystem; | ||
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem; | ||
import tech.pegasys.pantheon.services.kvstore.RocksDbConfiguration; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class RocksDbStorageProviderTest { | ||
|
||
@Mock private RocksDbConfiguration rocksDbConfiguration; | ||
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
private final MetricsSystem metricsSystem = new NoOpMetricsSystem(); | ||
|
||
@Test | ||
public void shouldCreateCorrectMetadataFileForLatestVersion() throws Exception { | ||
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db"); | ||
when(rocksDbConfiguration.getDatabaseDir()).thenReturn(tempDatabaseDir); | ||
RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem); | ||
assertEquals( | ||
RocksDbStorageProvider.DEFAULT_VERSION, | ||
DatabaseMetadata.fromDirectory(rocksDbConfiguration.getDatabaseDir()).getVersion()); | ||
} | ||
|
||
@Test | ||
public void shouldDetectVersion0DatabaseIfNoMetadataFileFound() throws Exception { | ||
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db"); | ||
Files.createDirectories(tempDatabaseDir); | ||
tempDatabaseDir.resolve("IDENTITY").toFile().createNewFile(); | ||
when(rocksDbConfiguration.getDatabaseDir()).thenReturn(tempDatabaseDir); | ||
RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem); | ||
assertEquals(0, DatabaseMetadata.fromDirectory(tempDatabaseDir).getVersion()); | ||
} | ||
|
||
@Test | ||
public void shouldDetectCorrectVersionIfMetadataFileExists() throws Exception { | ||
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db"); | ||
Files.createDirectories(tempDatabaseDir); | ||
tempDatabaseDir.resolve("IDENTITY").toFile().createNewFile(); | ||
new DatabaseMetadata(1).writeToDirectory(tempDatabaseDir); | ||
when(rocksDbConfiguration.getDatabaseDir()).thenReturn(tempDatabaseDir); | ||
RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem); | ||
assertEquals(1, DatabaseMetadata.fromDirectory(tempDatabaseDir).getVersion()); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionWhenVersionNumberIsInvalid() throws Exception { | ||
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db"); | ||
Files.createDirectories(tempDatabaseDir); | ||
tempDatabaseDir.resolve("IDENTITY").toFile().createNewFile(); | ||
new DatabaseMetadata(-1).writeToDirectory(tempDatabaseDir); | ||
when(rocksDbConfiguration.getDatabaseDir()).thenReturn(tempDatabaseDir); | ||
assertThatThrownBy(() -> RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem)) | ||
.isInstanceOf(IllegalStateException.class); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionWhenMetaDataFileIsCorrupted() throws Exception { | ||
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db"); | ||
Files.createDirectories(tempDatabaseDir); | ||
when(rocksDbConfiguration.getDatabaseDir()).thenReturn(tempDatabaseDir); | ||
tempDatabaseDir.resolve("IDENTITY").toFile().createNewFile(); | ||
|
||
final String badVersion = "{\"🦄\":1}"; | ||
Files.write( | ||
tempDatabaseDir.resolve(DatabaseMetadata.METADATA_FILENAME), | ||
badVersion.getBytes(Charset.defaultCharset())); | ||
assertThatThrownBy(() -> RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem)) | ||
.isInstanceOf(IllegalStateException.class); | ||
|
||
final String badValue = "{\"version\":\"iomedae\"}"; | ||
Files.write( | ||
tempDatabaseDir.resolve(DatabaseMetadata.METADATA_FILENAME), | ||
badValue.getBytes(Charset.defaultCharset())); | ||
assertThatThrownBy(() -> RocksDbStorageProvider.create(rocksDbConfiguration, metricsSystem)) | ||
.isInstanceOf(IllegalStateException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters