@earth-app/collegedb
    Preparing search index...

    Class ShardCoordinator

    Durable Object for coordinating shard allocation and maintaining statistics

    The ShardCoordinator is a Cloudflare Durable Object that provides centralized coordination for shard allocation across multiple D1 databases. It maintains state about available shards, allocation strategies, and usage statistics.

    Key responsibilities:

    • Track available D1 shards and their current load
    • Implement allocation strategies (round-robin, random, hash-based)
    • Provide HTTP API for shard allocation and management
    • Maintain persistent state using Durable Object storage
    // Allocate a shard for a new primary key
    const response = await coordinator.fetch('http://coordinator/allocate', {
    method: 'POST',
    body: JSON.stringify({ primaryKey: 'user-456', strategy: 'hash' })
    });
    const { shard } = await response.json();
    Index

    Constructors

    • Creates a new ShardCoordinator instance

      Parameters

      • state: DurableObjectState

        Durable Object state provided by Cloudflare runtime

      Returns ShardCoordinator

    Methods

    • Atomically decrements the key count for a specific shard and updates the last modified timestamp. Used when primary keys are removed or moved from a shard. Prevents negative counts.

      Parameters

      • shard: string

        The shard binding name to decrement

      Returns Promise<void>

      Promise that resolves when the count is updated

      If the shard is not known to the coordinator

      await coordinator.decrementShardCount('db-west');
      
    • Handles HTTP requests to the Durable Object

      Main entry point for all HTTP requests to the ShardCoordinator. Routes requests based on method and path to appropriate handler functions.

      Supported endpoints:

      • GET /shards - List all known shards
      • POST /shards - Add a new shard
      • DELETE /shards - Remove a shard
      • GET /stats - Get shard statistics
      • POST /stats - Update shard statistics
      • POST /allocate - Allocate a shard for a primary key
      • POST /flush - Clear all coordinator state (development only)
      • GET /health - Health check endpoint

      Parameters

      • request: Request

        The incoming HTTP request

      Returns Promise<Response>

      Promise resolving to HTTP response

      // Allocate a shard
      const response = await coordinator.fetch('http://coordinator/allocate', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ primaryKey: 'user-123' })
      });
    • Atomically increments the key count for a specific shard and updates the last modified timestamp. Used when new primary keys are assigned to a shard.

      Parameters

      • shard: string

        The shard binding name to increment

      Returns Promise<void>

      Promise that resolves when the count is updated

      If the shard is not known to the coordinator

      await coordinator.incrementShardCount('db-east');