Skip to content

Commit

Permalink
Add housekeeping project fetch size option to the CLI flag
Browse files Browse the repository at this point in the history
* set project fetch size to 100 in config.sample.yml

fix lint
  • Loading branch information
tedkimdev committed Jul 22, 2023
1 parent 21b6c37 commit e20e17c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 29 deletions.
6 changes: 6 additions & 0 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ func init() {
server.DefaultHousekeepingCandidatesLimitPerProject,
"candidates limit per project for a single housekeeping run",
)
cmd.Flags().IntVar(
&conf.Housekeeping.HousekeepingProjectFetchSize,
"housekeeping-project-fetch-size",
server.DefaultHousekeepingProjectFetchSize,
"housekeeping project fetch size for a single housekeeping run",
)
cmd.Flags().StringVar(
&mongoConnectionURI,
"mongo-connection-uri",
Expand Down
61 changes: 61 additions & 0 deletions server/backend/housekeeping/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package housekeeping

import (
"fmt"
"time"
)

// Config is the configuration for the housekeeping service.
type Config struct {
// Interval is the time between housekeeping runs.
Interval string `yaml:"Interval"`

// CandidatesLimitPerProject is the maximum number of candidates to be returned per project.
CandidatesLimitPerProject int `yaml:"CandidatesLimitPerProject"`

// HousekeepingProjectFetchSize is the maximum number of projects to be returned to deactivate candidates.
HousekeepingProjectFetchSize int `yaml:"HousekeepingProjectFetchSize"`
}

// Validate validates the configuration.
func (c *Config) Validate() error {
if _, err := time.ParseDuration(c.Interval); err != nil {
return fmt.Errorf(
`invalid argument %s for "--housekeeping-interval" flag: %w`,
c.Interval,
err,
)
}

if c.CandidatesLimitPerProject <= 0 {
return fmt.Errorf(
`invalid argument %d for "--housekeeping-candidates-limit-per-project" flag`,
c.HousekeepingProjectFetchSize,
)
}

if c.HousekeepingProjectFetchSize <= 0 {
return fmt.Errorf(
`invalid argument %d for "--housekeeping-project-fetc-size" flag`,
c.HousekeepingProjectFetchSize,
)
}

return nil
}
48 changes: 48 additions & 0 deletions server/backend/housekeeping/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package housekeeping_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/yorkie-team/yorkie/server/backend/housekeeping"
)

func TestConfig(t *testing.T) {
t.Run("validate test", func(t *testing.T) {
validConf := housekeeping.Config{
Interval: "1m",
CandidatesLimitPerProject: 100,
HousekeepingProjectFetchSize: 100,
}
assert.NoError(t, validConf.Validate())

conf1 := validConf
conf1.Interval = "hour"
assert.Error(t, conf1.Validate())

conf2 := validConf
conf2.CandidatesLimitPerProject = 0
assert.Error(t, conf2.Validate())

conf3 := validConf
conf3.HousekeepingProjectFetchSize = -1
assert.Error(t, conf3.Validate())
})
}
25 changes: 0 additions & 25 deletions server/backend/housekeeping/housekeeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,6 @@ const (
deactivateCandidatesKey = "housekeeping/deactivateCandidates"
)

// Config is the configuration for the housekeeping service.
type Config struct {
// Interval is the time between housekeeping runs.
Interval string `yaml:"Interval"`

// CandidatesLimitPerProject is the maximum number of candidates to be returned per project.
CandidatesLimitPerProject int `yaml:"CandidatesLimitPerProject"`

// HousekeepingProjectFetchSize is the maximum number of projects to be returned to deactivate candidates.
HousekeepingProjectFetchSize int `yaml:"HousekeepingProjectFetchSize"`
}

// Validate validates the configuration.
func (c *Config) Validate() error {
if _, err := time.ParseDuration(c.Interval); err != nil {
return fmt.Errorf(
`invalid argument %s for "--housekeeping-interval" flag: %w`,
c.Interval,
err,
)
}

return nil
}

// Housekeeping is the housekeeping service. It periodically runs housekeeping
// tasks. It is responsible for deactivating clients that have not been active
// for a long time.
Expand Down
4 changes: 2 additions & 2 deletions server/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Housekeeping:
# CandidatesLimitPerProject is the maximum number of candidates to be returned per project (default: 100).
CandidatesLimitPerProject: 100

# HousekeepingProjectFetchSize is the maximum number of projects to be returned to deactivate candidates. (default: 100)
HousekeepingProjectFetchSize: 10
# HousekeepingProjectFetchSize is the maximum number of projects to be returned to deactivate candidates. (default: 100).
HousekeepingProjectFetchSize: 100

# Backend is the configuration for the backend of Yorkie.
Backend:
Expand Down
6 changes: 4 additions & 2 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var (
AdminPassword = server.DefaultAdminPassword
HousekeepingInterval = 10 * gotime.Second
HousekeepingCandidatesLimitPerProject = 10
HousekeepingProjectFetchSize = 10

AdminTokenDuration = "10s"
ClientDeactivateThreshold = "10s"
Expand Down Expand Up @@ -221,8 +222,9 @@ func TestConfig() *server.Config {
Port: ProfilingPort + portOffset,
},
Housekeeping: &housekeeping.Config{
Interval: HousekeepingInterval.String(),
CandidatesLimitPerProject: HousekeepingCandidatesLimitPerProject,
Interval: HousekeepingInterval.String(),
CandidatesLimitPerProject: HousekeepingCandidatesLimitPerProject,
HousekeepingProjectFetchSize: HousekeepingProjectFetchSize,
},
Backend: &backend.Config{
AdminUser: server.DefaultAdminUser,
Expand Down

0 comments on commit e20e17c

Please sign in to comment.