Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6e415b3

Browse files
committed
Merge pull requestsorintlab#442 from sgotti/keeper_improve_postgres_instance_stop
keeper: improve postgres instance stop
2 parentsf17ae60 +df006e6 commit6e415b3

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

‎cmd/keeper/keeper.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func (p *PostgresKeeper) Start(ctx context.Context) {
681681
pgm:=postgresql.NewManager(p.pgBinPath,p.dataDir,p.getLocalConnParams(),p.getLocalReplConnParams(),p.pgSUAuthMethod,p.pgSUUsername,p.pgSUPassword,p.pgReplAuthMethod,p.pgReplUsername,p.pgReplPassword,p.requestTimeout)
682682
p.pgm=pgm
683683

684-
p.pgm.Stop(true)
684+
p.pgm.StopIfStarted(true)
685685

686686
smTimerCh:=time.NewTimer(0).C
687687
updatePGStateTimerCh:=time.NewTimer(0).C
@@ -690,7 +690,9 @@ func (p *PostgresKeeper) Start(ctx context.Context) {
690690
select {
691691
case<-ctx.Done():
692692
log.Debugw("stopping stolon keeper")
693-
p.pgm.Stop(true)
693+
iferr=p.pgm.StopIfStarted(true);err!=nil {
694+
log.Errorw("failed to stop pg instance",zap.Error(err))
695+
}
694696
p.end<-nil
695697
return
696698

@@ -896,7 +898,9 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
896898

897899
ifp.bootUUID!=k.Status.BootUUID {
898900
log.Infow("our db boot UID is different than the cluster data one, waiting for it to be updated","bootUUID",p.bootUUID,"clusterBootUUID",k.Status.BootUUID)
899-
pgm.Stop(true)
901+
iferr=pgm.StopIfStarted(true);err!=nil {
902+
log.Errorw("failed to stop pg instance",zap.Error(err))
903+
}
900904
return
901905
}
902906

@@ -912,7 +916,10 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
912916
// resync has failed so we have to clean up stale data
913917
log.Errorw("db failed to initialize or resync")
914918

915-
pgm.Stop(true)
919+
iferr=pgm.StopIfStarted(true);err!=nil {
920+
log.Errorw("failed to stop pg instance",zap.Error(err))
921+
return
922+
}
916923

917924
// Clean up cluster db datadir
918925
iferr=pgm.RemoveAll();err!=nil {
@@ -993,7 +1000,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
9931000
}
9941001

9951002
ifstarted {
996-
iferr=pgm.Stop(true);err!=nil {
1003+
iferr=pgm.StopIfStarted(true);err!=nil {
9971004
log.Errorw("failed to stop pg instance",zap.Error(err))
9981005
return
9991006
}
@@ -1047,7 +1054,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
10471054
log.Errorw("failed to save db local state",zap.Error(err))
10481055
return
10491056
}
1050-
iferr=pgm.Stop(true);err!=nil {
1057+
iferr=pgm.StopIfStarted(true);err!=nil {
10511058
log.Errorw("failed to stop pg instance",zap.Error(err))
10521059
return
10531060
}
@@ -1071,7 +1078,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
10711078
pgm.SetParameters(pgParameters)
10721079

10731080
ifstarted {
1074-
iferr=pgm.Stop(true);err!=nil {
1081+
iferr=pgm.StopIfStarted(true);err!=nil {
10751082
log.Errorw("failed to stop pg instance",zap.Error(err))
10761083
return
10771084
}
@@ -1127,7 +1134,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
11271134
log.Errorw("failed to save db local state",zap.Error(err))
11281135
return
11291136
}
1130-
iferr=pgm.Stop(true);err!=nil {
1137+
iferr=pgm.StopIfStarted(true);err!=nil {
11311138
log.Errorw("failed to stop pg instance",zap.Error(err))
11321139
return
11331140
}
@@ -1147,7 +1154,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
11471154
}
11481155

11491156
ifstarted {
1150-
iferr=pgm.Stop(true);err!=nil {
1157+
iferr=pgm.StopIfStarted(true);err!=nil {
11511158
log.Errorw("failed to stop pg instance",zap.Error(err))
11521159
return
11531160
}
@@ -1230,7 +1237,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
12301237

12311238
iffullResync {
12321239
ifstarted {
1233-
iferr=pgm.Stop(true);err!=nil {
1240+
iferr=pgm.StopIfStarted(true);err!=nil {
12341241
log.Errorw("failed to stop pg instance",zap.Error(err))
12351242
return
12361243
}
@@ -1263,7 +1270,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
12631270
pgm.SetParameters(pgParameters)
12641271

12651272
ifstarted {
1266-
iferr=pgm.Stop(true);err!=nil {
1273+
iferr=pgm.StopIfStarted(true);err!=nil {
12671274
log.Errorw("failed to stop pg instance",zap.Error(err))
12681275
return
12691276
}
@@ -1305,7 +1312,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
13051312
log.Errorw("failed to save db local state",zap.Error(err))
13061313
return
13071314
}
1308-
iferr=pgm.Stop(true);err!=nil {
1315+
iferr=pgm.StopIfStarted(true);err!=nil {
13091316
log.Errorw("failed to stop pg instance",zap.Error(err))
13101317
return
13111318
}

‎pkg/postgresql/postgresql.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package postgresql
1717
import (
1818
"bufio"
1919
"context"
20+
"errors"
2021
"fmt"
2122
"io/ioutil"
2223
"os"
@@ -45,6 +46,10 @@ const (
4546
startTimeout=60*time.Second
4647
)
4748

49+
var (
50+
ErrUnknownState=errors.New("unknown postgres state")
51+
)
52+
4853
varlog=slog.S()
4954

5055
typeManagerstruct {
@@ -337,6 +342,8 @@ func (p *Manager) start(args ...string) error {
337342
returnnil
338343
}
339344

345+
// Stop tries to stop an instance. An error will be returned if the instance isn't started, stop fails or
346+
// times out (60 second).
340347
func (p*Manager)Stop(fastbool)error {
341348
log.Infow("stopping database")
342349
name:=filepath.Join(p.pgBinPath,"pg_ctl")
@@ -365,6 +372,9 @@ func (p *Manager) IsStarted() (bool, error) {
365372
ifstatus==3 {
366373
returnfalse,nil
367374
}
375+
ifstatus==4 {
376+
returnfalse,ErrUnknownState
377+
}
368378
}
369379
returnfalse,fmt.Errorf("cannot get instance state: %v",err)
370380
}
@@ -396,9 +406,39 @@ func (p *Manager) Reload() error {
396406
returnnil
397407
}
398408

409+
// StopIfStarted checks if the instance is started, then calls stop and
410+
// then check if the instance is really stopped
411+
func (p*Manager)StopIfStarted(fastbool)error {
412+
// Stop will return an error if the instance isn't started, so first check
413+
// if it's started
414+
started,err:=p.IsStarted()
415+
iferr!=nil {
416+
iferr==ErrUnknownState {
417+
// if IsStarted returns an unknown state error then assume that the
418+
// instance is stopped
419+
returnnil
420+
}
421+
returnerr
422+
}
423+
if!started {
424+
returnnil
425+
}
426+
iferr=p.Stop(fast);err!=nil {
427+
returnerr
428+
}
429+
started,err=p.IsStarted()
430+
iferr!=nil {
431+
returnerr
432+
}
433+
ifstarted {
434+
returnfmt.Errorf("failed to stop")
435+
}
436+
returnnil
437+
}
438+
399439
func (p*Manager)Restart(fastbool)error {
400440
log.Infow("restarting database")
401-
iferr:=p.Stop(fast);err!=nil {
441+
iferr:=p.StopIfStarted(fast);err!=nil {
402442
returnerr
403443
}
404444
iferr:=p.Start();err!=nil {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp