Skip to content

Commit

Permalink
Move housekeepingProjectPage variable into the Housekeeping.run function
Browse files Browse the repository at this point in the history
  • Loading branch information
tedkimdev committed Jul 26, 2023
1 parent e20e17c commit cfd3d0b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions server/backend/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,8 +47,7 @@ func New() (*DB, error) {
}

return &DB{
db: memDB,
housekeepingProjectPage: 0,
db: memDB,
}, nil
}

Expand Down Expand Up @@ -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",
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions server/backend/database/memory/housekeeping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions server/backend/housekeeping/housekeeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -143,6 +145,7 @@ func (h *Housekeeping) deactivateCandidates(ctx context.Context) error {
ctx,
h.candidatesLimitPerProject,
h.projectFetchSize,
page,
)
if err != nil {
return err
Expand Down

0 comments on commit cfd3d0b

Please sign in to comment.