-
Notifications
You must be signed in to change notification settings - Fork 353
Check network on start and reset db #8642
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
62100c1
Add check for Ephemery and reset db
gconnect d5dc6c4
Remove unused comment
gconnect a8f6afa
Add a shouldResetForEphemery method and test
gconnect a33707b
Merge branch 'master' into check-network-on-start
gconnect c1149ac
Merge branch 'master' into check-network-on-start
gconnect 34ecc2d
update null check
gconnect 572d91b
Merge branch 'master' into check-network-on-start
gconnect 7b6584b
Merge branch 'master' into check-network-on-start
gconnect e935764
Merge branch 'master' into check-network-on-start
gconnect d496675
Merge branch 'master' into check-network-on-start
gconnect 1bbc3c4
optimized the reset code and add a custom EphemeryException
gconnect 8a3d40c
Merge branch 'master' into check-network-on-start
gconnect d23b3c4
Update the reset method and remove migrate
gconnect 6a5d9fc
Remove private modifier
gconnect c2aced4
Apply spotlessApply
gconnect 6d2cf43
Apply spotlessApply
gconnect 7b2871c
Create custom Ephemery reset class and test
gconnect d9d1951
Add fnal and move global variable to local variable
gconnect 0d47959
Remove repeated code
gconnect 4306df8
Merge branch 'master' into check-network-on-start
gconnect ee57df8
Merge branch 'master' into check-network-on-start
gconnect f73e491
Ensure reset method delete specific files and update test and change …
gconnect f95a61a
Remove print statement and add final
gconnect b2bcefd
Update the recursive delete method and test
gconnect 769c6f8
Merge branch 'master' into check-network-on-start
gconnect 6cdc994
Remove print statement
gconnect 5588f16
Merge branch 'master' into check-network-on-start
gconnect e96e6e3
Merge branch 'master' into check-network-on-start
gconnect 92f6d5b
Merge branch 'master' into check-network-on-start
rolfyone c1ad18d
Merge branch 'master' into check-network-on-start
rolfyone b20eedc
Merge branch 'master' into check-network-on-start
rolfyone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...nstorage/src/main/java/tech/pegasys/teku/services/chainstorage/EphemeryDatabaseReset.java
This file contains hidden or 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,79 @@ | ||
| /* | ||
| * 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.services.chainstorage; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException; | ||
| import tech.pegasys.teku.service.serviceutils.ServiceConfig; | ||
| import tech.pegasys.teku.storage.server.Database; | ||
| import tech.pegasys.teku.storage.server.VersionedDatabaseFactory; | ||
|
|
||
| public class EphemeryDatabaseReset { | ||
|
|
||
| /** This method is called only on Ephemery network when reset is due. */ | ||
| Database resetDatabaseAndCreate( | ||
| final ServiceConfig serviceConfig, final VersionedDatabaseFactory dbFactory) { | ||
| try { | ||
| final Path beaconDataDir = serviceConfig.getDataDirLayout().getBeaconDataDirectory(); | ||
| final Path dbDataDir = beaconDataDir.resolve("db"); | ||
| final Path networkFile = beaconDataDir.resolve("network.yml"); | ||
| final Path validatorDataDir = serviceConfig.getDataDirLayout().getValidatorDataDirectory(); | ||
| final Path slashProtectionDir; | ||
| if (validatorDataDir.endsWith("slashprotection")) { | ||
| slashProtectionDir = validatorDataDir; | ||
| } else { | ||
| slashProtectionDir = validatorDataDir.resolve("slashprotection"); | ||
| } | ||
|
|
||
| System.out.println("beaconDir" + beaconDataDir); | ||
| System.out.println("networkFile" + networkFile); | ||
| System.out.println("dbdir" + dbDataDir); | ||
gconnect marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| deleteDirectoryRecursively(dbDataDir); | ||
| deleteDirectoryRecursively(networkFile); | ||
| deleteDirectoryRecursively(slashProtectionDir); | ||
| return dbFactory.createDatabase(); | ||
| } catch (final Exception ex) { | ||
| throw new InvalidConfigurationException( | ||
| "The existing ephemery database was old, and was unable to reset it.", ex); | ||
| } | ||
| } | ||
|
|
||
| void deleteDirectoryRecursively(final Path path) throws IOException { | ||
| if (Files.exists(path)) { | ||
| if (Files.isDirectory(path)) { | ||
| try (var stream = Files.walk(path)) { | ||
| stream | ||
| .sorted((o1, o2) -> o2.compareTo(o1)) | ||
| .forEach( | ||
| p -> { | ||
| try { | ||
| Files.delete(p); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to delete file/directory: " + p, e); | ||
| } | ||
| }); | ||
| } | ||
| } else { | ||
| try { | ||
| Files.delete(path); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to delete file: " + path, e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
135 changes: 135 additions & 0 deletions
135
...rage/src/test/java/tech/pegasys/teku/services/chainstorage/EphemeryDatabaseResetTest.java
This file contains hidden or 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,135 @@ | ||
| /* | ||
| * 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.services.chainstorage; | ||
|
|
||
| import static java.nio.file.Files.createTempDirectory; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException; | ||
| import tech.pegasys.teku.service.serviceutils.ServiceConfig; | ||
| import tech.pegasys.teku.service.serviceutils.layout.DataDirLayout; | ||
| import tech.pegasys.teku.storage.server.Database; | ||
| import tech.pegasys.teku.storage.server.VersionedDatabaseFactory; | ||
|
|
||
| class EphemeryDatabaseResetTest { | ||
|
|
||
| @Mock private ServiceConfig serviceConfig; | ||
|
|
||
| @Mock private VersionedDatabaseFactory dbFactory; | ||
|
|
||
| @Mock private DataDirLayout dataDirLayout; | ||
| private Path beaconDataDir; | ||
| private Path dbDataDir; | ||
| private Path resolvedSlashProtectionDir; | ||
| private Path networkFilePath; | ||
| @Mock private Database database; | ||
|
|
||
| @Mock private EphemeryDatabaseReset ephemeryDatabaseReset; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws IOException { | ||
| MockitoAnnotations.openMocks(this); | ||
| ephemeryDatabaseReset = spy(new EphemeryDatabaseReset()); | ||
| beaconDataDir = createTempDirectory("beacon"); | ||
| dbDataDir = beaconDataDir.resolve("db"); | ||
| Files.createDirectory(dbDataDir); | ||
| Path networkFile = beaconDataDir.resolve("network.yml"); | ||
| Files.createFile(networkFile); | ||
| networkFilePath = networkFile; | ||
|
|
||
| final Path validatorDataDir = createTempDirectory("validator"); | ||
| resolvedSlashProtectionDir = validatorDataDir.resolve("slashprotection"); | ||
|
|
||
| when(serviceConfig.getDataDirLayout()).thenReturn(dataDirLayout); | ||
| when(dataDirLayout.getBeaconDataDirectory()).thenReturn(beaconDataDir); | ||
| when(dataDirLayout.getValidatorDataDirectory()).thenReturn(validatorDataDir); | ||
| when(dataDirLayout.getValidatorDataDirectory().resolve("slashprotection")) | ||
| .thenReturn(resolvedSlashProtectionDir); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldResetSpecificDirectoriesAndCreateDatabase() throws IOException { | ||
| final Path kvStoreDir = beaconDataDir.resolve("kvstore"); | ||
| Files.createDirectory(kvStoreDir); | ||
| final Path dbVersion = beaconDataDir.resolve("db.version"); | ||
| Files.createFile(dbVersion); | ||
|
|
||
| when(dbFactory.createDatabase()).thenReturn(database); | ||
|
|
||
| final Database result = ephemeryDatabaseReset.resetDatabaseAndCreate(serviceConfig, dbFactory); | ||
| verify(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir); | ||
| verify(ephemeryDatabaseReset).deleteDirectoryRecursively(networkFilePath); | ||
| verify(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir); | ||
|
|
||
| verify(dbFactory).createDatabase(); | ||
| verifyNoMoreInteractions(dbFactory); | ||
|
|
||
| assertTrue(Files.exists(kvStoreDir)); | ||
| assertTrue(Files.exists(dbVersion)); | ||
| assertEquals(database, result); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldThrowInvalidConfigurationExceptionWhenDirectoryDeletionFails() throws IOException { | ||
| doThrow(new IOException("Failed to delete directory")) | ||
| .when(ephemeryDatabaseReset) | ||
| .deleteDirectoryRecursively(dbDataDir); | ||
| final InvalidConfigurationException exception = | ||
| assertThrows( | ||
| InvalidConfigurationException.class, | ||
| () -> { | ||
| ephemeryDatabaseReset.resetDatabaseAndCreate(serviceConfig, dbFactory); | ||
| }); | ||
| assertEquals( | ||
| "The existing ephemery database was old, and was unable to reset it.", | ||
| exception.getMessage()); | ||
| verify(dbFactory, never()).createDatabase(); | ||
| verify(ephemeryDatabaseReset, never()).deleteDirectoryRecursively(resolvedSlashProtectionDir); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldThrowInvalidConfigurationExceptionWhenDatabaseCreationFails() throws IOException { | ||
| doNothing().when(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir); | ||
| doNothing().when(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir); | ||
| when(dbFactory.createDatabase()).thenThrow(new RuntimeException("Database creation failed")); | ||
| final InvalidConfigurationException exception = | ||
| assertThrows( | ||
| InvalidConfigurationException.class, | ||
| () -> { | ||
| ephemeryDatabaseReset.resetDatabaseAndCreate(serviceConfig, dbFactory); | ||
| }); | ||
| assertEquals( | ||
| "The existing ephemery database was old, and was unable to reset it.", | ||
| exception.getMessage()); | ||
| verify(ephemeryDatabaseReset).deleteDirectoryRecursively(dbDataDir); | ||
| verify(ephemeryDatabaseReset).deleteDirectoryRecursively(resolvedSlashProtectionDir); | ||
| verify(dbFactory).createDatabase(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
storage/src/main/java/tech/pegasys/teku/storage/server/network/EphemeryException.java
This file contains hidden or 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,16 @@ | ||
| /* | ||
| * 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.server.network; | ||
|
|
||
| public class EphemeryException extends RuntimeException {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.