Skip to content

Commit

Permalink
allow clock error in the cluster (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz authored Mar 13, 2022
1 parent b1d497a commit 63cfc78
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,17 @@ func (config *RecommendConfig) validate() {

// ServerConfig is the configuration for the server.
type ServerConfig struct {
APIKey string `mapstructure:"api_key"` // default number of returned items
DefaultN int `mapstructure:"default_n"` // secret key for RESTful APIs (SSL required)
APIKey string `mapstructure:"api_key"` // default number of returned items
DefaultN int `mapstructure:"default_n"` // secret key for RESTful APIs (SSL required)
EpsilonTime int `mapstructure:"epsilon_time"` // clock error in the cluster in seconds
}

// LoadDefaultIfNil loads default settings if config is nil.
func (config *ServerConfig) LoadDefaultIfNil() *ServerConfig {
if config == nil {
return &ServerConfig{
DefaultN: 10,
DefaultN: 10,
EpsilonTime: 5,
}
}
return config
Expand Down Expand Up @@ -265,6 +267,7 @@ func init() {
defaultServerConfig := *(*ServerConfig)(nil).LoadDefaultIfNil()
viper.SetDefault("server.api_key", defaultServerConfig.APIKey)
viper.SetDefault("server.default_n", defaultServerConfig.DefaultN)
viper.SetDefault("server.epsilon_time", defaultServerConfig.EpsilonTime)
// Default recommend config
defaultRecommendConfig := *(*RecommendConfig)(nil).LoadDefaultIfNil()
viper.SetDefault("recommend.popular_window", defaultRecommendConfig.PopularWindow)
Expand Down
1 change: 1 addition & 0 deletions config/config.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dashboard_password = "password" # dashboard password
[server]
default_n = 10 # default number of returned items
api_key = "" # secret key for RESTful APIs (SSL required)
epsilon_time = 5 # clock error in the cluster in seconds

# This section declares settings for recommendation.
[recommend]
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestLoadConfig(t *testing.T) {
// server configuration
assert.Equal(t, 10, config.Server.DefaultN)
assert.Equal(t, "", config.Server.APIKey)
assert.Equal(t, 5, config.Server.EpsilonTime)

// recommend configuration
assert.Equal(t, 30, config.Recommend.PopularWindow)
Expand Down
1 change: 1 addition & 0 deletions docker/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ meta_timeout = 10 # cluster meta timeout (second)
[server]
default_n = 10 # default number of returned items
api_key = "" # secret key for RESTful APIs (SSL required)
epsilon_time = 5 # clock error in the cluster in seconds

# This section declares settings for recommendation.
[recommend]
Expand Down
5 changes: 3 additions & 2 deletions server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ type recommendContext struct {

func (s *RestServer) createRecommendContext(userId, category string, n int) (*recommendContext, error) {
// pull ignored items
ignoreItems, err := s.CacheClient.GetSortedByScore(cache.Key(cache.IgnoreItems, userId), math.Inf(-1), float64(time.Now().Unix()))
ignoreItems, err := s.CacheClient.GetSortedByScore(cache.Key(cache.IgnoreItems, userId),
math.Inf(-1), float64(time.Now().Add(time.Duration(s.GorseConfig.Server.EpsilonTime)*time.Second).Unix()))
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -956,7 +957,7 @@ func (s *RestServer) getRecommend(request *restful.Request, response *restful.Re
ItemId: itemId,
FeedbackType: writeBackFeedback,
},
Timestamp: time.Now().Add(time.Minute * time.Duration(writeBackDelay)),
Timestamp: startTime.Add(time.Minute * time.Duration(writeBackDelay)),
}
err = s.DataClient.BatchInsertFeedback([]data.Feedback{feedback}, false, false, false)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,13 @@ func (w *Worker) refreshCache(userId string) error {
} else {
return errors.Trace(err)
}
// clear cache
err = w.cacheClient.SetSorted(cache.IgnoreItems, nil)
if err != nil {
return errors.Trace(err)
}
// load cache
if !w.cfg.Recommend.EnableReplacement {
// reload cache
if w.cfg.Recommend.EnableReplacement {
err = w.cacheClient.SetSorted(cache.IgnoreItems, nil)
if err != nil {
return errors.Trace(err)
}
} else {
feedback, err := w.dataClient.GetUserFeedback(userId, true)
if err != nil {
return errors.Trace(err)
Expand Down

0 comments on commit 63cfc78

Please sign in to comment.