Skip to content

fix(l1,l2): allow non-empty datadir without existing DB - #6061

Open
Forostovec wants to merge 1 commit into
lambdaclass:mainfrom
Forostovec:fix/datadir-non-empty-support
Open

Forostovec wants to merge 1 commit into
lambdaclass:mainfrom
Forostovec:fix/datadir-non-empty-support

Conversation

@Forostovec

Copy link
Copy Markdown

Motivation

Before this change, ethrex refused to start if the datadir directory was not empty and the metadata file was missing. This broke setups like EthDocker that store JWT and other auxiliary files in the same directory, even when no old database was present.

Description

Updated the schema validation to look for RocksDB marker files (CURRENT, IDENTITY, LOCK) instead of requiring an empty directory, so we still detect legacy databases without metadata but allow non-empty datadirs that only contain non-DB files.

Resolves #5680

@Forostovec
Forostovec requested a review from a team as a code owner January 28, 2026 22:09
@greptile-apps

greptile-apps Bot commented Jan 28, 2026

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR fixes a startup issue where ethrex would refuse to start if the datadir contained any files (like JWT tokens) but no database metadata file. The solution replaces the empty-directory check with a more precise check for RocksDB marker files (CURRENT, IDENTITY, LOCK), allowing auxiliary files to coexist with the database while still detecting legacy databases without metadata.

The changes are focused and surgical:

  • Renamed dir_is_empty() to dir_contains_db_files() to better reflect its purpose
  • Changed logic from "directory is not empty" to "directory contains RocksDB marker files"
  • Updated comments to explain the rationale for EthDocker compatibility

This approach correctly distinguishes between a directory with auxiliary files (like JWTs) and a directory with an actual database that lacks metadata.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - it's a targeted fix for a specific compatibility issue
  • The change is well-scoped and addresses a real issue. The marker files chosen (CURRENT, IDENTITY, LOCK) are standard RocksDB files. Minor risk: using OR logic means a stray LOCK file alone would trigger an error, but this is overly cautious rather than dangerous. The lack of tests for the new behavior prevents a perfect score.
  • No files require special attention - the single changed file is straightforward

Important Files Changed

Filename Overview
crates/storage/store.rs Replaced empty directory check with RocksDB marker file detection to allow non-DB files in datadir

Sequence Diagram

sequenceDiagram
    participant App as Application
    participant Store as Store::new()
    participant Validate as validate_store_schema_version()
    participant Check as dir_contains_db_files()
    participant FS as File System
    participant Init as init_metadata_file()

    App->>Store: new(datadir_path, engine_type)
    Store->>Validate: validate_store_schema_version(datadir_path)
    
    Validate->>FS: Check if metadata file exists
    alt Metadata file exists
        FS-->>Validate: File exists
        Validate->>Validate: Read and validate schema version
        Validate-->>Store: Ok() or Version Error
    else Metadata file missing
        FS-->>Validate: File does not exist
        Validate->>Check: dir_contains_db_files(datadir_path)
        
        Check->>FS: Check for CURRENT file
        Check->>FS: Check for IDENTITY file
        Check->>FS: Check for LOCK file
        
        alt Any RocksDB marker found
            FS-->>Check: Marker file exists
            Check-->>Validate: true
            Validate-->>Store: Error: Old DB without metadata
        else No markers found (JWT files OK)
            FS-->>Check: No marker files
            Check-->>Validate: false
            Validate->>Init: init_metadata_file()
            Init->>FS: Create metadata file
            Init-->>Validate: Ok()
            Validate-->>Store: Ok()
        end
    end
    
    Store-->>App: Store instance or Error
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread crates/storage/store.rs
fn dir_contains_db_files(path: &Path) -> Result<bool, StoreError> {
// RocksDB creates these marker files when initializing a database.
// If any of these exist, we likely have an old database without metadata.
const ROCKSDB_MARKER_FILES: &[&str] = &["CURRENT", "IDENTITY", "LOCK"];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking for CURRENT first or primarily, as it's the most reliable RocksDB marker file. The LOCK file alone might exist without an actual database (e.g., if it was left behind), though this is rare. Current implementation is overly cautious rather than risky.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/storage/store.rs
Line: 2981:2981

Comment:
Consider checking for `CURRENT` first or primarily, as it's the most reliable RocksDB marker file. The `LOCK` file alone might exist without an actual database (e.g., if it was left behind), though this is rare. Current implementation is overly cautious rather than risky.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment thread crates/storage/store.rs
fn dir_contains_db_files(path: &Path) -> Result<bool, StoreError> {
// RocksDB creates these marker files when initializing a database.
// If any of these exist, we likely have an old database without metadata.
const ROCKSDB_MARKER_FILES: &[&str] = &["CURRENT", "IDENTITY", "LOCK"];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: LOCK is a fairly generic filename — other tools or processes could create a LOCK file in the datadir. Since CURRENT alone is sufficient to identify a RocksDB database (it's always created first and is the most distinctive marker), you could consider dropping LOCK from this list to avoid a false positive. Very minor though — the combination of all three being present by coincidence is extremely unlikely.

@ilitteri ilitteri added the external-contributor PR opened by a contributor outside the team label Aug 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor PR opened by a contributor outside the team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support non-empty directory as datadir

3 participants