From cfd3d0b0575eb95d1e3972f85988690766e37503 Mon Sep 17 00:00:00 2001 From: Ted Date: Wed, 26 Jul 2023 11:15:42 -0400 Subject: [PATCH] Move housekeepingProjectPage variable into the Housekeeping.run function --- server/backend/database/database.go | 1 + server/backend/database/memory/database.go | 16 +++++++-------- .../database/memory/housekeeping_test.go | 2 ++ server/backend/database/mongo/client.go | 20 +++++++++---------- server/backend/housekeeping/housekeeping.go | 7 +++++-- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/server/backend/database/database.go b/server/backend/database/database.go index e9e694e01..0f8961c24 100644 --- a/server/backend/database/database.go +++ b/server/backend/database/database.go @@ -139,6 +139,7 @@ type Database interface { ctx context.Context, candidatesLimitPerProject int, projectFetchSize int, + projectPage *int, ) ([]*ClientInfo, error) // FindDocInfoByKey finds the document of the given key. diff --git a/server/backend/database/memory/database.go b/server/backend/database/memory/database.go index 6e5d290c6..69d997e61 100644 --- a/server/backend/database/memory/database.go +++ b/server/backend/database/memory/database.go @@ -36,8 +36,7 @@ import ( // DB is an in-memory database for testing or temporarily. type DB struct { - db *memdb.MemDB - housekeepingProjectPage int + db *memdb.MemDB } // New returns a new in-memory database. @@ -48,8 +47,7 @@ func New() (*DB, error) { } return &DB{ - db: memDB, - housekeepingProjectPage: 0, + db: memDB, }, nil } @@ -230,11 +228,12 @@ func (d *DB) CreateProjectInfo( func (d *DB) listProjectInfos( ctx context.Context, pageSize int, + page *int, ) ([]*database.ProjectInfo, error) { txn := d.db.Txn(false) defer txn.Abort() - offset := d.housekeepingProjectPage * pageSize + offset := (*page) * pageSize iter, err := txn.Get( tblProjects, "id", @@ -252,14 +251,14 @@ func (d *DB) listProjectInfos( for i := 0; i < pageSize; i++ { raw := iter.Next() if raw == nil { - d.housekeepingProjectPage = 0 + *page = 0 break } info := raw.(*database.ProjectInfo).DeepCopy() infos = append(infos, info) } - d.housekeepingProjectPage++ + *page++ return infos, nil } @@ -612,8 +611,9 @@ func (d *DB) FindDeactivateCandidates( ctx context.Context, candidatesLimitPerProject int, projectFetchSize int, + projectPage *int, ) ([]*database.ClientInfo, error) { - projects, err := d.listProjectInfos(ctx, projectFetchSize) + projects, err := d.listProjectInfos(ctx, projectFetchSize, projectPage) if err != nil { return nil, err } diff --git a/server/backend/database/memory/housekeeping_test.go b/server/backend/database/memory/housekeeping_test.go index 7f04606c8..db6dc6ee0 100644 --- a/server/backend/database/memory/housekeeping_test.go +++ b/server/backend/database/memory/housekeeping_test.go @@ -59,10 +59,12 @@ func TestHousekeeping(t *testing.T) { clientC, err := memdb.ActivateClient(ctx, project.ID, fmt.Sprintf("%s-C", t.Name())) assert.NoError(t, err) + page := 0 candidates, err := memdb.FindDeactivateCandidates( ctx, 10, 10, + &page, ) assert.NoError(t, err) assert.Len(t, candidates, 2) diff --git a/server/backend/database/mongo/client.go b/server/backend/database/mongo/client.go index 079b98cbd..a6be57c4b 100644 --- a/server/backend/database/mongo/client.go +++ b/server/backend/database/mongo/client.go @@ -42,9 +42,8 @@ import ( // Client is a client that connects to Mongo DB and reads or saves Yorkie data. type Client struct { - config *Config - client *mongo.Client - housekeepingProjectPage int + config *Config + client *mongo.Client } // Dial creates an instance of Client and dials the given MongoDB. @@ -77,9 +76,8 @@ func Dial(conf *Config) (*Client, error) { logging.DefaultLogger().Infof("MongoDB connected, URI: %s, DB: %s", conf.ConnectionURI, conf.YorkieDatabase) return &Client{ - config: conf, - client: client, - housekeepingProjectPage: 0, + config: conf, + client: client, }, nil } @@ -241,9 +239,10 @@ func (c *Client) CreateProjectInfo( func (c *Client) listProjectInfos( ctx context.Context, pageSize int, + page *int, ) ([]*database.ProjectInfo, error) { opts := options.Find() - opts.SetSkip(int64(c.housekeepingProjectPage * pageSize)) + opts.SetSkip(int64((*page) * pageSize)) opts.SetLimit(int64(pageSize)) cursor, err := c.collection(colProjects).Find(ctx, bson.D{{}}, opts) @@ -258,10 +257,10 @@ func (c *Client) listProjectInfos( isLastPage := len(infos) < pageSize if isLastPage { - c.housekeepingProjectPage = 0 + *page = 0 } - c.housekeepingProjectPage++ + *page++ return infos, nil } @@ -668,8 +667,9 @@ func (c *Client) FindDeactivateCandidates( ctx context.Context, candidatesLimitPerProject int, projectFetchSize int, + projectPage *int, ) ([]*database.ClientInfo, error) { - projects, err := c.listProjectInfos(ctx, projectFetchSize) + projects, err := c.listProjectInfos(ctx, projectFetchSize, projectPage) if err != nil { return nil, err } diff --git a/server/backend/housekeeping/housekeeping.go b/server/backend/housekeeping/housekeeping.go index fd394a01c..18a89b71b 100644 --- a/server/backend/housekeeping/housekeeping.go +++ b/server/backend/housekeeping/housekeeping.go @@ -107,9 +107,11 @@ func (h *Housekeeping) Stop() error { // run is the housekeeping loop. func (h *Housekeeping) run() { + housekeepingProjectPage := 0 + for { ctx := context.Background() - if err := h.deactivateCandidates(ctx); err != nil { + if err := h.deactivateCandidates(ctx, &housekeepingProjectPage); err != nil { continue } @@ -122,7 +124,7 @@ func (h *Housekeeping) run() { } // deactivateCandidates deactivates candidates. -func (h *Housekeeping) deactivateCandidates(ctx context.Context) error { +func (h *Housekeeping) deactivateCandidates(ctx context.Context, page *int) error { start := time.Now() locker, err := h.coordinator.NewLocker(ctx, deactivateCandidatesKey) if err != nil { @@ -143,6 +145,7 @@ func (h *Housekeeping) deactivateCandidates(ctx context.Context) error { ctx, h.candidatesLimitPerProject, h.projectFetchSize, + page, ) if err != nil { return err