go.temporal.io/server@v1.23.0/common/persistence/xdc_cache.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 "time" 29 30 commonpb "go.temporal.io/api/common/v1" 31 32 historyspb "go.temporal.io/server/api/history/v1" 33 persistencepb "go.temporal.io/server/api/persistence/v1" 34 workflowspb "go.temporal.io/server/api/workflow/v1" 35 "go.temporal.io/server/common/cache" 36 "go.temporal.io/server/common/definition" 37 "go.temporal.io/server/common/persistence/versionhistory" 38 ) 39 40 type ( 41 XDCCacheKey struct { 42 WorkflowKey definition.WorkflowKey 43 MinEventID int64 // inclusive 44 MaxEventID int64 // exclusive 45 Version int64 46 } 47 XDCCacheValue struct { 48 BaseWorkflowInfo *workflowspb.BaseExecutionInfo 49 VersionHistoryItems []*historyspb.VersionHistoryItem 50 EventBlob *commonpb.DataBlob 51 } 52 53 XDCCache interface { 54 Put(key XDCCacheKey, value XDCCacheValue) 55 Get(key XDCCacheKey) (XDCCacheValue, bool) 56 } 57 58 XDCCacheImpl struct { 59 cache cache.Cache 60 } 61 ) 62 63 const ( 64 xdcMinCacheSize = 64 * 1024 // 64KB 65 ) 66 67 var _ XDCCache = (*XDCCacheImpl)(nil) 68 var _ cache.SizeGetter = XDCCacheValue{} 69 70 func NewXDCCacheKey( 71 workflowKey definition.WorkflowKey, 72 minEventID int64, 73 maxEventID int64, 74 version int64, 75 ) XDCCacheKey { 76 return XDCCacheKey{ 77 WorkflowKey: workflowKey, 78 MinEventID: minEventID, 79 MaxEventID: maxEventID, 80 Version: version, 81 } 82 } 83 84 func NewXDCCacheValue( 85 baseWorkflowInfo *workflowspb.BaseExecutionInfo, 86 versionHistoryItems []*historyspb.VersionHistoryItem, 87 eventBlob *commonpb.DataBlob, 88 ) XDCCacheValue { 89 return XDCCacheValue{ 90 BaseWorkflowInfo: baseWorkflowInfo, 91 VersionHistoryItems: versionHistoryItems, 92 EventBlob: eventBlob, 93 } 94 } 95 96 func (v XDCCacheValue) CacheSize() int { 97 size := 0 98 for _, item := range v.VersionHistoryItems { 99 size += item.Size() 100 } 101 return v.BaseWorkflowInfo.Size() + size + v.EventBlob.Size() 102 } 103 104 func NewEventsBlobCache( 105 maxBytes int, 106 ttl time.Duration, 107 ) *XDCCacheImpl { 108 return &XDCCacheImpl{ 109 cache: cache.New(max(xdcMinCacheSize, maxBytes), &cache.Options{ 110 TTL: ttl, 111 Pin: false, 112 }), 113 } 114 } 115 116 func (e *XDCCacheImpl) Put( 117 key XDCCacheKey, 118 value XDCCacheValue, 119 ) { 120 e.cache.Put(key, value) 121 } 122 123 func (e *XDCCacheImpl) Get(key XDCCacheKey) (XDCCacheValue, bool) { 124 value := e.cache.Get(key) 125 if value == nil { 126 return XDCCacheValue{}, false 127 } 128 return value.(XDCCacheValue), true 129 } 130 131 func GetXDCCacheValue( 132 executionInfo *persistencepb.WorkflowExecutionInfo, 133 eventID int64, 134 version int64, 135 ) ([]*historyspb.VersionHistoryItem, []byte, *workflowspb.BaseExecutionInfo, error) { 136 baseWorkflowInfo := CopyBaseWorkflowInfo(executionInfo.BaseExecutionInfo) 137 versionHistories := executionInfo.VersionHistories 138 versionHistoryIndex, err := versionhistory.FindFirstVersionHistoryIndexByVersionHistoryItem( 139 versionHistories, 140 versionhistory.NewVersionHistoryItem( 141 eventID, 142 version, 143 ), 144 ) 145 if err != nil { 146 return nil, nil, nil, err 147 } 148 149 versionHistoryBranch, err := versionhistory.GetVersionHistory(versionHistories, versionHistoryIndex) 150 if err != nil { 151 return nil, nil, nil, err 152 } 153 return versionhistory.CopyVersionHistory(versionHistoryBranch).GetItems(), versionHistoryBranch.GetBranchToken(), baseWorkflowInfo, nil 154 } 155 156 func CopyBaseWorkflowInfo( 157 baseWorkflowInfo *workflowspb.BaseExecutionInfo, 158 ) *workflowspb.BaseExecutionInfo { 159 if baseWorkflowInfo == nil { 160 return nil 161 } 162 return &workflowspb.BaseExecutionInfo{ 163 RunId: baseWorkflowInfo.RunId, 164 LowestCommonAncestorEventId: baseWorkflowInfo.LowestCommonAncestorEventId, 165 LowestCommonAncestorEventVersion: baseWorkflowInfo.LowestCommonAncestorEventVersion, 166 } 167 }