Files
tasks/service/history/shard/context_testutil.go
Roey BermanandGitHub c76f4a2caa Hierarchical state machines (#5494)
## Notes for reviewers

I don't consider this a final approach but I do think it's a step in
the right direction, we need to model more state machines on top of this
to form a more solid API.

Note that mutable state itself is registered as a state machine in the HSM framework and used as the root node in the HSM tree.

It's a bit annoying that all of the unit tests need to register the workflow state machine but I think this is ended up being the cleanest approach from the HSM framework perspective.

There's still some follow-up work, like collapsing timer tasks and support for interacting with history event, that I'll take on later.

Replication support for the framework will also come at a later time.


- See discussions in related PRs (#5446, #5495)

## What changed?

- Modified the statemachines abstraction to be a bit more generic
- Rewrote callbacks as a plugin using this framework

## Why?

This centralizes most the callback code in the plugin directory instead
of having it spread out the entire project moving common concerns such
as staleness checks, task generation, and (in the future) replication
into a framework and should generally help speed up feature delivery and
maintainability.

I plan to leverage this framework when implementing Nexus operations.

## How did you test it?

Existing tests from the feature branch and added unit tests.
2024-03-11 12:59:47 -07:00

225 lines
7.1 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 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 shard
import (
"context"
"fmt"
"sync"
"github.com/golang/mock/gomock"
"golang.org/x/sync/semaphore"
"go.temporal.io/server/api/historyservice/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/future"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/resourcetest"
"go.temporal.io/server/service/history/configs"
"go.temporal.io/server/service/history/events"
"go.temporal.io/server/service/history/hsm"
"go.temporal.io/server/service/history/tasks"
)
type ContextTest struct {
*ContextImpl
Resource *resourcetest.Test
MockEventsCache *events.MockCache
}
var _ Context = (*ContextTest)(nil)
func NewTestContextWithTimeSource(
ctrl *gomock.Controller,
shardInfo *persistencespb.ShardInfo,
config *configs.Config,
timeSource clock.TimeSource,
) *ContextTest {
result := NewTestContext(ctrl, shardInfo, config)
result.timeSource = timeSource
result.taskKeyManager.generator.timeSource = timeSource
result.Resource.TimeSource = timeSource
return result
}
func NewTestContext(
ctrl *gomock.Controller,
shardInfo *persistencespb.ShardInfo,
config *configs.Config,
) *ContextTest {
resourceTest := resourcetest.NewTest(ctrl, primitives.HistoryService)
eventsCache := events.NewMockCache(ctrl)
shard := newTestContext(
resourceTest,
eventsCache,
ContextConfigOverrides{
ShardInfo: shardInfo,
Config: config,
},
)
return &ContextTest{
Resource: resourceTest,
ContextImpl: shard,
MockEventsCache: eventsCache,
}
}
type ContextConfigOverrides struct {
ShardInfo *persistencespb.ShardInfo
Config *configs.Config
Registry namespace.Registry
ClusterMetadata cluster.Metadata
ExecutionManager persistence.ExecutionManager
}
type StubContext struct {
ContextTest
engine Engine
}
func NewStubContext(
ctrl *gomock.Controller,
overrides ContextConfigOverrides,
engine Engine,
) *StubContext {
resourceTest := resourcetest.NewTest(ctrl, primitives.HistoryService)
eventsCache := events.NewMockCache(ctrl)
shard := newTestContext(resourceTest, eventsCache, overrides)
result := &StubContext{
ContextTest: ContextTest{
Resource: resourceTest,
ContextImpl: shard,
MockEventsCache: eventsCache,
},
engine: engine,
}
return result
}
func newTestContext(t *resourcetest.Test, eventsCache events.Cache, config ContextConfigOverrides) *ContextImpl {
hostInfoProvider := t.GetHostInfoProvider()
lifecycleCtx, lifecycleCancel := context.WithCancel(context.Background())
if config.ShardInfo.QueueStates == nil {
config.ShardInfo.QueueStates = make(map[int32]*persistencespb.QueueState)
}
registry := config.Registry
if registry == nil {
registry = t.GetNamespaceRegistry()
}
clusterMetadata := config.ClusterMetadata
if clusterMetadata == nil {
clusterMetadata = t.GetClusterMetadata()
}
executionManager := config.ExecutionManager
if executionManager == nil {
executionManager = t.ExecutionMgr
}
ctx := &ContextImpl{
shardID: config.ShardInfo.GetShardId(),
owner: config.ShardInfo.GetOwner(),
stringRepr: fmt.Sprintf("Shard(%d)", config.ShardInfo.GetShardId()),
executionManager: executionManager,
metricsHandler: t.MetricsHandler,
eventsCache: eventsCache,
config: config.Config,
contextTaggedLogger: t.GetLogger(),
throttledLogger: t.GetThrottledLogger(),
lifecycleCtx: lifecycleCtx,
lifecycleCancel: lifecycleCancel,
queueMetricEmitter: sync.Once{},
state: contextStateAcquired,
engineFuture: future.NewFuture[Engine](),
shardInfo: config.ShardInfo,
remoteClusterInfos: make(map[string]*remoteClusterInfo),
handoverNamespaces: make(map[namespace.Name]*namespaceHandOverInfo),
clusterMetadata: clusterMetadata,
timeSource: t.TimeSource,
namespaceRegistry: registry,
persistenceShardManager: t.GetShardManager(),
clientBean: t.GetClientBean(),
saProvider: t.GetSearchAttributesProvider(),
saMapperProvider: t.GetSearchAttributesMapperProvider(),
historyClient: t.GetHistoryClient(),
payloadSerializer: t.GetPayloadSerializer(),
archivalMetadata: t.GetArchivalMetadata(),
hostInfoProvider: hostInfoProvider,
taskCategoryRegistry: tasks.NewDefaultTaskCategoryRegistry(),
ioSemaphore: semaphore.NewWeighted(1),
}
ctx.taskKeyManager = newTaskKeyManager(
ctx.taskCategoryRegistry,
ctx.timeSource,
config.Config,
ctx.GetLogger(),
func() error {
return ctx.renewRangeLocked(false)
},
)
ctx.taskKeyManager.setRangeID(config.ShardInfo.RangeId)
return ctx
}
// SetEngineForTest sets s.engine. Only used by tests.
func (s *ContextTest) SetEngineForTesting(engine Engine) {
s.engineFuture.Set(engine, nil)
}
// SetEventsCacheForTesting sets s.eventsCache. Only used by tests.
func (s *ContextTest) SetEventsCacheForTesting(c events.Cache) {
// for testing only, will only be called immediately after initialization
s.eventsCache = c
}
// SetHistoryClientForTesting sets history client. Only used by tests.
func (s *ContextTest) SetHistoryClientForTesting(client historyservice.HistoryServiceClient) {
s.historyClient = client
}
// SetStateMachineRegistry sets the state machine registry on this shard.
func (s *ContextTest) SetStateMachineRegistry(reg *hsm.Registry) {
s.stateMachineRegistry = reg
}
// StopForTest calls FinishStop(). In general only the controller
// should call that, but integration tests need to do it also to clean up any
// background acquireShard goroutines that may exist.
func (s *ContextTest) StopForTest() {
s.FinishStop()
}
func (s *StubContext) GetEngine(_ context.Context) (Engine, error) {
return s.engine, nil
}