1 2 3 Previous Next 33 Replies Latest reply on Dec 8, 2005 9:19 AM by manik Go to original post
      • 15. Re: JBCACHE-359 Discussion Thread
        belaban

        Can the ThreadLocal stuff be replaced with using the payload in the new JGroups 2.2.9 MethodCall when it is final ?
        Or is the common agreement that ThreadLocals are the way to go ?

        • 16. Re: JBCACHE-359 Discussion Thread

          I will vote for the payload with Invocation object embeded in there. We will need it for other purposes as well, e.g., maybe authentication/security in the future anyway?

          • 17. Re: JBCACHE-359 Discussion Thread
            belaban

            The good thing is that once TxInterceptor fetches the TX from the TXManager, we just stuff it into the payload and don't have to fetch it again. But this creates a dependency on JGroups 2.2.9. In the future I would want to replace MethodCall with Invocation anyway

            • 18. Re: JBCACHE-359 Discussion Thread
              manik

              I think a payload and Invocation object, as Ben mentioned, is the way to go.

              ThreadLocal is just a temporary solution until we can change the API, deprecate passing up MethodCalls in Interceptor.invoke(), etc. Temporary because we will probably not have payloads, Invocation objects till 2.0? I presume this is the timeframe in mind?

              • 19. Re: JBCACHE-359 Discussion Thread
                brian.stansberry

                +1 on payload and Invocation object; ThreadLocal is just a temporary fix.

                • 20. Re: JBCACHE-359 Discussion Thread
                  manik

                  Is this common consensus then?

                  * For now, use thread local to hold tx and gtx for each invocation
                  - thread local object resides as a static in Interceptor
                  - set by the tx interceptor upon each invocation
                  - all other interceptors retrieve tx and gtx stuff from thread local

                  * Later on, move to an Invocation object that contains all specifics of an invocation.

                  • 21. Re: JBCACHE-359 Discussion Thread
                    belaban

                    #1 Why do we need a thread local for TX/GTX ? I say we don't change anything and simply call TxManager.getTransaction(), which uses a ThreadLocal itself. Once we have the invocation payload in MethodCall, we can switch to using the payload directly, rather than taking the detour over ThreadLocals.

                    #2 I understand you use ThreadLocals for Options. But I assume the Option parameter is exposed at the TreeCache API, e.g. in put() methods. Correct ?

                    • 22. Re: JBCACHE-359 Discussion Thread
                      manik

                      #1 - this is fine in simplistic cases where we just need access to the TX. In the case of the GTX though, there are several ways to get a hold of it:

                      - TreeCache.getCurrentTransaction() - gets the existing gtx if there is one, creates a new one otherwise.
                      - txTable.get() - if the gtx has been registered
                      - getting the gtx from a method call args
                      - getting the gtx from a locally cached copy (as per Brian's fix for JBCACHE-359).

                      This makes interceptor code very unreadable as the gtx seems to come from several places and interceptors are not consistent in how and where they get a gtx reference.

                      I propose that the TxInterceptor goes through all the above ways of retrieving the gtx depending on the method call, and places this in a ThreadLocal accessible via the Interceptor so that all other interceptors don't need to care where they get the gtx from - it is already decided and set by the TxInterceptor.

                      Now this doesn't *have* to be in a separate ThreadLocal - we could use any of the existing mechanisms, I'd just like to stick to a standardised mechanism and have the TxInterceptor sort out any differences if for a particular call, the gtx needs to come from somewhere else.

                      Also see http://www.jboss.com/index.html?module=bb&op=viewtopic&t=73645

                      • 23. Re: JBCACHE-359 Discussion Thread
                        manik

                        #2 - correct, these are exposed in the TreeCache api. Rather than adding more Method objects in the TreeCache for the interceptors to trap and extract args out of though, I put the options in a thread local and let the interceptors get it from there just to make the interceptor code clearer since, after all, this parameter controls an overriding behaviour, an aspect if you will, and not really data used in the operation.

                        E.g., my get(fqn, option) method looks like:

                        // put option in thread local
                        // call get(fqn)
                        // reset option in thread local

                        Again, see the discussion on InvocationContexts, which is a clean way of managing such invocation specific details.

                        http://www.jboss.com/index.html?module=bb&op=viewtopic&t=73645

                        • 24. Re: JBCACHE-359 Discussion Thread
                          belaban

                           

                          "manik.surtani@jboss.com" wrote:
                          #1 - this is fine in simplistic cases where we just need access to the TX. In the case of the GTX though, there are several ways to get a hold of it:

                          - TreeCache.getCurrentTransaction() - gets the existing gtx if there is one, creates a new one otherwise.
                          - txTable.get() - if the gtx has been registered
                          - getting the gtx from a method call args
                          - getting the gtx from a locally cached copy (as per Brian's fix for JBCACHE-359).

                          This makes interceptor code very unreadable as the gtx seems to come from several places and interceptors are not consistent in how and where they get a gtx reference.

                          I propose that the TxInterceptor goes through all the above ways of retrieving the gtx depending on the method call, and places this in a ThreadLocal accessible via the Interceptor so that all other interceptors don't need to care where they get the gtx from - it is already decided and set by the TxInterceptor.

                          Now this doesn't *have* to be in a separate ThreadLocal - we could use any of the existing mechanisms, I'd just like to stick to a standardised mechanism and have the TxInterceptor sort out any differences if for a particular call, the gtx needs to come from somewhere else.

                          Also see http://www.jboss.com/index.html?module=bb&op=viewtopic&t=73645



                          I understand, but my question was why do this now and not switch directly to payloads once they become available. We will be busy enough as it is to ship 1.3 in Feb, even with moving some features into 1.4, so I think this has low prio.

                          • 25. Re: JBCACHE-359 Discussion Thread
                            belaban

                             

                            "manik.surtani@jboss.com" wrote:
                            #2 - correct, these are exposed in the TreeCache api. Rather than adding more Method objects in the TreeCache for the interceptors to trap and extract args out of though, I put the options in a thread local and let the interceptors get it from there just to make the interceptor code clearer since, after all, this parameter controls an overriding behaviour, an aspect if you will, and not really data used in the operation.

                            E.g., my get(fqn, option) method looks like:

                            // put option in thread local
                            // call get(fqn)
                            // reset option in thread local

                            Again, see the discussion on InvocationContexts, which is a clean way of managing such invocation specific details.

                            http://www.jboss.com/index.html?module=bb&op=viewtopic&t=73645


                            You're taking the decision here that options are aspects and should not be seen at the signature level, e.g. as arguments to invoke(). Again, tying in with the previous statement, I believe this is not a bad idea, but we need to document this, so writers of interceptors will understand it. In the end, I think even options should be part of the payload of an invocation, not of a ThreadLocal. Make sure you have a clean migration when payloads become available / when we switch to Invocation.

                            • 26. Re: JBCACHE-359 Discussion Thread
                              manik

                              Re #1; I don't think this is as complex or big a change, I can probably have it done by the end of today.

                              - Just a case of adding the ThreadLocal variables
                              - adding some code in the TxInterceptor to set these values
                              - search for all other direct uses of tx and gtx in the other interceptors and replace them with this call.

                              Also, I think it will help Brian get his JBCACHE-359 fix in.

                              • 27. Re: JBCACHE-359 Discussion Thread
                                manik

                                Re #2; I agree that this will become a part of the payload. Migration should not be that hard since this is accessed by interceptors by calling a method on the abstract Interceptor. So that method will be all that we'd have to change.

                                And you're right, I do need to document this.

                                • 28. Re: JBCACHE-359 Discussion Thread
                                  belaban

                                   

                                  "manik.surtani@jboss.com" wrote:
                                  Re #1; I don't think this is as complex or big a change, I can probably have it done by the end of today.

                                  - Just a case of adding the ThreadLocal variables
                                  - adding some code in the TxInterceptor to set these values
                                  - search for all other direct uses of tx and gtx in the other interceptors and replace them with this call.

                                  Also, I think it will help Brian get his JBCACHE-359 fix in.


                                  I know you can change a lot of code in very short time (judging by the number of CVS commits) :-)
                                  But let's complete the discussion on this first. So I propose something like the following to solve both the issue of options and the issue of transactions or other stuff that needs to accompany an invocation:
                                  - Let's introduce an Interceptor.getInvocationContext()
                                  - The InvocationContext has a hashmap, same as payload in Invocation
                                  - InvocationContext has methods
                                  Object get(Object key)
                                  and
                                  Object set(Object key, Object val);
                                  - All InvocationContexts are purely passed between interceptors in the same VM, at this time. In the future, this will be mapped to MethodCall.payload
                                  - The InvocationContext can also be used to implement options
                                  - Certain keys in the InvocationContext's hashmap are reserved
                                  - The implementation of Interceptor.getInvocationContext() can use a ThreadLocal (or ThreadLocalInheritable)
                                  - Example (no casting and error checking done):
                                  public Object invoke(MethodCall call) throws Throwable {
                                   GlobalTransaction gtx=getInvocationContext().get("GTX");
                                   int replMode=getInvocationContext().get("repl_mode");
                                   ...
                                  }
                                  


                                  Once we switch to payloads, the code will look like this:
                                  public Object invoke(MethodCall call) throws Throwable {
                                   GlobalTransaction gtx=call.get("GTX");
                                   int replMode=call.get("repl_mode");
                                   ...
                                  }
                                  




                                  • 29. Re: JBCACHE-359 Discussion Thread
                                    manik

                                    Yeah, that would do it ... we're getting close, now we just need to think of a way of making it more strongly typed! :-)

                                    Like I suggested in http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3911423, perhaps a JBossCacheMethodCall subclass that would add strongly typed helper methods to extract specific objects out of the payload.