How to fix that annoying land bug...

Started by bjornredtail, July 24, 2009, 07:04:09 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

bjornredtail

Remember this?
http://www.redwallwarlords.com/forums/index.php?topic=9469.0

There's a whole class of subtle bugs in this game caused by race conditions. That is one of them. Imagen what happens if two warlords (We'll call them The Daughters of Delor and Peace Alliance) attack a third warlord (We'll call him 'Northland Raiders' :)) at the exact same time.  Suppose Northland Raiders has around 10,000 land at the start. Both of the attacks read the warlord info from the database. Then, both attacks run their calculations as if there is 10,000 land. Now, lets suppose that both the Daughters of Delor and Peace Alliance break Northland Raiders. Suppose that they both get 400 Acers in the attack. When the attack code goes to write back to the database, both the instance caused by Daughters of Delor's attack and Peace Alliance's attack will tell the database that Northland Raiders has 9,600 acers of land, and that they have gained 400 acers of land. So, Northland Raider's land will be set to 9,600 Acers, twice, when the real result would have been 9,200 Acers.

The solution to this is something called MySQL transactions. This tells the database to lock one row, so that any pending operations on it must wait for the row to be unlocked. So, in that case, the functions for Peace Alliance's attack wouldn't be called until after Daughters of Delor's attack.

A decant, but not particularly great article on the subject.
http://onlamp.com/pub/a/php/2003/12/18/transaction_pitfalls.html
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

The Lady Shael

Thanks Nev! Though I was under the impression that databases automatically use this locking system on all transactions...that's what I learned in my class anyway. The problem is that land is recorded in many different fields...land, freeland, then all the buildings. Technically freeland is superfluous...so even if one operation has a lock on land, freeland can still be altered to a different value.

I haven't been worrying about it much lately....it used to happen frequently months ago, but since we got rid of group attacking, it's only happening about once every 3-4 months now.
~The Lady Shael Varonne the Benevolent of the Southern Islands, First Empress of Mossflower Country, and Commandress of the Daughters of Delor

RWLers, your wish is my command...as long as it complies with the rules.


bjornredtail

Within an individual transaction, that's true. But, that's not really the problem here, it's the fact that one thread can UPDATE between another thread's SELECT and UPDATE queries.

The good news here is I think there's only two functions you have to use this for.
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Peace Alliance