为什么我得到"指定的附加位置条件未满足"来自CloudAppendBlob.AppendTextAsync?

时间:2018-04-26 14:17:43

标签: azure-storage-blobs

我得到"未达到指定的附加位置条件"在我使用CloudAppendBlob.AppendTextAsync实现的Logger组件中。我已经搜索并看到如果你从多个任务使用AppendTextAsync可能会出现问题,但我不会在我的代码中这样做。我使用ConcurrentQueue来记录消息,它是从队列中读取的单个任务,并调用AppendTextAsync。

这是我的代码:

private async Task MessageLogger()
{
    string messages = "";
    while (true)
    {
        // Check if a new log file should be created
        if (DateTime.UtcNow.Day != _logCreatedUtc.Day)
        {
            // Create a new log file
            await CreateNewLogAsync();
        }

        if (_messageQueue.Count == 0 && messages.Length == 0)
        {
            // No messages to write, wait
            await Task.Delay(50);
            continue;
        }

        int n = 0;
        while ((this._messageQueue.Count > 0) && (n < 50))
        {
            string message;
            if (this._messageQueue.TryDequeue(out message))
            {
                messages += message;
                n++;
            }
        }

        try
        {
            // Append messages to Azure Blob
            await _appendBlob.AppendTextAsync(messages);
            messages = "";
        }
        catch (Microsoft.WindowsAzure.Storage.StorageException se)
            when(se.RequestInformation.HttpStatusCode == 404)
        {
            LogException("StorageException from CloudAppendBlob.AppendTextAsync", se);
            // Log file was deleted. Create a new log file
            await CreateNewLogAsync();
        }
        catch (AggregateException ex)
        {
            LogException("AggregateException from CloudAppendBlob.AppendTextAsync", ex);
            StorageException se = ExtractStorageException(ex);
            if (se != null && se.RequestInformation.ExtendedErrorInformation != null)
            {
                if (se.RequestInformation.ExtendedErrorInformation.ErrorCode == "BlockCountExceedsLimit")
                    // Block count exceeds limit. Create a new log file
                    await CreateNewLogAsync();
            }
        }
        catch (Exception ex)
        {
            LogException("Exception from CloudAppendBlob.AppendTextAsync", ex);
            await Task.Delay(1000);
        }
    }
}

这是一个示例日志文件:

19.04.2018 00:00:00.115 - INFO: Logfile created: 2018_04_19/SensorDataProcessor_2018_04_19_0000.log
19.04.2018 00:00:00.116 - INFO: LogLevel is currently set to Information
19.04.2018 00:00:00.136 - INFO: Logfile created: 2018_04_19/SensorDataProcessor_2018_04_19_0000.log
19.04.2018 00:00:00.137 - INFO: LogLevel is currently set to Information
19.04.2018 00:00:00.364 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:00.365 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:01.434 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:00.684 Value=259.2 ElapsedTime=566
19.04.2018 00:00:02.348 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:01.688 Value=532.9 ElapsedTime=444
19.04.2018 00:00:05.643 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:04.660 Value=446 ElapsedTime=636
19.04.2018 00:00:06.552 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:05.660 Value=32 ElapsedTime=522
19.04.2018 00:00:09.779 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:08.682 Value=259.2 ElapsedTime=651
19.04.2018 00:00:10.255 - INFO: Processed SensorData: Id=2301002 Timestamp=19.04.2018 00:00:08.976 Value=0 ElapsedTime=604
19.04.2018 00:00:10.841 - INFO: Processed SensorData: Id=2002100 Timestamp=19.04.2018 00:00:09.383 Value=0 ElapsedTime=445
19.04.2018 00:00:10.773 - INFO: Processed SensorData: Id=2002001 Timestamp=19.04.2018 00:00:09.205 Value=1 ElapsedTime=644
19.04.2018 00:00:11.079 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:11.082 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:11.479 - INFO: Processed SensorData: Id=2005001 Timestamp=19.04.2018 00:00:09.404 Value=14 ElapsedTime=473
19.04.2018 00:00:11.758 - INFO: Processed SensorData: Id=2006001 Timestamp=19.04.2018 00:00:09.515 Value=15.3 ElapsedTime=677
19.04.2018 00:00:12.059 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:09.677 Value=532.3 ElapsedTime=449
19.04.2018 00:00:12.762 - INFO: Processed SensorData: Id=2002102 Timestamp=19.04.2018 00:00:09.558 Value=0 ElapsedTime=561
19.04.2018 00:00:12.714 - INFO: Processed SensorData: Id=2006003 Timestamp=19.04.2018 00:00:09.517 Value=0 ElapsedTime=672
19.04.2018 00:00:13.168 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:13.171 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:13.381 - INFO: Processed SensorData: Id=2003001 Timestamp=19.04.2018 00:00:09.663 Value=1.1 ElapsedTime=483
19.04.2018 00:00:13.749 - INFO: Processed SensorData: Id=2003100 Timestamp=19.04.2018 00:00:09.779 Value=0 ElapsedTime=761

获得此异常的原因是什么?

如何进一步排查?

1 个答案:

答案 0 :(得分:0)

CloudAppendBlob.AppendTextAsync()的文档标题提及:

  

此API应严格用于单个编写器方案,因为API在内部使用append-offset条件头来避免在多个编写器方案中不起作用的重复块。

如果您确实需要支持排序不重要的多作者场景,我建议您使用CloudAppendBlob.AppendBlockAsync()