• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: URLyBird locking/dirty reads

 
Rob Pearson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm doing URLyBird 1.3.3, and am looking at the issue of "dirty reads". I've read through a number of other posts on this, but not sure if they all apply to my assignment.
I've currently got a Data class that implements the interface provided. This contains a static list of records that are currently locked. The "lock" method synchronizes on this list with the usual sync/wait/notifyAll. I've then got an adapter for the Data class with the lock/update/unlock sequence to book a room.
This all seems fairly straight forward (assuming it works!) considering locking is worth 80 marks out of 400 (GUI's only worth 40). I'm wondering if I'm missing something. What I'm wondering is do I need read locks too?
My adapter find/search method needs to call "find" on the Data class (returns int[]), then call "read" for each int returned to get the room details. ie lots of calls and lots of opportunities for other threads to get in and change data that was identified by "find". If the change was just that the room is now booked, then I can handle that, but what if some other detail has changed? Is SUNs auto testing software going to try this? If I don't lock a record on read, then half way through a read an update could change the rooms details and possibly result in rooms being returned to the GUI that didn't match the search! (In the interface, the update call is passed all the room details so it could easily change details as well as book rooms).
80 points for locking, plus 40 for the Data class itself, I get the feeling they are expecting more. What do others think?
Cheers
Rob
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob
But then, if 3 clients all request hotels in Smallville, doesnt that mean that none of them will be able to book a room, since all the records will now have read locks by all the clients?
You say: If the change was just that the room is now booked, then I can handle that.
In the section of code where you handle checking if the room is still available, is it possible to check the other fields for changes as well?
I did this in my Fly By Night Services assignment: when about to book a flight I checked that flight details had not changed and that there were still enough seats left for my request.
This was all done client side.
Regards, Andrew
 
Rob Pearson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then, if 3 clients all request hotels in Smallville, doesnt that mean that none of them will be able to book a room, since all the records will now have read locks by all the clients?

I was thinking of allowing multiple read locks (say a count that increments and decrements) and then a write lock waiting until the read lock count (for the record of interest) was zero. Haven't actually coded it yet, but thats what I was planning. ie writes will have to wait for reads to finish.
Does anyone know has "real" databases do this? Can anyone point me to any relavent doco (for my own interest, not for thsi assignment).
In the section of code where you handle checking if the room is still available, is it possible to check the other fields for changes as well?

I was considering an initial "show all rooms" query. If an update happened during this, then the GUI could show incorrect (or possibly garbage) data, since its got nothing to validate against. My point about handling booked rooms was that that is simply a case of checking whether that field (owner) is blank or not.
I agree that when its comes to booking a room I can check that the details are as expected, so there's no chance of booking something unexpected. I'm more concerned with the users attempting to sell rooms that don't match the spec because the data is corrupt.
I keep thinking, 80 points... thats a lot of points for a sprinkling of synchronized, one call to wait and a couple notifyAll?
Cheers
Rob
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
G'day Rob
I was considering an initial "show all rooms" query.

Does this mean that every record in the database would then have a read lock applied?
I was thinking of allowing multiple read locks (say a count that increments and decrements) and then a write lock waiting until the read lock count (for the record of interest) was zero.

But this could result in a client appearing to hang if a second client has done a matching query and is just sitting there looking at the results.
Does anyone know has "real" databases do this?

I have worked with "real" databases that had explicit read locks, but I dont remember working with any that had implicit read locks.
I keep thinking, 80 points... thats a lot of points for a sprinkling of synchronized, one call to wait and a couple notifyAll?

Well, there could be other things as well.
Do you handle releasing locks if a client dies (or even just shuts down without clearing locks)?
How about the instruction If the specified record is already locked by a different client, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked.?
Taken literally, this would mean that a notifyAll() that wakes up every thread currently waiting for ANY lock could actually be wrong. Do you try and handle that? (That could be interesting to try and cater for, and I suspect that it is not necessary )
Regards, Andrew
 
Rob Pearson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
Does this mean that every record in the database would then have a read lock applied?

No, I was thinking of something like:

readLock and readUnLock just required to protect calls to read, so they are not around for long.
Do you handle releasing locks if a client dies (or even just shuts down without clearing locks)?

I've noticed a few posts on unreferenced and WeakHashMaps. I was thinking my client just calls a single bookRoom method (using RMI) on my Data adapter class, that runs on the server. This does the lock/modify/unlock routine. I've tried killing the GUI in the middle of this (by adding a sleep in the modify) but the bookRoom always completes, so always releases the lock. Am I missing something? I can't see a problem with this...maybe I need to test more, with the GUI on another machine?
If the specified record is already locked by a different client, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked.?

Hmmm... may have a point there, I guess the tread will also wake up when a read lock/unlock occurs.
Although as you point out, taken literally, notifyAll will wake all threads! Too scarry I think I might go home!
Thanks for the advice.
Cheers
Rob
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob
I don't think your read lock is going to gain you anything in the code snippet you provided. Another client can still modify the record after that code snippet has finished. So the client can still receive an array of Room that has records which are no longer valid.
In fact, for your code snippet the readLock would only need to be around the String[] r = Data.read(rooms[ i ]); line, since once it completes, you are working with local data which you can run your tests on. And I think there are easier ways to ensure that the read works atomically.
I've noticed a few posts on unreferenced and WeakHashMaps. I was thinking my client just calls a single bookRoom method (using RMI) on my Data adapter class, that runs on the server.

This is a valid way of handling the issue of dead clients (at least people have passed the old assignment doing this, dont know about the new assignment). Personally I dont like it, as you are now dedicating your server to a hotel database. But no wukkas, you should be fine with it
I've tried killing the GUI in the middle of this (by adding a sleep in the modify) but the bookRoom always completes, so always releases the lock. Am I missing something?

Try setting your sleep to 30 minutes with only one client and see what happens
You should be able to tell by code inspection whether it is possible for the client to die in the middle of the bookRooms method - that is, is the bookRooms method atomic or does it always do clean up even if the client dies.
Regards, Andrew
[ June 24, 2003: Message edited by: Andrew Monkhouse ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic