Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 442 forks source link

When mixing Imperative bindings for blob and Declarative bindings for Queue messages, queue messages are written first even when Blob messages are explicitly flushed #1986

Open hrkulkarMsft opened 7 years ago

hrkulkarMsft commented 7 years ago

I am using a binder to construct the blob name at runtime in my Azure Function. Additionally, I have a queue binding that is declarative. I write to the blob and then flush and close the text writer. Then I add the queue message to the collection. However, the queue message is written before the blob, resulting in the next function in the chain seeing an error.

Investigative information

Repro steps

  1. Create an Azure Function
  2. Set an Azure Queue as the output.
  3. Have the function output a blob using a binder.
  4. Write to the blob, and then the queue. Example:

` using (var writer = await binder.BindAsync(attributes)) { writer.Write(output); writer.Flush(); writer.Close(); }

await outputQueueItem.AddAsync(new SomePoco {Name =  blobName});

`

  1. Create an Azure Function which takes a QueueMessage with SomePoco as a trigger, and takes a blob with the Name of SomePoco as input.

Expected behavior

I expect the write to the blob to be blocking, and for the queue message to be written after the blob has been written.

Actual behavior

The blob messages are not written until the parent function completely finishes execution. While the queue messages are written while the parent function is still running.

Known workarounds

Have a copy of the function which processes the SomePoco queue messages, except it accepts input from the poison queue message stream. Then use this to manually clear out poison queue messages.

Related information

Provide any related information

mathewc commented 7 years ago

I experimented, and it does appear that the binding returned from Binder (e.g. in this case the TextWriter) isn't finalized (the blob isn't written) when the TextWriter is disposed as I had initially thought. The dynamic bindings created by Binder are "finalized" after the function returns, as part of processing the rest of the output bindings. However, the queue message output via AddAsync will be output immediately.

The bindings created via Binder aren't fully flushed until the method returns, as part of processing/finalizing output bindings (see code SetValueAsync).

We need to decide whether this is by design (probably has been this way for years?) or whether we want to provide a Flush API on Binder/IBinder to allow users to force flush.

paulbatum commented 6 years ago

@MikeStall any thoughts on this? Also, is this bug in the right repo? Sounds like a sdk level issue to me..

DimaGershman commented 5 years ago

Thanks! It took a while to figure out that something like that I going on there ;)

Regarding

Known workarounds

Have a copy of the function which processes the SomePoco queue messages, except it accepts input from the poison queue message stream. Then use this to manually clear out poison queue messages.

Another temporary workaround could be writing to blob storage explicitly, without the Binder, before creating queue message..

Something like:

var account = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("BlobStorageConnectionStringName", EnvironmentVariableTarget.Process));
var blobClient = account.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("container name");
var cloudBlockBlob = blobContainer.GetBlockBlobReference("blobname");
cloudBlockBlob.UploadText("blob file contents");