tep 0.11.10 → 0.11.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab07017a55b832e36f074079ebb1b1f84cb7b7fb7223bf099b05649f876f6762
4
- data.tar.gz: 98a0328f60412b5a65859ca2a17856992775c7c9e33ea57efa595b67db6f8444
3
+ metadata.gz: d48ebe0e98b19848548906bca9b1da41c8d98bc18e01b76299dabf50a8287ecb
4
+ data.tar.gz: c52abc13d471eb8c1d7fa089e33b49ec707b18270053e67105cd23e516d9afb5
5
5
  SHA512:
6
- metadata.gz: 05bc8d5707d6a2a838cb3e2a845d6ef3be3fb11ebb6a66ff72f9ff5e5bb06fa2d135714368a8b8af4caf673b1b6a4e21afbd6d5252196bdebef4bcc616db574f
7
- data.tar.gz: d2d77d39aa8b589a5617b55c32197d43100b3291f028139b404eef99d8edcf00ade30e521294c61d1cf03806385605470b974b0b5302e030a98bc35095d63773
6
+ metadata.gz: 883c5f954aa6f7d5db7b3d58eee2bca7b57e4a4ee63507d01b18ee80f27047347d29610444487dac72c82559b5bc2501530b12cd1c9735bd47cf9c2a0477ccb0
7
+ data.tar.gz: b1afdff3218b4c70b3d387f03dc0e1c8b172264435f349a321def1fe283ced5866a295381daa16675d6e39322310f11ee4b39f1e59106be66ad0dea31b4da905
data/lib/tep/pg.rb CHANGED
@@ -1095,9 +1095,8 @@ module PG
1095
1095
  @free.push(c)
1096
1096
  if @waiter_idxs.length > 0
1097
1097
  widx = @waiter_idxs.delete_at(0)
1098
- # wake_at = -1 makes the fiber the "earliest due" in the
1099
- # next tick's pick (the tick comparator chooses the lowest
1100
- # wake_at among the time-due set, so -1 always wins).
1098
+ # wake_at = -1 marks the fiber due: the next tick's
1099
+ # resume_ready pass picks it up with the other due fibers.
1101
1100
  Tep::APP.sched_wake_at[widx] = -1
1102
1101
  end
1103
1102
  0
data/lib/tep/scheduler.rb CHANGED
@@ -45,19 +45,51 @@ module Tep
45
45
  READ = 1
46
46
  WRITE = 2
47
47
 
48
+ # Register `f` with the scheduler. Reuses the first dead slot if
49
+ # there is one, else appends. Reuse matters under connection churn:
50
+ # tick() only reclaims dead slots at the TAIL, and a fiber-per-
51
+ # connection server always has a live fiber there (the accept
52
+ # fiber, or the newest connection), so without reuse every closed
53
+ # connection leaves a dead slot -- and its mmap'd fiber stack --
54
+ # behind forever, and every poll_round walks all of them. Live
55
+ # slots never move, so indices captured across Fiber.yield (pg.rb's
56
+ # PG::Pool @waiter_idxs) stay valid.
48
57
  def self.spawn_fiber(f)
49
- Tep::APP.sched_fibers.push(Tep::FiberSlot.new(f))
50
- Tep::APP.sched_wake_at.push(-1)
51
- Tep::APP.sched_io_fd.push(-1)
52
- Tep::APP.sched_io_mode.push(0)
53
- Tep::APP.sched_io_ready.push(0)
58
+ idx = Scheduler.free_slot
59
+ if idx < 0
60
+ Tep::APP.sched_fibers.push(Tep::FiberSlot.new(f))
61
+ Tep::APP.sched_wake_at.push(-1)
62
+ Tep::APP.sched_io_fd.push(-1)
63
+ Tep::APP.sched_io_mode.push(0)
64
+ Tep::APP.sched_io_ready.push(0)
65
+ else
66
+ Tep::APP.sched_fibers[idx] = Tep::FiberSlot.new(f)
67
+ Tep::APP.sched_wake_at[idx] = -1
68
+ Tep::APP.sched_io_fd[idx] = -1
69
+ Tep::APP.sched_io_mode[idx] = 0
70
+ Tep::APP.sched_io_ready[idx] = 0
71
+ end
54
72
  f
55
73
  end
56
74
 
