Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Tables delete and create table fails

My problem is that from time to time I want to delete the contents of an Azure table from Java. The table has millions of rows so it didn't seem like a good method to delete all entities one by one (because of the many REST API calls).

I tried to delete and then create the table again, but my code fails.

getTableClient().deleteTableIfExists("tablename");
getTableClient().createTableIfNotExists("tablename");

Here is my stacktrace:

com.microsoft.windowsazure.services.table.client.TableServiceException: Conflict
at com.microsoft.windowsazure.services.table.client.TableServiceException.generateTableServiceException(TableServiceException.java:57)
at com.microsoft.windowsazure.services.table.client.TableOperation$2.execute(TableOperation.java:339)
at com.microsoft.windowsazure.services.table.client.TableOperation$2.execute(TableOperation.java:322)
at com.microsoft.windowsazure.services.core.storage.utils.implementation.ExecutionEngine.executeWithRetry(ExecutionEngine.java:110)
at com.microsoft.windowsazure.services.table.client.TableOperation.performInsert(TableOperation.java:374)
at com.microsoft.windowsazure.services.table.client.TableOperation.execute(TableOperation.java:551)
at com.microsoft.windowsazure.services.table.client.CloudTableClient.execute(CloudTableClient.java:638)
at com.microsoft.windowsazure.services.table.client.CloudTableClient.createTable(CloudTableClient.java:173)
at com.microsoft.windowsazure.services.table.client.CloudTableClient.createTableIfNotExists(CloudTableClient.java:251)
at com.microsoft.windowsazure.services.table.client.CloudTableClient.createTableIfNotExists(CloudTableClient.java:197)

Do I have to sleep for a while between to two calls? I would rather not have too long wait here because I want to make this operation as short as possible so other threads can write again in the table.

Or is there a safe way to do this?


Edit

Yes, I am looking for an equivalent of the SQL statement: DELETE FROM tablename; in Azure Tables with as few REST API calls as possible.

My current best shot is:

getTableClient().deleteTableIfExists("tablename");
// TODO: solve problem with new creation!
boolean success = false;
while (!success) {
try {
  success = getTableClient().createTableIfNotExists("tablename");
} catch (StorageException e) {
  System.err.println("Log table recreation failed retrying in 5 sec");
  try {
    Thread.sleep(5000);
  } catch (InterruptedException ex) {}
}

I am looking for a better and safer solution then this!

like image 651
hpityu Avatar asked Dec 21 '22 19:12

hpityu


2 Answers

The delete operation against a Windows Azure table is not an instant operation. When the delete request is issued, it is marked for deletion by the storage service and no longer accessible by clients. The table is then deleted (garbage collected) in the background. This could take a few seconds to complete.

You're getting a 409 (Conflict) because the table still exists when the code requests a new table be created of the same name.

See the Remarks section (bottom of the page) of the MSDN document at http://msdn.microsoft.com/en-us/library/windowsazure/dd179387.aspx.

Other than waiting (like you have done) or creating a table with a different name, I'm not aware of an alternative.

like image 134
mcollier Avatar answered Jan 07 '23 05:01

mcollier


@mcollier is correct as to the cause here, but I wanted to point out a possible solution. Whenever we are faced with a scenario where we know we are going to have lots of table data that we want to prune occasionally, we design around it by rolling our tables. So, on a periodic basis we either increment the tablename or put the date in it for daily data (e.g. logs20120628). Then we can delete the entire table whenever we don't need that data anymore without impacting the current table or being forced to wait (sometimes very long waits) until we can recreate the table.

Just design a little flexibility into your table naming and this won't be a big issue for you.

like image 30
dunnry Avatar answered Jan 07 '23 07:01

dunnry