Hello,
I have implemented a variation of the sharded counter form Google IO
speech (http://sites.google.com/site/io/building-scalable-web-
applications-with-google-app-engine)
My variation is that instead of haveing a fixed number my number of
shards increases if contention on the counter increases.
But I am not quite sure if I have done it right:
Please have a look:
def inc(self):
try:
def txn():
c = self._get_one() # get sharded counter by random
c.count += 1 # increase counter
c.put() # write counter
try:
db.run_in_transaction(txn)
except db.TransactionFailedError(): # if this transaction
fails...
logging.debug('Counter %s: inc failed.' %
self.name)
self.num_shards += 5 # increase number of shards
self.put()
except:
pass
You see, when the transaction fails due to high contention I modify
the number of shards. But I am not sure, if I should place this
self.num_shards += 5 # increase number of shards
self.put()
inside a transaction or not...? I mean if contention is high a lot
threads are going to try to increase the number of shards. Is it
correct to ignore the case when this code fails (the outer try/except
block) or should it be placed inside a transaction (which would leed
to retries of this code)?
Regards,
Constantin