75
+ # Index of the first slot whose fiber has finished, or -1.
76
+ def self.free_slot
77
+ i = 0
78
+ n = Tep::APP.sched_fibers.length
79
+ while i < n
80
+ if !Tep::APP.sched_fibers[i].f.alive?
81
+ return i
82
+ end
83
+ i += 1
84
+ end
85
+ -1
86
+ end
87
+
57
88
  # One scheduler pass. If any fibers are parked on I/O, build a
58
89
  # poll set, run poll(2) for up to `poll_timeout_ms`, and mark
59
- # ready ones. Then resume the soonest-due fiber whose wake_at
60
- # is <= now. Returns true if it resumed something.
90
+ # ready ones. Then resume EVERY fiber whose wake_at is <= now, in
91
+ # slot order (see resume_ready). Returns true if it resumed
92
+ # something.
61
93
  #
62
94
  # If a fiber is already time-due (wake_at <= now -- e.g. a newly
63
95
  # spawned fiber with wake_at=-1, or a fiber that just called
@@ -69,15 +101,22 @@ module Tep
69
101
  # each "hand off to the freshly-spawned fiber" step costs a full
70
102
  # poll-timeout's worth of latency.
71
103
  def self.tick(poll_timeout_ms)
72
- # Reclaim trailing dead slots. Without this, the parallel
73
- # arrays grow once per accepted connection and never shrink --
74
- # a slow leak and per-tick iteration tax in a long-running
75
- # Scheduled server. Tail-only (stop at first alive) is
76
- # deliberate: it keeps every surviving slot's index stable,
77
- # so external captures of sched_current held across Fiber.yield
78
- # (e.g. pg.rb's PG::Pool @waiter_idxs) stay valid. Middle
79
- # dead slots aren't reclaimed until the tail catches up; for
80
- # FIFO request lifecycles that's the common case.
104
+ Scheduler.reclaim_tail
105
+ ms = poll_timeout_ms
106
+ if Scheduler.any_time_ready
107
+ ms = 0
108
+ end
109
+ Scheduler.poll_round(ms)
110
+ Scheduler.resume_ready > 0
111
+ end
112
+
113
+ # Reclaim trailing dead slots so the parallel arrays shrink back
114
+ # when the youngest fibers finish. Tail-only (stop at first alive)
115
+ # is deliberate: it keeps every surviving slot's index stable, so
116
+ # external captures of sched_current held across Fiber.yield (e.g.
117
+ # pg.rb's PG::Pool @waiter_idxs) stay valid. Dead slots in the
118
+ # middle are recycled by spawn_fiber instead.
119
+ def self.reclaim_tail
81
120
  i = Tep::APP.sched_fibers.length - 1
82
121
  while i >= 0 && !Tep::APP.sched_fibers[i].f.alive?
83
122
  Tep::APP.sched_fibers.delete_at(i)
@@ -87,33 +126,41 @@ module Tep
87
126
  Tep::APP.sched_io_ready.delete_at(i)
88
127
  i -= 1
89
128
  end
129
+ 0
130
+ end
90
131
 
91
- ms = poll_timeout_ms
92
- if Scheduler.any_time_ready
93
- ms = 0
94
- end
95
- Scheduler.poll_round(ms)
96
-
97
- now = Time.now.to_i
98
- best = -1
132
+ # Resume every due fiber (wake_at <= now -- which poll_round just
133
+ # set on the I/O-ready ones) once, in slot order. Returns how many
134
+ # were resumed.
135
+ #
136
+ # One resume per due fiber per pass is what keeps the scheduler
137
+ # fair. The earlier shape resumed only the single lowest-wake_at
138
+ # fiber per tick, and wake_at has whole-second resolution: every
139
+ # fiber readied within the same second tied, the lowest slot won
140
+ # the tie, and a keep-alive client that pipelines its next request
141
+ # straight away is readied again before anyone else runs -- so a
142
+ # few low-slot connections monopolised the worker and the rest
143
+ # stalled until the clock ticked over (max latency pinned at
144
+ # exactly 1s under load, p50 well under 1ms).
145
+ #
146
+ # `n` is captured before the loop: a resumed fiber may spawn (slot
147
+ # reuse or append); appended ones run next pass.
148
+ def self.resume_ready
149
+ now = Time.now.to_i
150
+ resumed = 0
99
151
  i = 0
