Skip to content

WorkerThread

Xiahua Liu edited this page Jul 5, 2025 · 3 revisions

Worker Thread

PawnDB utilizes worker threads to handle transaction processing, ensuring isolation and efficient resource management for concurrent database operations.

Overview

Each worker thread is responsible for managing a single transaction throughout its lifecycle. This design provides strong isolation guarantees and simplifies transaction management by dedicating computational resources to individual transactions.

Worker Context

Each worker is initialized with a WorkerContext that is built by the main thread. The WorkerContext contains essential information required for transaction execution:

  • Server socket file descriptor - For network communication
  • Communication channels - For inter-thread messaging
  • Unique transaction ID - For transaction identification and tracking

Worker Runtime

Once started, the worker encapsulates the WorkerContext along with a WorkerRuntime. The WorkerRuntime manages the transaction's execution state and contains:

Lock Table

Tracks all locks acquired during the transaction runtime. This ensures proper lock management and prevents lock leaks.

Rollback Stack

Stores rollback actions that must be executed if the transaction is aborted. Actions are stored in reverse order to ensure proper rollback sequence.

Commit Queue

Stores commit actions that must be executed when the transaction is successfully committed. This ensures all changes are properly persisted.

Architecture Benefits

This worker thread design provides several key advantages:

  • Transaction Isolation - Each transaction runs in its own dedicated worker, preventing interference between concurrent transactions
  • Resource Management - Dedicated threads ensure predictable resource allocation and cleanup
  • Lock Management - Centralized lock tracking within each worker simplifies lock acquisition and release
  • Controlled Commit/Rollback - Structured queues ensure proper transaction completion procedures

Lifecycle

  1. Initialization - Main thread creates WorkerContext and spawns worker thread
  2. Runtime Setup - Worker initializes WorkerRuntime with empty lock table, rollback stack, and commit queue
  3. Transaction Processing - Worker executes transaction operations while maintaining state
  4. Cleanup - Worker performs either commit or rollback operations and releases resourcesd
Clone this wiki locally