aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Lease pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Lease pattern" Watch "Lease pattern" New topic
Author

Lease pattern

Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
Hi,
Would you recommend implementation of a leasing system to track client connection loss from server side in order to clean the locking tables appropriately.
If yes would you use net.jini.core.lease.*, or would you implement something from scratch.
Many thanks,
Charles.


Charles.<br />(SCJD2)
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Charles,
I'd be careful with anything beyond the scope of core Java. And packages such as net.jini... sound quite exotic to me! The assignment is about solving a problem with standard java technology. Therefore I'd advise you to develop something from scratch (besides: I don't know what "leasing pattern" means - could you explain that to me?)
Thanks!


Mag
Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
Hi Mag,
The leasing pattern: When a client wants to obtain some resource from the server (a lock for example) it must first acquire a Lease (a permission) from the server. This Lease is valid for a certain amount of time. The client must renew the lease before its expiration if it wants to keep the resource.
If the client fails to renew the lease, the server can take action pertaining to this situation. In our case, the server can free the lock and notifyAll. With this pattern the server can easily detect if the client connection breaks.
This involves quite a lot of nice thread programming of course.
Regards,
Charles.
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
Charles,
Remember that no timeouts or extra complexity are specified in the requirements. If you give each remote client a unique connection object, you can use RMI's built in unreferenced() functionality to achieve a simple solution for dealing with clients that disconnect while holding one or more locks. Anything beyond that is overkill and just more chance for mistakes. Also, the assignment must be made up entirely of classes from java.* and suncertify.* that are either provided with the JDK or written by you, so any third party classes are strictly off limits.
Pete
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Pete is absolutely correct: use of net.jini.core.lease.*, com.borland.jbcl.layout.*, and the like, falls under this stern caveat:
Target Platform
Throughout this exercise, you must use exclusively the Java 2 platform. You are not required to develop your code using any particular implementation of the Java 2 platform, but the submission that you return must have been tested and shown to work under a production (not development) version of the Sun Microsystems' Java 2 platform.
Failure to adhere to these directions will result in automatic failure


On the other hand, I did choose to implement a 60 second lock-lease (configurable in my config.xml file ), mostly to take a small step in the direction of database transaction-rollback functionality, and to demonstrate the "gracefulness" of my server shutdown.
Every "spiffy" feature you add must be adequately commented, however, or you'll end up with a score of 154, like Michael Morris :roll:


Fly by Night Consultants<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr><i>I climbed on the back of a giant albatross<br />which flew through a crack in the cloud<br />to a place where happiness reigned...<br />all year 'round<br />the music played ever so loudly!</i><p><a href="http://thomasfly.com/songs/Traffic/Hole_in_my_Shoe_qt.htm" target="_blank" rel="nofollow">Hole in My Shoe</a><hr></blockquote>
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
Just a thought off the top of my head: is the lock and unlock methods the client class calls not essentially an implementation of the lease pattern? The call to lock provides the client the right (i.e. lease) to use the record for X amount of time. Of course, unlock gives up the lease.
If we decided to add the lease pattern on top of the lock/unlock methods, we would essentially have a double lock check; namely:

The double 'lock' is worriesome to me. So my thought was to say in my DESIGN_DECISIONS.txt that the lock/unlock mechanism follows the lease pattern, with the caveat that clients cannot renew a lease. The server researves the right to revoke a clients' lease on any lock if the lease period expires.
Comments?
--Dave.
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
The usual approach to locking is to attach a ClientID to a record, using HashMap. Instead, I use a class that contains the ClientID along with a timestamp & progress counter, which can be checked by the server to decide whether or not to revoke the lock held by a given client.
It's hard to say how big a problem a "stalled" (as opposed to "dead") client would really be in a production environment, however.
There's really not even much of a good reason to attach a ClientID to the locks, except to handle the infrequent occurence of a client dying just after acquiring a lock- but if you're writing an online banking application...
Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
Dave,
I do not consider doing a leasing pattern like the one you described here :
lease(recordNum);
lock(recordNum);
unlock(recordNum);
release(recordNum);
Leasing and locking done by two different threads on both sides.
lets say leases are valid 5 seconds
Client X:
Th1---------------------------- Th2---------------
t1-- ask for a lease (5 secs)
t2-- get it tell thread 2 to work --- lock
t3-- renew the lease (5 secs) -----read write
t4-------------------------------- unlock
t5-- quit leasing
t6--
Server:
Th1------------------------- Th2---------------
t1++ give the lease to X(5 secs)
t2-- -------------------------------- check all leases
t3++ give the lease to X(5 secs)
t4-- ----------------------------- --- check all leases
t5--
t6-- -------------------------------- --- find dead lease of X ( > 5 secs), check lock table
for X dead locks

Regards. :roll:
[ November 08, 2002: Message edited by: Charles Dupin ]
[ November 08, 2002: Message edited by: Charles Dupin ]
[ November 08, 2002: Message edited by: Charles Dupin ]
Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
Sorry for the poor readability of the previous message, I dont know how to edit tables with this bulletin board.
Regards
Charles
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Re: tables -- anything you put between [ CODE ] tags will be displayed using a fixed-with font. So you can use good oldfashioned ASCII art.
- Peter
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Originally posted by Peter den Haan:
Re: tables -- anything you put between [ CODE ] tags will be displayed using a fixed-with font. So you can use good oldfashioned ASCII art.
- Peter


Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
to Peter,
I could not make the [CODE] stuff working.
Sorry.
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Just use the CODE button:
Charles Dupin
Ranch Hand

Joined: Oct 18, 2002
Posts: 94
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lease pattern