Class: DispatchPolicy::PoliciesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dispatch_policy/policies_controller.rb

Constant Summary collapse

DRAIN_MAX_PER_REQUEST =
10_000

Instance Method Summary collapse

Instance Method Details

#drainObject

Force-admits every staged job across every partition of the policy, bypassing all gates. Walks partitions in pending-DESC order so the busiest ones drain first. Bounded at DRAIN_MAX_PER_REQUEST per click.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 116

def drain
  drained = 0
  Partition.for_policy(@policy_name)
           .where("pending_count > 0")
           .order(pending_count: :desc, id: :asc)
           .limit(500)
           .each do |partition|
    break if drained >= DRAIN_MAX_PER_REQUEST

    # Pass the REMAINING budget so a single partition can't push the
    # total past the cap (a fixed per-partition cap could overshoot by
    # nearly 2× when the first partition nearly fills it).
    batch, = PartitionsController.drain_partition!(partition, cap: DRAIN_MAX_PER_REQUEST - drained)
    drained += batch
  end

  remaining = Partition.for_policy(@policy_name).sum(:pending_count)
  notice = if remaining.positive?
    "Drained #{drained} job(s) across this policy; #{remaining} still pending — click drain again to continue."
  else
    "Drained #{drained} job(s); policy fully drained."
  end
  redirect_to policy_path(@policy_name), notice: notice
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 9

def index
  registry_names = DispatchPolicy.registry.names
  db_names       = Partition.distinct.pluck(:policy_name)
  names          = (registry_names + db_names).uniq.sort

  in_flight_by_policy = InflightJob.where(policy_name: names).group(:policy_name).count
  # One grouped query for pending / partition count / paused count
  # across every policy instead of three per policy.
  counts_by_policy    = Repository.partition_counts_by_policy
  # Policy-level pause flags — the source of truth the tick honors
  # (partitions.status alone misses partitions created after the pause).
  paused_policies     = PolicySetting.paused.pluck(:policy_name).to_set

  @rows = names.map do |name|
    counts = counts_by_policy[name] || {}
    {
      name:           name,
      registered:     registry_names.include?(name),
      paused:         paused_policies.include?(name),
      pending:        counts[:pending] || 0,
      in_flight:      in_flight_by_policy[name] || 0,
      partitions:     counts[:partitions] || 0,
      paused_count:   counts[:paused] || 0
    }
  end
end

#pauseObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 90

def pause
  # Policy-level flag is the source of truth the tick honors (so a key
  # that first appears AFTER the pause is held too). The per-partition
  # status update is kept for the partitions index display. One TX so
  # both writes commit or neither: a flag without the statuses (or vice
  # versa) leaves the partition list contradicting what admission
  # actually does until the next toggle. set_policy_paused! shares the
  # connection (same role via around_action), so it joins this TX.
  Partition.transaction do
    Repository.set_policy_paused!(policy_name: @policy_name, paused: true)
    Partition.for_policy(@policy_name).update_all(status: "paused", updated_at: Time.current)
  end
  redirect_to policy_path(@policy_name), notice: "Policy paused."
end

#resumeObject



105
106
107
108
109
110
111
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 105

def resume
  Partition.transaction do
    Repository.set_policy_paused!(policy_name: @policy_name, paused: false)
    Partition.for_policy(@policy_name).update_all(status: "active", updated_at: Time.current)
  end
  redirect_to policy_path(@policy_name), notice: "Policy resumed."
end

#showObject



36
37
38
39
40
41
42
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
85
86
87
88
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 36

def show
  @policy_object = DispatchPolicy.registry.fetch(@policy_name)
  @paused        = PolicySetting.for_policy(@policy_name).pick(:paused) || false
  @partitions    = Partition.for_policy(@policy_name)
                            .order(Arel.sql("pending_count DESC, last_admit_at DESC NULLS LAST"))
                            .limit(100)
  @top_admitted  = Partition.for_policy(@policy_name)
                            .order(total_admitted: :desc)
                            .limit(20)

  @totals = {
    pending:    Partition.for_policy(@policy_name).sum(:pending_count),
    in_flight:  InflightJob.where(policy_name: @policy_name).count,
    partitions: Partition.for_policy(@policy_name).count
  }

  now = Time.current
  @windows = {
    "1m"  => Repository.tick_summary(policy_name: @policy_name, since: now - 60),
    "5m"  => Repository.tick_summary(policy_name: @policy_name, since: now - 5 * 60),
    "15m" => Repository.tick_summary(policy_name: @policy_name, since: now - 15 * 60)
  }
  @denied_reasons = Repository.denied_reasons_summary(policy_name: @policy_name, since: now - 15 * 60)
  @round_trip     = Repository.partition_round_trip_stats(policy_name: @policy_name)
  @sparkline      = Repository.tick_samples_buckets(policy_name: @policy_name, since: now - 30 * 60, bucket_seconds: 60)
  @pending_trend  = Repository.trend_direction(@sparkline.map { |b| b[:pending_total] })

  cfg = DispatchPolicy.config
  @capacity = {
    admitted_per_minute:  @windows["1m"][:jobs_admitted],
    adapter_target_jps:   cfg.adapter_throughput_target,
    avg_tick_ms:          @windows["1m"][:avg_duration_ms],
    max_tick_ms:          @windows["1m"][:max_duration_ms],
    tick_max_duration_ms: cfg.tick_max_duration.to_i * 1000
  }

  @hints = OperatorHints.for(
    tick_max_duration_ms: @capacity[:tick_max_duration_ms],
    avg_tick_ms:          @capacity[:avg_tick_ms],
    max_tick_ms:          @capacity[:max_tick_ms],
    pending_total:        @totals[:pending],
    admitted_per_minute:  @capacity[:admitted_per_minute],
    forward_failures:     @windows["1m"][:forward_failures],
    jobs_admitted:        @windows["1m"][:jobs_admitted],
    active_partitions:    @round_trip[:active_partitions],
    never_checked:        @round_trip[:never_checked],
    in_backoff:           @round_trip[:in_backoff],
    total_partitions:     @totals[:partitions],
    adapter_target_jps:   @capacity[:adapter_target_jps],
    pending_trend:        @pending_trend,
    paused:               @paused
  )
end