Skip to content

feat(runtime): add idempotent wrapper lifecycle - #10

Merged
korayakpinar merged 7 commits into
developmentfrom
feat/flexible-deployment-phase-2
Aug 5, 2026
Merged

korayakpinar merged 7 commits into
developmentfrom
feat/flexible-deployment-phase-2

Conversation

@korayakpinar

@korayakpinar korayakpinar commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR replaces the separate first-boot and restart commands with one idempotent runtime lifecycle:

archive-wrapper run --config <path> [--home <path>] [runtime overrides]

The command classifies the actual LevelDB state, initializes a fresh deployment or resumes a valid existing deployment, rejects unsafe persisted state before external side effects, and shuts down all runtime resources through one bounded ownership path.

This PR builds on the explicit configuration and transport contract introduced in #9.

Motivation

The previous start and proceed split required operators or shell scripts to infer wrapper state from filesystem paths. Path existence is not a reliable representation of application state and creates race-prone first-boot/restart behavior.

Container orchestration needs one long-running command that behaves correctly with both a fresh volume and a persisted volume. It also needs deterministic failures for mismatched, incomplete, corrupt, or concurrently opened state.

Idempotent run Command

The long-running command surface is now:

archive-wrapper run [--config <path>] [--home <path>] [runtime overrides]
archive-wrapper stop [--config <path>] [--control-socket-path <path>]
archive-wrapper help

The legacy start, proceed, and stop --socket-path interfaces have been removed because the software has not been deployed and does not require a compatibility migration.

run uses the same command and startup path for both first boot and restart.

LevelDB Deployment State

LevelDB initialization now returns an explicit state:

fresh
initialized

The zero value is not treated as a valid state.

Fresh database

When the application keyspace is empty, the wrapper validates and stores deployment metadata containing:

  • Metadata schema version
  • Mina network ID
  • Bridge contract address
  • Bridge start height

The cursor is not fabricated during initialization. It is created only after the indexer completes a successful reconciliation.

Initialized database

When metadata already exists, the wrapper verifies that it exactly matches the requested deployment and validates any persisted cursor before starting PostgreSQL or gRPC resources.

A metadata-only database is valid because the deployment may still be waiting for its first finalized block.

Invalid database states

The runtime distinguishes:

  • Database lock held by another process
  • LevelDB corruption
  • Application state without deployment metadata
  • Deployment metadata mismatch
  • Malformed, zero, or negative cursor values

Persisted block records are validated when read, including protobuf validity, action validity, and record-key consistency. Startup deliberately avoids an O(N) scan of all historical records.

Startup Ordering

The runtime validates all local prerequisites before opening external resources:

  1. Resolve and validate configuration.
  2. Read Pulsar genesis and bridge parameters.
  3. Require POSTGRES_URI.
  4. Construct expected deployment metadata.
  5. Open LevelDB once.
  6. Initialize or validate persisted deployment state.
  7. Open the control socket.
  8. Create the PostgreSQL pool.
  9. Start gRPC services.
  10. Start the indexer supervisor.

Missing secrets, invalid genesis, corrupt state, metadata mismatch, and database locks therefore fail before PostgreSQL or gRPC listeners are opened.

Bounded Shutdown

Signals, the control socket, fatal indexer failures, and fatal gRPC errors now converge on one cancellation path.

Shutdown performs the following ordered cleanup:

  1. Mark diagnostics as STOPPING.
  2. Withdraw overall and query readiness.
  3. Cancel the indexer runtime context.
  4. Close the control socket listener.
  5. Attempt graceful gRPC shutdown.
  6. Force-stop gRPC after the ten-second timeout.
  7. Wait for runtime workers.
  8. Close PostgreSQL resources.
  9. Close LevelDB exactly once.

Normal SIGINT, SIGTERM, and STOP shutdowns return success. Runtime, worker, database, or cleanup failures return a failure exit code.

Control Socket Ownership

The control listener is managed by a dedicated component with idempotent close and a completion signal for the accept loop.

The existing PING/PONG and STOP protocol is preserved. Stale sockets can be removed safely, live owners are rejected, and an older listener cannot unlink a newer replacement socket.

Logging and Process Bootstrap

Runtime logs now go to stderr by default, which matches container logging expectations.

When ARCHIVE_WRAPPER_LOG_PATH is explicitly configured, logs are written to both stderr and the selected file. New files are created with mode 0600.

The binary no longer loads .env automatically. Environment loading is now the responsibility of the shell, Compose, or another process supervisor.

Process startup is exposed through a testable runMain(args, stderr) function, while main only converts its result into the process exit code.

Tests

The expanded tests cover:

  • Fresh and initialized database classification
  • Metadata-only databases
  • Metadata mismatch and incomplete state
  • Malformed metadata and cursor values
  • LevelDB lock and corruption mapping
  • Persisted block-record validation
  • First boot and restart through the same command
  • Startup failure before network side effects
  • Graceful and forced gRPC shutdown
  • Signal, control-socket, and supervisor cancellation
  • Control socket replacement safety
  • Default stderr logging and optional secure file logging
  • PostgreSQL credential redaction
  • Race-safe runtime cleanup

Stack

@korayakpinar korayakpinar changed the title feat/flexible deployment phase 2 feat(runtime): add idempotent wrapper lifecycle Aug 3, 2026
@korayakpinar
korayakpinar requested a review from yusufozmis August 3, 2026 14:48
Comment thread cmd/archive-wrapper/control_socket.go Resolved
Comment thread cmd/archive-wrapper/logging.go Resolved
Comment thread database/db.go Resolved
Base automatically changed from feat/flexible-deployment-phase-1 to development August 5, 2026 16:05
@korayakpinar
korayakpinar force-pushed the feat/flexible-deployment-phase-2 branch from 37ae5f5 to ded291d Compare August 5, 2026 16:05
@korayakpinar
korayakpinar force-pushed the feat/flexible-deployment-phase-2 branch from ded291d to 1a712d0 Compare August 5, 2026 16:42
@korayakpinar
korayakpinar merged commit 7674e76 into development Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants