Exemplo n.º 1
0
        public static async Task EnqueueBlobReplicationAsync(NamespaceBlob namespaceBlob, bool deleteReplica, bool saveNamespaceEntry = true)
        {
            if (!await namespaceBlob.ExistsAsync())
            {
                return;
            }
            // Trim down the namespace replication list to the first 'master' item. This is sufficient to ensure that the
            // orphaned blobs are not effectively in the account. The master blob will be replicated over the top of the
            // orphaned blobs.
            string primaryAccount = namespaceBlob.PrimaryAccountName;

            if (namespaceBlob.IsReplicated)
            {
                namespaceBlob.PrimaryAccountName = primaryAccount;
                if (saveNamespaceEntry)
                {
                    await namespaceBlob.SaveAsync();
                }
            }
            // This rest of this method does not block. Enqueueing the replication is a completely async process
            var task = Task.Factory.StartNew(() =>
            {
                var queue = new AzureMessageQueue();
                var tasks = DashConfiguration.DataAccounts
                            .Where(dataAccount => !dataAccount.Credentials.AccountName.Equals(primaryAccount, StringComparison.OrdinalIgnoreCase))
                            .Select(async dataAccount => await queue.EnqueueAsync(ConstructReplicationMessage(deleteReplica,
                                                                                                              primaryAccount,
                                                                                                              dataAccount.Credentials.AccountName,
                                                                                                              namespaceBlob.Container,
                                                                                                              namespaceBlob.BlobName,
                                                                                                              deleteReplica ? await GetBlobETagAsync(dataAccount, namespaceBlob.Container, namespaceBlob.BlobName) : null)));
                Task.WhenAll(tasks)
                .ContinueWith(antecedent =>
                {
                    if (antecedent.Exception != null)
                    {
                        DashTrace.TraceWarning("Error queueing replication message for blob: {0}. Details: {1}",
                                               PathUtils.CombineContainerAndBlob(namespaceBlob.Container, namespaceBlob.BlobName),
                                               antecedent.Exception.Flatten());
                    }
                    else
                    {
                        DashTrace.TraceInformation("Blob: {0} has been enqueued for replication.",
                                                   PathUtils.CombineContainerAndBlob(namespaceBlob.Container, namespaceBlob.BlobName));
                    }
                });
            });
        }