go.temporal.io/server@v1.23.0/common/persistence/shard_manager.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package persistence
    26  
    27  import (
    28  	"context"
    29  
    30  	commonpb "go.temporal.io/api/common/v1"
    31  	enumspb "go.temporal.io/api/enums/v1"
    32  
    33  	persistencespb "go.temporal.io/server/api/persistence/v1"
    34  	"go.temporal.io/server/common/persistence/serialization"
    35  	"go.temporal.io/server/common/primitives/timestamp"
    36  )
    37  
    38  type shardManagerImpl struct {
    39  	shardStore ShardStore
    40  	serializer serialization.Serializer
    41  }
    42  
    43  // NewShardManager create a new instance of ShardManager
    44  func NewShardManager(
    45  	shardStore ShardStore,
    46  	serializer serialization.Serializer,
    47  ) ShardManager {
    48  	return &shardManagerImpl{
    49  		shardStore: shardStore,
    50  		serializer: serializer,
    51  	}
    52  }
    53  
    54  func (m *shardManagerImpl) Close() {
    55  	m.shardStore.Close()
    56  }
    57  
    58  func (m *shardManagerImpl) GetName() string {
    59  	return m.shardStore.GetName()
    60  }
    61  
    62  func (m *shardManagerImpl) GetOrCreateShard(
    63  	ctx context.Context,
    64  	request *GetOrCreateShardRequest,
    65  ) (*GetOrCreateShardResponse, error) {
    66  	createShardInfo := func() (int64, *commonpb.DataBlob, error) {
    67  		shardInfo := request.InitialShardInfo
    68  		if shardInfo == nil {
    69  			shardInfo = &persistencespb.ShardInfo{}
    70  		}
    71  		shardInfo.ShardId = request.ShardID
    72  		shardInfo.UpdateTime = timestamp.TimeNowPtrUtc()
    73  		data, err := m.serializer.ShardInfoToBlob(shardInfo, enumspb.ENCODING_TYPE_PROTO3)
    74  		if err != nil {
    75  			return 0, nil, err
    76  		}
    77  		return shardInfo.GetRangeId(), data, nil
    78  	}
    79  	internalResp, err := m.shardStore.GetOrCreateShard(ctx, &InternalGetOrCreateShardRequest{
    80  		ShardID:          request.ShardID,
    81  		CreateShardInfo:  createShardInfo,
    82  		LifecycleContext: request.LifecycleContext,
    83  	})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	shardInfo, err := m.serializer.ShardInfoFromBlob(internalResp.ShardInfo)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return &GetOrCreateShardResponse{
    92  		ShardInfo: shardInfo,
    93  	}, nil
    94  }
    95  
    96  func (m *shardManagerImpl) UpdateShard(
    97  	ctx context.Context,
    98  	request *UpdateShardRequest,
    99  ) error {
   100  	shardInfo := request.ShardInfo
   101  	shardInfo.UpdateTime = timestamp.TimeNowPtrUtc()
   102  
   103  	shardInfoBlob, err := m.serializer.ShardInfoToBlob(shardInfo, enumspb.ENCODING_TYPE_PROTO3)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	internalRequest := &InternalUpdateShardRequest{
   108  		ShardID:         request.ShardInfo.GetShardId(),
   109  		RangeID:         request.ShardInfo.GetRangeId(),
   110  		Owner:           request.ShardInfo.GetOwner(),
   111  		ShardInfo:       shardInfoBlob,
   112  		PreviousRangeID: request.PreviousRangeID,
   113  	}
   114  	return m.shardStore.UpdateShard(ctx, internalRequest)
   115  }
   116  
   117  func (m *shardManagerImpl) AssertShardOwnership(
   118  	ctx context.Context,
   119  	request *AssertShardOwnershipRequest,
   120  ) error {
   121  	return m.shardStore.AssertShardOwnership(ctx, request)
   122  }