Skip to content

Commit

Permalink
First attempt to cleanup worker code. (#982)
Browse files Browse the repository at this point in the history
- Moved replayer, register and encoding to their own files.
- Moved some util functions to common.
- No functional change.
- Fix licence scanner
  • Loading branch information
meiliang86 committed Jun 4, 2020
1 parent b707830 commit 44428ce
Show file tree
Hide file tree
Showing 11 changed files with 741 additions and 638 deletions.
3 changes: 1 addition & 2 deletions interceptors/workflow_interceptor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright (c) 2017-2020 Uber Technologies Inc.
// Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
// Modifications Copyright (c) 2020 Uber Technologies Inc.
// Copyright (c) 2020 Temporal Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
15 changes: 8 additions & 7 deletions internal/cmd/tools/copyright/licensegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -48,12 +49,12 @@ type (
// licenseFileName is the name of the license file
const licenseFileName = "LICENSE"

// unique prefix that identifies a license header
const licenseHeaderPrefix = "// Copyright (c)"
// unique indicator that identifies a license header
const licenseHeaderIndicator = "Copyright (c)"

var (
// directories to be excluded
dirDenylist = []string{"vendor/"}
dirExcludeList = []string{"vendor/"}
// default perms for the newly created files
defaultFilePerms = os.FileMode(0644)
)
Expand Down Expand Up @@ -121,15 +122,15 @@ func (task *addLicenseHeaderTask) handleFile(path string, fileInfo os.FileInfo,
return err
}

buf := make([]byte, len(licenseHeaderPrefix))
_, err = io.ReadFull(f, buf)
reader := bufio.NewReader(f)
firstLine, err := reader.ReadString('\n')
f.Close()

if err != nil && !isEOF(err) {
return err
}

if string(buf) == licenseHeaderPrefix {
if strings.Contains(firstLine, licenseHeaderIndicator) {
return nil // file already has the copyright header
}

Expand All @@ -153,7 +154,7 @@ func isFileAutogenerated(path string) bool {
}

func mustProcessPath(path string) bool {
for _, d := range dirDenylist {
for _, d := range dirExcludeList {
if strings.HasPrefix(path, d) {
return false
}
Expand Down
49 changes: 49 additions & 0 deletions internal/common/thrift_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package common

import (
"reflect"

"github.com/apache/thrift/lib/go/thrift"
)

Expand Down Expand Up @@ -82,3 +84,50 @@ func TListDeserialize(ts []thrift.TStruct, b []byte) (err error) {

return
}

// IsUseThriftEncoding checks if the objects passed in are all encoded using thrift.
func IsUseThriftEncoding(objs []interface{}) bool {
// NOTE: our criteria to use which encoder is simple if all the types are serializable using thrift then we use
// thrift encoder. For everything else we default to gob.

if len(objs) == 0 {
return false
}

for i := 0; i < len(objs); i++ {
if !IsThriftType(objs[i]) {
return false
}
}
return true
}

// IsUseThriftDecoding checks if the objects passed in are all de-serializable using thrift.
func IsUseThriftDecoding(objs []interface{}) bool {
// NOTE: our criteria to use which encoder is simple if all the types are de-serializable using thrift then we use
// thrift decoder. For everything else we default to gob.

if len(objs) == 0 {
return false
}

for i := 0; i < len(objs); i++ {
rVal := reflect.ValueOf(objs[i])
if rVal.Kind() != reflect.Ptr || !IsThriftType(reflect.Indirect(rVal).Interface()) {
return false
}
}
return true
}

// IsThriftType checks whether the object passed in is a thrift encoded object.
func IsThriftType(v interface{}) bool {
// NOTE: Thrift serialization works only if the values are pointers.
// Thrift has a validation that it meets thift.TStruct which has Read/Write pointer receivers.

if reflect.ValueOf(v).Kind() != reflect.Ptr {
return false
}
t := reflect.TypeOf((*thrift.TStruct)(nil)).Elem()
return reflect.TypeOf(v).Implements(t)
}
10 changes: 9 additions & 1 deletion internal/common/util/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 Uber Technologies, Inc.
// Copyright (c) 2017-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
Expand All @@ -21,6 +21,7 @@
package util

import (
"reflect"
"sync"
"time"
)
Expand Down Expand Up @@ -59,3 +60,10 @@ func AwaitWaitGroup(wg *sync.WaitGroup, timeout time.Duration) bool {
return false
}
}

var typeOfByteSlice = reflect.TypeOf(([]byte)(nil))

// IsTypeByteSlice checks whether the type passed in is a ByteSlice type
func IsTypeByteSlice(inType reflect.Type) bool {
return inType == typeOfByteSlice || inType == reflect.PtrTo(typeOfByteSlice)
}
11 changes: 7 additions & 4 deletions internal/encoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ package internal

import (
"reflect"

"go.uber.org/cadence/internal/common"
"go.uber.org/cadence/internal/common/util"
)

type (
Expand Down Expand Up @@ -74,12 +77,12 @@ func getDefaultDataConverter() DataConverter {
}

func (dc *defaultDataConverter) ToData(r ...interface{}) ([]byte, error) {
if len(r) == 1 && isTypeByteSlice(reflect.TypeOf(r[0])) {
if len(r) == 1 && util.IsTypeByteSlice(reflect.TypeOf(r[0])) {
return r[0].([]byte), nil
}

var encoder encoding
if isUseThriftEncoding(r) {
if common.IsUseThriftEncoding(r) {
encoder = &thriftEncoding{}
} else {
encoder = &jsonEncoding{}
Expand All @@ -93,13 +96,13 @@ func (dc *defaultDataConverter) ToData(r ...interface{}) ([]byte, error) {
}

func (dc *defaultDataConverter) FromData(data []byte, to ...interface{}) error {
if len(to) == 1 && isTypeByteSlice(reflect.TypeOf(to[0])) {
if len(to) == 1 && util.IsTypeByteSlice(reflect.TypeOf(to[0])) {
reflect.ValueOf(to[0]).Elem().SetBytes(data)
return nil
}

var encoder encoding
if isUseThriftDecoding(to) {
if common.IsUseThriftDecoding(to) {
encoder = &thriftEncoding{}
} else {
encoder = &jsonEncoding{}
Expand Down
109 changes: 109 additions & 0 deletions internal/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2017-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 internal

import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"

"github.com/apache/thrift/lib/go/thrift"
"go.uber.org/cadence/internal/common"
)

// encoding is capable of encoding and decoding objects
type encoding interface {
Marshal([]interface{}) ([]byte, error)
Unmarshal([]byte, []interface{}) error
}

// jsonEncoding encapsulates json encoding and decoding
type jsonEncoding struct {
}

// Marshal encodes an array of object into bytes
func (g jsonEncoding) Marshal(objs []interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for i, obj := range objs {
if err := enc.Encode(obj); err != nil {
if err == io.EOF {
return nil, fmt.Errorf("missing argument at index %d of type %T", i, obj)
}
return nil, fmt.Errorf(
"unable to encode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
}
}
return buf.Bytes(), nil
}

// Unmarshal decodes a byte array into the passed in objects
func (g jsonEncoding) Unmarshal(data []byte, objs []interface{}) error {
dec := json.NewDecoder(bytes.NewBuffer(data))
for i, obj := range objs {
if err := dec.Decode(obj); err != nil {
return fmt.Errorf(
"unable to decode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
}
}
return nil
}

// thriftEncoding encapsulates thrift serializer/de-serializer.
type thriftEncoding struct{}

// Marshal encodes an array of thrift into bytes
func (g thriftEncoding) Marshal(objs []interface{}) ([]byte, error) {
var tlist []thrift.TStruct
for i := 0; i < len(objs); i++ {
if !common.IsThriftType(objs[i]) {
return nil, fmt.Errorf("pointer to thrift.TStruct type is required for %v argument", i+1)
}
t := reflect.ValueOf(objs[i]).Interface().(thrift.TStruct)
tlist = append(tlist, t)
}
return common.TListSerialize(tlist)
}

// Unmarshal decodes an array of thrift into bytes
func (g thriftEncoding) Unmarshal(data []byte, objs []interface{}) error {
var tlist []thrift.TStruct
for i := 0; i < len(objs); i++ {
rVal := reflect.ValueOf(objs[i])
if rVal.Kind() != reflect.Ptr || !common.IsThriftType(reflect.Indirect(rVal).Interface()) {
return fmt.Errorf("pointer to pointer thrift.TStruct type is required for %v argument", i+1)
}
t := reflect.New(rVal.Elem().Type().Elem()).Interface().(thrift.TStruct)
tlist = append(tlist, t)
}

if err := common.TListDeserialize(tlist, data); err != nil {
return err
}

for i := 0; i < len(tlist); i++ {
reflect.ValueOf(objs[i]).Elem().Set(reflect.ValueOf(tlist[i]))
}

return nil
}
1 change: 0 additions & 1 deletion internal/interceptors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright (c) 2017-2020 Uber Technologies Inc.
// Modifications Copyright (c) 2020 Uber Technologies Inc.
// Copyright (c) 2020 Temporal Technologies, Inc.
//
Expand Down
Loading

0 comments on commit 44428ce

Please sign in to comment.