[storm] Consistent multithreaded operations

NeedBenzyn NeedBenzyn needbenzyn at gmail.com
Fri Sep 5 10:42:06 BST 2008


Hi,
thanks for you answer, i'm sorry I forgot to notice that my store object is
retrieved from an helper function which handle one store instance by thread
:

Database = create_database("mysql://"+
ServerConfig.dbUser+":"+ServerConfig.dbPassword+"@"+ServerConfig.dbHost+"/"+ServerConfig.dbName)
LocalStoreHolder = threading.local()


def GetStore():
    global LocalStoreHolder
    global Database

    if (not hasattr(LocalStoreHolder,"store")):
        LocalStoreHolder.store = Store(Database)

    log.msg("Returning store instance :%s for thread :
%s"%(LocalStoreHolder.store,threading.currentThread().getName()))

    return LocalStoreHolder.store

The problem I think is there is no lock put on the selected row after the
first thread has read it - On Mysql it'is handled with running the query
with "SELECT ... FOR UPDATE"  or "SELECT ... LOCK IN SHARE MODE"

Any idea on how to make storm using this behaviour ?

Thanks


2008/9/4 Jamu Kakar <jkakar at kakar.ca>

> Hi,
>
> NeedBenzyn NeedBenzyn wrote:
>
>> I'd like to know what would be the proper way in storm to execute that
>> kind of code on a multithreaded server (acessing MySql server InnoDb
>> tables):
>>
>>    def bankOperation():
>>
>>        account = store.find(BankAccount,BankAccount.id == 1).one()
>>        if account == None:
>>            return
>>
>>        #Simulate some processing
>>        waittime = random.random()*4
>>        time.sleep(waittime)
>>
>>        account.value = account.value + 100
>>
>>        store.flush()                store.commit()
>>
>> ==> If I have more than one thread executing that code data become
>> inconsistent.
>>
>
> You need to make sure you use one Store per-thread.  Something like
> this (untested) code should work:
>
> def bank_operation(store, id):
>    account = store.find(BankAccount, BankAccount.id == id).one()
>    if account:
>        account.value += 100
>
>
> def run_thread():
>    store = Store(create_database(...))
>    try:
>        bank_operation(store, 1)
>    except:
>        store.rollback()
>        raise
>    else:
>        store.commit()
>
> Storm automatically uses transactions, so you shouldn't have to
> worry about locking tables and such (though this may not be true for
> SQLite).
>
> Thanks,
> J.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/storm/attachments/20080905/2e20d754/attachment.htm 


More information about the storm mailing list