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

Enable the workflow ID cache in shadow mode for start workflow #5641

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 21 additions & 1 deletion service/history/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ import (
"github.com/uber/cadence/service/history/resource"
"github.com/uber/cadence/service/history/shard"
"github.com/uber/cadence/service/history/task"
"github.com/uber/cadence/service/history/workflowcache"
)

const shardOwnershipTransferDelay = 5 * time.Second
const (
shardOwnershipTransferDelay = 5 * time.Second
workflowIDCacheTTL = 1 * time.Second
workflowIDCacheMaxCount = 10_000
jakobht marked this conversation as resolved.
Show resolved Hide resolved
)

type (
// handlerImpl is an implementation for history service independent of wire protocol
Expand All @@ -73,6 +78,7 @@ type (
replicationTaskFetchers replication.TaskFetchers
queueTaskProcessor task.Processor
failoverCoordinator failover.Coordinator
workflowIDCache workflowcache.WFCache
}
)

Expand Down Expand Up @@ -102,6 +108,16 @@ func NewHandler(
config: config,
tokenSerializer: common.NewJSONTaskTokenSerializer(),
rateLimiter: quotas.NewDynamicRateLimiter(config.RPS.AsFloat64()),
workflowIDCache: workflowcache.New(workflowcache.Params{
TTL: workflowIDCacheTTL,
ExternalLimiterFactory: quotas.NewSimpleDynamicRateLimiterFactory(config.WorkflowIDExternalRPS),
InternalLimiterFactory: quotas.NewSimpleDynamicRateLimiterFactory(config.WorkflowIDInternalRPS),
WorkflowIDCacheEnabled: config.WorkflowIDCacheEnabled,
MaxCount: workflowIDCacheMaxCount,
DomainCache: resource.GetDomainCache(),
Logger: resource.GetLogger(),
MetricsClient: resource.GetMetricsClient(),
}),
}

// prevent us from trying to serve requests before shard controller is started and ready
Expand Down Expand Up @@ -681,6 +697,10 @@ func (h *handlerImpl) StartWorkflowExecution(

startRequest := wrappedRequest.StartRequest
workflowID := startRequest.GetWorkflowID()

// TODO: actually rate limit
jakobht marked this conversation as resolved.
Show resolved Hide resolved
h.workflowIDCache.AllowExternal(domainID, workflowID)
jakobht marked this conversation as resolved.
Show resolved Hide resolved

engine, err1 := h.controller.GetEngine(workflowID)
if err1 != nil {
return nil, h.error(err1, scope, domainID, workflowID, "")
Expand Down
43 changes: 43 additions & 0 deletions service/history/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ import (
"github.com/uber/cadence/service/history/engine"
"github.com/uber/cadence/service/history/resource"
"github.com/uber/cadence/service/history/shard"
"github.com/uber/cadence/service/history/workflowcache"
)

const (
testWorkflowID = "test-workflow-id"
testWorkflowRunID = "test-workflow-run-id"
testDomainID = "BF80C53A-ED56-4DD9-84EB-BE9AD4E45867"
)

type (
Expand All @@ -53,6 +60,7 @@ type (
mockResource *resource.Test
mockShardController *shard.MockController
mockEngine *engine.MockEngine
mockWFCache *workflowcache.MockWFCache

handler *handlerImpl
}
Expand All @@ -72,9 +80,11 @@ func (s *handlerSuite) SetupTest() {
s.mockShardController = shard.NewMockController(s.controller)
s.mockEngine = engine.NewMockEngine(s.controller)
s.mockShardController.EXPECT().GetEngineForShard(gomock.Any()).Return(s.mockEngine, nil).AnyTimes()
s.mockWFCache = workflowcache.NewMockWFCache(s.controller)

s.handler = NewHandler(s.mockResource, config.NewForTest()).(*handlerImpl)
s.handler.controller = s.mockShardController
s.handler.workflowIDCache = s.mockWFCache
s.handler.startWG.Done()
}

Expand Down Expand Up @@ -294,3 +304,36 @@ func TestCorrectUseOfErrorHandling(t *testing.T) {
})
}
}

func (s *handlerSuite) TestStartWorkflowExecution() {

request := &types.HistoryStartWorkflowExecutionRequest{
DomainUUID: testDomainID,
StartRequest: &types.StartWorkflowExecutionRequest{
WorkflowID: testWorkflowID,
},
}

expectedResponse := &types.StartWorkflowExecutionResponse{
RunID: testWorkflowRunID,
}

// We should _always_ see the startworkflowexecution call no matter if allow external is true or false
// as we are in shadow mode
tests := map[string]struct{ allowExternal bool }{
"allow external": {allowExternal: true},
"disallow external": {allowExternal: false},
}

for name, test := range tests {
s.Run(name, func() {
s.mockWFCache.EXPECT().AllowExternal(gomock.Any(), gomock.Any()).Return(test.allowExternal).Times(1)
s.mockShardController.EXPECT().GetEngine(testWorkflowID).Return(s.mockEngine, nil).AnyTimes()
s.mockEngine.EXPECT().StartWorkflowExecution(gomock.Any(), gomock.Any()).Return(expectedResponse, nil).Times(1)

response, err := s.handler.StartWorkflowExecution(context.Background(), request)
s.Equal(expectedResponse, response)
s.Nil(err)
})
}
}
2 changes: 2 additions & 0 deletions service/history/workflowcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//go:generate mockgen -package=$GOPACKAGE -destination=cache_mock.go github.com/uber/cadence/service/history/workflowcache WFCache

package workflowcache

import (
Expand Down
84 changes: 84 additions & 0 deletions service/history/workflowcache/cache_mock.go

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