• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

NX: Thread safe and WeakHashMap vs. Unrefence Interface

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
risking that this question has been asked before, I would like to clarify my ideas concerning thread safety. It would be very nice if somebody could help me, because I'm confused about the function and use of WeakHashMap vs. the implementation of the Unreference Interface.
1.
If I use a static WeakHashMap in function of a Lockmanager and one client crashes, so the locked Record would in certain conditions be removed from the Collection. But the other threads waiting wouldn't be notified until a new thread would access the WeakHashMap, check the locked records, would lock the removed record and after completing his task, would notify the waiting threads. I'm right? Is this the disadvantage I would have to tolerate and which would also be accepted by the assignment?
2.
Because the value passed to the methods of Data.class is a primitive long and not a Wrapper-Instance of the Long-Class, the remote client wouldn't have a direct reference to the key-Object in the WeakHashMap? Is that right?
So after locking the record and putting him to the Collection and after leaving the synchronized block, it's possible that the Record would be removed from WeakHashMap when a client crashes at that moment?
3.
If each client would have their own Data-Instance or share a singleton Data-Instance, there would be a difference concerning the use of WeakHashMap?
4.
If I decide to use the Unreference interface, in which class would I have to implement this interface:
the remote Data class, the lockmanager-class, the Record-wrapping class?
5.
using the Unreference interface I risk that the unreference method would be called more than once, I'm right? But, if that would happen, why does it matter?
as a greenhorn like I am, I would think that is more reasonnable to implement the Unreference interface rather than to use the WeakHashMap, but as I said, I'm just a Java-greenhorn
Could you give me some advices? I would be very happy
Thank you for your patience, I hope my questions weren't too confusing
Ulrich
 
Ulrich Heeger
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, a little correction of point 2:
Of course, I mean that the Record could be removed without that a client crashes, only because the client doesn't have a direct reference to the concerning key object.
Greetings
Ulrich
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The unreferenced method will only be called once. You want the class that each client receives remotely to implement this interface. In the unreferenced method you simply call unlock on all the records that that client has locked.
Max can help better for the WeakHashMap implementation.
Mark
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulrich,
I cannot help you for the Unreferenced interface because I don't use RMI.

1.If I use a static WeakHashMap in function of a Lockmanager and one client crashes, so the locked Record would in certain conditions be removed from the Collection. But the other threads waiting wouldn't be notified until a new thread would access the WeakHashMap, check the locked records, would lock the removed record and after completing his task, would notify the waiting threads. I'm right? Is this the disadvantage I would have to tolerate and which would also be accepted by the assignment?


You are right : there is a notification issue that you can solve by using WeakReferences stored in a regular HashMap and registred with a ReferenceQueue. My LockManager class is based on them.

2.
Because the value passed to the methods of Data.class is a primitive long and not a Wrapper-Instance of the Long-Class, the remote client wouldn't have a direct reference to the key-Object in the WeakHashMap? Is that right?
So after locking the record and putting him to the Collection and after leaving the synchronized block, it's possible that the Record would be removed from WeakHashMap when a client crashes at that moment?


In such a design the object which must be "weakly-reachable" is the one which represents the client connection, not the locks by themselves.

3.If each client would have their own Data-Instance or share a singleton Data-Instance, there would be a difference concerning the use of WeakHashMap?


Whatever the number of instances of Data, Data cannot represent the "owner" of a lock : if your system is multi-table (or becomes in the future), even if you instantiate one Data instance per client, you may have one client owning locks on multiple tables. The owner must be some object which uniquely represents the client, if you want that, in case of a client crash, all locks, on any table, are properly freed.
Best,
Phil.
[ September 08, 2003: Message edited by: Philippe Maquet ]
 
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ulrich,
I have URLy Bird 1.3.1, I am using RMI. I am not using an external lock manager. I am using a static WeakHashMap in my Data class to manage locks. You will find information on it in the following thread:
NX: About data consistent
Andrew and Max have walked me through the lock manager design.
Most of the issues that you raise are true about keying in on the Integer of the passed "recNo" for the WeakHashMap, it will get rid of the row immediately since the Data class client, i.e., calling routine (DataAdapter or whatever) doesn't have a direct reference to the Integer of the recNo. You may want to turn it around, i.e., use a reference to the "Data.this" value as the key component for your WeakHashMap and the Integer of recNo for the value piece. Now, if you design your system such that there is a new instance of Data class created for each client connection, then you are guaranteed that:
1. You have a unique value for the key for the WeakHashMap
2. The lock entry in the WeakHashMap stays around until the reference to it is removed, i.e., the instance of Data created (I do it in a DataAdapter class which in turn is instantiated in the RemoteData class for network connection and directly in the case of no network connection) is no longer valid. This may be due to any number of reasons such as loss of connection, client crash etc.
Max mentions this in one of his early threads, that is where I picked up the idea of "flipping" Data.this instance and the Integer(recNo) as key/value pair in the static WeakHashMap.
Good luck. You are already making very impressive progress.
Regards.
Bharat
 
Ulrich Heeger
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thank you very much for your answers. You guys are really phantastic and I'm very glad to have discovered this forum.
Hi Phil,
you write:

there is a notification issue that you can solve by using WeakReferences stored in a regular HashMap and registred with a ReferenceQueue. My LockManager class is based on them.


