Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit 1 running object file #1071

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions pkg/supervisor/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ type (
configSyncer cluster.Syncer
configSyncChan <-chan map[string]string
configPrefix string
configLocalPathPrefix string
configLocalPath string
backupConfigLocalPath string

mutex sync.Mutex
entities map[string]*ObjectEntity
Expand All @@ -83,8 +84,9 @@ type (
)

const (
defaultDumpInterval = 1 * time.Hour
configFilePrefix = "running_objects"
defaultDumpInterval = 1 * time.Hour
configFilePath = "running_objects.json"
backupdConfigFilePath = "running_objects.bak.json"
)

// FilterCategory returns a bool function to check if the object entity is filtered by category or not
Expand Down Expand Up @@ -158,7 +160,8 @@ func newObjectRegistry(super *Supervisor, initObjs map[string]string, interval s
configSyncer: syncer,
configSyncChan: syncChan,
configPrefix: prefix,
configLocalPathPrefix: filepath.Join(super.Options().AbsHomeDir, configFilePrefix),
configLocalPath: filepath.Join(super.Options().AbsHomeDir, configFilePath),
backupConfigLocalPath: filepath.Join(super.Options().AbsHomeDir, backupdConfigFilePath),
entities: make(map[string]*ObjectEntity),
watchers: map[string]*ObjectEntityWatcher{},
done: make(chan struct{}),
Expand Down Expand Up @@ -295,11 +298,6 @@ func (or *ObjectRegistry) CloseWatcher(name string) {
delete(or.watchers, name)
}

func (or *ObjectRegistry) getConfigLocalFileName() string {
timestamp := time.Now().Format("20060102150405")
return or.configLocalPathPrefix + "_" + timestamp + ".json"
}

func (or *ObjectRegistry) storeConfigInLocal(config map[string]string) {
buff := bytes.NewBuffer(nil)

Expand All @@ -310,10 +308,15 @@ func (or *ObjectRegistry) storeConfigInLocal(config map[string]string) {
}
buff.Write(configBuff)

configLocalFileName := or.getConfigLocalFileName()
err = os.WriteFile(configLocalFileName, buff.Bytes(), 0o644)
err = os.Rename(or.configLocalPath, or.backupConfigLocalPath)
if err != nil {
logger.Errorf("rename %s to %s failed: %v", or.configLocalPath, or.backupConfigLocalPath, err)
return
}

err = os.WriteFile(or.configLocalPath, buff.Bytes(), 0o644)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before write new running_objects.json, how about rename old one to running_objects.bak.json?
Since we remove the time from file name, how about add a timestamp to config, or use os.Chtimes(fileName, currentTime, currentTime) to change to file create time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Json file does not support comment, we could just use stat running_objects.json to get the time.

if err != nil {
logger.Errorf("write %s failed: %v", configLocalFileName, err)
logger.Errorf("write %s failed: %v", or.configLocalPath, err)
return
}
}
Expand Down