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

Add sanity check for shard id validation #3714

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions common/persistence/cassandra/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (s *TestCluster) Config() config.Persistence {
"test": {Cassandra: &cfg, FaultInjection: s.faultInjection},
},
TransactionSizeLimit: dynamicconfig.GetIntPropertyFn(common.DefaultTransactionSizeLimit),
NumHistoryShards: 1024,
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/persistence/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (f *factoryImpl) NewShardManager() (p.ShardManager, error) {
return nil, err
}

result := p.NewShardManager(shardStore, f.serializer)
result := p.NewShardManager(shardStore, f.serializer, f.config.NumHistoryShards)
if f.ratelimiter != nil {
result = p.NewShardPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
Expand Down
33 changes: 29 additions & 4 deletions common/persistence/shard_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,29 @@ import (

commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"

persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/common/primitives/timestamp"
)

type shardManagerImpl struct {
shardStore ShardStore
serializer serialization.Serializer
shardStore ShardStore
serializer serialization.Serializer
maxShardCount int32
}

// NewShardManager create a new instance of ShardManager
func NewShardManager(
shardStore ShardStore,
serializer serialization.Serializer,
maxShardCount int32,
) ShardManager {
return &shardManagerImpl{
shardStore: shardStore,
serializer: serializer,
shardStore: shardStore,
serializer: serializer,
maxShardCount: maxShardCount,
}
}

Expand All @@ -63,6 +67,10 @@ func (m *shardManagerImpl) GetOrCreateShard(
ctx context.Context,
request *GetOrCreateShardRequest,
) (*GetOrCreateShardResponse, error) {
if err := m.validateShardId(request.ShardID); err != nil {
return nil, err
}

createShardInfo := func() (int64, *commonpb.DataBlob, error) {
shardInfo := request.InitialShardInfo
if shardInfo == nil {
Expand Down Expand Up @@ -99,6 +107,9 @@ func (m *shardManagerImpl) UpdateShard(
) error {
shardInfo := request.ShardInfo
shardInfo.UpdateTime = timestamp.TimeNowPtrUtc()
if err := m.validateShardId(shardInfo.ShardId); err != nil {
return err
}

shardInfoBlob, err := m.serializer.ShardInfoToBlob(shardInfo, enumspb.ENCODING_TYPE_PROTO3)
if err != nil {
Expand All @@ -117,5 +128,19 @@ func (m *shardManagerImpl) AssertShardOwnership(
ctx context.Context,
request *AssertShardOwnershipRequest,
) error {
if err := m.validateShardId(request.ShardID); err != nil {
return err
}
return m.shardStore.AssertShardOwnership(ctx, request)
}

func (m *shardManagerImpl) validateShardId(shardId int32) error {
if shardId <= 0 {
return serviceerror.NewInvalidArgument("Cannot handle shard Id below the lower bound")
}

if shardId > m.maxShardCount {
return serviceerror.NewInvalidArgument("Cannot handle shard Id exceed the upper bound")
}
return nil
}
1 change: 1 addition & 0 deletions common/persistence/sql/sqlPersistenceTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (s *TestCluster) Config() config.Persistence {
"test": {SQL: &cfg, FaultInjection: s.faultInjection},
},
TransactionSizeLimit: dynamicconfig.GetIntPropertyFn(common.DefaultTransactionSizeLimit),
NumHistoryShards: 1024,
}
}

Expand Down
1 change: 1 addition & 0 deletions common/persistence/tests/execution_mutable_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewExecutionMutableStateSuite(
ShardManager: p.NewShardManager(
shardStore,
serializer,
1024,
),
ExecutionManager: p.NewExecutionManager(
executionStore,
Expand Down
1 change: 1 addition & 0 deletions common/persistence/tests/execution_mutable_state_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func NewExecutionMutableStateTaskSuite(
ShardManager: p.NewShardManager(
shardStore,
serializer,
1024,
),
ExecutionManager: p.NewExecutionManager(
executionStore,
Expand Down
1 change: 1 addition & 0 deletions common/persistence/tests/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewShardSuite(
ShardManager: p.NewShardManager(
shardStore,
serializer,
4,
),
Logger: logger,
}
Expand Down
4 changes: 3 additions & 1 deletion service/history/shard/context_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,9 +1468,11 @@ func (s *ContextImpl) handleWriteErrorAndUpdateMaxReadLevelLocked(err error, new
// No special handling required for these errors.
return err

case *persistence.ShardOwnershipLostError:
case *persistence.ShardOwnershipLostError,
*serviceerror.InvalidArgument:
// Shard is stolen, trigger shutdown of history engine.
// Handling of max read level doesn't matter here.
// Or the shard id is invalid. Stop the shard.
s.transition(contextRequestStop{})
return err

Expand Down