Skip to content

Commit

Permalink
Add runtime config to paramtable
Browse files Browse the repository at this point in the history
Signed-off-by: aoiasd <[email protected]>
  • Loading branch information
aoiasd committed Mar 7, 2024
1 parent eb0cfe3 commit c6e6224
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
24 changes: 20 additions & 4 deletions pkg/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,28 @@ func (m *Manager) GetConfigs() map[string]string {
func (m *Manager) GetBy(filters ...Filter) map[string]string {
matchedConfig := make(map[string]string)

for key, value := range m.GetConfigs() {
m.keySourceMap.Range(func(key, value string) bool {
newkey, ok := filterate(key, filters...)
if ok {
matchedConfig[newkey] = value
if !ok {
return true
}
}
sValue, err := m.GetConfig(key)
if err != nil {
return true
}

matchedConfig[newkey] = sValue
return true
})

m.overlays.Range(func(key, value string) bool {
newkey, ok := filterate(key, filters...)
if !ok {
return true
}
matchedConfig[newkey] = value
return true
})

return matchedConfig
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/paramtable/component_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ type ComponentParam struct {
IndexNodeGrpcClientCfg GrpcClientConfig

IntegrationTestCfg integrationTestConfig

RuntimeConfig runtimeConfig
}

// Init initialize once
Expand Down Expand Up @@ -3504,6 +3506,13 @@ func (p *indexNodeConfig) init(base *BaseTable) {
p.GracefulStopTimeout.Init(base.mgr)
}

type runtimeConfig struct {
CreateTime RuntimeParamItem
UpdateTime RuntimeParamItem
Role RuntimeParamItem
NodeID RuntimeParamItem
}

type integrationTestConfig struct {
IntegrationMode ParamItem `refreshable:"false"`
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/util/paramtable/param_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,39 @@ func getAndConvert[T any](v string, converter func(input string) (T, error), def
}
return t
}

type RuntimeParamItem struct {
value atomic.Value
}

func (rpi *RuntimeParamItem) GetValue() any {
return rpi.value.Load()
}

func (rpi *RuntimeParamItem) GetAsString() string {
value, ok := rpi.value.Load().(string)
if !ok {
return ""
}
return value
}

func (rpi *RuntimeParamItem) GetAsTime() time.Time {
value, ok := rpi.value.Load().(time.Time)
if !ok {
return time.Time{}
}
return value
}

func (rpi *RuntimeParamItem) GetAsInt64() int64 {
value, ok := rpi.value.Load().(int64)
if !ok {
return 0
}
return value
}

func (rpi *RuntimeParamItem) SetValue(value any) {
rpi.value.Store(value)
}
33 changes: 8 additions & 25 deletions pkg/util/paramtable/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@
package paramtable

import (
"strconv"
"sync"
"time"
)

const (
runtimeNodeIDKey = "runtime.nodeID"
runtimeRoleKey = "runtime.role"
runtimeCreateTimeKey = "runtime.createTime"
runtimeUpdateTimeKey = "runtime.updateTime"
)

var (
once sync.Once
params ComponentParam
Expand Down Expand Up @@ -60,42 +52,33 @@ func GetHookParams() *hookConfig {
}

func SetNodeID(newID UniqueID) {
params.baseTable.Save(runtimeNodeIDKey, strconv.FormatInt(newID, 10))
params.RuntimeConfig.NodeID.SetValue(newID)
}

func GetNodeID() UniqueID {
nodeID, err := strconv.ParseInt(params.baseTable.Get(runtimeNodeIDKey), 10, 64)
if err != nil {
return 0
}
return nodeID
return params.RuntimeConfig.NodeID.GetAsInt64()
}

func SetRole(role string) {
params.baseTable.Save(runtimeRoleKey, role)
params.RuntimeConfig.Role.SetValue(role)
}

func GetRole() string {
if params.baseTable == nil {
return ""
}
return params.baseTable.Get(runtimeRoleKey)
return params.RuntimeConfig.Role.GetAsString()
}

func SetCreateTime(d time.Time) {
params.baseTable.Save(runtimeCreateTimeKey, strconv.FormatInt(d.UnixNano(), 10))
params.RuntimeConfig.CreateTime.SetValue(d)
}

func GetCreateTime() time.Time {
v, _ := strconv.ParseInt(params.baseTable.Get(runtimeCreateTimeKey), 10, 64)
return time.Unix(v/1e9, v%1e9)
return params.RuntimeConfig.CreateTime.GetAsTime()
}

func SetUpdateTime(d time.Time) {
params.baseTable.Save(runtimeUpdateTimeKey, strconv.FormatInt(d.UnixNano(), 10))
params.RuntimeConfig.UpdateTime.SetValue(d)
}

func GetUpdateTime() time.Time {
v, _ := strconv.ParseInt(params.baseTable.Get(runtimeUpdateTimeKey), 10, 64)
return time.Unix(v/1e9, v%1e9)
return params.RuntimeConfig.UpdateTime.GetAsTime()
}

0 comments on commit c6e6224

Please sign in to comment.