forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mutableState checkums part#1 (cadence-workflow#2941)
- Loading branch information
1 parent
b694784
commit d695915
Showing
17 changed files
with
1,999 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2019 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 checksum | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"hash/crc32" | ||
|
||
"github.com/uber/cadence/common/codec" | ||
) | ||
|
||
// GenerateCRC32 generates an IEEE crc32 checksum on the | ||
// serilized byte array of the given thrift object. The | ||
// serialization proto used will be of type thriftRW | ||
func GenerateCRC32( | ||
payload codec.ThriftObject, | ||
payloadVersion int, | ||
) (Checksum, error) { | ||
|
||
encoder := codec.NewThriftRWEncoder() | ||
payloadBytes, err := encoder.Encode(payload) | ||
if err != nil { | ||
return Checksum{}, err | ||
} | ||
|
||
crc := crc32.ChecksumIEEE(payloadBytes) | ||
checksum := make([]byte, 4) | ||
binary.BigEndian.PutUint32(checksum, crc) | ||
return Checksum{ | ||
Value: checksum, | ||
Version: payloadVersion, | ||
Flavor: FlavorIEEECRC32OverThriftBinary, | ||
}, nil | ||
} | ||
|
||
// Verify verifies that the checksum generated from the | ||
// given thrift object matches the specified expected checksum | ||
// Return ErrMismatch when checksums mismatch | ||
func Verify( | ||
payload codec.ThriftObject, | ||
checksum Checksum, | ||
) error { | ||
|
||
if !checksum.Flavor.IsValid() || checksum.Flavor != FlavorIEEECRC32OverThriftBinary { | ||
return fmt.Errorf("unknown checksum flavor %v", checksum.Flavor) | ||
} | ||
|
||
expected, err := GenerateCRC32(payload, checksum.Version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !bytes.Equal(expected.Value, checksum.Value) { | ||
return ErrMismatch | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2019 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 checksum | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pborman/uuid" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/uber/cadence/.gen/go/shared" | ||
"github.com/uber/cadence/common" | ||
) | ||
|
||
func TestCRC32OverThrift(t *testing.T) { | ||
// note: do not use a struct with map since | ||
// iteration order is not guaranteed in Go and | ||
// so, each call to thrift encode will result in | ||
// different set of serialized bytes | ||
obj := &shared.WorkflowExecutionInfo{ | ||
Execution: &shared.WorkflowExecution{ | ||
WorkflowId: common.StringPtr(uuid.New()), | ||
RunId: common.StringPtr(uuid.New()), | ||
}, | ||
StartTime: common.Int64Ptr(time.Now().UnixNano()), | ||
HistoryLength: common.Int64Ptr(550), | ||
} | ||
|
||
parallism := 10 | ||
loopCount := 100 | ||
successCount := int64(0) | ||
|
||
startC := make(chan struct{}) | ||
doneWG := sync.WaitGroup{} | ||
doneWG.Add(parallism) | ||
|
||
for i := 0; i < parallism; i++ { | ||
go func() { | ||
defer doneWG.Done() | ||
<-startC | ||
for count := 0; count < loopCount; count++ { | ||
csum, err := GenerateCRC32(obj, 1) | ||
if err != nil { | ||
return | ||
} | ||
if err := Verify(obj, csum); err != nil { | ||
return | ||
} | ||
atomic.AddInt64(&successCount, 1) | ||
} | ||
}() | ||
} | ||
|
||
close(startC) | ||
success := common.AwaitWaitGroup(&doneWG, time.Second) | ||
assert.True(t, success, "timed out waiting for goroutines to finish") | ||
assert.Equal(t, int64(parallism*loopCount), successCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2019 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 checksum | ||
|
||
import "errors" | ||
|
||
type ( | ||
// Checksum represents a checksum value along | ||
// with associated metadata | ||
Checksum struct { | ||
// Version represents version of the payload from | ||
Version int | ||
// which this checksum was derived | ||
Flavor Flavor | ||
// Value is the checksum value | ||
Value []byte | ||
} | ||
|
||
// Flavor is an enum type that represents the type of checksum | ||
Flavor int | ||
) | ||
|
||
const ( | ||
// FlavorUnknown represents an unknown/uninitialized checksum flavor | ||
FlavorUnknown Flavor = iota | ||
// FlavorIEEECRC32OverThriftBinary represents crc32 checksum generated over thriftRW serialized payload | ||
FlavorIEEECRC32OverThriftBinary | ||
maxFlavors | ||
) | ||
|
||
// ErrMismatch indicates a checksum verification failure due to | ||
// a derived checksum not being equal to expected checksum | ||
var ErrMismatch = errors.New("checksum mismatch error") | ||
|
||
// IsValid returns true if the checksum flavor is valid | ||
func (f Flavor) IsValid() bool { | ||
return f > FlavorUnknown && f < maxFlavors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule idls
updated
from 56ca0b to a62e74
Oops, something went wrong.