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

History engine start/stop unit tests #5985

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions common/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type (
GetClientBean() client.Bean

// persistence clients

GetDomainManager() persistence.DomainManager
GetTaskManager() persistence.TaskManager
GetVisibilityManager() persistence.VisibilityManager
Expand All @@ -103,7 +102,6 @@ type (
GetHostName() string

// loggers

GetLogger() log.Logger
GetThrottledLogger() log.Logger

Expand Down
95 changes: 46 additions & 49 deletions service/history/engine/engineimpl/history_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,48 @@ var (
errDomainDeprecated = &types.BadRequestError{Message: "Domain is deprecated."}
)

type (
historyEngineImpl struct {
currentClusterName string
shard shard.Context
timeSource clock.TimeSource
decisionHandler decision.Handler
clusterMetadata cluster.Metadata
historyV2Mgr persistence.HistoryManager
executionManager persistence.ExecutionManager
visibilityMgr persistence.VisibilityManager
txProcessor queue.Processor
timerProcessor queue.Processor
crossClusterProcessor queue.Processor
nDCReplicator ndc.HistoryReplicator
nDCActivityReplicator ndc.ActivityReplicator
historyEventNotifier events.Notifier
tokenSerializer common.TaskTokenSerializer
executionCache *execution.Cache
metricsClient metrics.Client
logger log.Logger
throttledLogger log.Logger
config *config.Config
archivalClient warchiver.Client
workflowResetter reset.WorkflowResetter
queueTaskProcessor task.Processor
crossClusterTaskProcessors common.Daemon
replicationTaskProcessors []replication.TaskProcessor
replicationAckManager replication.TaskAckManager
replicationTaskStore *replication.TaskStore
replicationHydrator replication.TaskHydrator
replicationMetricsEmitter *replication.MetricsEmitterImpl
publicClient workflowserviceclient.Interface
eventsReapplier ndc.EventsReapplier
matchingClient matching.Client
rawMatchingClient matching.Client
clientChecker client.VersionChecker
replicationDLQHandler replication.DLQHandler
failoverMarkerNotifier failover.MarkerNotifier
wfIDCache workflowcache.WFCache
ratelimitInternalPerWorkflowID dynamicconfig.BoolPropertyFnWithDomainFilter

updateWithActionFn func(context.Context, *execution.Cache, string, types.WorkflowExecution, bool, time.Time, func(wfContext execution.Context, mutableState execution.MutableState) error) error
}
)

var _ engine.Engine = (*historyEngineImpl)(nil)
type historyEngineImpl struct {
currentClusterName string
shard shard.Context
timeSource clock.TimeSource
decisionHandler decision.Handler
clusterMetadata cluster.Metadata
historyV2Mgr persistence.HistoryManager
executionManager persistence.ExecutionManager
visibilityMgr persistence.VisibilityManager
txProcessor queue.Processor
timerProcessor queue.Processor
crossClusterProcessor queue.Processor
nDCReplicator ndc.HistoryReplicator
nDCActivityReplicator ndc.ActivityReplicator
historyEventNotifier events.Notifier
tokenSerializer common.TaskTokenSerializer
executionCache *execution.Cache
metricsClient metrics.Client
logger log.Logger
throttledLogger log.Logger
config *config.Config
archivalClient warchiver.Client
workflowResetter reset.WorkflowResetter
queueTaskProcessor task.Processor
crossClusterTaskProcessors common.Daemon
replicationTaskProcessors []replication.TaskProcessor
replicationAckManager replication.TaskAckManager
replicationTaskStore *replication.TaskStore
replicationHydrator replication.TaskHydrator
replicationMetricsEmitter *replication.MetricsEmitterImpl
publicClient workflowserviceclient.Interface
eventsReapplier ndc.EventsReapplier
matchingClient matching.Client
rawMatchingClient matching.Client
clientChecker client.VersionChecker
replicationDLQHandler replication.DLQHandler
failoverMarkerNotifier failover.MarkerNotifier
wfIDCache workflowcache.WFCache
ratelimitInternalPerWorkflowID dynamicconfig.BoolPropertyFnWithDomainFilter

updateWithActionFn func(context.Context, *execution.Cache, string, types.WorkflowExecution, bool, time.Time, func(wfContext execution.Context, mutableState execution.MutableState) error) error
}

var (
// FailedWorkflowCloseState is a set of failed workflow close states, used for start workflow policy
Expand Down Expand Up @@ -153,6 +149,7 @@ func NewEngineWithShardContext(
failoverCoordinator failover.Coordinator,
wfIDCache workflowcache.WFCache,
ratelimitInternalPerWorkflowID dynamicconfig.BoolPropertyFnWithDomainFilter,
queueProcessorFactory queue.ProcessorFactory,
) engine.Engine {
currentClusterName := shard.GetService().GetClusterMetadata().GetCurrentClusterName()

Expand Down Expand Up @@ -251,7 +248,7 @@ func NewEngineWithShardContext(
)
openExecutionCheck := invariant.NewConcreteExecutionExists(pRetry, shard.GetDomainCache())

historyEngImpl.txProcessor = queue.NewTransferQueueProcessor(
historyEngImpl.txProcessor = queueProcessorFactory.NewTransferQueueProcessor(
shard,
historyEngImpl,
queueTaskProcessor,
Expand All @@ -263,7 +260,7 @@ func NewEngineWithShardContext(
historyEngImpl.ratelimitInternalPerWorkflowID,
)

historyEngImpl.timerProcessor = queue.NewTimerQueueProcessor(
historyEngImpl.timerProcessor = queueProcessorFactory.NewTimerQueueProcessor(
shard,
historyEngImpl,
queueTaskProcessor,
Expand All @@ -272,7 +269,7 @@ func NewEngineWithShardContext(
openExecutionCheck,
)

historyEngImpl.crossClusterProcessor = queue.NewCrossClusterQueueProcessor(
historyEngImpl.crossClusterProcessor = queueProcessorFactory.NewCrossClusterQueueProcessor(
shard,
historyEngImpl,
executionCache,
Expand Down
36 changes: 36 additions & 0 deletions service/history/engine/engineimpl/history_engine_start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2017-2021 Uber Technologies Inc.
// Portions of the Software are attributed to Copyright (c) 2021 Temporal 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 engineimpl

import (
"testing"

"github.com/uber/cadence/service/history/engine/testdata"
)

func TestHistoryEngineStartStop(t *testing.T) {
eft := testdata.NewEngineForTest(t, NewEngineWithShardContext)
defer eft.Cleanup()

eft.Engine.Start()
eft.Engine.Stop()
}
187 changes: 187 additions & 0 deletions service/history/engine/testdata/engine_for_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// 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 testdata

import (
"testing"

"github.com/golang/mock/gomock"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
"go.uber.org/cadence/.gen/go/cadence/workflowservicetest"

"github.com/uber/cadence/client/matching"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/dynamicconfig"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/quotas"
"github.com/uber/cadence/service/history/config"
"github.com/uber/cadence/service/history/constants"
"github.com/uber/cadence/service/history/engine"
"github.com/uber/cadence/service/history/events"
"github.com/uber/cadence/service/history/failover"
"github.com/uber/cadence/service/history/queue"
"github.com/uber/cadence/service/history/replication"
"github.com/uber/cadence/service/history/shard"
"github.com/uber/cadence/service/history/task"
"github.com/uber/cadence/service/history/workflowcache"
)

type EngineForTest struct {
Engine engine.Engine
Cleanup func()
// Add mocks or other fields here
Copy link
Member Author

Choose a reason for hiding this comment

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

Planning to attach mock objects created inline inside NewEngineForTest function here so different unit test scenarios can adjust the behavior/expectations

}

// NewEngineFn is defined as an alias for engineimpl.NewEngineWithShardContext to avoid circular dependency
type NewEngineFn func(
taylanisikdemir marked this conversation as resolved.
Show resolved Hide resolved
shard shard.Context,
visibilityMgr persistence.VisibilityManager,
matching matching.Client,
publicClient workflowserviceclient.Interface,
historyEventNotifier events.Notifier,
config *config.Config,
crossClusterTaskFetchers task.Fetchers,
replicationTaskFetchers replication.TaskFetchers,
rawMatchingClient matching.Client,
queueTaskProcessor task.Processor,
failoverCoordinator failover.Coordinator,
wfIDCache workflowcache.WFCache,
ratelimitInternalPerWorkflowID dynamicconfig.BoolPropertyFnWithDomainFilter,
queueProcessorFactory queue.ProcessorFactory,
) engine.Engine

func NewEngineForTest(t *testing.T, newEngineFn NewEngineFn) *EngineForTest {
t.Helper()
controller := gomock.NewController(t)
historyCfg := config.NewForTest()
shardCtx := shard.NewTestContext(
t,
controller,
&persistence.ShardInfo{
RangeID: 1,
TransferAckLevel: 0,
},
historyCfg,
)

domainCache := shardCtx.Resource.DomainCache
domainCache.EXPECT().GetDomainByID(constants.TestDomainID).Return(constants.TestLocalDomainEntry, nil).AnyTimes()
domainCache.EXPECT().GetDomainName(constants.TestDomainID).Return(constants.TestDomainName, nil).AnyTimes()
domainCache.EXPECT().GetDomain(constants.TestDomainName).Return(constants.TestLocalDomainEntry, nil).AnyTimes()
domainCache.EXPECT().GetDomainID(constants.TestDomainName).Return(constants.TestDomainID, nil).AnyTimes()
domainCache.EXPECT().RegisterDomainChangeCallback(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
domainCache.EXPECT().UnregisterDomainChangeCallback(gomock.Any()).Times(1)

membershipResolver := shardCtx.Resource.MembershipResolver
membershipResolver.EXPECT().MemberCount(gomock.Any()).Return(1, nil).AnyTimes()

eventsCache := events.NewCache(
shardCtx.GetShardID(),
shardCtx.GetHistoryManager(),
historyCfg,
shardCtx.GetLogger(),
shardCtx.GetMetricsClient(),
domainCache,
)
shardCtx.SetEventsCache(eventsCache)

historyEventNotifier := events.NewNotifier(
clock.NewRealTimeSource(),
shardCtx.Resource.MetricsClient,
func(workflowID string) int {
return len(workflowID)
},
)

visibilityMgr := persistence.NewMockVisibilityManager(controller)
mockWFServiceClient := workflowservicetest.NewMockClient(controller)
xClusterTaskFetcher := task.NewMockFetcher(controller)

replicatonTaskFetchers := replication.NewMockTaskFetchers(controller)
replicationTaskFetcher := replication.NewMockTaskFetcher(controller)
// TODO: this should probably return another cluster name, not current
replicationTaskFetcher.EXPECT().GetSourceCluster().Return(constants.TestClusterMetadata.GetCurrentClusterName()).AnyTimes()
replicationTaskFetcher.EXPECT().GetRateLimiter().Return(quotas.NewDynamicRateLimiter(func() float64 { return 100 })).AnyTimes()
replicationTaskFetcher.EXPECT().GetRequestChan().Return(nil).AnyTimes()
replicatonTaskFetchers.EXPECT().GetFetchers().Return([]replication.TaskFetcher{replicationTaskFetcher}).AnyTimes()

queueTaskProcessor := task.NewMockProcessor(controller)
queueTaskProcessor.EXPECT().StopShardProcessor(gomock.Any()).Return().Times(1)

failoverCoordinator := failover.NewMockCoordinator(controller)
wfIDCache := workflowcache.NewMockWFCache(controller)
ratelimitInternalPerWorkflowID := dynamicconfig.GetBoolPropertyFnFilteredByDomain(false)

queueProcessorFactory := queue.NewMockProcessorFactory(controller)
timerQProcessor := queue.NewMockProcessor(controller)
timerQProcessor.EXPECT().Start().Return().Times(1)
timerQProcessor.EXPECT().NotifyNewTask(gomock.Any(), gomock.Any()).Return().AnyTimes()
timerQProcessor.EXPECT().Stop().Return().Times(1)
queueProcessorFactory.EXPECT().
NewTimerQueueProcessor(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(timerQProcessor).
Times(1)

transferQProcessor := queue.NewMockProcessor(controller)
transferQProcessor.EXPECT().Start().Return().Times(1)
transferQProcessor.EXPECT().NotifyNewTask(gomock.Any(), gomock.Any()).Return().AnyTimes()
transferQProcessor.EXPECT().Stop().Return().Times(1)
queueProcessorFactory.EXPECT().
NewTransferQueueProcessor(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(transferQProcessor).
Times(1)

xClusterQProcessor := queue.NewMockProcessor(controller)
xClusterQProcessor.EXPECT().Start().Return().Times(1)
xClusterQProcessor.EXPECT().NotifyNewTask(gomock.Any(), gomock.Any()).Return().AnyTimes()
xClusterQProcessor.EXPECT().Stop().Return().Times(1)
queueProcessorFactory.EXPECT().
NewCrossClusterQueueProcessor(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(xClusterQProcessor).
Times(1)

engine := newEngineFn(
shardCtx,
visibilityMgr,
shardCtx.Resource.MatchingClient,
mockWFServiceClient,
historyEventNotifier,
historyCfg,
task.Fetchers{xClusterTaskFetcher},
replicatonTaskFetchers,
shardCtx.Resource.MatchingClient,
queueTaskProcessor,
failoverCoordinator,
wfIDCache,
ratelimitInternalPerWorkflowID,
queueProcessorFactory,
)

shardCtx.SetEngine(engine)

historyEventNotifier.Start()
return &EngineForTest{
Engine: engine,
Cleanup: func() {
historyEventNotifier.Stop()
},
}
}
3 changes: 3 additions & 0 deletions service/history/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"github.com/uber/cadence/service/history/events"
"github.com/uber/cadence/service/history/failover"
"github.com/uber/cadence/service/history/lookup"
"github.com/uber/cadence/service/history/queue"
"github.com/uber/cadence/service/history/replication"
"github.com/uber/cadence/service/history/resource"
"github.com/uber/cadence/service/history/shard"
Expand Down Expand Up @@ -80,6 +81,7 @@
failoverCoordinator failover.Coordinator
workflowIDCache workflowcache.WFCache
ratelimitInternalPerWorkflowID dynamicconfig.BoolPropertyFnWithDomainFilter
queueProcessorFactory queue.ProcessorFactory
}
)

Expand Down Expand Up @@ -228,6 +230,7 @@
h.failoverCoordinator,
h.workflowIDCache,
h.ratelimitInternalPerWorkflowID,
queue.NewProcessorFactory(),

Check warning on line 233 in service/history/handler/handler.go

View check run for this annotation

Codecov / codecov/patch

service/history/handler/handler.go#L233

Added line #L233 was not covered by tests
)
}

Expand Down
Loading
Loading