Skip to content

Commit b481d7a

Browse files
authored
Fix Reload Workers (#1250)
* wait for one worker to be reloaded * wait for one worker to be reloaded * update defaults
1 parent cb94c58 commit b481d7a

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

internal/config/defaults.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const (
1717
DefNginxApiTlsCa = ""
1818

1919
// Nginx Reload Backoff defaults
20-
DefNginxReloadBackoffInitialInterval = 1 * time.Second
20+
DefNginxReloadBackoffInitialInterval = 500 * time.Millisecond
2121
DefNginxReloadBackoffRandomizationFactor = 0.5 // the value is 0 <= and < 1
22-
DefNginxReloadBackoffMultiplier = 5
23-
DefNginxReloadBackoffMaxInterval = 10 * time.Second
24-
DefNginxReloadBackoffMaxElapsedTime = 30 * time.Second
22+
DefNginxReloadBackoffMultiplier = 2
23+
DefNginxReloadBackoffMaxInterval = 3 * time.Second
24+
DefNginxReloadBackoffMaxElapsedTime = 10 * time.Second
2525

2626
DefCommandServerHostKey = ""
2727
DefCommandServerPortKey = 0

internal/resource/nginx_instance_operator.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (i *NginxInstanceOperator) Validate(ctx context.Context, instance *mpi.Inst
6262
}
6363

6464
func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instance) error {
65-
var reloadTime time.Time
65+
var createdTime time.Time
6666
var errorsFound error
6767
pid := instance.GetInstanceRuntime().GetProcessId()
6868

@@ -72,7 +72,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
7272
workers := i.nginxProcessOperator.NginxWorkerProcesses(ctx, pid)
7373

7474
if len(workers) > 0 {
75-
reloadTime = workers[0].Created
75+
createdTime = workers[0].Created
7676
}
7777

7878
errorLogs := i.errorLogs(instance)
@@ -92,7 +92,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
9292
slog.WarnContext(ctx, "Error finding parent process ID, unable to check if NGINX worker "+
9393
"processes have reloaded", "error", procErr)
9494
} else {
95-
i.checkWorkers(ctx, instance.GetInstanceMeta().GetInstanceId(), reloadTime, processes)
95+
i.checkWorkers(ctx, instance.GetInstanceMeta().GetInstanceId(), createdTime, processes)
9696
}
9797

9898
slog.InfoContext(ctx, "NGINX reloaded", "process_id", pid)
@@ -117,7 +117,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
117117
return nil
118118
}
119119

120-
func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID string, reloadTime time.Time,
120+
func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID string, createdTime time.Time,
121121
processes []*nginxprocess.Process,
122122
) {
123123
backoffSettings := &config.BackOff{
@@ -148,13 +148,13 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
148148
}
149149

150150
for _, worker := range currentWorkers {
151-
if !worker.Created.After(reloadTime) {
152-
return fmt.Errorf("waiting for all NGINX workers to be newer "+
153-
"than %v, found worker with time %v", reloadTime, worker.Created)
151+
if worker.Created.After(createdTime) {
152+
return nil
154153
}
155154
}
156155

157-
return nil
156+
return fmt.Errorf("waiting for NGINX worker to be newer "+
157+
"than %v", createdTime)
158158
})
159159
if err != nil {
160160
slog.WarnContext(ctx, "Failed to check if NGINX worker processes have successfully reloaded, "+
@@ -163,7 +163,7 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
163163
return
164164
}
165165

166-
slog.InfoContext(ctx, "All NGINX workers have been reloaded")
166+
slog.InfoContext(ctx, "NGINX workers have been reloaded")
167167
}
168168

169169
func (i *NginxInstanceOperator) validateConfigCheckResponse(out []byte) error {

internal/resource/nginx_instance_operator_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
282282
}{
283283
{
284284
name: "Test 1: Successful reload",
285-
expectedLog: "All NGINX workers have been reloaded",
285+
expectedLog: "NGINX workers have been reloaded",
286286
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
287287
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
288288
workers: []*nginxprocess.Process{
@@ -315,7 +315,41 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
315315
},
316316
},
317317
{
318-
name: "Test 2: Unsuccessful reload",
318+
name: "Test 2: Successful reload - one workers is reloaded",
319+
expectedLog: "NGINX workers have been reloaded",
320+
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
321+
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
322+
masterProcess: []*nginxprocess.Process{
323+
{
324+
PID: 1234,
325+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
326+
PPID: 1,
327+
Name: "nginx",
328+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
329+
Exe: exePath,
330+
},
331+
},
332+
workers: []*nginxprocess.Process{
333+
{
334+
PID: 567,
335+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
336+
PPID: 1234,
337+
Name: "nginx",
338+
Cmd: "nginx: worker process",
339+
Exe: exePath,
340+
},
341+
{
342+
PID: 789,
343+
PPID: 1234,
344+
Created: time.Date(2025, 8, 13, 7, 1, 0, 0, time.Local),
345+
Name: "nginx",
346+
Cmd: "nginx: worker process",
347+
Exe: exePath,
348+
},
349+
},
350+
},
351+
{
352+
name: "Test 3: Unsuccessful reload",
319353
expectedLog: "\"Failed to check if NGINX worker processes have successfully reloaded, timed out " +
320354
"waiting\" error=\"waiting for NGINX worker processes\"",
321355
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
@@ -333,7 +367,7 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
333367
workers: []*nginxprocess.Process{
334368
{
335369
PID: 567,
336-
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
370+
Created: time.Date(2025, 8, 13, 7, 1, 0, 0, time.Local),
337371
PPID: 1234,
338372
Name: "nginx",
339373
Cmd: "nginx: worker process",

0 commit comments

Comments
 (0)