Class: DiscordRDA::ReshardManager

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/connection/reshard_manager.rb

Overview

Zero-downtime resharding manager. Allows adding/removing shards without stopping the bot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, shard_manager, logger) ⇒ ReshardManager

Initialize reshard manager

Parameters:

  • bot (Bot)

    Bot instance

  • shard_manager (ShardManager)

    Shard manager

  • logger (Logger)

    Logger instance



30
31
32
33
34
35
36
37
38
# File 'lib/discord_rda/connection/reshard_manager.rb', line 30

def initialize(bot, shard_manager, logger)
  @bot = bot
  @shard_manager = shard_manager
  @logger = logger
  @resharding = false
  @new_shards = []
  @old_shards = []
  @mutex = Mutex.new
end

Instance Attribute Details

#botBot (readonly)

Returns Bot instance.

Returns:

  • (Bot)

    Bot instance



9
10
11
# File 'lib/discord_rda/connection/reshard_manager.rb', line 9

def bot
  @bot
end

#loggerLogger (readonly)

Returns Logger instance.

Returns:

  • (Logger)

    Logger instance



15
16
17
# File 'lib/discord_rda/connection/reshard_manager.rb', line 15

def logger
  @logger
end

#new_shardsArray<GatewayClient> (readonly)

Returns New shards being added.

Returns:



21
22
23
# File 'lib/discord_rda/connection/reshard_manager.rb', line 21

def new_shards
  @new_shards
end

#old_shardsArray<GatewayClient> (readonly)

Returns Old shards being removed.

Returns:



24
25
26
# File 'lib/discord_rda/connection/reshard_manager.rb', line 24

def old_shards
  @old_shards
end

#reshardingBoolean (readonly)

Returns Whether resharding is in progress.

Returns:

  • (Boolean)

    Whether resharding is in progress



18
19
20
# File 'lib/discord_rda/connection/reshard_manager.rb', line 18

def resharding
  @resharding
end

#shard_managerShardManager (readonly)

Returns Original shard manager.

Returns:



12
13
14
# File 'lib/discord_rda/connection/reshard_manager.rb', line 12

def shard_manager
  @shard_manager
end

Instance Method Details

#calculate_guilds_per_shard(shard_count) ⇒ Integer

Calculate optimal guilds per shard

Parameters:

  • shard_count (Integer)

    Target shard count

Returns:

  • (Integer)

    Guilds per shard



89
90
91
92
# File 'lib/discord_rda/connection/reshard_manager.rb', line 89

def calculate_guilds_per_shard(shard_count)
  total_guilds = @shard_manager.total_guilds || 1000
  (total_guilds / shard_count.to_f).ceil
end

#reshard_to(new_shard_count) ⇒ void

This method returns an undefined value.

Start zero-downtime resharding to new shard count

Parameters:

  • new_shard_count (Integer)

    New total shard count



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/discord_rda/connection/reshard_manager.rb', line 43

def reshard_to(new_shard_count)
  return if new_shard_count <= @shard_manager.shard_count.to_i
  return if @resharding

  @mutex.synchronize do
    @resharding = true
    old_count = @shard_manager.shard_count || 1

    @logger.info('Starting zero-downtime resharding',
      old_count: old_count,
      new_count: new_shard_count
    )

    # Step 1: Identify which shards need to be moved
    guilds_per_shard = calculate_guilds_per_shard(new_shard_count)

    # Step 2: Start new shards
    start_new_shards(old_count, new_shard_count)

    # Step 3: Wait for new shards to be ready
    wait_for_new_shards_ready

    # Step 4: Begin session transfer
    transfer_sessions

    # Step 5: Mark old shards for shutdown
    mark_old_shards_for_shutdown

    # Step 6: Wait for old shards to drain
    wait_for_old_shards_drain

    # Step 7: Update shard manager state
    finalize_reshard

    @logger.info('Resharding complete', new_shard_count: new_shard_count)
    @resharding = false
  end
rescue => e
  @logger.error('Resharding failed', error: e)
  @resharding = false
  raise
end

#statusHash

Get reshard status

Returns:

  • (Hash)

    Status information



96
97
98
99
100
101
102
103
104
105
# File 'lib/discord_rda/connection/reshard_manager.rb', line 96

def status
  @mutex.synchronize do
    {
      resharding: @resharding,
      new_shards: @new_shards.length,
      old_shards: @old_shards.length,
      total_shards: @shard_manager.shard_count
    }
  end
end