-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathIActorStateManager.cs
More file actions
355 lines (336 loc) · 20.5 KB
/
IActorStateManager.cs
File metadata and controls
355 lines (336 loc) · 20.5 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
namespace Dapr.Actors.Runtime;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Represents an interface that exposes methods to manage state of an <see cref="Dapr.Actors.Runtime.Actor" />.
/// This interface is implemented by <see cref="Dapr.Actors.Runtime.Actor.StateManager"/>.
/// </summary>
public interface IActorStateManager
{
/// <summary>
/// Unloads the specified state from the in-memory cache/tracker, but does not remove it from the underlying store.
/// </summary>
/// <param name="stateName">Name of the actor state to unload.</param>
/// <param name="options">Options for unloading state (e.g., allow unloading modified state).</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous unload operation.</returns>
/// <exception cref="InvalidOperationException">Thrown if the state is modified and not yet persisted, unless allowed by options.</exception>
Task UnloadStateAsync(string stateName, UnloadStateOptions options = null, CancellationToken cancellationToken = default);
/// <summary>
/// Adds an actor state with given state name.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add.</param>
/// <param name="value">Value of the actor state to add.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous add operation.
/// </returns>
/// <exception cref="InvalidOperationException">
/// An actor state with given state name already exists.
/// </exception>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task AddStateAsync<T>(string stateName, T value, CancellationToken cancellationToken = default);
/// <summary>
/// Adds an actor state with given state name.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add.</param>
/// <param name="value">Value of the actor state to add.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="ttl">The time to live for the state.</param>
/// <returns>
/// A task that represents the asynchronous add operation.
/// </returns>
/// <exception cref="InvalidOperationException">
/// An actor state with given state name already exists.
/// </exception>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task AddStateAsync<T>(string stateName, T value, TimeSpan ttl, CancellationToken cancellationToken = default);
/// <summary>
/// Gets an actor state with specified state name.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to get.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous get operation. The value of TResult
/// parameter contains value of actor state with given state name.
/// </returns>
/// <exception cref="KeyNotFoundException">
/// An actor state with given state name does not exist.
/// </exception>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<T> GetStateAsync<T>(string stateName, CancellationToken cancellationToken = default);
/// <summary>
/// Sets an actor state with given state name to specified value.
/// If an actor state with specified name does not exist, it is added.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to set.</param>
/// <param name="value">Value of the actor state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous set operation.
/// </returns>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task SetStateAsync<T>(string stateName, T value, CancellationToken cancellationToken = default);
/// <summary>
/// Sets an actor state with given state name to specified value.
/// If an actor state with specified name does not exist, it is added.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to set.</param>
/// <param name="value">Value of the actor state.</param>
/// <param name="ttl">The time to live for the state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous set operation.
/// </returns>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task SetStateAsync<T>(string stateName, T value, TimeSpan ttl, CancellationToken cancellationToken = default);
/// <summary>
/// Removes an actor state with specified state name.
/// </summary>
/// <param name="stateName">Name of the actor state to remove.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous remove operation.</returns>
/// <exception cref="KeyNotFoundException">
/// An actor state with given state name does not exist.
/// </exception>
/// <exception cref="ArgumentNullException"> The specified state name is null. </exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
Task RemoveStateAsync(string stateName, CancellationToken cancellationToken = default);
/// <summary>
/// Attempts to add an actor state with given state name and value. Returns false if an actor state with
/// the same name already exists.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add.</param>
/// <param name="value">Value of the actor state to add.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.
/// This is optional and defaults to <see cref="System.Threading.CancellationToken.None" />.</param>
/// <returns>
/// A boolean task that represents the asynchronous add operation. Returns true if the
/// value was successfully added and false if an actor state with the same name already exists.
/// </returns>
/// <exception cref="ArgumentNullException">The specified state name is null.
/// Provide a valid state name string.</exception>
/// <exception cref="OperationCanceledException">The request was canceled using the specified
/// <paramref name="cancellationToken" />.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<bool> TryAddStateAsync<T>(string stateName, T value, CancellationToken cancellationToken = default);
/// <summary>
/// Attempts to add an actor state with given state name and value. Returns false if an actor state with
/// the same name already exists.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add.</param>
/// <param name="value">Value of the actor state to add.</param>
/// <param name="ttl">The time to live for the state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.
/// This is optional and defaults to <see cref="System.Threading.CancellationToken.None" />.</param>
/// <returns>
/// A boolean task that represents the asynchronous add operation. Returns true if the
/// value was successfully added and false if an actor state with the same name already exists.
/// </returns>
/// <exception cref="ArgumentNullException">The specified state name is null.
/// Provide a valid state name string.</exception>
/// <exception cref="OperationCanceledException">The request was canceled using the specified
/// <paramref name="cancellationToken" />.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<bool> TryAddStateAsync<T>(string stateName, T value, TimeSpan ttl, CancellationToken cancellationToken = default);
/// <summary>
/// Attempts to get an actor state with specified state name.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to get.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous get operation. The value of TResult
/// parameter contains <see cref="ConditionalValue{TValue}"/>
/// indicating whether the actor state is present and the value of actor state if it is present.
/// </returns>
/// <exception cref="ArgumentNullException">The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<ConditionalValue<T>> TryGetStateAsync<T>(string stateName, CancellationToken cancellationToken = default);
/// <summary>
/// Attempts to remove an actor state with specified state name.
/// </summary>
/// <param name="stateName">Name of the actor state to remove.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous remove operation. The value of TResult
/// parameter indicates if the state was successfully removed.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
Task<bool> TryRemoveStateAsync(string stateName, CancellationToken cancellationToken = default);
/// <summary>
/// Checks if an actor state with specified name exists.
/// </summary>
/// <param name="stateName">Name of the actor state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous check operation. The value of TResult
/// parameter is <c>true</c> if state with specified name exists otherwise <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
Task<bool> ContainsStateAsync(string stateName, CancellationToken cancellationToken = default);
/// <summary>
/// Gets an actor state with the given state name if it exists. If it does not
/// exist, creates and new state with the specified name and value.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to get or add.</param>
/// <param name="value">Value of the actor state to add if it does not exist.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous get or add operation. The value of TResult
/// parameter contains value of actor state with given state name.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.
/// Provide a valid state name string.</exception>
/// <exception cref="OperationCanceledException">The request was canceled using the specified
/// <paramref name="cancellationToken" />.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<T> GetOrAddStateAsync<T>(string stateName, T value, CancellationToken cancellationToken = default);
/// <summary>
/// Gets an actor state with the given state name if it exists. If it does not
/// exist, creates and new state with the specified name and value.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to get or add.</param>
/// <param name="value">Value of the actor state to add if it does not exist.</param>
/// <param name="ttl">The time to live for the state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous get or add operation. The value of TResult
/// parameter contains value of actor state with given state name.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.
/// Provide a valid state name string.</exception>
/// <exception cref="OperationCanceledException">The request was canceled using the specified
/// <paramref name="cancellationToken" />.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<T> GetOrAddStateAsync<T>(string stateName, T value, TimeSpan ttl, CancellationToken cancellationToken = default);
/// <summary>
/// Adds an actor state with given state name, if it does not already exist or updates
/// the state with specified state name, if it exists.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add or update.</param>
/// <param name="addValue">Value of the actor state to add if it does not exist.</param>
/// <param name="updateValueFactory">Factory function to generate value of actor state to update if it exists.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous add/update operation. The value of TResult
/// parameter contains value of actor state that was added/updated.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<T> AddOrUpdateStateAsync<T>(string stateName, T addValue, Func<string, T, T> updateValueFactory, CancellationToken cancellationToken = default);
/// <summary>
/// Adds an actor state with given state name, if it does not already exist or updates
/// the state with specified state name, if it exists.
/// </summary>
/// <typeparam name="T">Type of value associated with given state name.</typeparam>
/// <param name="stateName">Name of the actor state to add or update.</param>
/// <param name="addValue">Value of the actor state to add if it does not exist.</param>
/// <param name="updateValueFactory">Factory function to generate value of actor state to update if it exists.</param>
/// <param name="ttl">The time to live for the state.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous add/update operation. The value of TResult
/// parameter contains value of actor state that was added/updated.
/// </returns>
/// <exception cref="ArgumentNullException"> The specified state name is null.</exception>
/// <exception cref="OperationCanceledException">The operation was canceled.</exception>
/// <remarks>
/// The type of state value <typeparamref name="T"/> must be
/// <see href="https://msdn.microsoft.com/library/ms731923.aspx">Data Contract</see> serializable.
/// </remarks>
Task<T> AddOrUpdateStateAsync<T>(string stateName, T addValue, Func<string, T, T> updateValueFactory, TimeSpan ttl, CancellationToken cancellationToken = default);
/// <summary>
/// Clears all the cached actor states and any operation(s) performed on <see cref="IActorStateManager"/>
/// since last state save operation.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous clear cache operation.
/// </returns>
/// <remarks>
/// All the operation(s) performed on <see cref="IActorStateManager"/> since last save operation are cleared on
/// clearing the cache and will not be included in next save operation.
/// </remarks>
Task ClearCacheAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Saves all the cached state changes (add/update/remove) that were made since last call to
/// <see cref="IActorStateManager.SaveStateAsync"/> by actor runtime or by user explicitly.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous save operation.
/// </returns>
Task SaveStateAsync(CancellationToken cancellationToken = default);
}