Now, I read the API for the ReferenceQueue, but I don't really understand. I have some questions:
1.Does it mean, the ReferenceQueue is a certain Collection to which objects are put when their reachability changes?
2.How do I register objects to the ReferenceQueue? How do I determine if an object is put to the ReferenceQueue due to its weak reference or for example due to its soft reference?
3.When does the notification call happen? It's integrated in a method of the ReferenceQueue?
Perhaps you can give me an example of your implementation, if it's ok?
Regards
Ulrich
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulrich,

1.Does it mean, the ReferenceQueue is a certain Collection to which objects are put when their reachability changes?


Yes.

2a.How do I register objects to the ReferenceQueue?


A ReferenceQueue (only one) must be first created. Now WeakReferences (or SoftReferences) are registred in the queue by passing it to its constructor.

2b.How do I determine if an object is put to the ReferenceQueue due to its weak reference or for example due to its soft reference?


Simply by calling your reference queue poll() method from time to time (every second for example), from within some thread. It's typically done within a loop.

3.When does the notification call happen? It's integrated in a method of the ReferenceQueue?


See 2b.

Perhaps you can give me an example of your implementation, if it's ok?


Just an excerpt of it then :

As you can see, it's very simple to implement !
Best,
Phil.
 
Ulrich Heeger
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat,
thank you for your advice, it was very helpfull for me.
I have one more question.
You wrote:

1. You have a unique value for the key for the WeakHashMap
2. The lock entry in the WeakHashMap stays around until the reference to it is removed, i.e., the instance of Data created (I do it in a DataAdapter class which in turn is instantiated in the RemoteData class for network connection and directly in the case of no network connection) is no longer valid. This may be due to any number of reasons such as loss of connection, client crash etc.


this is really a good idea, but I have no the problem, that my lock and unlock method are a little bit different:

so, beside Long(recNo) and Data.this, I would have to store a long value for the cookie in the static Map. But that wouldn't cause a problem, if I would create instead of Long(recNo) an Array with recNo and cookie or a Record Object containing the two values. What do you think about it?
Regards
Ulrich
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulrich,

this is really a good idea, but I have no the problem, that my lock and unlock method are a little bit different:


Besides, have a look to my comment in this thread (posted September 09, 2003 12:30 AM). I am not quite sure, but I think there are two issues in Bharat's lock implementation.
Best,
Phil.
[ September 09, 2003: Message edited by: Philippe Maquet ]
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ulrich,
You wrote:


so, beside Long(recNo) and Data.this, I would have to store a long value for the cookie in the static Map. But that wouldn't cause a problem, if I would create instead of Long(recNo) an Array with recNo and cookie or a Record Object containing the two values. What do you think about it?


Sorry, I forgot all about the slight variations in the signatures of the locking methods in the URLy Bird assignment. Based on the method signatures provided in your version of DBMain interface, I don't see why you cannot do what you are mention above.
Regards.
Bharat
Hello Phil,
You wrote:


Besides, have a look to my comment in this thread (posted September 09, 2003 12:30 AM). I am not quite sure, but I think there are two issues in Bharat's lock implementation.


I am really glad that you have taken time to look into the aforementioned thread. I am going to pursue it further in that thread. Ulrich, please keep an eye on the thread that Phil mentions above. It might answer a slew of questions for you.
Phil, by the way, I haven't forgotten the following thread:
My idea about get field value.
I will pick-up on it once we are done with the locking stuff that is being discussed in my "Exceptions" thread. Anything I can help you with, please let me know.
Regards.
Bharat
 
Ulrich Heeger
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Phil,
I'm still confused about ReferenceQueue. so, trying to implement your and Bharats' ideas, I have created a new Reference Class:

Further I would design:

1.
The LockManager which is a HashMap would contain Data.this as the KeyObject and the locked Record as the Value-Object.
2.
Data-Class would have a static variable referenceQueue and an instance variable lockReference which would be instantiated within Data's constructor:


3. So I in the concerning thread I could write:


I don't know if this is OK. Because I haven't understood very well the notification within the ReferenceQueue, do I need the line: lockManager.notifyAll()? You haven't written it, but I don't understand why not?
I'm sorry to border you once more, but I'm stuck
Regards
Ulrich
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ulrich,
I do not know ReferenceQueue and how it works. I have not done it and am not going to do it until (if at all) I take care of the other requirements. May be Phil and Andrew can help you out with this?
Regards.
Bharat
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulrich,

Hi Phil,
I'm still confused about ReferenceQueue. so, trying to implement your and Bharats' ideas, I have created a new Reference Class:


I am sorry you got confused, all the more so since I have a little responsability in it.
I'll repair it by coming back soon (I mean today yet) with a new thread on this subject, because it's quite difficult (anyway to me) to follow three parallel discussions on the same topic (see How to expire locks and NX: URLy Bird 1.3.1 Explicit Fatal Exception Handling).
Unfortunately, I cannot comment your code above, because I lack of a global view of its running/use context.
Looking forward to read you soon in a thread I'll probably call "Single table / Simple Locking - WeakHashMap vs WeakReferences".
Best,
Phil.
 
Ulrich Heeger
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat, hi Phil, hi Mark
so we'll discuss further in other threads.
Thank you very much for your help and see(read) you later
Regards
Ulrich
 
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic