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

feat: rewrite duration in conf #3387

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 5 additions & 15 deletions internal/meta/yamlConfigMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,11 @@ func GetYamlConf(configOperatorKey, language string) (b []byte, err error) {
}
}

func replaceConfigurations(plgName string, cf YamlConfigurations) YamlConfigurations {
switch plgName {
case "sql":
for key, props := range cf {
replaced, newProps := replace.ReplacePropsDBURL(props)
if replaced {
cf[key] = newProps
}
}
default:
for key, props := range cf {
replaced, newProps := replace.ReplacePassword(props)
if replaced {
cf[key] = newProps
}
func replaceConfigurations(plg string, cf YamlConfigurations) YamlConfigurations {
for key, props := range cf {
replaced, newProps := replace.ReplacePropsWithPlug(plg, props)
if replaced {
cf[key] = newProps
}
}
return cf
Expand Down
54 changes: 54 additions & 0 deletions internal/server/bump/bump4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bump

import (
"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/pkg/replace"
)

func bumpFrom3TO4() error {
return rewriteReplacedProps()
}

func rewriteReplacedProps() error {
if err := rewritePlugProps("sources"); err != nil {
return err
}

Check warning on line 29 in internal/server/bump/bump4.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump4.go#L28-L29

Added lines #L28 - L29 were not covered by tests
if err := rewritePlugProps("sinks"); err != nil {
return err
}

Check warning on line 32 in internal/server/bump/bump4.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump4.go#L31-L32

Added lines #L31 - L32 were not covered by tests
if err := rewritePlugProps("connections"); err != nil {
return err
}

Check warning on line 35 in internal/server/bump/bump4.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump4.go#L34-L35

Added lines #L34 - L35 were not covered by tests
return nil
}

func rewritePlugProps(typ string) error {
keyProps, err := conf.GetCfgFromKVStorage(typ, "", "")
if err != nil {
return err
}

Check warning on line 43 in internal/server/bump/bump4.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump4.go#L42-L43

Added lines #L42 - L43 were not covered by tests
for key, props := range keyProps {
_, plug, confKey, valid := extractKey(key)
if valid {
changed, newProps := replace.ReplacePropsWithPlug(plug, props)
if changed {
return conf.WriteCfgIntoKVStorage(typ, plug, confKey, newProps)
}
}
}
return nil
}
41 changes: 41 additions & 0 deletions internal/server/bump/bump4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bump

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/v2/internal/conf"
)

func TestBumpTo4(t *testing.T) {
conf.IsTesting = true
require.NoError(t, conf.WriteCfgIntoKVStorage("sources", "sql", "sql1", map[string]interface{}{
"url": "123",
"cacheTtl": 1000,
}))
require.NoError(t, bumpFrom3TO4())
mm, err := conf.GetCfgFromKVStorage("sources", "sql", "sql1")
require.NoError(t, err)
for _, props := range mm {
require.Equal(t, map[string]interface{}{
"dburl": "123",
"cacheTtl": "1s",
}, props)
break
}
}
10 changes: 9 additions & 1 deletion internal/server/bump/bump_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

const (
currentVersion = 3
currentVersion = 4
bumpTable = "eKuiperMeta_bump_version"
)

Expand Down Expand Up @@ -100,6 +100,14 @@
return err
}
GlobalBumpManager.Version = 3
case 3:
if err := bumpFrom3TO4(); err != nil {
return err
}

Check warning on line 106 in internal/server/bump/bump_manager.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump_manager.go#L105-L106

Added lines #L105 - L106 were not covered by tests
if err := storeGlobalVersion(4); err != nil {
return err
}

Check warning on line 109 in internal/server/bump/bump_manager.go

View check run for this annotation

Codecov / codecov/patch

internal/server/bump/bump_manager.go#L108-L109

Added lines #L108 - L109 were not covered by tests
GlobalBumpManager.Version = 4
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/server/bump/bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestBumpVersion(t *testing.T) {
}
require.NoError(t, InitBumpManager())
GlobalBumpManager.Version = 0
testBumpVersion(t, 3)
testBumpVersion(t, currentVersion)
}

func prepareBumpVersion(t *testing.T, dir string) {
Expand Down
17 changes: 4 additions & 13 deletions internal/server/yaml_import_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,18 +575,9 @@ func replaceConfigurations(key string, props map[string]any) map[string]any {
if err != nil {
return props
}
switch plgName {
case "sql":
changed, newProps := replace.ReplacePropsDBURL(props)
if changed {
return newProps
}
return props
default:
changed, newProps := replace.ReplacePassword(props)
if changed {
return newProps
}
return props
changed, newProps := replace.ReplacePropsWithPlug(plgName, props)
if changed {
return newProps
}
return props
}
82 changes: 78 additions & 4 deletions pkg/replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import (
"encoding/json"
"time"
)

var (
replaceURL = []string{"url"}
replacePassword = []string{"saslPassword"}
replaceDuration = []string{"cacheTtl", "timeout", "expiration", "interval"}
replaceAction = map[string]struct{}{
"kafka": {},
"sql": {},
Expand Down Expand Up @@ -35,11 +37,10 @@
_, ok1 := replaceAction[actionTyp]
actionPropsMap, ok2 := actionProps.(map[string]interface{})
if ok1 && ok2 {
changed1, m1 := ReplacePassword(actionPropsMap)
changed2, m2 := ReplacePropsDBURL(m1)
if changed1 || changed2 {
replaced, newProps := ReplacePropsWithPlug(actionTyp, actionPropsMap)
if replaced {
changed = true
actionPropsMap = m2
actionPropsMap = newProps
actionMap[actionTyp] = actionPropsMap
actions[index] = actionMap
}
Expand All @@ -59,6 +60,18 @@
return string(got)
}

func WithDisableReplaceDburl() ReplacePropsOption {
return func(c *ReplacePropsConfig) {
c.DisableReplaceDbUrl = true
}
}

func WithDisableReplacePassword() ReplacePropsOption {
return func(c *ReplacePropsConfig) {
c.DisableReplacePassword = true
}

Check warning on line 72 in pkg/replace/replace.go

View check run for this annotation

Codecov / codecov/patch

pkg/replace/replace.go#L69-L72

Added lines #L69 - L72 were not covered by tests
}

func ReplacePropsDBURL(props map[string]interface{}) (bool, map[string]interface{}) {
changed := false
for _, replaceWord := range replaceURL {
Expand Down Expand Up @@ -86,3 +99,64 @@
}
return changed, props
}

func ReplaceDuration(props map[string]interface{}) (bool, map[string]interface{}) {
changed := false
for _, replaceWord := range replaceDuration {
v, ok := props[replaceWord]
if ok {
intRaw, ok := v.(int)
if ok {
props[replaceWord] = (time.Duration(intRaw) * time.Millisecond).String()
changed = true
continue
}
int64Raw, ok := v.(int64)
if ok {
props[replaceWord] = (time.Duration(int64Raw) * time.Millisecond).String()
changed = true
continue
}
}
}
return changed, props
}

func ReplacePropsWithPlug(plug string, props map[string]interface{}) (bool, map[string]interface{}) {
switch plug {
case "sql":
return ReplacePropsWithOption(props)
default:
return ReplacePropsWithOption(props, WithDisableReplaceDburl())
}
}

func ReplacePropsWithOption(props map[string]interface{}, opts ...ReplacePropsOption) (bool, map[string]interface{}) {
ReplaceConfig := &ReplacePropsConfig{}
for _, opt := range opts {
opt(ReplaceConfig)
}
replaced := false
var changed bool
if !ReplaceConfig.DisableReplacePassword {
changed, props = ReplacePassword(props)
replaced = replaced || changed
}
if !ReplaceConfig.DisableReplaceDbUrl {
changed, props = ReplacePropsDBURL(props)
replaced = replaced || changed
}
if !ReplaceConfig.DisableReplaceDuration {
changed, props = ReplaceDuration(props)
replaced = replaced || changed
}
return replaced, props
}

type ReplacePropsOption func(c *ReplacePropsConfig)

type ReplacePropsConfig struct {
DisableReplaceDbUrl bool
DisableReplacePassword bool
DisableReplaceDuration bool
}
42 changes: 42 additions & 0 deletions pkg/replace/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,45 @@ func TestReplaceRuleJson(t *testing.T) {
got = ReplaceRuleJson(data, false)
require.Equal(t, data, got)
}

func TestReplaceDuration(t *testing.T) {
props := map[string]interface{}{
"cacheTtl": 1000,
}
changed, newProps := ReplaceDuration(props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
}, newProps)
props = map[string]interface{}{
"cacheTtl": int64(1000),
}
changed, newProps = ReplaceDuration(props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
}, newProps)
}

func TestRelacePropsPlug(t *testing.T) {
props := map[string]interface{}{
"cacheTtl": 1000,
"url": "123",
}
changed, newProps := ReplacePropsWithPlug("", props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"url": "123",
}, newProps)
props = map[string]interface{}{
"cacheTtl": 1000,
"url": "123",
}
changed, newProps = ReplacePropsWithPlug("sql", props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"dburl": "123",
}, newProps)
}
Loading