Skip to content

Commit

Permalink
Unalias and validate search attributes when import workflow history (#…
Browse files Browse the repository at this point in the history
…6563)

## What changed?
<!-- Describe what has changed in this PR -->
Unalias and validate search attributes when import workflow history.

## Why?
<!-- Tell your future self why have you made these changes -->
Search attributes in workflow histostory JSON file might use aliases (if
history was exported using `temporal` cli) or be invalid
(name/type/length). Previously import blindly accepted any search
attributes and then background visibility task processing could fail.
Now search attributes are validated and unaliased (if needed) before
getting to the history service.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
Added new unit tests for both valid and invalid cases.

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
No risks.

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->
No.
  • Loading branch information
alexshtin authored Oct 4, 2024
1 parent 5e517f1 commit e6e61ed
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bins: temporal-server temporal-cassandra-tool temporal-sql-tool tdbg
all: clean proto bins check test

# Used in CI
ci-build-misc: print-go-version proto go-generate buf-breaking bins temporal-server-debug shell-check copyright-check goimports-all gomodtidy ensure-no-changes
ci-build-misc: print-go-version proto go-generate buf-breaking bins temporal-server-debug shell-check copyright-check goimports-all gomodtidy ensure-no-changes

# Delete all build artifacts
clean: clean-bins clean-test-results
Expand All @@ -18,7 +18,7 @@ clean: clean-bins clean-test-results
rm -rf $(LOCALBIN)

# Recompile proto files.
proto: lint-protos lint-api protoc service-clients server-interceptors
proto: lint-protos lint-api protoc proto-codegen
########################################################################

