public async Task <ITask> GetTask(CancellationToken token) { await EnsureInit(); ITransaction tx = null; ITask ret = null; try { tx = ServiceFabricUtils.CreateTransaction(); await ServiceFabricUtils.DoWithTimeoutRetry(async() => { var result = await m_queue.TryDequeueAsync(tx, TimeSpan.FromSeconds(10), m_cancel); if (result.HasValue) { ret = Utils.Deserialize <ITask>(result.Value); } }); if (ret == null) { tx.Dispose(); return(null); } m_tx[ret] = tx; return(ret); } catch { ReleaseTx(ret, tx); throw; } }
private async Task <IEnumerable <Chunk> > GetChunks_impl(Guid?replicaId) { using (var tx = ServiceFabricUtils.CreateTransaction()) { retry: try { if (replicaId.HasValue) { var res = await m_chunktable.TryGetValueAsync(tx, replicaId.Value); if (res.HasValue) { return(Utils.Deserialize <Chunk[]>(res.Value)); } } else { var res = await m_chunktable.CreateEnumerableAsync(tx, EnumerationMode.Unordered); var enumerator = res.GetAsyncEnumerator(); List <Chunk> chunks = new List <Chunk>(); while (await enumerator.MoveNextAsync(m_cancel)) { chunks.AddRange(Utils.Deserialize <Chunk[]>(enumerator.Current.Value)); } return(chunks); } } catch (TimeoutException) { await Task.Delay(1000); goto retry; } catch (Exception ex) { Log.WriteLine(LogLevel.Error, "{0}", $"ServiceFabricChunkTable: {ex.ToString()}"); } finally { await tx.CommitAsync(); } } return(null); }
private async Task InitAsync() { m_queue = await ServiceFabricUtils.CreateReliableStateAsync <IReliableQueue <byte[]> > ("Trinity.ServiceFabric.GarphEngine.Infrastructure.TaskQueue"); m_tagCounter = await ServiceFabricUtils.CreateReliableStateAsync <IReliableDictionary <Guid, int> > ("Trinity.ServiceFabric.GarphEngine.Infrastructure.TaskTagCounter"); }
/// <inheritdocs/> public async Task DeleteEntry(Guid replicaId) { await EnsureChunkTables(); using (var tx = ServiceFabricUtils.CreateTransaction()) { await ServiceFabricUtils.DoWithTimeoutRetry( async() => await m_chunktable.TryRemoveAsync(tx, replicaId)); } }
public async Task PostTask(ITask task) { await EnsureInit(); using (var tx = ServiceFabricUtils.CreateTransaction()) { await ServiceFabricUtils.DoWithTimeoutRetry( async() => await m_queue.EnqueueAsync(tx, Utils.Serialize(task), TimeSpan.FromSeconds(10), m_cancel), async() => await m_tagCounter.AddOrUpdateAsync(tx, task.Tag, 1, (k, v) => v + 1), async() => await tx.CommitAsync()); } }
/// <inheritdocs/> public async Task SetChunks(Guid replicaId, IEnumerable <Chunk> chunks) { var payload = Utils.Serialize(chunks.ToArray()); await EnsureChunkTables(); using (var tx = ServiceFabricUtils.CreateTransaction()) { await ServiceFabricUtils.DoWithTimeoutRetry( async() => await m_chunktable.SetAsync(tx, replicaId, payload), async() => await tx.CommitAsync()); } }
public async Task UpdateTask(ITask task) { if (!m_tx.TryGetValue(task, out var tx)) { return; } try { await ServiceFabricUtils.DoWithTimeoutRetry( async() => await m_queue.EnqueueAsync(tx, Utils.Serialize(task), TimeSpan.FromSeconds(10), m_cancel), async() => await tx.CommitAsync()); } finally { ReleaseTx(task, tx); } }
public async Task RemoveTask(ITask task) { if (!m_tx.TryGetValue(task, out var tx)) { return; } try { await ServiceFabricUtils.DoWithTimeoutRetry( async() => await m_tagCounter.AddOrUpdateAsync(tx, task.Tag, _ => 0, (k, v) => v - 1, TimeSpan.FromSeconds(10), m_cancel), async() => await tx.CommitAsync()); } finally { ReleaseTx(task, tx); } }
public async Task Wait(Guid tag) { await EnsureInit(); await ServiceFabricUtils.DoWithTimeoutRetry( async() => { using (var tx = ServiceFabricUtils.CreateTransaction()) { int cnt = 0; var result = await m_tagCounter.TryGetValueAsync(tx, tag, TimeSpan.FromSeconds(10), m_cancel); if (result.HasValue) { cnt = result.Value; } if (cnt != 0) { throw new TimeoutException(); } } }); }
private async Task InitChunkTablesAsync() { m_chunktable = await ServiceFabricUtils.CreateReliableStateAsync <IReliableDictionary <Guid, byte[]> >("Trinity.ServiceFabric.GarphEngine.Infrastructure.ChunkTable"); }