Skip to content

Commit

Permalink
Introduce Number util for dynamic config (#1464)
Browse files Browse the repository at this point in the history
* Number struct / util will handling conversion of uint*, int*, float* to uint, int, float64
* Using number util for parsing retry policy from dynamic config
  • Loading branch information
wxing1292 committed Apr 16, 2021
1 parent 682b1f2 commit 5d17b1e
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 47 deletions.
155 changes: 155 additions & 0 deletions common/number/number.go
@@ -0,0 +1,155 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package number

import (
"fmt"
)

const (
TypeUnknown Type = iota
TypeFloat
TypeInt
TypeUint
)

type (
Type int
Number struct {
numberType Type
value interface{}
}
)

func NewNumber(
value interface{},
) Number {

var numberType Type
var number interface{}
switch n := value.(type) {
case int8:
numberType = TypeInt
number = int(n)
case int16:
numberType = TypeInt
number = int(n)
case int32:
numberType = TypeInt
number = int(n)
case int64:
numberType = TypeInt
number = int(n)
case int:
numberType = TypeInt
number = n

case uint8:
numberType = TypeUint
number = uint(n)
case uint16:
numberType = TypeUint
number = uint(n)
case uint32:
numberType = TypeUint
number = uint(n)
case uint64:
numberType = TypeUint
number = uint(n)
case uint:
numberType = TypeUint
number = n

case float32:
numberType = TypeFloat
number = float64(n)
case float64:
numberType = TypeFloat
number = n

default:
// DO NOT panic here
// the value is provided during runtime
// the logic cannot just panic if input is not a number
numberType = TypeUnknown
number = nil
}

return Number{
numberType: numberType,
value: number,
}
}

func (n Number) GetIntOrDefault(
defaultValue int,
) int {
switch n.numberType {
case TypeFloat:
return int(n.value.(float64))
case TypeInt:
return n.value.(int)
case TypeUint:
return int(n.value.(uint))
case TypeUnknown:
return defaultValue
default:
panic(fmt.Sprintf("unknown number type: %v", n.numberType))
}
}

func (n Number) GetUintOrDefault(
defaultValue uint,
) uint {
switch n.numberType {
case TypeFloat:
return uint(n.value.(float64))
case TypeInt:
return uint(n.value.(int))
case TypeUint:
return n.value.(uint)
case TypeUnknown:
return defaultValue
default:
panic(fmt.Sprintf("unknown number type: %v", n.numberType))
}
}

func (n Number) GetFloatOrDefault(
defaultValue float64,
) float64 {
switch n.numberType {
case TypeFloat:
return n.value.(float64)
case TypeInt:
return float64(n.value.(int))
case TypeUint:
return float64(n.value.(uint))
case TypeUnknown:
return defaultValue
default:
panic(fmt.Sprintf("unknown number type: %v", n.numberType))
}
}
99 changes: 99 additions & 0 deletions common/number/number_test.go
@@ -0,0 +1,99 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package number

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type (
numberSuite struct {
suite.Suite
*require.Assertions
}
)

func TestNumberSuite(t *testing.T) {
s := new(numberSuite)
suite.Run(t, s)
}

func (s *numberSuite) SetupSuite() {}

func (s *numberSuite) TearDownSuite() {}

func (s *numberSuite) SetupTest() {
s.Assertions = require.New(s.T())
}

func (s *numberSuite) TearDownTest() {

}

func (s *numberSuite) TestInt() {
number := rand.Intn(128)
for _, n := range []interface{}{
int8(number),
int16(number),
int32(number),
int64(number),
int(number),
} {
s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
}
}

func (s *numberSuite) TestUint() {
number := rand.Intn(256)
for _, n := range []interface{}{
uint8(number),
uint16(number),
uint32(number),
uint64(number),
uint(number),
} {
s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
}
}

func (s *numberSuite) TestFloat() {
number := rand.Float32() * float32(rand.Int())
for _, n := range []interface{}{
float32(number),
float64(number),
} {
s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
}
}
32 changes: 20 additions & 12 deletions common/util.go
Expand Up @@ -42,6 +42,8 @@ import (
"go.temporal.io/api/serviceerror"
"go.temporal.io/api/workflowservice/v1"

"go.temporal.io/server/common/number"

workflowspb "go.temporal.io/server/api/workflow/v1"
"go.temporal.io/server/common/metrics"

Expand Down Expand Up @@ -488,24 +490,30 @@ func FromConfigToDefaultRetrySettings(options map[string]interface{}) DefaultRet
MaximumAttempts: defaultMaximumAttempts,
}

initialIntervalInSeconds, ok := options[initialIntervalInSecondsConfigKey]
if ok {
defaultSettings.InitialInterval = time.Duration(initialIntervalInSeconds.(int)) * time.Second
if seconds, ok := options[initialIntervalInSecondsConfigKey]; ok {
defaultSettings.InitialInterval = time.Duration(
number.NewNumber(
seconds,
).GetIntOrDefault(int(defaultInitialInterval.Nanoseconds())),
) * time.Second
}

maximumIntervalCoefficient, ok := options[maximumIntervalCoefficientConfigKey]
if ok {
defaultSettings.MaximumIntervalCoefficient = maximumIntervalCoefficient.(float64)
if coefficient, ok := options[maximumIntervalCoefficientConfigKey]; ok {
defaultSettings.MaximumIntervalCoefficient = number.NewNumber(
coefficient,
).GetFloatOrDefault(defaultMaximumIntervalCoefficient)
}

backoffCoefficient, ok := options[backoffCoefficientConfigKey]
if ok {
defaultSettings.BackoffCoefficient = backoffCoefficient.(float64)
if coefficient, ok := options[backoffCoefficientConfigKey]; ok {
defaultSettings.BackoffCoefficient = number.NewNumber(
coefficient,
).GetFloatOrDefault(defaultBackoffCoefficient)
}

maximumAttempts, ok := options[maximumAttemptsConfigKey]
if ok {
defaultSettings.MaximumAttempts = int32(maximumAttempts.(int))
if attempts, ok := options[maximumAttemptsConfigKey]; ok {
defaultSettings.MaximumAttempts = int32(number.NewNumber(
attempts,
).GetIntOrDefault(defaultMaximumAttempts))
}

return defaultSettings
Expand Down

0 comments on commit 5d17b1e

Please sign in to comment.