Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Table Storage (412) Precondition Failed

I have used the GitHub solution for URLshortener and deployed it on my Azure tenant. Now suddenly I am getting an error:

"Exception while executing function: Functions.UrlIngest. 
Microsoft.Azure.WebJobs.Host: Error while handling parameter keyTable after 
function returned:. Microsoft.WindowsAzure.Storage: The remote server 
returned an error: (412) Precondition Failed."

While research on this issue, I found one link and it is written that:

"If the entity's ETag differs from that specified with the update request,
the update operation fails with status code 412 (Precondition Failed). This 
error indicates that the entity has been changed on the server since it was 
retrieved. To resolve this error, retrieve the entity again and reissue the request."

Hence in order to resolve this issue, I made the following changes to the Azure function code:

try
{
    await tableOut.ExecuteAsync(operation);
}
catch (StorageException ex)
{
    if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
    {
        log.Info("Precondition failure as expected.");

        TableOperation retrieveOperation = TableOperation.Retrieve<NextId>(keyTable.PartitionKey, keyTable.RowKey);

        // Execute the operation.
        TableResult retrievedResult = tableOut.Execute(retrieveOperation);

        int idCount = keyTable.Id;

        // Assign the result to a CustomerEntity object.
        keyTable = (NextId)retrievedResult.Result;

        if (keyTable != null)
        {
            // Change the phone number.
            keyTable.Id = idCount;

            // Create the Replace TableOperation.
            TableOperation updateOperation = TableOperation.Replace(keyTable);

            // Execute the operation.
            tableOut.Execute(updateOperation);

            log.Info("Entity updated.");
        }
        else
        {
            log.Info("Entity could not be retrieved.");
        }
    }
}

And it still throws the same error, however when I checked the Azure Table storage, it has the correct values.

Can anyone please help me on this?

like image 930
User5590 Avatar asked Mar 22 '19 11:03

User5590


1 Answers

The reason of this exception (source):

If the entity's ETag differs from that specified with the update request, the update operation fails with status code 412 (Precondition Failed). This error indicates that the entity has been changed on the server since it was retrieved. To resolve this error, retrieve the entity again and reissue the request.

If a Last writer wins approach

An approach that allows any update operations to proceed without verifying if any other application has updated the data since the application first read the data.

is good enough for your scenario, you will just need to add wildcard ETag before your update operation. Something like:

myEntityToUpdate.ETag = "*";

Otherwise, you have to deal with unsynchronized content yourself - e.g. catch an exception, fetch updated data again (with updated ETag) and try to update newer data (with newer ETag.) You can learn more about concurrency in Microsoft Azure Storage here.

like image 157
Martin Makarsky Avatar answered Oct 11 '22 18:10

Martin Makarsky