Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Size() measuring size estimation for execution cache #6681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,7 @@ const (
ShardModeHashRing = "hash-ring"
ShardModeShardDistributor = "shard-distributor"
)

const (
StringSizeOverheadBytes = 16
)
11 changes: 11 additions & 0 deletions common/definition/workflowIdentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package definition

import "github.com/uber/cadence/common"

type (
// WorkflowIdentifier is the combinations which represent a workflow
WorkflowIdentifier struct {
Expand All @@ -29,6 +31,15 @@ type (
}
)

// Size calculates the size in bytes of the WorkflowIdentifier struct.
func (wi *WorkflowIdentifier) Size() uint64 {
// Calculate the size of strings in bytes, we assume that all those fields are using ASCII which is 1 byte per char
size := len(wi.DomainID) + len(wi.WorkflowID) + len(wi.RunID)
// Each string internally holds a reference pointer and a length, which are 8 bytes each
stringOverhead := 3 * common.StringSizeOverheadBytes
return uint64(size + stringOverhead)
}

// NewWorkflowIdentifier create a new WorkflowIdentifier
func NewWorkflowIdentifier(domainID string, workflowID string, runID string) WorkflowIdentifier {
return WorkflowIdentifier{
Expand Down
71 changes: 71 additions & 0 deletions common/definition/workflowidentifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// The MIT License (MIT)

// 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 definition

import (
"testing"

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

// WorkflowIdentifierTestSuite is a test suite for the WorkflowIdentifier struct.
type WorkflowIdentifierTestSuite struct {
suite.Suite
}

// SetupTest runs before each test function in the suite.
func (suite *WorkflowIdentifierTestSuite) SetupTest() {
// Any necessary setup would go here, but it's not required in this case.
}

// TestSize verifies the Size method of WorkflowIdentifier.
func (suite *WorkflowIdentifierTestSuite) TestSize() {
tests := []struct {
wi WorkflowIdentifier
expected uint64
}{
{
wi: NewWorkflowIdentifier("domain", "workflow", "run"),
expected: uint64(len("domain") + len("workflow") + len("run") + 3*16),
},
{
wi: NewWorkflowIdentifier("", "", ""),
expected: uint64(3 * 16),
},
{
wi: NewWorkflowIdentifier("a", "b", "c"),
expected: uint64(3 + 3*16),
},
}

for _, test := range tests {
size := test.wi.Size()
assert.Equal(suite.T(), test.expected, size)
}
}

// TestWorkflowIdentifierTestSuite runs the test suite.
func TestWorkflowIdentifierTestSuite(t *testing.T) {
suite.Run(t, new(WorkflowIdentifierTestSuite))
}
23 changes: 23 additions & 0 deletions service/history/execution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ type (
ctx context.Context,
now time.Time,
) error

Size() uint64
}
)

Expand Down Expand Up @@ -1385,6 +1387,27 @@ func (c *contextImpl) ReapplyEvents(
)
}

func (c *contextImpl) Size() uint64 {

var size int
// Estimate size of strings
size += len(c.domainID)

size += len(c.workflowExecution.GetWorkflowID()) + len(c.workflowExecution.GetRunID())
size += 3 * common.StringSizeOverheadBytes
size += int(c.shard.Size())

size += 3 * 8 // logger
size += 512 // MetricsClient estimation
size += 256 // ExecutionManager estimation
size += 8 // Mutex
size += c.mutableState.GetEstimatedMutableStateSize()
size += 8 // stats pointer

size += 18 * 8 // 18 function pointers with 8 bytes each
return uint64(size)
}

func isOperationPossiblySuccessfulError(err error) bool {
switch err.(type) {
case nil:
Expand Down
14 changes: 14 additions & 0 deletions service/history/execution/context_mock.go

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

2 changes: 2 additions & 0 deletions service/history/execution/mutable_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,7 @@ type (

GetHistorySize() int64
SetHistorySize(size int64)

GetEstimatedMutableStateSize() int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you have a Size interface somewhere, but would it make more sense for using that?

The thing about creating an interface for Size() int is that it'll allow whatever caches that need to take inputs can just require the interface, rather than any specific concrete types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I will change the naming to Size(). Thanks.

}
)
5 changes: 5 additions & 0 deletions service/history/execution/mutable_state_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,11 @@ func (e *mutableStateBuilder) SetHistorySize(size int64) {
e.executionStats.HistorySize = size
}

func (e *mutableStateBuilder) GetEstimatedMutableStateSize() int {
// TODO: To be implemented
return 0
}

func (e *mutableStateBuilder) prepareCloseTransaction(
transactionPolicy TransactionPolicy,
) error {
Expand Down
14 changes: 14 additions & 0 deletions service/history/execution/mutable_state_mock.go

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

7 changes: 7 additions & 0 deletions service/history/shard/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type (
ReplicateFailoverMarkers(ctx context.Context, markers []*persistence.FailoverMarkerTask) error
AddingPendingFailoverMarker(*types.FailoverMarkerAttributes) error
ValidateAndUpdateFailoverMarkers() ([]*types.FailoverMarkerAttributes, error)

Size() uint64
}

// TransferFailoverLevel contains corresponding start / end level
Expand Down Expand Up @@ -1413,6 +1415,11 @@ func (s *contextImpl) ValidateAndUpdateFailoverMarkers() ([]*types.FailoverMarke
return s.shardInfo.PendingFailoverMarkers, nil
}

func (s *contextImpl) Size() uint64 {
// TODO: To be implemented
return 0
}

func acquireShard(
shardItem *historyShardsItem,
closeCallback func(int, *historyShardsItem),
Expand Down
14 changes: 14 additions & 0 deletions service/history/shard/context_mock.go

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

Loading