-
Notifications
You must be signed in to change notification settings - Fork 3
/
upgrader_test.go
385 lines (337 loc) · 11.1 KB
/
upgrader_test.go
1
2
3
4
5
6
7
8
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
35
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package tableroll
import (
"context"
"io"
"math/rand"
"net"
"net/http"
"net/http/httptest"
"os"
"runtime"
"strconv"
"testing"
"time"
"github.com/inconshreveable/log15"
"github.com/stretchr/testify/require"
"k8s.io/utils/clock"
fakeclock "k8s.io/utils/clock/testing"
)
var l = log15.New()
// TestGCingUpgradeHandoff tests that the upgradehandoff test works even with
// gc running more frequently.
// This test exists because there was a point when `fd`s were owned by two file
// objects, and one file being gc'd would break everything.
func TestGCingUpgradeHandoff(t *testing.T) {
done := make(chan struct{})
go func() {
for {
select {
case <-done:
return
default:
}
runtime.GC()
time.Sleep(10 * time.Nanosecond)
}
}()
for i := 0; i < 5; i++ {
time.Sleep(10 * time.Nanosecond)
TestUpgradeHandoff(t)
}
done <- struct{}{}
}
// TestUpgradeHandoff tests the happy path flow of two servers handing off the listening socket.
// It includes one client connection that spans the listener handoff and must be drained.
func TestUpgradeHandoff(t *testing.T) {
coordDir := tmpDir(t)
// Server 1 starts listening
server1Reqs, server1Msgs, upg1, s1 := createTestServer(t, clock.RealClock{}, 1, coordDir)
defer s1.Close()
defer upg1.Stop()
c1 := s1.Client()
c1t := c1.Transport.(closeIdleTransport)
go func() {
<-server1Reqs
server1Msgs <- "msg1"
}()
assertResp(t, s1.URL, c1, "msg1")
// s1 is listening
// leave a hanging client connection for s1 before upgrading, then read it after upgrading
msg2Response := make(chan struct{})
go func() {
assertResp(t, s1.URL, c1, "msg2")
msg2Response <- struct{}{}
}()
<-server1Reqs
// now have s2 take over for s1
server2Reqs, server2Msgs, upg2, s2 := createTestServer(t, clock.RealClock{}, 2, coordDir)
defer upg2.Stop()
defer s2.Close()
<-upg1.UpgradeComplete()
s1.Listener.Close()
// make sure the existing tcp connections aren't re-used anymore
c1t.CloseIdleConnections()
go func() {
<-server2Reqs
server2Msgs <- "msg3"
}()
// Using the client for s1 should work for s2 now since the listener was passed along
assertResp(t, s1.URL, c1, "msg3")
// Hanging server1 request should still be service-able even after s2 has taken over
server1Msgs <- "msg2"
<-msg2Response
}
func TestMutableUpgrading(t *testing.T) {
coordDir := tmpDir(t)
upg1, err := newUpgrader(context.Background(), clock.RealClock{}, coordDir, "1", WithLogger(l))
require.NoError(t, err)
require.NoError(t, upg1.Ready())
defer upg1.Stop()
upgradeDone := make(chan error, 1)
expectedFis := map[string]*os.File{}
// Mutably add a bunch of fds to the store at random, make sure that all the ones that were added without error are inherited
go func() {
var err error
var fi *os.File
for err != ErrUpgradeCompleted {
// add 2/3 of the time, remove 1/3 of the time, pool of 1000 ids
id := strconv.Itoa(rand.Intn(1000))
if expectedFis[id] != nil {
if err = upg1.Fds.Remove(id); err == nil {
delete(expectedFis, id)
} else if err != ErrUpgradeInProgress && err != ErrUpgradeCompleted {
upgradeDone <- err
return
}
} else {
if fi, err = upg1.Fds.OpenFileWith(id, id, memoryOpenFile); err == nil {
expectedFis[id] = fi
} else if err != ErrUpgradeInProgress && err != ErrUpgradeCompleted {
upgradeDone <- err
return
}
}
}
close(upgradeDone)
}()
upg2, err := newUpgrader(context.Background(), clock.RealClock{}, coordDir, "2", WithLogger(l))
require.NoError(t, err)
require.NoError(t, upg2.Ready())
// we expect that upg1 should have gotten a terminal error and we should have
// got the full set of ids it thinks it stored
require.NoError(t, <-upgradeDone)
for id, expectedFi := range expectedFis {
if fi, err := upg2.Fds.File(id); err != nil {
t.Errorf("expected upg2 to have file for %v of %#v, but had file %#v, err %v", id, expectedFi, fi, err)
}
}
upg2.Stop()
<-upg2.UpgradeComplete()
}
// TestPIDReuse verifies that if a new server gets a pid of a previous server,
// it can still listen on the `${pid}.sock` socket correctly.
func TestPIDReuse(t *testing.T) {
coordDir := tmpDir(t)
// Server 1 starts listening
server1Reqs, server1Msgs, upg1, s1 := createTestServer(t, clock.RealClock{}, 1, coordDir)
defer s1.Close()
defer upg1.Stop()
c1 := s1.Client()
c1t := c1.Transport.(closeIdleTransport)
go func() {
<-server1Reqs
server1Msgs <- "msg1"
}()
assertResp(t, s1.URL, c1, "msg1")
// s1 is listening
// now have s2 take over for s1
server2Reqs, server2Msgs, upg2, s2 := createTestServer(t, clock.RealClock{}, 2, coordDir)
defer upg2.Stop()
defer s2.Close()
<-upg1.UpgradeComplete()
// Shut down server 1, have a new server reuse it
s1.Listener.Close()
c1t.CloseIdleConnections()
upg1.Stop()
go func() {
<-server2Reqs
server2Msgs <- "msg2"
}()
// Using the client for s1 should work for s2 now since the listener was passed along
assertResp(t, s1.URL, c1, "msg2")
// server 3, reusing pid1
server1Reqs, server1Msgs, upg3, s3 := createTestServer(t, clock.RealClock{}, 1, coordDir)
defer upg3.Stop()
defer s3.Close()
<-upg2.UpgradeComplete()
s2.Close()
go func() {
<-server1Reqs
server1Msgs <- "msg3"
}()
assertResp(t, s1.URL, c1, "msg3")
}
// TestFdPassMultipleTimes tests that a given owner process can attempt to pass
// the same listening fds to multiple processes if a 'Ready' is not received in
// time.
func TestFdPassMultipleTimes(t *testing.T) {
ctx := context.Background()
clock := fakeclock.NewFakeClock(time.Now())
coordDir := tmpDir(t)
server1Reqs, server1Msgs, upg1, s1 := createTestServer(t, clock, 1, coordDir)
defer upg1.Stop()
defer s1.Close()
c1 := s1.Client()
c1t := c1.Transport.(closeIdleTransport)
go func() {
<-server1Reqs
server1Msgs <- "msg1"
}()
assertResp(t, s1.URL, c1, "msg1")
// s1 listening
syncUpgraderTimeout := make(chan error, 1)
go func() {
// Now make an s2 that fails to ready-up
upg2, err := newUpgrader(ctx, clock, coordDir, "2", WithLogger(l))
if err != nil {
syncUpgraderTimeout <- err
return
}
// now the upgrader is connected, but not 'Ready', so the server should be
// waiting on a ready timeout soon
syncUpgraderTimeout <- nil
// wait for the server to get a timeout before we stop, otherwise
// `HasWaiters` could deadlock due to `Stop` causing the server to not
// timeout
<-syncUpgraderTimeout
upg2.Stop()
}()
require.NoError(t, <-syncUpgraderTimeout)
// wait for the upgrade server to start waiting for a ready, then skip
// forward 3 minutes since the timeout is 2 minutes by default
for !clock.HasWaiters() {
time.Sleep(1 * time.Millisecond)
}
clock.Step(3 * time.Minute)
close(syncUpgraderTimeout)
// now see that we get a working s3
server3Reqs, server3Msgs, upg3, s3 := createTestServer(t, clock, 3, coordDir)
defer upg3.Stop()
defer s3.Close()
<-upg1.UpgradeComplete()
s1.Listener.Close()
c1t.CloseIdleConnections()
go func() {
<-server3Reqs
server3Msgs <- "msg3"
}()
assertResp(t, s1.URL, c1, "msg3")
}
// TestUpgradeHandoffCloseCtx closes the 'New' context as soon as possible and
// verifies that the context was only for instantiation.
func TestUpgradeHandoffCloseCtx(t *testing.T) {
coordDir := tmpDir(t)
ctx1, cancel1 := context.WithTimeout(context.Background(), 1*time.Second)
upg1, err := newUpgrader(ctx1, clock.RealClock{}, coordDir, "1", WithLogger(l))
require.NoError(t, err)
defer upg1.Stop()
cancel1()
require.NoError(t, upg1.Ready())
ctx2, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
upg2, err := newUpgrader(ctx2, clock.RealClock{}, coordDir, "2", WithLogger(l))
require.NoError(t, err)
defer upg2.Stop()
cancel2()
require.NoError(t, upg2.Ready())
}
func TestUpgradeTimeout(t *testing.T) {
ctx := context.Background()
clock := fakeclock.NewFakeClock(time.Now())
coordDir := tmpDir(t)
// If upg1 times out serving the upgrade, upg2 should not be able to think it's the owner
upg1, err := newUpgrader(ctx, clock, coordDir, "1", WithLogger(l.New("pid", "1")), WithUpgradeTimeout(30*time.Millisecond))
require.NoError(t, err)
require.NoError(t, upg1.Ready())
upg2, err := newUpgrader(ctx, clock, coordDir, "2", WithLogger(l.New("pid", "2")))
require.NoError(t, err)
// upg1 serve timeout
for !clock.HasWaiters() {
time.Sleep(1 * time.Millisecond)
}
clock.Step(40 * time.Millisecond)
// Hack: we need to wait for upg2 to actually close the connection/file as
// part of the timeout, so wait a sec to make sure they're closed...
// A more proper fix would be to let us instrument the upgrader with a
// callback or upgrade failure channel so we can explicitly wait for the timeout here.
time.Sleep(10 * time.Millisecond)
if err := upg2.Ready(); err == nil {
t.Fatalf("should not be able to mark as ready after parent timed out")
}
}
// TestFTestFailedUpgradeAccept tests that 'ln.Accept' works for a listener
// correctly after a failed upgrade. This is a regression test for a bug that
// left file descriptors in 'blocking' mode, which resulted in accept + close
// deadlocking.
func TestFailedUpgradeListen(t *testing.T) {
ctx := context.Background()
coordDir := tmpDir(t)
upg1, err := newUpgrader(ctx, clock.RealClock{}, coordDir, "1", WithLogger(l.New("pid", "1")))
require.Nil(t, err)
ln, err := upg1.Fds.Listen(ctx, "id", &net.ListenConfig{}, "tcp", "127.0.0.1:0")
require.Nil(t, err)
require.NoError(t, upg1.Ready())
// fail an upgrade
upg2, err := newUpgrader(ctx, clock.RealClock{}, coordDir, "2", WithLogger(l.New("pid", "2")))
require.Nil(t, err)
upg2.Stop()
// Accept, then Close
go func() {
_, err := ln.Accept()
require.Contains(t, err.Error(), "use of closed network connection")
}()
// let the accept happen first
time.Sleep(1 * time.Millisecond)
err = ln.Close()
require.Nil(t, err)
// if we aren't deadlocked here, the regression test passes
}
func assertResp(t *testing.T, url string, c *http.Client, expected string) {
resp, err := c.Get(url)
require.NoError(t, err)
respData, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, expected, string(respData))
}
func createTestServer(t *testing.T, clock clock.Clock, pid int, coordDir string) (chan struct{}, chan string, *Upgrader, *httptest.Server) {
requests := make(chan struct{})
responses := make(chan string)
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
l.Info("server got a request", "pid", pid)
// Let the test harness know a client is waiting on us
requests <- struct{}{}
// And now respond, as requested by the test harness
resp := <-responses
_, err := w.Write([]byte(resp))
if err != nil {
panic(err)
}
}))
upg, err := newUpgrader(context.Background(), clock, coordDir, strconv.Itoa(pid), WithLogger(l))
require.NoError(t, err)
listen, err := upg.Fds.Listen(context.Background(), "testListen", nil, "tcp", "127.0.0.1:0")
require.NoError(t, err)
server.Listener = listen
server.Start()
require.NoError(t, upg.Ready())
return requests, responses, upg, server
}
func memoryOpenFile(name string) (*os.File, error) {
_, w, err := os.Pipe()
if err != nil {
panic(err)
}
return w, nil
}
type closeIdleTransport interface {
CloseIdleConnections()
}