100
152
  n = Tep::APP.sched_fibers.length
101
153
  while i < n
102
154
  if Tep::APP.sched_fibers[i].f.alive? && Tep::APP.sched_wake_at[i] <= now
103
- if best < 0 || Tep::APP.sched_wake_at[i] < Tep::APP.sched_wake_at[best]
104
- best = i
105
- end
155
+ Tep::APP.sched_current = i
156
+ Tep::APP.sched_wake_at[i] = -1
157
+ Tep::APP.sched_fibers[i].f.resume
158
+ Tep::APP.sched_current = -1
159
+ resumed += 1
106
160
  end
107
161
  i += 1
108
162
  end
109
- if best < 0
110
- return false
111
- end
112
- Tep::APP.sched_current = best
113
- Tep::APP.sched_wake_at[best] = -1
114
- Tep::APP.sched_fibers[best].f.resume
115
- Tep::APP.sched_current = -1
116
- true
163
+ resumed
117
164
  end
118
165
 
119
166
  # Build poll set from parked-on-I/O fibers, call poll(2), and
@@ -164,8 +211,14 @@ module Tep
164
211
  # Pure non-blocking; no poll() wait between passes.
165
212
  def self.run_until_empty
166
213
  n = 0
167
- while Scheduler.tick(0)
168
- n += 1
214
+ while true
215
+ Scheduler.reclaim_tail
216
+ Scheduler.poll_round(0)
217
+ r = Scheduler.resume_ready
218
+ if r == 0
219
+ return n
220
+ end
221
+ n += r
169
222
  end
170
223
  n
171
224
  end
@@ -134,11 +134,23 @@ module Tep
134
134
  next
135
135
  end
136
136
  Sock.sphttp_set_nonblock(client)
137
- conn = Fiber.new { Tep::Server::Scheduled.handle_connection(client) }
138
- Tep::Scheduler.spawn_fiber(conn)
137
+ Tep::Server::Scheduled.spawn_connection(client)
139
138
  end
140
139
  end
141
140
 
141
+ # One fiber per accepted connection. A method of its own so the
142
+ # block closes over THIS call's `client`. Built inline in
143
+ # accept_loop it captured the loop's variable, which the next
144
+ # accept overwrites -- and the scheduler resumes the accept fiber
145
+ # (slot 0) before a fiber appended on the previous pass has
146
+ # started. Two fibers then served the newer fd and the older
147
+ # connection was never read.
148
+ def self.spawn_connection(client)
149
+ conn = Fiber.new { Tep::Server::Scheduled.handle_connection(client) }
150
+ Tep::Scheduler.spawn_fiber(conn)
151
+ 0
152
+ end
153
+
142
154
  # Non-blocking server-side TLS handshake on an accepted fd.
143
155
  # Returns 1 on success (SSL* registered -- reads/writes are now
144
156
  # transparent), 0 on failure. Drives SSL_do_handshake, parking on
data/lib/tep/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tep
2
- VERSION = "0.11.10"
2
+ VERSION = "0.11.12"
3
3
  end
@@ -151,3 +151,67 @@ class TestScheduler < TepTest
151
151
  assert_match(/elapsed=[12]/, res.body)
152
152
  end
153
153
  end
