private void DeleteOldEntities() { var storageAccount = this.storageAccountFactory.GetStorageAccount(); Debug.Assert(null == storageAccount); // Create a table client CloudTableClient tableClient = AzureTableCommon.CreateNewTableClient(storageAccount); // Create a new table service context CloudTableClient tableServiceContext = tableClient; // Figure out the timestamp before which all entities will be deleted DateTime cutoffTime = DateTime.UtcNow.Add(-this.entityDeletionAge); // Delete the entities in batches. Do this in a loop until our query // for "delete-eligible" entities yields no results. TableContinuationToken continuationToken = null; do { QueryAndDeleteEntityBatch(storageAccount, tableServiceContext, cutoffTime, ref continuationToken); if (this.stopping) { this.traceSource.WriteInfo( this.logSourceId, "The consumer is being stopped. Therefore, no more old entities will be deleted in this pass."); break; } } while (null != continuationToken); }
public void OnEtwEventDeliveryStart() { this.perfHelper.TableUploadPeriodBegin(); var storageAccount = this.tableUploadSettings.StorageAccountFactory.GetStorageAccount(); Debug.Assert(null == storageAccount); // Create a table client CloudTableClient tableClient = AzureTableCommon.CreateNewTableClient(storageAccount); // Create new table service contexts using the new credentials for (int i = 0; i < this.batchConcurrencyCount; i++) { Debug.Assert(null == this.tableClients[i]); this.tableClients[i] = tableClient; } }
private void CreateTableWorker(CloudStorageAccount storageAccount) { // Create the table client object CloudTableClient cloudTableClient = AzureTableCommon.CreateNewTableClient(storageAccount); // Create the table #if DotNetCoreClrLinux cloudTableClient.GetTableReference(this.tableUploadSettings.TableName).CreateIfNotExistsAsync().Wait(); #else cloudTableClient.GetTableReference(this.tableUploadSettings.TableName).CreateIfNotExists(); #endif // Create an array of table service context objects this.availableIndexes = new Stack <int>(); this.tableClients = new CloudTableClient[this.batchConcurrencyCount]; for (int i = 0; i < this.batchConcurrencyCount; i++) { this.availableIndexes.Push(i); } }
private void QueryAndDeleteEntityBatch(CloudStorageAccount storageAccount, CloudTableClient tableServiceContext, DateTime cutoffTime, ref TableContinuationToken continuationToken) { TableContinuationToken initialContinuationToken = continuationToken; continuationToken = null; this.traceSource.WriteInfo(this.logSourceId, "Deleting table entries older than {0}.", cutoffTime); // Create a query object TableQuery <TableEntity> query = this.queryCreationMethod(cutoffTime); TableQuerySegment <TableEntity> resultSegment; try { this.perfHelper.ExternalOperationBegin( ExternalOperationTime.ExternalOperationType.TableQuery, 0); // Execute the query #if DotNetCoreClr resultSegment = tableServiceContext.GetTableReference(this.tableName).ExecuteQuerySegmentedAsync(query, initialContinuationToken).Result; #else resultSegment = tableServiceContext.GetTableReference(this.tableName).ExecuteQuerySegmented(query, initialContinuationToken); #endif this.perfHelper.ExternalOperationEnd( ExternalOperationTime.ExternalOperationType.TableQuery, 0); } catch (Exception e) { AzureTableCommon.TableServiceExceptionAction action = AzureTableCommon.ProcessTableServiceQueryException( this.traceSource, this.logSourceId, e, AzureTableCommon.TableServiceAction.QueryEntitiesForDeletion); // Give up on this batch Debug.Assert(AzureTableCommon.TableServiceExceptionAction.Abort == action); return; } continuationToken = resultSegment.ContinuationToken; if (!resultSegment.Results.Any()) { // Query did not give us any entities to delete. Nothing more to // be done here. return; } this.perfHelper.BatchQueriedFromAzureTable((ulong)resultSegment.Results.Count()); // Create a table client CloudTableClient tableClient = AzureTableCommon.CreateNewTableClient(storageAccount); // Walk through the query results var entitiesToDelete = new List <TableEntity>(); string partitionKey = String.Empty; foreach (var entity in resultSegment.Results) { // Azure table service requires all entities in a batched transaction to // have the same parition key. Hence if the partition key for this entity // is not the same as that of the entities we already added to the batch, // then delete those entities first and add this one to a new batch. if ((false == String.IsNullOrEmpty(partitionKey)) && (false == entity.PartitionKey.Equals(partitionKey))) { DeleteEntityBatch(tableClient, entitiesToDelete); // Clear for the next batch entitiesToDelete.Clear(); } // Record the partition key for the current batch partitionKey = entity.PartitionKey; entitiesToDelete.Add(entity); // If we have reached that maximum entity count for a batch, then // delete those entities. if (entitiesToDelete.Count == AzureTableCommon.MaxEntitiesInBatch) { DeleteEntityBatch(tableClient, entitiesToDelete); // Clear for the next batch entitiesToDelete.Clear(); } } if (entitiesToDelete.Any()) { DeleteEntityBatch(tableClient, entitiesToDelete); } }