private void DoTableServiceQueryProjection(TablePayloadFormat format) { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); SetPayloadFormatOnDataServiceContext(ctx, format, tableClient); // Retrieve Entities TableServiceQuery <UnionEnitity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name) select new UnionEnitity { A = ent.A, RowKey = ent.RowKey }).AsTableServiceQuery(ctx); List <UnionEnitity> totalResults = query.Execute().ToList(); Assert.AreEqual(totalResults.Count, totalTestEntities); foreach (UnionEnitity ent in totalResults) { Assert.IsNotNull(ent.A); Assert.AreEqual(ent.A, ent.RowKey); Assert.IsNull(ent.B); Assert.IsNull(ent.C); Assert.IsNull(ent.D); Assert.IsNull(ent.E); Assert.IsNull(ent.F); } }
public void TableServiceQueryWithRetrySync() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 1500; m++) { // Insert Entity ComplexEntity insertEntity = new ComplexEntity("insert test", m.ToString()); ctx.AddObject(currentTable.Name, insertEntity); if ((m + 1) % 100 == 0) { ctx.SaveChangesWithRetries(SaveChangesOptions.Batch); } } TableServiceQuery <ComplexEntity> query = (from ent in ctx.CreateQuery <ComplexEntity>(currentTable.Name) select ent).AsTableServiceQuery(ctx); TestHelper.ExecuteMethodWithRetry( 4, // 2 segments, 2 failures new[] { //Insert upstream network delay to prevent upload to server @ 1000ms / kb PerformanceBehaviors.InsertDownstreamNetworkDelay(10000, AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true), new BehaviorOptions(4)), // After 100 ms return throttle message DelayedActionBehaviors.ExecuteAfter(Actions.ThrottleTableRequest, 100, AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true), new BehaviorOptions(4)) }, (options, opContext) => query.Execute((TableRequestOptions)options, opContext).ToList()); }
public void TableServiceQueryExecuteBasic() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); // Retrieve Entities TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name) select ent).AsTableServiceQuery(ctx); List <BaseEntity> totalResults = query.Execute().ToList(); Assert.AreEqual(totalResults.Count, totalTestEntities); }
private void DoTableServiceQueryExecuteBasic(TablePayloadFormat format) { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); SetPayloadFormatOnDataServiceContext(ctx, format, tableClient); // Retrieve Entities TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name) select ent).AsTableServiceQuery(ctx); List <BaseEntity> totalResults = query.Execute().ToList(); Assert.AreEqual(totalResults.Count, totalTestEntities); }
public void TableServiceQueryExecuteSecondary() { CloudTableClient tableClient = GenerateCloudTableClient(); tableClient.LocationMode = RetryPolicies.LocationMode.SecondaryOnly; TableServiceContext ctx = tableClient.GetTableServiceContext(); // Retrieve Entities TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name) select ent).AsTableServiceQuery(ctx); TestHelper.ExpectedException <InvalidOperationException>( () => query.Execute().ToList(), "Even queries cannot be sent to secondary location when using TableServiceContext"); }
private void DoTableServiceQueryProjectionWithUpdate(TablePayloadFormat format) { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); SetPayloadFormatOnDataServiceContext(ctx, format, tableClient); // Retrieve Entities TableServiceQuery <UnionEnitity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name) select new UnionEnitity { A = ent.A, B = ent.B }).Take(20).AsTableServiceQuery(ctx); List <UnionEnitity> totalResults = query.Execute().ToList(); foreach (UnionEnitity ent in totalResults) { Assert.IsNotNull(ent.A); Assert.IsNotNull(ent.B); ent.PartitionKey = string.Empty; ent.RowKey = string.Empty; ent.B += "_updated"; ctx.UpdateObject(ent); } ctx.SaveChangesWithRetries(SaveChangesOptions.Batch); TableServiceContext queryContext = tableClient.GetTableServiceContext(); // Verify update TableServiceQuery <BaseEntity> secondQuery = (from ent in queryContext.CreateQuery <BaseEntity>(currentTable.Name) select ent).Take(20).AsTableServiceQuery(queryContext); foreach (BaseEntity ent in secondQuery.Execute()) { Assert.IsTrue(ent.B.EndsWith("_updated")); } }
public void TableServiceContextConcurrencyAllowsOnlySingleOperationAtOnce() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext tableContext = tableClient.GetTableServiceContext(); // insert entities to query against for (int i = 0; i < 5; i++) { for (int m = 0; m < 100; m++) { BaseEntity ent = new BaseEntity("testpartition" + i, m.ToString()); ent.Randomize(); ent.A = ent.RowKey; tableContext.AddObject(currentTable.Name, ent); } tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch); } List <OperationContext> opContexts = new List <OperationContext>(); object lockerObj = new object(); DateTime start = DateTime.Now; int threadsRunning = 0; Exception lastEx = null; // Start 10 simultaneous threads to query entities associated with same context. for (int j = 0; j < 10; j++) { opContexts.Add(new OperationContext()); Thread newThread = new Thread((arg) => { Interlocked.Increment(ref threadsRunning); try { lock (lockerObj) { Monitor.Wait(lockerObj); } TableServiceQuery <BaseEntity> query = (from ent in tableContext.CreateQuery <BaseEntity>(currentTable.Name) select ent).AsTableServiceQuery(tableContext); Debug.WriteLine(String.Format("Thread {0} start operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds)); try { query.Execute(null, arg as OperationContext).ToList(); } catch (Exception) { // no op, expected to have some exceptions } Debug.WriteLine(String.Format("Thread {0} end operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds)); } catch (Exception ex) { lastEx = ex; } finally { Interlocked.Decrement(ref threadsRunning); } }); newThread.Start(opContexts[j]); } // Wait for all threads to start while (Interlocked.CompareExchange(ref threadsRunning, 10, 10) < 10) { Thread.Sleep(200); } // pulse all threads lock (lockerObj) { Monitor.PulseAll(lockerObj); } // Wait for all threads to complete while (Interlocked.CompareExchange(ref threadsRunning, -1, 0) > -1) { Thread.Sleep(200); } if (lastEx != null) { throw lastEx; } foreach (OperationContext opContext in opContexts) { if (opContext.LastResult == null || opContext.LastResult.StartTime == null || opContext.LastResult.EndTime == null) { continue; } TestHelper.AssertNAttempts(opContext, 1); RequestResult currRes = opContext.LastResult; // Make sure this results start time does not occur in between any other results start & end time var overlappingResults = (from ctx in opContexts where ctx.LastResult != null && ctx.LastResult != currRes && ctx.LastResult.StartTime != null && ctx.LastResult.EndTime != null && ctx.LastResult.StartTime.Ticks <currRes.StartTime.Ticks && ctx.LastResult.EndTime.Ticks> currRes.StartTime.Ticks select ctx.LastResult); Assert.AreEqual(overlappingResults.Count(), 0, "Detected overlapping query"); } }