1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System.Collections.Concurrent;
using Aging.Infrastructure.Contracts;
using log4net;
 
namespace Aging.Infrastructure.Core
{
    /// <summary>
    /// 资源管理实现。
    /// 提供线程安全的资源预约、租约获取和释放功能。
    /// 使用 ConcurrentDictionary 存储活跃预约,使用 SemaphoreSlim 实现 per-resource 锁。
    /// </summary>
    public sealed class ResourceManager : IResourceManager
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ResourceManager));
 
        /// <summary>
        /// 活跃预约字典(key = ResourceKey.ToString())
        /// </summary>
        private readonly ConcurrentDictionary<string, ResourceReservation> _reservations = new();
 
        /// <summary>
        /// 每资源信号量字典,用于实现细粒度锁
        /// </summary>
        private readonly ConcurrentDictionary<string, SemaphoreSlim> _resourceLocks = new();
 
        /// <summary>
        /// 全局世代号,每次新预约递增
        /// </summary>
        private long _generation;
 
        /// <summary>
        /// 默认租约有效期(秒)
        /// </summary>
        private const int DefaultLeaseDurationSeconds = 30;
 
        /// <inheritdoc/>
        public async Task<ResourceReservation> ReserveAsync(
            ResourceOwnerToken owner, ResourceKey resource, CancellationToken ct)
        {
            if (owner == null) throw new ArgumentNullException(nameof(owner));
            if (resource == null) throw new ArgumentNullException(nameof(resource));
 
            var resourceKey = resource.ToString();
            var semaphore = _resourceLocks.GetOrAdd(resourceKey, _ => new SemaphoreSlim(1, 1));
 
            try
            {
                await semaphore.WaitAsync(ct).ConfigureAwait(false);
 
                try
                {
                    // 检查是否已有 Active 预约
                    if (_reservations.TryGetValue(resourceKey, out var existingReservation))
                    {
                        if (existingReservation.State == ReservationState.Active)
                        {
                            // 检查是否为同一 Owner(续期场景)
                            if (existingReservation.Owner.TokenId == owner.TokenId)
                            {
                                // 同一所有者续期:更新世代号
                                var renewedGeneration = Interlocked.Increment(ref _generation);
                                owner.Generation = renewedGeneration;
                                existingReservation.Owner.Generation = renewedGeneration;
 
                                Log.Info($"[ResourceManager] 资源 {resourceKey} 续期成功,新世代号: {renewedGeneration}");
                                return existingReservation;
                            }
 
                            // 不同所有者冲突
                            var conflictMsg = $"资源 {resourceKey} 已被所有者 {existingReservation.Owner.OwnerId} 预约," +
                                             $"当前请求者: {owner.OwnerId}";
                            Log.Warn($"[ResourceManager] 资源冲突: {conflictMsg}");
                            throw new ResourceConflictException(conflictMsg);
                        }
                    }
 
                    // 创建新预约
                    var newGeneration = Interlocked.Increment(ref _generation);
                    owner.Generation = newGeneration;
 
                    var reservation = new ResourceReservation(resource, owner)
                    {
                        State = ReservationState.Active
                    };
 
                    _reservations[resourceKey] = reservation;
 
                    Log.Info($"[ResourceManager] 资源 {resourceKey} 预约成功,所有者: {owner.OwnerId},世代号: {newGeneration}");
                    return reservation;
                }
                finally
                {
                    semaphore.Release();
                }
            }
            catch (OperationCanceledException)
            {
                Log.Warn($"[ResourceManager] 资源 {resourceKey} 预约被取消");
                throw;
            }
            catch (ResourceConflictException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Log.Error($"[ResourceManager] 资源 {resourceKey} 预约失败: {ex.Message}", ex);
                throw;
            }
        }
 
        /// <inheritdoc/>
        public async Task<IOperationLease> AcquireOperationAsync(
            ResourceOwnerToken owner, ResourceKey resource, CancellationToken ct)
        {
            if (owner == null) throw new ArgumentNullException(nameof(owner));
            if (resource == null) throw new ArgumentNullException(nameof(resource));
 
            var resourceKey = resource.ToString();
 
            try
            {
                // 检查资源是否有有效预约
                if (!_reservations.TryGetValue(resourceKey, out var reservation))
                {
                    throw new InvalidOperationException($"资源 {resourceKey} 未被预约,无法获取操作租约");
                }
 
                if (reservation.State != ReservationState.Active)
                {
                    throw new InvalidOperationException($"资源 {resourceKey} 预约状态为 {reservation.State},无法获取操作租约");
                }
 
                // 验证 Owner 匹配(TokenId + Generation)
                if (reservation.Owner.TokenId != owner.TokenId || reservation.Owner.Generation != owner.Generation)
                {
                    throw new InvalidOperationException(
                        $"资源 {resourceKey} 所有者不匹配,期望 TokenId: {reservation.Owner.TokenId},Generation: {reservation.Owner.Generation}");
                }
 
                // 创建 OperationLease(短期,默认30秒过期)
                var lease = new OperationLease(
                    resource,
                    owner,
                    onRelease: () => Log.Debug($"[ResourceManager] 租约释放: {resourceKey}"),
                    leaseDurationSeconds: DefaultLeaseDurationSeconds);
 
                Log.Info($"[ResourceManager] 获取操作租约: {resourceKey},所有者: {owner.OwnerId},有效期: {DefaultLeaseDurationSeconds}秒");
 
                await Task.CompletedTask; // 保持异步签名
                return lease;
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Log.Error($"[ResourceManager] 获取操作租约失败: {ex.Message}", ex);
                throw;
            }
        }
 
        /// <inheritdoc/>
        public async Task ReleaseReservationAsync(
            ResourceOwnerToken owner, ResourceKey resource, CancellationToken ct)
        {
            if (owner == null) throw new ArgumentNullException(nameof(owner));
            if (resource == null) throw new ArgumentNullException(nameof(resource));
 
            var resourceKey = resource.ToString();
            var semaphore = _resourceLocks.GetOrAdd(resourceKey, _ => new SemaphoreSlim(1, 1));
 
            try
            {
                await semaphore.WaitAsync(ct).ConfigureAwait(false);
 
                try
                {
                    if (!_reservations.TryGetValue(resourceKey, out var reservation))
                    {
                        Log.Warn($"[ResourceManager] 尝试释放不存在的资源预约: {resourceKey}");
                        return;
                    }
 
                    // 验证 Owner 匹配
                    if (reservation.Owner.TokenId != owner.TokenId)
                    {
                        throw new InvalidOperationException(
                            $"资源 {resourceKey} 释放失败:所有者不匹配,期望 TokenId: {reservation.Owner.TokenId}");
                    }
 
                    // 标记预约为 Released
                    reservation.State = ReservationState.Released;
                    _reservations.TryRemove(resourceKey, out _);
 
                    Log.Info($"[ResourceManager] 资源 {resourceKey} 预约已释放,所有者: {owner.OwnerId}");
                }
                finally
                {
                    semaphore.Release();
                }
 
                await Task.CompletedTask; // 保持异步签名
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            catch (OperationCanceledException)
            {
                Log.Warn($"[ResourceManager] 资源 {resourceKey} 释放操作被取消");
                throw;
            }
            catch (Exception ex)
            {
                Log.Error($"[ResourceManager] 释放资源 {resourceKey} 失败: {ex.Message}", ex);
                throw;
            }
        }
 
        /// <inheritdoc/>
        public bool IsReserved(ResourceKey resource, out ResourceReservation? reservation)
        {
            if (resource == null) throw new ArgumentNullException(nameof(resource));
 
            var resourceKey = resource.ToString();
            var result = _reservations.TryGetValue(resourceKey, out var res);
 
            if (result && res != null && res.State == ReservationState.Active)
            {
                reservation = res;
                return true;
            }
 
            reservation = null;
            return false;
        }
 
        /// <inheritdoc/>
        public bool ValidateOwner(ResourceOwnerToken owner, ResourceKey resource)
        {
            if (owner == null) throw new ArgumentNullException(nameof(owner));
            if (resource == null) throw new ArgumentNullException(nameof(resource));
 
            var resourceKey = resource.ToString();
 
            if (!_reservations.TryGetValue(resourceKey, out var reservation))
                return false;
 
            if (reservation.State != ReservationState.Active)
                return false;
 
            return reservation.Owner.TokenId == owner.TokenId &&
                   reservation.Owner.Generation == owner.Generation;
        }
    }
}