.PHONY: proto protoc install bins ci-build-misc clean
Expand Down Expand Up @@ -243,13 +243,13 @@ protoc: $(PROTOGEN) $(MOCKGEN) $(GOIMPORTS) $(PROTOC_GEN_GO) $(PROTOC_GEN_GO_GRP
API_BINPB=$(API_BINPB) PROTO_ROOT=$(PROTO_ROOT) PROTO_OUT=$(PROTO_OUT) \
./develop/protoc.sh

service-clients:
proto-codegen:
@printf $(COLOR) "Generate service clients..."
@go generate -run genrpcwrappers ./client/...

server-interceptors:
@printf $(COLOR) "Generate server interceptors..."
@go generate ./common/rpc/interceptor/logtags/...
@printf $(COLOR) "Generate search attributes helpers..."
@go generate -run gensearchattributehelpers ./common/searchattribute/...

update-go-api:
@printf $(COLOR) "Update go.temporal.io/api@master..."
Expand Down
158 changes: 158 additions & 0 deletions cmd/tools/gensearchattributehelpers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// 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 main

import (
"flag"
"fmt"
"io"
"log"
"os"
"path"
"reflect"
"regexp"
"strings"
"text/template"

historypb "go.temporal.io/api/history/v1"
)

type (
eventData struct {
AttributesTypeName string
}

searchAttributesHelpersData struct {
Events []eventData
}
)

var (
// Is used to find attribute getters and extract the event type (match[1]).
attributesGetterRegex = regexp.MustCompile("^Get(.+EventAttributes)$")
)

func main() {
outPathFlag := flag.String("out", ".", "path to write generated files")
licenseFlag := flag.String("copyright_file", "LICENSE", "path to license to copy into header")
flag.Parse()

licenseText := readLicenseFile(*licenseFlag)

eventHelpersFile := path.Join(*outPathFlag, "event_gen.go")
callWithFile(generateSearchAttributesEventHelpers, eventHelpersFile, licenseText)
}

func generateSearchAttributesEventHelpers(w io.Writer) {

writeSearchAttributesEventHelpers(w, `
package searchattribute
import (
commonpb "go.temporal.io/api/common/v1"
historypb "go.temporal.io/api/history/v1"
)
func SetToEvent(event *historypb.HistoryEvent, sas *commonpb.SearchAttributes) bool {
switch e := event.GetAttributes().(type) {
{{- range .Events}}
case *historypb.HistoryEvent_{{.AttributesTypeName}}:
e.{{.AttributesTypeName}}.SearchAttributes = sas
return true
{{- end}}
default:
return false
}
}
func GetFromEvent(event *historypb.HistoryEvent) (*commonpb.SearchAttributes, bool) {
switch e := event.GetAttributes().(type) {
{{- range .Events}}
case *historypb.HistoryEvent_{{.AttributesTypeName}}:
return e.{{.AttributesTypeName}}.GetSearchAttributes(), true
{{- end}}
default:
return nil, false
}
}
`)
}

func writeSearchAttributesEventHelpers(w io.Writer, tmpl string) {
sahd := searchAttributesHelpersData{}

historyEventT := reflect.TypeOf((*historypb.HistoryEvent)(nil))

for i := 0; i < historyEventT.NumMethod(); i++ {
attributesGetter := historyEventT.Method(i)
matches := attributesGetterRegex.FindStringSubmatch(attributesGetter.Name)
if len(matches) < 2 {
continue
}
if attributesGetter.Type.NumOut() != 1 {
continue
}
if _, found := attributesGetter.Type.Out(0).MethodByName("GetSearchAttributes"); !found {
continue
}

ed := eventData{
AttributesTypeName: matches[1],
}
sahd.Events = append(sahd.Events, ed)
}

fatalIfErr(template.Must(template.New("code").Parse(tmpl)).Execute(w, sahd))
}

func callWithFile(generator func(io.Writer), outFile string, licenseText string) {
w, err := os.Create(outFile)
fatalIfErr(err)
defer func() {
fatalIfErr(w.Close())
}()
_, err = fmt.Fprintf(w, "%s\n// Code generated by cmd/tools/gensearchattributehelpers. DO NOT EDIT.\n", licenseText)
fatalIfErr(err)
generator(w)
}

func readLicenseFile(filePath string) string {
text, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
var lines []string
for _, line := range strings.Split(string(text), "\n") {
lines = append(lines, strings.TrimRight("// "+line, " "))
}
return strings.Join(lines, "\n") + "\n"
}

func fatalIfErr(err error) {
if err != nil {
//nolint:revive // calls to log.Fatal only in main() or init() functions (revive)
log.Fatal(err)
}
}
28 changes: 28 additions & 0 deletions common/searchattribute/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.

// Generates event helper functions for setting and getting search attributes on history events.
//go:generate go run ../../cmd/tools/gensearchattributehelpers -copyright_file ../../LICENSE

package searchattribute
66 changes: 66 additions & 0 deletions common/searchattribute/event_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions common/testing/testvars/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (a Any) Int() int {
return randInt(a.testHash, 3, 3, 3)
}

func (a Any) Int64() int64 {
return int64(a.Int())
}

func (a Any) EventID() int64 {
// This produces EventID in XX0YY format, where XX is unique for every test and YY is a random number.
return int64(randInt(a.testHash, 2, 1, 2))
Expand Down
12 changes: 10 additions & 2 deletions common/testing/testvars/test_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,16 @@ func (tv *TestVars) QueryType(key ...string) string {
return tv.getOrCreate("query_type", key).(string)
}

func (tv *TestVars) WithQueryType(queryTypeID string, key ...string) *TestVars {
return tv.cloneSet("query_type", key, queryTypeID)
func (tv *TestVars) WithQueryType(queryType string, key ...string) *TestVars {
return tv.cloneSet("query_type", key, queryType)
}

func (tv *TestVars) IndexName(key ...string) string {
return tv.getOrCreate("index_name", key).(string)
}

func (tv *TestVars) WithIndexName(indexName string, key ...string) *TestVars {
return tv.cloneSet("index_name", key, indexName)
}

// ----------- Generic methods ------------
Expand Down
Loading

0 comments on commit e6e61ed

Please sign in to comment.