-
Notifications
You must be signed in to change notification settings - Fork 32
Dynamic Config #664
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
Merged
Dynamic Config #664
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e87c370
dyncfg 90% done
snadrus 0662132
tests, docs, and clean-up
snadrus 260e345
updates
snadrus 0403006
checkers
snadrus d88a898
allow cmp.Equal
snadrus 886dfd0
fixed dynamic
snadrus 02772cc
avoid unsettable
snadrus de69e43
Merge branch 'main' into dyn-cfg
snadrus 3240f22
change-notification
snadrus bb71ceb
go mod tidy
snadrus cabb400
dbg basetext
snadrus 8f7a6ac
fix bad import
snadrus ff6ebdc
test logger
snadrus 17f1050
dbgFail: is it in baseTest
snadrus aa24527
found 1 blocker for test failure: base was not included right
snadrus dcc4786
rm dbg
snadrus 3779387
lint
snadrus 2c0286e
no pq, fix unmarshal
snadrus b9cb21c
complex equal
snadrus deb9967
mod tidy & naming
snadrus 6d721d8
fix Fil cmp panic
snadrus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/filecoin-project/curio/harmony/harmonydb" | ||
logging "github.com/ipfs/go-log/v2" | ||
) | ||
|
||
var logger = logging.Logger("config-dynamic") | ||
var DynamicMx sync.RWMutex | ||
|
||
type Dynamic[T any] struct { | ||
Value T | ||
} | ||
|
||
func NewDynamic[T any](value T) *Dynamic[T] { | ||
return &Dynamic[T]{Value: value} | ||
} | ||
|
||
func (d *Dynamic[T]) Set(value T) { | ||
DynamicMx.Lock() | ||
defer DynamicMx.Unlock() | ||
d.Value = value | ||
} | ||
|
||
func (d *Dynamic[T]) Get() T { | ||
DynamicMx.RLock() | ||
defer DynamicMx.RUnlock() | ||
return d.Value | ||
} | ||
|
||
func (d *Dynamic[T]) UnmarshalText(text []byte) error { | ||
DynamicMx.Lock() | ||
defer DynamicMx.Unlock() | ||
return toml.Unmarshal(text, d.Value) | ||
} | ||
|
||
type cfgRoot struct { | ||
db *harmonydb.DB | ||
layers []string | ||
treeCopy *CurioConfig | ||
} | ||
|
||
func EnableChangeDetection(db *harmonydb.DB, obj *CurioConfig, layers []string) error { | ||
r := &cfgRoot{db: db, treeCopy: obj, layers: layers} | ||
err := r.copyWithOriginalDynamics(obj) | ||
if err != nil { | ||
return err | ||
} | ||
go r.changeMonitor() | ||
return nil | ||
} | ||
|
||
// copyWithOriginalDynamics copies the original dynamics from the original object to the new object. | ||
func (r *cfgRoot) copyWithOriginalDynamics(orig *CurioConfig) error { | ||
typ := reflect.TypeOf(orig) | ||
if typ.Kind() != reflect.Struct { | ||
return fmt.Errorf("expected struct, got %s", typ.Kind()) | ||
} | ||
result := reflect.New(typ) | ||
// recursively walk the struct tree, and copy the dynamics from the original object to the new object. | ||
var walker func(orig, result reflect.Value) | ||
walker = func(orig, result reflect.Value) { | ||
for i := 0; i < orig.NumField(); i++ { | ||
field := orig.Field(i) | ||
if field.Kind() == reflect.Struct { | ||
walker(field, result.Field(i)) | ||
} else if field.Kind() == reflect.Ptr { | ||
walker(field.Elem(), result.Field(i).Elem()) | ||
} else if field.Kind() == reflect.Interface { | ||
walker(field.Elem(), result.Field(i).Elem()) | ||
} else { | ||
result.Field(i).Set(field) | ||
} | ||
} | ||
} | ||
walker(reflect.ValueOf(orig), result) | ||
r.treeCopy = result.Interface().(*CurioConfig) | ||
return nil | ||
} | ||
|
||
func (r *cfgRoot) changeMonitor() { | ||
lastTimestamp := time.Now().Add(-30 * time.Second) // plenty of time for start-up | ||
|
||
for { | ||
time.Sleep(30 * time.Second) | ||
configCount := 0 | ||
err := r.db.QueryRow(context.Background(), `SELECT COUNT(*) FROM harmony_config WHERE timestamp > $1 AND title IN ($2)`, lastTimestamp, strings.Join(r.layers, ",")).Scan(&configCount) | ||
if err != nil { | ||
logger.Errorf("error selecting configs: %s", err) | ||
continue | ||
} | ||
if configCount == 0 { | ||
continue | ||
} | ||
lastTimestamp = time.Now() | ||
|
||
// 1. get all configs | ||
configs, err := GetConfigs(context.Background(), r.db, r.layers) | ||
if err != nil { | ||
logger.Errorf("error getting configs: %s", err) | ||
continue | ||
} | ||
|
||
// 2. lock "dynamic" mutex | ||
func() { | ||
DynamicMx.Lock() | ||
defer DynamicMx.Unlock() | ||
ApplyLayers(context.Background(), r.treeCopy, configs) | ||
snadrus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}() | ||
DynamicMx.Lock() | ||
snadrus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.