Files
tasks/common/dynamicconfig/file_based_client.go
Tim Deeb-SwihartandGitHub 95169a53c3 Replace gogo/protobuf with google/protobuf (#5032)
**What changed?**

gogo/protobuf has been replaced with Google's official go compiler. 

**Why?**

gogo/protobuf has been deprecated for some time and the community is
moving on, building new tools (like vtproto) atop google's v2 compiler.

**How did you test it?**

`make test`

**Potential risks**

1. The change from embedded gogo-generated-structs to
google-generated-pointers-to-structs created a risk of nil pointer
exceptions. I've fixed all the ones our tests found but it's possible
there are more lurking in the new code.
2. This change may cause our performance to decrease. Certainly
encoding/deconding of proto objects will become slower, but the overuse
of pointers by the google compiler may negatively affect our overall
performance. We'll need to keep an eye on the GC stats
3. This breaks the HTTP API. We will not support [shortand payload
encoding](https://github.com/temporalio/proposals/blob/master/api/http-api.md#payload-formatting)
in this first pass; that will come once this initial work is in testing.

**Breaking changes for developers**

- `*time.Time` in proto structs will now be
[timestamppb.Timestamp](https://pkg.go.dev/google.golang.org/[email protected]/types/known/timestamppb#section-documentation)
- `*time.Duration` will now be
[durationpb.Duration](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb)
- V2-generated structs embed locks, so you cannot dereference them. `go
vet` will scream at you about this. If you need a copy, use
`proto.Clone`.
- If the performance of this sucks then I will either update our code
generator to add shallow-clone methods or hand-roll the ones we need
- Proto enums will, when formatted to JSON, now be in
`SCREAMING_SNAKE_CASE` rather than `PascalCase`. We decided (in
discussion with the SDK team) that now was as good a time as any to rip
the bandage off.
- Proto objects, or objects embedding protos, cannot be compared using
`reflect.DeepEqual` or _anything_ that uses it. This includes `testify`
and `mock` equality testers!
- You will need to use the `common/testing/protorequire`,
`common/testing/protoassert`, or `common/testing/protomock` packages
instead. I've implemented proto-compatible matchers and assertions there
for all cases I've encountered
- If you need `reflect.DeepEqual` for any reason you can use
`go.temporal.io/api/temporalproto.DeepEqual` instead

Note that history loading will not be impacted by the JSON changes: I
rewrote history loading to dynamically fix incoming history JSON data
(like all our other sdks); you can find this code in [my fork of our go
API](https://github.com/tdeebswihart/temporal-api-go/blob/master/internal/temporalhistoryv1/load.go)
alongside its tests.

**🚨Sharp Edges Introduced🚨**

Beware `*timestamppb.Timestamp.AsTime()`. If you need to extract a time
value from a proto time (timestamppb) **always** make sure to check
whether it's nil first. When the proto object is `nil` `AsTime()` will
return a non-zero time at the proto epoch: UTC midnight on January 1,
1970.

I've made this mistake multiple times during this transition and each
time it's been a pain to debug

**Is hotfix candidate?**

No.
2023-11-21 15:19:37 -08:00

420 lines
12 KiB
Go

// 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.
//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination file_based_client_mock.go
package dynamicconfig
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"sync/atomic"
"time"
enumspb "go.temporal.io/api/enums/v1"
"gopkg.in/yaml.v3"
enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
)
var _ Client = (*fileBasedClient)(nil)
const (
minPollInterval = time.Second * 5
fileMode = 0644 // used for update config file
)
type (
fileReader interface {
Stat(src string) (os.FileInfo, error)
ReadFile(src string) ([]byte, error)
}
// FileBasedClientConfig is the config for the file based dynamic config client.
// It specifies where the config file is stored and how often the config should be
// updated by checking the config file again.
FileBasedClientConfig struct {
Filepath string `yaml:"filepath"`
PollInterval time.Duration `yaml:"pollInterval"`
}
configValueMap map[string][]ConstrainedValue
fileBasedClient struct {
values atomic.Value // configValueMap
logger log.Logger
reader fileReader
lastUpdatedTime time.Time
config *FileBasedClientConfig
doneCh <-chan interface{}
}
osReader struct {
}
)
// NewFileBasedClient creates a file based client.
func NewFileBasedClient(config *FileBasedClientConfig, logger log.Logger, doneCh <-chan interface{}) (*fileBasedClient, error) {
client := &fileBasedClient{
logger: logger,
reader: &osReader{},
config: config,
doneCh: doneCh,
}
err := client.init()
if err != nil {
return nil, err
}
return client, nil
}
func NewFileBasedClientWithReader(reader fileReader, config *FileBasedClientConfig, logger log.Logger, doneCh <-chan interface{}) (*fileBasedClient, error) {
client := &fileBasedClient{
logger: logger,
reader: reader,
config: config,
doneCh: doneCh,
}
err := client.init()
if err != nil {
return nil, err
}
return client, nil
}
func (fc *fileBasedClient) GetValue(key Key) []ConstrainedValue {
values := fc.values.Load().(configValueMap)
return values[strings.ToLower(key.String())]
}
func (fc *fileBasedClient) init() error {
if err := fc.validateConfig(fc.config); err != nil {
return fmt.Errorf("unable to validate dynamic config: %w", err)
}
if err := fc.update(); err != nil {
return fmt.Errorf("unable to read dynamic config: %w", err)
}
go func() {
ticker := time.NewTicker(fc.config.PollInterval)
for {
select {
case <-ticker.C:
err := fc.update()
if err != nil {
fc.logger.Error("Unable to update dynamic config.", tag.Error(err))
}
case <-fc.doneCh:
ticker.Stop()
return
}
}
}()
return nil
}
func (fc *fileBasedClient) update() error {
defer func() {
fc.lastUpdatedTime = time.Now().UTC()
}()
info, err := fc.reader.Stat(fc.config.Filepath)
if err != nil {
return fmt.Errorf("dynamic config file: %s: %w", fc.config.Filepath, err)
}
if !info.ModTime().After(fc.lastUpdatedTime) {
return nil
}
confContent, err := fc.reader.ReadFile(fc.config.Filepath)
if err != nil {
return fmt.Errorf("dynamic config file: %s: %w", fc.config.Filepath, err)
}
var yamlValues map[string][]struct {
Constraints map[string]any
Value any
}
if err = yaml.Unmarshal(confContent, &yamlValues); err != nil {
return fmt.Errorf("unable to decode dynamic config: %w", err)
}
newValues := make(configValueMap, len(yamlValues))
for key, yamlCV := range yamlValues {
cvs := make([]ConstrainedValue, len(yamlCV))
for i, cv := range yamlCV {
// yaml will unmarshal map into map[interface{}]interface{} instead of map[string]interface{}
// manually convert key type to string for all values here
cvs[i].Value, err = convertKeyTypeToString(cv.Value)
if err != nil {
return err
}
cvs[i].Constraints, err = convertYamlConstraints(cv.Constraints)
if err != nil {
return err
}
}
newValues[strings.ToLower(key)] = cvs
}
prev := fc.values.Swap(newValues)
oldValues, _ := prev.(configValueMap)
fc.logDiff(oldValues, newValues)
fc.logger.Info("Updated dynamic config")
return nil
}
func (fc *fileBasedClient) validateConfig(config *FileBasedClientConfig) error {
if config == nil {
return errors.New("configuration for dynamic config client is nil")
}
if _, err := fc.reader.Stat(config.Filepath); err != nil {
return fmt.Errorf("dynamic config: %s: %w", config.Filepath, err)
}
if config.PollInterval < minPollInterval {
return fmt.Errorf("poll interval should be at least %v", minPollInterval)
}
return nil
}
func (fc *fileBasedClient) logDiff(old configValueMap, new configValueMap) {
for key, newValues := range new {
oldValues, ok := old[key]
if !ok {
for _, newValue := range newValues {
// new key added
fc.logValueDiff(key, nil, &newValue)
}
} else {
// compare existing keys
fc.logConstraintsDiff(key, oldValues, newValues)
}
}
// check for removed values
for key, oldValues := range old {
if _, ok := new[key]; !ok {
for _, oldValue := range oldValues {
fc.logValueDiff(key, &oldValue, nil)
}
}
}
}
func (fc *fileBasedClient) logConstraintsDiff(key string, oldValues []ConstrainedValue, newValues []ConstrainedValue) {
for _, oldValue := range oldValues {
matchFound := false
for _, newValue := range newValues {
if oldValue.Constraints == newValue.Constraints {
matchFound = true
if !reflect.DeepEqual(oldValue.Value, newValue.Value) {
fc.logValueDiff(key, &oldValue, &newValue)
}
}
}
if !matchFound {
fc.logValueDiff(key, &oldValue, nil)
}
}
for _, newValue := range newValues {
matchFound := false
for _, oldValue := range oldValues {
if oldValue.Constraints == newValue.Constraints {
matchFound = true
}
}
if !matchFound {
fc.logValueDiff(key, nil, &newValue)
}
}
}
func (fc *fileBasedClient) logValueDiff(key string, oldValue *ConstrainedValue, newValue *ConstrainedValue) {
logLine := &strings.Builder{}
logLine.Grow(128)
logLine.WriteString("dynamic config changed for the key: ")
logLine.WriteString(key)
logLine.WriteString(" oldValue: ")
fc.appendConstrainedValue(logLine, oldValue)
logLine.WriteString(" newValue: ")
fc.appendConstrainedValue(logLine, newValue)
fc.logger.Info(logLine.String())
}
func (fc *fileBasedClient) appendConstrainedValue(logLine *strings.Builder, value *ConstrainedValue) {
if value == nil {
logLine.WriteString("nil")
} else {
logLine.WriteString("{ constraints: {")
if value.Constraints.Namespace != "" {
logLine.WriteString(fmt.Sprintf("{Namespace:%s}", value.Constraints.Namespace))
}
if value.Constraints.NamespaceID != "" {
logLine.WriteString(fmt.Sprintf("{NamespaceID:%s}", value.Constraints.NamespaceID))
}
if value.Constraints.TaskQueueName != "" {
logLine.WriteString(fmt.Sprintf("{TaskQueueName:%s}", value.Constraints.TaskQueueName))
}
if value.Constraints.TaskQueueType != enumspb.TASK_QUEUE_TYPE_UNSPECIFIED {
logLine.WriteString(fmt.Sprintf("{TaskQueueType:%s}", value.Constraints.TaskQueueType))
}
if value.Constraints.ShardID != 0 {
logLine.WriteString(fmt.Sprintf("{ShardID:%d}", value.Constraints.ShardID))
}
if value.Constraints.TaskType != enumsspb.TASK_TYPE_UNSPECIFIED {
logLine.WriteString(fmt.Sprintf("{HistoryTaskType:%s}", value.Constraints.TaskType))
}
logLine.WriteString(fmt.Sprint("} value: ", value.Value, " }"))
}
}
func convertKeyTypeToString(v interface{}) (interface{}, error) {
switch v := v.(type) {
case map[interface{}]interface{}:
return convertKeyTypeToStringMap(v)
case []interface{}:
return convertKeyTypeToStringSlice(v)
default:
return v, nil
}
}
func convertKeyTypeToStringMap(m map[interface{}]interface{}) (map[string]interface{}, error) {
stringKeyMap := make(map[string]interface{})
for key, value := range m {
stringKey, ok := key.(string)
if !ok {
return nil, fmt.Errorf("type of key %v is not string", key)
}
convertedValue, err := convertKeyTypeToString(value)
if err != nil {
return nil, err
}
stringKeyMap[stringKey] = convertedValue
}
return stringKeyMap, nil
}
func convertKeyTypeToStringSlice(s []interface{}) ([]interface{}, error) {
stringKeySlice := make([]interface{}, len(s))
for idx, value := range s {
convertedValue, err := convertKeyTypeToString(value)
if err != nil {
return nil, err
}
stringKeySlice[idx] = convertedValue
}
return stringKeySlice, nil
}
func convertYamlConstraints(m map[string]any) (Constraints, error) {
var cs Constraints
for k, v := range m {
switch strings.ToLower(k) {
case "namespace":
if v, ok := v.(string); ok {
cs.Namespace = v
} else {
return cs, fmt.Errorf("namespace constraint must be string")
}
case "namespaceid":
if v, ok := v.(string); ok {
cs.NamespaceID = v
} else {
return cs, fmt.Errorf("namespaceID constraint must be string")
}
case "taskqueuename":
if v, ok := v.(string); ok {
cs.TaskQueueName = v
} else {
return cs, fmt.Errorf("taskQueueName constraint must be string")
}
case "tasktype":
switch v := v.(type) {
case string:
i, err := enumspb.TaskQueueTypeFromString(v)
if err != nil {
return cs, fmt.Errorf("invalid value for taskType: %w", err)
} else if i <= enumspb.TASK_QUEUE_TYPE_UNSPECIFIED {
return cs, fmt.Errorf("taskType constraint must be Workflow/Activity")
}
cs.TaskQueueType = i
case int:
if v > int(enumspb.TASK_QUEUE_TYPE_UNSPECIFIED) {
cs.TaskQueueType = enumspb.TaskQueueType(v)
} else {
return cs, fmt.Errorf("taskType constraint must be Workflow/Activity")
}
default:
return cs, fmt.Errorf("taskType constraint must be Workflow/Activity")
}
case "historytasktype":
switch v := v.(type) {
case string:
tt, err := enumsspb.TaskTypeFromString(v)
if err != nil {
return cs, fmt.Errorf("invalid value for historytasktype constraint: %w", err)
} else if tt <= enumsspb.TASK_TYPE_UNSPECIFIED {
return cs, fmt.Errorf("historytasktype %s constraint is not supported", v)
}
cs.TaskType = tt
case int:
cs.TaskType = enumsspb.TaskType(v)
default:
return cs, fmt.Errorf("historytasktype %T constraint is not supported", v)
}
case "shardid":
if v, ok := v.(int); ok {
cs.ShardID = int32(v)
} else {
return cs, fmt.Errorf("shardID constraint must be integer")
}
default:
return cs, fmt.Errorf("unknown constraint type %q", k)
}
}
return cs, nil
}
func (r *osReader) ReadFile(src string) ([]byte, error) {
return os.ReadFile(src)
}
func (r *osReader) Stat(src string) (os.FileInfo, error) {
return os.Stat(src)
}