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

Commit07b9dfb

Browse files
Store only last 2 postgres timeline histories
Co-authored-by: Arunvel Sriram <arunvelsriram@gmail.com>
1 parenta27bcae commit07b9dfb

File tree

4 files changed

+243
-20
lines changed

4 files changed

+243
-20
lines changed

‎cmd/keeper/cmd/keeper.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var CmdKeeper = &cobra.Command{
5757
Version:cmd.Version,
5858
}
5959

60+
constmaxPostgresTimelinesHistory=2
61+
6062
typeKeeperLocalStatestruct {
6163
UIDstring
6264
ClusterUIDstring
@@ -676,27 +678,12 @@ func (p *PostgresKeeper) GetPGState(pctx context.Context) (*cluster.PostgresStat
676678
pgState.TimelineID=sd.TimelineID
677679
pgState.XLogPos=sd.XLogPos
678680

679-
// if timeline <= 1 then no timeline history file exists.
680-
pgState.TimelinesHistory= cluster.PostgresTimelinesHistory{}
681-
ifpgState.TimelineID>1 {
682-
vartlsh []*postgresql.TimelineHistory
683-
tlsh,err=p.pgm.GetTimelinesHistory(pgState.TimelineID)
684-
iferr!=nil {
685-
log.Errorw("error getting timeline history",zap.Error(err))
686-
returnpgState,nil
687-
}
688-
ctlsh:= cluster.PostgresTimelinesHistory{}
689-
690-
for_,tlh:=rangetlsh {
691-
ctlh:=&cluster.PostgresTimelineHistory{
692-
TimelineID:tlh.TimelineID,
693-
SwitchPoint:tlh.SwitchPoint,
694-
Reason:tlh.Reason,
695-
}
696-
ctlsh=append(ctlsh,ctlh)
697-
}
698-
pgState.TimelinesHistory=ctlsh
681+
ctlsh,err:=getTimeLinesHistory(pgState,p.pgm,maxPostgresTimelinesHistory)
682+
iferr!=nil {
683+
log.Errorw("error getting timeline history",zap.Error(err))
684+
returnpgState,nil
699685
}
686+
pgState.TimelinesHistory=ctlsh
700687

701688
ow,err:=p.pgm.OlderWalFile()
702689
iferr!=nil {
@@ -711,6 +698,31 @@ func (p *PostgresKeeper) GetPGState(pctx context.Context) (*cluster.PostgresStat
711698
returnpgState,nil
712699
}
713700

701+
funcgetTimeLinesHistory(pgState*cluster.PostgresState,pgm postgresql.PGManager,maxPostgresTimelinesHistoryint) (cluster.PostgresTimelinesHistory,error) {
702+
ctlsh:= cluster.PostgresTimelinesHistory{}
703+
// if timeline <= 1 then no timeline history file exists.
704+
ifpgState.TimelineID>1 {
705+
vartlsh []*postgresql.TimelineHistory
706+
tlsh,err:=pgm.GetTimelinesHistory(pgState.TimelineID)
707+
iferr!=nil {
708+
log.Errorw("error getting timeline history",zap.Error(err))
709+
returnctlsh,err
710+
}
711+
iflen(tlsh)>maxPostgresTimelinesHistory {
712+
tlsh=tlsh[len(tlsh)-maxPostgresTimelinesHistory:]
713+
}
714+
for_,tlh:=rangetlsh {
715+
ctlh:=&cluster.PostgresTimelineHistory{
716+
TimelineID:tlh.TimelineID,
717+
SwitchPoint:tlh.SwitchPoint,
718+
Reason:tlh.Reason,
719+
}
720+
ctlsh=append(ctlsh,ctlh)
721+
}
722+
}
723+
returnctlsh,nil
724+
}
725+
714726
func (p*PostgresKeeper)getLastPGState()*cluster.PostgresState {
715727
p.pgStateMutex.Lock()
716728
pgState:=p.lastPGState.DeepCopy()

‎cmd/keeper/cmd/keeper_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"bytes"
1919
"errors"
2020
"fmt"
21+
"github.com/golang/mock/gomock"
22+
"github.com/sorintlab/stolon/internal/mock/postgresql"
23+
"github.com/sorintlab/stolon/internal/postgresql"
2124
"reflect"
2225
"testing"
2326

@@ -248,3 +251,158 @@ func TestGenerateHBA(t *testing.T) {
248251
}
249252
}
250253
}
254+
255+
funcTestGetTimeLinesHistory(t*testing.T) {
256+
t.Run("should return empty if timelineID is not greater than 1",func(t*testing.T) {
257+
pgState:=&cluster.PostgresState{
258+
TimelineID:1,
259+
}
260+
261+
ctlsh,err:=getTimeLinesHistory(pgState,nil,3)
262+
263+
iferr!=nil {
264+
t.Errorf("err should be nil, but got %v",err)
265+
}
266+
iflen(ctlsh)!=0 {
267+
t.Errorf("expecting empty ctlsh, but got %v",ctlsh)
268+
}
269+
})
270+
271+
t.Run("should return error if there is error in getting timeline history",func(t*testing.T) {
272+
vartimelineIDuint64=2
273+
pgState:=&cluster.PostgresState{
274+
TimelineID:timelineID,
275+
}
276+
277+
ctrl:=gomock.NewController(t)
278+
deferctrl.Finish()
279+
280+
pgm:=mocks.NewMockPGManager(ctrl)
281+
pgm.EXPECT().GetTimelinesHistory(timelineID).Return([]*postgresql.TimelineHistory{},fmt.Errorf("failed to get timeline history"))
282+
ctlsh,err:=getTimeLinesHistory(pgState,pgm,3)
283+
284+
iferr==nil {
285+
t.Errorf("err should be not be nil")
286+
}
287+
iferr!=nil&&err.Error()!="failed to get timeline history" {
288+
t.Errorf("err should be failed to get timeline history, but got %v",err)
289+
}
290+
iflen(ctlsh)!=0 {
291+
t.Errorf("expecting empty ctlsh, but got %v",ctlsh)
292+
}
293+
})
294+
295+
t.Run("should return timeline history as is if the given length is less than maxPostgresTimelinesHistory",func(t*testing.T) {
296+
vartimelineIDuint64=2
297+
pgState:=&cluster.PostgresState{
298+
TimelineID:timelineID,
299+
}
300+
301+
ctrl:=gomock.NewController(t)
302+
deferctrl.Finish()
303+
304+
pgm:=mocks.NewMockPGManager(ctrl)
305+
timelineHistories:= []*postgresql.TimelineHistory{
306+
{
307+
TimelineID:1,
308+
SwitchPoint:1,
309+
Reason:"reason1",
310+
},
311+
{
312+
TimelineID:2,
313+
SwitchPoint:2,
314+
Reason:"reason2",
315+
},
316+
}
317+
pgm.EXPECT().GetTimelinesHistory(timelineID).Return(timelineHistories,nil)
318+
ctlsh,err:=getTimeLinesHistory(pgState,pgm,3)
319+
320+
iferr!=nil {
321+
t.Errorf("err should be not be nil")
322+
}
323+
iflen(ctlsh)!=2 {
324+
t.Errorf("expecting length of ctlsh to be 2, but got %d",len(ctlsh))
325+
}
326+
expectedTimelineHistories:= cluster.PostgresTimelinesHistory{
327+
&cluster.PostgresTimelineHistory{
328+
TimelineID:1,
329+
SwitchPoint:1,
330+
Reason:"reason1",
331+
},
332+
&cluster.PostgresTimelineHistory{
333+
TimelineID:2,
334+
SwitchPoint:2,
335+
Reason:"reason2",
336+
},
337+
}
338+
fmt.Println(ctlsh,expectedTimelineHistories)
339+
if*ctlsh[0]!=*expectedTimelineHistories[0] {
340+
t.Errorf("expected %v, but got %v ",*expectedTimelineHistories[0],*ctlsh[0])
341+
}
342+
if*ctlsh[1]!=*expectedTimelineHistories[1] {
343+
t.Errorf("expected %v, but got %v ",*expectedTimelineHistories[1],*ctlsh[1])
344+
}
345+
})
346+
347+
t.Run("should return timeline history with last maxPostgresTimelinesHistory elements if timeline history length is greater than maxPostgresTimelinesHistory",func(t*testing.T) {
348+
vartimelineIDuint64=4
349+
pgState:=&cluster.PostgresState{
350+
TimelineID:timelineID,
351+
}
352+
353+
ctrl:=gomock.NewController(t)
354+
deferctrl.Finish()
355+
356+
pgm:=mocks.NewMockPGManager(ctrl)
357+
timelineHistories:= []*postgresql.TimelineHistory{
358+
{
359+
TimelineID:1,
360+
SwitchPoint:1,
361+
Reason:"reason1",
362+
},
363+
{
364+
TimelineID:2,
365+
SwitchPoint:2,
366+
Reason:"reason2",
367+
},
368+
{
369+
TimelineID:3,
370+
SwitchPoint:3,
371+
Reason:"reason3",
372+
},
373+
{
374+
TimelineID:4,
375+
SwitchPoint:4,
376+
Reason:"reason4",
377+
},
378+
}
379+
pgm.EXPECT().GetTimelinesHistory(timelineID).Return(timelineHistories,nil)
380+
ctlsh,err:=getTimeLinesHistory(pgState,pgm,2)
381+
382+
iferr!=nil {
383+
t.Errorf("err should be not be nil")
384+
}
385+
iflen(ctlsh)!=2 {
386+
t.Errorf("expecting length of ctlsh to be 2, but got %d",len(ctlsh))
387+
}
388+
expectedTimelineHistories:= cluster.PostgresTimelinesHistory{
389+
&cluster.PostgresTimelineHistory{
390+
TimelineID:3,
391+
SwitchPoint:3,
392+
Reason:"reason3",
393+
},
394+
&cluster.PostgresTimelineHistory{
395+
TimelineID:4,
396+
SwitchPoint:4,
397+
Reason:"reason4",
398+
},
399+
}
400+
fmt.Println(ctlsh,expectedTimelineHistories)
401+
if*ctlsh[0]!=*expectedTimelineHistories[0] {
402+
t.Errorf("expected %v, but got %v ",*expectedTimelineHistories[0],*ctlsh[0])
403+
}
404+
if*ctlsh[1]!=*expectedTimelineHistories[1] {
405+
t.Errorf("expected %v, but got %v ",*expectedTimelineHistories[1],*ctlsh[1])
406+
}
407+
})
408+
}

‎internal/mock/postgresql/postgresql.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎internal/postgresql/postgresql.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"go.uber.org/zap"
3939
)
4040

41+
//go:generate mockgen -destination=../mock/postgresql/postgresql.go -package=mocks -source=$GOFILE
42+
4143
const (
4244
postgresConf="postgresql.conf"
4345
postgresRecoveryConf="recovery.conf"
@@ -53,6 +55,10 @@ var (
5355

5456
varlog=slog.S()
5557

58+
typePGManagerinterface {
59+
GetTimelinesHistory(timelineuint64) ([]*TimelineHistory,error)
60+
}
61+
5662
typeManagerstruct {
5763
pgBinPathstring
5864
dataDirstring

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp