Skip to content

Commit

Permalink
Add config to disable storage index 'index only' globally
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito committed Oct 9, 2023
1 parent 3dcf7a2 commit 4c16728
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,6 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/heroiclabs/nakama-common v1.28.1 h1:GOH8r27NBSdiBf7xjy2R/Ov7CSr9zp4DVJt9Y2LIbqg=
github.com/heroiclabs/nakama-common v1.28.1/go.mod h1:Os8XeXGvHAap/p6M/8fQ3gle4eEXDGRQmoRNcPQTjXs=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func main() {
tracker.SetMatchLeaveListener(matchRegistry.Leave)
streamManager := server.NewLocalStreamManager(config, sessionRegistry, tracker)

storageIndex, err := server.NewLocalStorageIndex(logger, db)
storageIndex, err := server.NewLocalStorageIndex(logger, db, config.GetStorageIndex())
if err != nil {
logger.Fatal("Failed to initialize storage index", zap.Error(err))
}
Expand Down
6 changes: 3 additions & 3 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
DiscardUnknown: false,
}
metrics = NewLocalMetrics(logger, logger, nil, cfg)
storageIdx, _ = NewLocalStorageIndex(logger, nil)
storageIdx, _ = NewLocalStorageIndex(logger, nil, &StorageIndexConfig{DisableIndexOnly: false})
_ = CheckConfig(logger, cfg)
)

Expand Down Expand Up @@ -150,8 +150,8 @@ func NewConsoleLogger(output *os.File, verbose bool) *zap.Logger {
}

func NewDB(t *testing.T) *sql.DB {
db, err := sql.Open("pgx", "postgresql://[email protected]:26257/nakama?sslmode=disable")
//db, err := sql.Open("pgx", "postgresql://[email protected]:5432/nakama?sslmode=disable")
//db, err := sql.Open("pgx", "postgresql://[email protected]:26257/nakama?sslmode=disable")
db, err := sql.Open("pgx", "postgresql://postgres:[email protected]:5432/nakama?sslmode=disable")
if err != nil {
t.Fatal("Error connecting to database", err)
}
Expand Down
57 changes: 37 additions & 20 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config interface {
GetIAP() *IAPConfig
GetGoogleAuth() *GoogleAuthConfig
GetSatori() *SatoriConfig
GetStorageIndex() *StorageIndexConfig

Clone() (Config, error)
}
Expand Down Expand Up @@ -437,25 +438,26 @@ func convertRuntimeEnv(logger *zap.Logger, existingEnv map[string]string, mergeE
}

type config struct {
Name string `yaml:"name" json:"name" usage:"Nakama server’s node name - must be unique."`
Config []string `yaml:"config" json:"config" usage:"The absolute file path to configuration YAML file."`
ShutdownGraceSec int `yaml:"shutdown_grace_sec" json:"shutdown_grace_sec" usage:"Maximum number of seconds to wait for the server to complete work before shutting down. Default is 0 seconds. If 0 the server will shut down immediately when it receives a termination signal."`
Datadir string `yaml:"data_dir" json:"data_dir" usage:"An absolute path to a writeable folder where Nakama will store its data."`
Logger *LoggerConfig `yaml:"logger" json:"logger" usage:"Logger levels and output."`
Metrics *MetricsConfig `yaml:"metrics" json:"metrics" usage:"Metrics settings."`
Session *SessionConfig `yaml:"session" json:"session" usage:"Session authentication settings."`
Socket *SocketConfig `yaml:"socket" json:"socket" usage:"Socket configuration."`
Database *DatabaseConfig `yaml:"database" json:"database" usage:"Database connection settings."`
Social *SocialConfig `yaml:"social" json:"social" usage:"Properties for social provider integrations."`
Runtime *RuntimeConfig `yaml:"runtime" json:"runtime" usage:"Script Runtime properties."`
Match *MatchConfig `yaml:"match" json:"match" usage:"Authoritative realtime match properties."`
Tracker *TrackerConfig `yaml:"tracker" json:"tracker" usage:"Presence tracker properties."`
Console *ConsoleConfig `yaml:"console" json:"console" usage:"Console settings."`
Leaderboard *LeaderboardConfig `yaml:"leaderboard" json:"leaderboard" usage:"Leaderboard settings."`
Matchmaker *MatchmakerConfig `yaml:"matchmaker" json:"matchmaker" usage:"Matchmaker settings."`
IAP *IAPConfig `yaml:"iap" json:"iap" usage:"In-App Purchase settings."`
GoogleAuth *GoogleAuthConfig `yaml:"google_auth" json:"google_auth" usage:"Google's auth settings."`
Satori *SatoriConfig `yaml:"satori" json:"satori" usage:"Satori integration settings."`
Name string `yaml:"name" json:"name" usage:"Nakama server’s node name - must be unique."`
Config []string `yaml:"config" json:"config" usage:"The absolute file path to configuration YAML file."`
ShutdownGraceSec int `yaml:"shutdown_grace_sec" json:"shutdown_grace_sec" usage:"Maximum number of seconds to wait for the server to complete work before shutting down. Default is 0 seconds. If 0 the server will shut down immediately when it receives a termination signal."`
Datadir string `yaml:"data_dir" json:"data_dir" usage:"An absolute path to a writeable folder where Nakama will store its data."`
Logger *LoggerConfig `yaml:"logger" json:"logger" usage:"Logger levels and output."`
Metrics *MetricsConfig `yaml:"metrics" json:"metrics" usage:"Metrics settings."`
Session *SessionConfig `yaml:"session" json:"session" usage:"Session authentication settings."`
Socket *SocketConfig `yaml:"socket" json:"socket" usage:"Socket configuration."`
Database *DatabaseConfig `yaml:"database" json:"database" usage:"Database connection settings."`
Social *SocialConfig `yaml:"social" json:"social" usage:"Properties for social provider integrations."`
Runtime *RuntimeConfig `yaml:"runtime" json:"runtime" usage:"Script Runtime properties."`
Match *MatchConfig `yaml:"match" json:"match" usage:"Authoritative realtime match properties."`
Tracker *TrackerConfig `yaml:"tracker" json:"tracker" usage:"Presence tracker properties."`
Console *ConsoleConfig `yaml:"console" json:"console" usage:"Console settings."`
Leaderboard *LeaderboardConfig `yaml:"leaderboard" json:"leaderboard" usage:"Leaderboard settings."`
Matchmaker *MatchmakerConfig `yaml:"matchmaker" json:"matchmaker" usage:"Matchmaker settings."`
IAP *IAPConfig `yaml:"iap" json:"iap" usage:"In-App Purchase settings."`
GoogleAuth *GoogleAuthConfig `yaml:"google_auth" json:"google_auth" usage:"Google's auth settings."`
Satori *SatoriConfig `yaml:"satori" json:"satori" usage:"Satori integration settings."`
StorageIndex *StorageIndexConfig `yaml:"storage_index" json:"storage_index" usage:"Storage index settings."`
}

// NewConfig constructs a Config struct which represents server settings, and populates it with default values.
Expand Down Expand Up @@ -483,6 +485,7 @@ func NewConfig(logger *zap.Logger) *config {
IAP: NewIAPConfig(),
GoogleAuth: NewGoogleAuthConfig(),
Satori: NewSatoriConfig(),
StorageIndex: NewStorageIndexConfig(),
}
}

Expand All @@ -501,6 +504,7 @@ func (c *config) Clone() (Config, error) {
configMatchmaker := *(c.Matchmaker)
configIAP := *(c.IAP)
configSatori := *(c.Satori)
configStorageIndex := *(c.StorageIndex)
configGoogleAuth := *(c.GoogleAuth)
nc := &config{
Name: c.Name,
Expand All @@ -521,6 +525,7 @@ func (c *config) Clone() (Config, error) {
IAP: &configIAP,
Satori: &configSatori,
GoogleAuth: &configGoogleAuth,
StorageIndex: &configStorageIndex,
}
nc.Socket.CertPEMBlock = make([]byte, len(c.Socket.CertPEMBlock))
copy(nc.Socket.CertPEMBlock, c.Socket.CertPEMBlock)
Expand Down Expand Up @@ -619,6 +624,10 @@ func (c *config) GetSatori() *SatoriConfig {
return c.Satori
}

func (c *config) GetStorageIndex() *StorageIndexConfig {
return c.StorageIndex
}

// LoggerConfig is configuration relevant to logging levels and output.
type LoggerConfig struct {
Level string `yaml:"level" json:"level" usage:"Log level to set. Valid values are 'debug', 'info', 'warn', 'error'. Default 'info'."`
Expand Down Expand Up @@ -803,7 +812,7 @@ func NewSocialConfig() *SocialConfig {
}
}

// RuntimeConfig is configuration relevant to the Runtime Lua VM.
// RuntimeConfig is configuration relevant to the Runtimes.
type RuntimeConfig struct {
Environment map[string]string `yaml:"-" json:"-"`
Env []string `yaml:"env" json:"env" usage:"Values to pass into Runtime as environment variables."`
Expand Down Expand Up @@ -1073,3 +1082,11 @@ func NewGoogleAuthConfig() *GoogleAuthConfig {
OAuthConfig: nil,
}
}

type StorageIndexConfig struct {
DisableIndexOnly bool `yaml:"disable_index_only" json:"disable_index_only" usage:"Override and disable 'index_only' storage indices config and fallback to reading from the database."`
}

func NewStorageIndexConfig() *StorageIndexConfig {
return &StorageIndexConfig{}
}
11 changes: 6 additions & 5 deletions server/storage_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ type LocalStorageIndex struct {
indexByName map[string]*storageIndex
indicesByCollection map[string][]*storageIndex
customFilterFunctions map[string]RuntimeStorageIndexFilterFunction
config *StorageIndexConfig
}

func NewLocalStorageIndex(logger *zap.Logger, db *sql.DB) (StorageIndex, error) {
func NewLocalStorageIndex(logger *zap.Logger, db *sql.DB, config *StorageIndexConfig) (StorageIndex, error) {
si := &LocalStorageIndex{
logger: logger,
db: db,
indexByName: make(map[string]*storageIndex),
indicesByCollection: make(map[string][]*storageIndex),
customFilterFunctions: make(map[string]RuntimeStorageIndexFilterFunction),
config: config,
}

return si, nil
Expand Down Expand Up @@ -250,7 +252,7 @@ func (si *LocalStorageIndex) List(ctx context.Context, indexName, query string,
return &api.StorageObjects{Objects: []*api.StorageObject{}}, nil
}

if idx.IndexOnly {
if !si.config.DisableIndexOnly && idx.IndexOnly {
objects := make([]*api.StorageObject, 0, len(indexResults))
for _, idxResult := range indexResults {
objects = append(objects, &api.StorageObject{
Expand Down Expand Up @@ -292,8 +294,7 @@ func (si *LocalStorageIndex) List(ctx context.Context, indexName, query string,

sortedObjects := make([]*api.StorageObject, 0, len(objects.Objects))
for _, r := range indexResults {
index, ok := objectIdToIdx[fmt.Sprintf("%s.%s.%s", r.Collection, r.Key, r.UserID)]
if ok {
if index, ok := objectIdToIdx[fmt.Sprintf("%s.%s.%s", r.Collection, r.Key, r.UserID)]; ok {
sortedObjects = append(sortedObjects, objects.Objects[index])
}
}
Expand Down Expand Up @@ -469,7 +470,7 @@ func (si *LocalStorageIndex) mapIndexStorageFields(userID, collection, key, vers
rv.AddField(bluge.NewNumericField("read", float64(read)).StoreValue())
rv.AddField(bluge.NewNumericField("write", float64(write)).StoreValue())

if indexOnly {
if !si.config.DisableIndexOnly && indexOnly {
json, err := json.Marshal(mapValue)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions server/storage_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestLocalStorageIndex_Write(t *testing.T) {
})
valueThree := string(valueThreeBytes)

storageIdx, err := NewLocalStorageIndex(logger, db)
storageIdx, err := NewLocalStorageIndex(logger, db, &StorageIndexConfig{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestLocalStorageIndex_Write(t *testing.T) {
UpdateTime: timestamppb.New(ts),
}

storageIdx, err := NewLocalStorageIndex(logger, db)
storageIdx, err := NewLocalStorageIndex(logger, db, &StorageIndexConfig{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestLocalStorageIndex_List(t *testing.T) {
})
valueThree := string(valueThreeBytes)

storageIdx, err := NewLocalStorageIndex(logger, db)
storageIdx, err := NewLocalStorageIndex(logger, db, &StorageIndexConfig{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestLocalStorageIndex_List(t *testing.T) {
})
valueThree := string(valueThreeBytes)

storageIdx, err := NewLocalStorageIndex(logger, db)
storageIdx, err := NewLocalStorageIndex(logger, db, &StorageIndexConfig{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestLocalStorageIndex_Delete(t *testing.T) {
})
valueOne := string(valueOneBytes)

storageIdx, err := NewLocalStorageIndex(logger, db)
storageIdx, err := NewLocalStorageIndex(logger, db, &StorageIndexConfig{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down

0 comments on commit 4c16728

Please sign in to comment.