154
+
155
+ # Scheduler fairness, exercised under Tep::Server::Scheduled (the
156
+ # prefork-handler variant above SIGSEGVs; here the handler already runs
157
+ # inside a scheduler fiber, and spawning more from it is the shape the
158
+ # accept loop uses).
159
+ class TestSchedulerFairness < TepTest
160
+ app_source <<~RB
161
+ require 'sinatra'
162
+ set :scheduler, :scheduled
163
+
164
+ class Trail
165
+ attr_accessor :s
166
+ def initialize
167
+ @s = ""
168
+ end
169
+ end
170
+
171
+ # Appends its name to the shared trail once per resume, `count`
172
+ # times, yielding plainly (wake_at stays -1 = due again next pass).
173
+ class Hopper
174
+ attr_accessor :fiber, :name, :trail, :remaining
175
+ def initialize(name, trail, count)
176
+ @name = name
177
+ @trail = trail
178
+ @remaining = count
179
+ @fiber = Fiber.new { run }
180
+ end
181
+ def run
182
+ while @remaining > 0
183
+ @trail.s = @trail.s + @name
184
+ @remaining -= 1
185
+ Fiber.yield
186
+ end
187
+ end
188
+ end
189
+
190
+ get '/round_robin' do
191
+ t = Trail.new
192
+ a = Hopper.new("A", t, 2)
193
+ b = Hopper.new("B", t, 2)
194
+ c = Hopper.new("C", t, 2)
195
+ Tep::Scheduler.spawn_fiber(a.fiber)
196
+ Tep::Scheduler.spawn_fiber(b.fiber)
197
+ Tep::Scheduler.spawn_fiber(c.fiber)
198
+ # Hand the scheduler a few passes so the hoppers get to run.
199
+ Tep::Scheduler.pause(0)
200
+ Tep::Scheduler.pause(0)
201
+ Tep::Scheduler.pause(0)
202
+ t.s
203
+ end
204
+ RB
205
+
206
+ def test_due_fibers_are_resumed_round_robin
207
+ res = get("/round_robin")
208
+ assert_equal "200", res.code
209
+ trail = res.body
210
+ assert_equal 6, trail.length, "trail=#{trail}"
211
+ # Each hopper runs once before any runs twice. The old picker
212
+ # resumed only the lowest slot among equally-due fibers per tick,
213
+ # which ran A to completion before B ever started ("AABBCC").
214
+ assert_equal %w[A B C], trail[0, 3].chars.sort, "trail=#{trail}"
215
+ assert_equal %w[A B C], trail[3, 3].chars.sort, "trail=#{trail}"
216
+ end
217
+ end
@@ -30,6 +30,10 @@ class TestServerScheduled < TepTest
30
30
  post "/echo_body" do
31
31
  request.body
32
32
  end
33
+
34
+ get "/sched_slots" do
35
+ Tep::APP.sched_fibers.length.to_s
36
+ end
33
37
  RB
34
38
 
35
39
  def test_basic_get_through_scheduler
@@ -91,4 +95,50 @@ class TestServerScheduled < TepTest
91
95
  assert_equal "pong", r2.body
92
96
  end
93
97
  end
98
+
99
+ def test_burst_of_new_connections_each_get_their_own_fiber
100
+ # Connections that arrive together are accepted on consecutive
101
+ # scheduler passes, each before the previous one's fiber has run.
102
+ # The fiber must own the fd it was spawned for: when it read the
103
+ # accept loop's variable instead, two fibers served the newest
104
+ # connection and the earlier one was never answered.
105
+ #
106
+ # The pause lets the harness's port probe (itself a connection)
107
+ # be accepted and finished first; arriving with the burst it takes
108
+ # the lost slot and every real request is still answered.
109
+ sleep 0.3
110
+ socks = Array.new(16) { TCPSocket.new("127.0.0.1", @port) }
111
+ sleep 0.3
112
+ socks.each_with_index do |s, i|
113
+ s.write("GET /echo/c#{i} HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n")
114
+ end
115
+ got = socks.map do |s|
116
+ s.wait_readable(5) ? s.read.to_s.split("\r\n\r\n", 2)[1] : nil
117
+ end
118
+ assert_equal (0...16).map { |i| "c#{i}" }, got
119
+ ensure
120
+ socks.each(&:close) if socks
121
+ end
122
+
123
+ def test_connection_churn_does_not_grow_scheduler_slots
124
+ # Overlapping connections: open the next one before closing the
125
+ # previous, so a live connection fiber always sits at the tail of
126
+ # the scheduler's slot arrays. Tail-only reclaim can't free the
127
+ # dead slots behind it; without slot reuse every closed connection
128
+ # leaves one behind (plus its fiber stack) for the worker's life.
129
+ prev = nil
130
+ 60.times do
131
+ h = Net::HTTP.new("127.0.0.1", @port)
132
+ h.read_timeout = 3
133
+ h.start
134
+ assert_equal "pong", h.get("/ping").body
135
+ prev.finish if prev
136
+ prev = h
137
+ end
138
+ slots = get("/sched_slots").body.to_i
139
+ prev.finish
140
+ # accept fiber + the two overlapping connections + this request's.
141
+ assert_operator slots, :<=, 8,
142
+ "scheduler kept #{slots} slots after 60 churned connections"
143
+ end
94
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.10
4
+ version: 0.11.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ori Pekelman