azure-contrib / AzureDirectory

A Lucene Directory Provider for Azure Blob Storage
Microsoft Public License
77 stars 57 forks source link

AzureLock Needs to Handle 409 #4

Closed haneytron closed 10 years ago

haneytron commented 10 years ago

Had this bite me at work today and I fixed it easily, however you should update your code. Azure will sometimes return a 409 on the lock obtain of Lucene, which will timeout the lock.

Current code:

    private bool _handleWebException(ICloudBlob blob, StorageException err)
    {
        if (err.RequestInformation.HttpStatusCode == 404)
        {
            _azureDirectory.CreateContainer();
            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(_lockFile);
                blob.UploadFromStream(stream);
            }
            return true;
        }
        return false;
    }

Fixed (and tested) code:

    private bool _handleWebException(ICloudBlob blob, StorageException err)
    {
        if (err.RequestInformation.HttpStatusCode == 404 || err.RequestInformation.HttpStatusCode == 409)
        {
            _azureDirectory.CreateContainer();
            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(_lockFile);
                blob.UploadFromStream(stream);
            }
            return true;
        }
        return false;
    }
richorama commented 10 years ago

Great, thanks. I'm happy to make the change, or you can fork and send me a PR if you prefer?

On 13 Nov 2013, at 21:47, David Haney notifications@github.com wrote:

Has this bite me at work today and I fixed it easily, however you should update your code. Azure will sometimes return a 409 on the lock obtain of Lucene, which will timeout the lock.

Current code:

private bool _handleWebException(ICloudBlob blob, StorageException err)
{
    if (err.RequestInformation.HttpStatusCode == 404)
    {
        _azureDirectory.CreateContainer();
        using (var stream = new MemoryStream())
        using (var writer = new StreamWriter(stream))
        {
            writer.Write(_lockFile);
            blob.UploadFromStream(stream);
        }
        return true;
    }
    return false;
}

Fixed (and tested) code:

private bool _handleWebException(ICloudBlob blob, StorageException err)
{
    if (err.RequestInformation.HttpStatusCode == 404 || err.RequestInformation.HttpStatusCode == 409)
    {
        _azureDirectory.CreateContainer();
        using (var stream = new MemoryStream())
        using (var writer = new StreamWriter(stream))
        {
            writer.Write(_lockFile);
            blob.UploadFromStream(stream);
        }
        return true;
    }
    return false;
}

— Reply to this email directly or view it on GitHub.

haneytron commented 10 years ago

I contributed to it under my company's account, so you should have a pull request there. Thanks!