You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Kohsuke Kawaguchi <kk...@kohsuke.org> on 2005/07/27 07:11:35 UTC

[javaflow] MethodLookup

I'm bit confused with MethodLookup. I could be just missing something, 
but here it goes...


I wonder if it would be better if the startWith method is:

Continuation startWith( Runnable target, ContinuationContext context )

... instead of:

Continuation startWith( String methodName, ContinuationContext context )

... where the semantics is that the execution starts by invoking the 
run() method of the target object.

I think developers relates to Runnable more easily, as java.lang.Thread 
uses it. I also think there isn't any loss in the expressiveness of the 
library, as we can emulate the old functionality by using a Runnable 
that looks up a method name from String and create a new instance.

This approach allows me to fill in the Runnable object by application 
specific information. This is analogous to how one would pass 
information to a newly launched java.lang.Thread.

I think reducing the developer-visible surface of javaflow by exploiting 
the core JDK libraries makes javaflow easier to use.

It also has some other minor benefits, like simplifying the 
implementation and improving the performance by avoiding reflection.

-- 
Kohsuke Kawaguchi

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [javaflow] MethodLookup

Posted by Kohsuke Kawaguchi <ko...@sun.com>.
Kohsuke Kawaguchi wrote:
> I'm bit confused with MethodLookup. I could be just missing something, 
> but here it goes...

I should have mentioned that I've already read [1] before writing this 
e-mail.

[1] http://vafer.org/blog/tcurdt/archives/2005_02.html
-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@sun.com

Re: [javaflow] MethodLookup

Posted by Kohsuke Kawaguchi <ko...@sun.com>.
Torsten Curdt wrote:
> The following problems are still on the TODO list
> 
>   o try/catch/finally
>   o synchronized
> 
> plus the code just asks for a cleanup and improvements.
> ..and I wanted to do that while porting to ASM.

You mean, you'd like those clean ups and etc done to both the BCEL 
version and the ASM version?


>> I saw that the instrumentation algorithm can be probably optimized  
>> (for example, no need to instrument StringBuffer.append()  
>> operation, share the stack capturing code, etc) Now that I actually  
>> saw the decompiled byte code, I'm more willing to change it than  
>> before.
> 
> Hehe :) ..see?

You got me there. I think I'll study the code more closely.
I started to develop a few ideas, but I'm wondering if I should hold 
those off until the ASM port to complete.


> TBH ...I admit I've missed that we can use the
> instance from the stack. So you got a real good point!
> 
> So you convinced me to go ahead with the Runnable
> approach. But still I am not to eager to have that
> Fiber class in the codebase ...at least for now.
> 
> Can you live with that for the moment?

Yes, yes, yes! I really appreciate you let me do this.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@sun.com

Re: [javaflow] MethodLookup

Posted by Kohsuke Kawaguchi <kk...@kohsuke.org>.
(Please check the other e-mail I sent, which is hopefully much less 
controversial.)

Torsten Curdt wrote:
>>> See the example in my blog ...which is actually
>>> taken from the Cocoon integration. (I think in
>>> there is also the link to the class) Would be
>>> great if you could also have a look into that
>>> class.
>>>
>>
>> I looked at Cocoon code you had in [1]. I didn't particularly see  
>> anything that made me rethink (IOW, I don't see anything that  
>> absolutely requires MethodLookup nor ContinuationContext instead of  
>> Runnable.)
> 
> Had a look into your implementation
> ..you have the "run" method delegating to
> the different methods.

The Invoker class was needed to work around the problem/feature in the 
current Continuation API that the first object to be invoked needs to 
have a default constructor. The Fiber class can be extended by users and 
therefore it may not have the default constructor, so I can't just call 
Fiber.run immediately from Continuation.execute().

Fiber.run delegates to another Runnable because that's what 
java.lang.Thread does.


> If you want to get rid of the of the method
> lookup you do need to pass in the Runnable
> also to the continueWith ...not sure if I
> like that.

No, I don't think you have to. That Runnable object is a part of 
Continuation already. The continueWith method can look it up from there. 
Specifically, it already retains a reference to that object in Stack. We 
just need to make it explicitly accessible (instead of fiddling with 
references in stacks to get to it.)


> I had similar idea in the beginning:
> 
>   startWith(Method, ContinuationContext)
>   continueWith(Method, Continuation, ContinuationContext)
> 
> What I did not like: Passing in of the
> Continuation already implies the Method.

I agree. That's why I originally suggested to use Runnable in this 
level, to get rid of Method altogether.

> Beforehand the method was saved inside
> the Continuation object. But this does
> not work when it comes down to serialization.

By calling Runnable.run() method, you no longer have to save the Method 
object in Continuation. That means no MethodLookup is necessary, no 
method name is necessary.

> BTW: IIRC I needed to mark the static as
> transient for the XStream serialization
> ..but not totally sure.

I used XStream in other projects, and I believe it didn't persist static 
fields. But I will put that back. Maybe we are using different versions.

>> But at the same time, I saw that you have some existing investments  
>> with the current javaflow Continuation API and it's very  
>> understandable that you don't want it to change.
> 
> Well ...it's still marked alpha so as long
> as it makes sense to me I am fine changing
> it.
> 
> But TBH I currently would like to focus on
> the current bytecode rewriting stuff first
> before changing the API that much.

OK.



>> See the attachments for the exact code. All in all, I didn't think  
>> this re-write is ugly, but that is always a subjective issue.
> 
> I don't understand this forking and cloning
> stuff - you don't need that. A continuation
> should be re-entrant and always create a new
> one as a result.

Unlike Continuation, Fiber is a mutable object. It updates itself as you 
continue execution. That's why sometimes you want a "fork" to create an 
identical copy.

Fiber.fork doesn't deep-copy Continuation, as Continuation itself 
doesn't need to be copied. It's just Fiber object that needs to be 
copied, hence the use of clone().



>> Anyway, I'm no longer too keen about convincing you to change the  
>> API of the Continuation class. If you like my rewrite, that's good,  
>> but if not, that's also fine with me. The current Continuation API  
>> makes the Fiber class implementation uglier, but that's not too big  
>> a problem.
>>
>> Instead, I'm hoping that you allow me to commit this Fiber class to  
>> javaflow, as this is what I primarily want to use in my code.
> 
> If you don't mind I would prefer if you keep
> that code in your codebase for now. Let's get
> back to that once we have the rewriting based
> on ASM in place. I would like to focus on the
> hard part first ;)
 >
 > Would that be ok for you?

Hmm...

Even if Fiber doesn't change any of the existing Continuation class, do 
you still feel uncomfortable with it?

As an user, it doesn't really matter to me what byte-code manipulation 
library javaflow is using, because it's completely behind the scene. The 
current BCEL one seems to be working, so it's not very high priority for 
me to rewrite javaflow by using ASM. Besides, I doubt if I can be of any 
help when it comes to porting javaflow from BCEL to ASM. That part is 
the guts of javaflow and it takes you to change it.

For me, the javaflow API surface is of a higher priority, because in the 
end my goal is to use it. I suspect that a thread-like API connects to 
users more easily, as there's less learning involved. I'm hoping that 
this contributes to an improved user experience. For this reason, I hope 
it's beneficial to the javaflow project.

Once again, I'm not trying to convince you to change the existing 
Continuation class in any substantial way. I'm only asking you to put 
this Fiber class side-by-side with the current Continuation class.

Please please can I have this class? I promise I won't talk about the 
Continuation class signature change any more :-)

If not, I guess I just need to accumulate more small improvements to 
make you feel comfortable with me. It's disappointing for me for now, 
but that's certainly fair, I guess.

-- 
Kohsuke Kawaguchi

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [javaflow] MethodLookup

Posted by Torsten Curdt <tc...@apache.org>.
>> See the example in my blog ...which is actually
>> taken from the Cocoon integration. (I think in
>> there is also the link to the class) Would be
>> great if you could also have a look into that
>> class.
>>
>
> I looked at Cocoon code you had in [1]. I didn't particularly see  
> anything that made me rethink (IOW, I don't see anything that  
> absolutely requires MethodLookup nor ContinuationContext instead of  
> Runnable.)

Had a look into your implementation
...you have the "run" method delegating to
the different methods.

If you want to get rid of the of the method
lookup you do need to pass in the Runnable
also to the continueWith ...not sure if I
like that.

I had similar idea in the beginning:

  startWith(Method, ContinuationContext)
  continueWith(Method, Continuation, ContinuationContext)

What I did not like: Passing in of the
Continuation already implies the Method.

Beforehand the method was saved inside
the Continuation object. But this does
not work when it comes down to serialization.

BTW: IIRC I needed to mark the static as
transient for the XStream serialization
...but not totally sure.

> But at the same time, I saw that you have some existing investments  
> with the current javaflow Continuation API and it's very  
> understandable that you don't want it to change.

Well ...it's still marked alpha so as long
as it makes sense to me I am fine changing
it.

But TBH I currently would like to focus on
the current bytecode rewriting stuff first
before changing the API that much.

> I see the desire to pass some contextual information to the invoked  
> method, but that is a fairly common requirement even if you aren't  
> using javaflow at all. javaflow just keeps it and stores it  
> somewhere for later retrieval, which can be better done by the code  
> that knows what the scope of a given context object is (like  
> ThreadLocal, singleton, HttpSession, etc.)
>
>
> Just to make sure that I'm not missing something, I rewrote the  
> JavaInterpreter and CocoonContinuationContext by using the  
> ContinuationThread class (which I renamed to Fiber) instead of the  
> current Continuation API. The only difference is that you now do:
>
> CocoonContinuationContext.getCurrent()
>
> instead of:
>
> (CocoonContinuationContext)Continuation.currentContinuation 
> ().getContext()

Hm... it's shorter but I don't like it.
It's not obvious where the context comes
from.

I would -1 that one.

> See the attachments for the exact code. All in all, I didn't think  
> this re-write is ugly, but that is always a subjective issue.

I don't understand this forking and cloning
stuff - you don't need that. A continuation
should be re-entrant and always create a new
one as a result.

> Anyway, I'm no longer too keen about convincing you to change the  
> API of the Continuation class. If you like my rewrite, that's good,  
> but if not, that's also fine with me. The current Continuation API  
> makes the Fiber class implementation uglier, but that's not too big  
> a problem.
>
> Instead, I'm hoping that you allow me to commit this Fiber class to  
> javaflow, as this is what I primarily want to use in my code.

If you don't mind I would prefer if you keep
that code in your codebase for now. Let's get
back to that once we have the rewriting based
on ASM in place. I would like to focus on the
hard part first ;)

Would that be ok for you?

cheers
--
Torsten

Re: [javaflow] MethodLookup

Posted by Kohsuke Kawaguchi <ko...@sun.com>.
Torsten Curdt wrote:
>> If I understand this correctly, for me to capture the continuation and
>> run it later, I do:
> 
> Have a look into the serialization testcase

Thanks. I did some experiments. I think I have misunderstood the way the 
stack restoration works. It seems like it's actually replacing local 
variable #0 (the 'this' pointer.)

This is interesting. I didn't know that you can do this in Java (I guess 
you can do it in JVM, just not in Java language.)





>> (I'm assuming that you designed ContinuationContext to
>> keep track of things you want to change between runs, so it's not  
>> reachable from Continuation if it's not executing.)
> 
> Sorry, did not get that ..what do you mean?
> 
> The ContinuationContext holds all the references
> that are required to restore the state but cannot
> really be part of the continuation. Logger,
> ComponentManager and things like that.

Yes, I think we are saying the same thing. Pardon my poor English.


> See the example in my blog ...which is actually
> taken from the Cocoon integration. (I think in
> there is also the link to the class) Would be
> great if you could also have a look into that
> class.

I looked at Cocoon code you had in [1]. I didn't particularly see 
anything that made me rethink (IOW, I don't see anything that absolutely 
requires MethodLookup nor ContinuationContext instead of Runnable.)

But at the same time, I saw that you have some existing investments with 
the current javaflow Continuation API and it's very understandable that 
you don't want it to change.

I see the desire to pass some contextual information to the invoked 
method, but that is a fairly common requirement even if you aren't using 
javaflow at all. javaflow just keeps it and stores it somewhere for 
later retrieval, which can be better done by the code that knows what 
the scope of a given context object is (like ThreadLocal, singleton, 
HttpSession, etc.)


Just to make sure that I'm not missing something, I rewrote the 
JavaInterpreter and CocoonContinuationContext by using the 
ContinuationThread class (which I renamed to Fiber) instead of the 
current Continuation API. The only difference is that you now do:

CocoonContinuationContext.getCurrent()

instead of:

(CocoonContinuationContext)Continuation.currentContinuation().getContext()

See the attachments for the exact code. All in all, I didn't think this 
re-write is ugly, but that is always a subjective issue.


Anyway, I'm no longer too keen about convincing you to change the API of 
the Continuation class. If you like my rewrite, that's good, but if not, 
that's also fine with me. The current Continuation API makes the Fiber 
class implementation uglier, but that's not too big a problem.

Instead, I'm hoping that you allow me to commit this Fiber class to 
javaflow, as this is what I primarily want to use in my code.



[1] http://svn.apache.org/repos/asf/cocoon/blocks/javaflow/trunk/
-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@sun.com

Re: [javaflow] MethodLookup

Posted by Torsten Curdt <tc...@apache.org>.
> If I understand this correctly, for me to capture the continuation and
> run it later, I do:

Have a look into the serialization testcase

>     // serialize the continuation
>     ObjectOutputStream oos = ...;
>     oos.writeObject(c);
>     oos.writeObject(myRunnable);

Since the resume will only work with the
exact same class there is no point in
making the runnable serializable and
saving it.


>     c.continueWith(new DefaultContinuationContext(myRunnable));
>
> is that correct?


Correct - except for the serialization of the
runnable.

> (I'm assuming that you designed ContinuationContext to
> keep track of things you want to change between runs, so it's not  
> reachable from Continuation if it's not executing.)

Sorry, did not get that ..what do you mean?

The ContinuationContext holds all the references
that are required to restore the state but cannot
really be part of the continuation. Logger,
ComponentManager and things like that.

See the example in my blog ...which is actually
taken from the Cocoon integration. (I think in
there is also the link to the class) Would be
great if you could also have a look into that
class.

> I thought it would be nice if I can write it instead as:
>
>     Runnable myRunnable = new MyRunnable(...);
>     Continuation  c = Continuation.startWith(myRunnable,null);

Did understand that ...but that restricts the
API a bit too much. With the propose way we
can have the best of both worlds - I think.

> Or maybe what I really wanted was to the "ContinuableThread" class  
> or something that wraps the existing API into a class that feels  
> like java.lang.Thread, so that I can do:
>
>     ContinuableThread t = new ContinuableThread() {
>       // override run or pass in Runnable to the constructor
>       void run() { ...; suspend(); ...; }
>     }
>
>     t.start();    // start running. return when suspended
>
>     oos.writeObject(t);    // serialize
>     t = ois.readObject();    // deserialize
>     t = t.clone();        // AKA thread fork

Don't understand the cloning.

>     while(t.isAlive())
>         t.continue();

Hmmm... looks interesting but you would
need to poll the continuations. So you
would have to start the "thread" with
every resume ..that's a bit awkward.

cheers
--
Torsten

Re: [javaflow] MethodLookup

Posted by Kohsuke Kawaguchi <kk...@kohsuke.org>.
Thank you for a quick response and listening to my suggestion.


Torsten Curdt wrote:
> ..but how about the other way around. We could
> also provide
> 
>   Continuation startWith(ContinuationContext) // means: use method "run"
>   Continuation startWith(String, ContinuationContext)
> 
> with a standard ContinuationContext that explicitly
> supports a Runnable by providing a default implementation.
> So a bit simplified you could do:
> 
>    startWith(new DefaultContinuationContext(Runnable))
> 
> That would give us all the flexibility and the simplified
> interface
> 
> WDYT?

If I understand this correctly, for me to capture the continuation and
run it later, I do:

	Runnable myRunnable = new MyRunnable(...);
	Continuation  c = Continuation.startWith(
	  new DefaultContinuationContext(myRunnable))

	// serialize the continuation
	ObjectOutputStream oos = ...;
	oos.writeObject(c);
	oos.writeObject(myRunnable);

/* later */
	ObjectInputStream ois = ...;
	Continuation c = ois.readObject();
	Runnable myRunnable = ois.readObject();

	c.continueWith(new DefaultContinuationContext(myRunnable));

is that correct? (I'm assuming that you designed ContinuationContext to
keep track of things you want to change between runs, so it's not 
reachable from Continuation if it's not executing.)

I thought it would be nice if I can write it instead as:

	Runnable myRunnable = new MyRunnable(...);
	Continuation  c = Continuation.startWith(myRunnable,null);

	// serialize the continuation
	ObjectOutputStream oos = ...;
	oos.writeObject(c);

/* later */
	ObjectInputStream ois = ...;
	Continuation c = ois.readObject();

	c.continueWith(null);


Or maybe what I really wanted was to the "ContinuableThread" class or 
something that wraps the existing API into a class that feels like 
java.lang.Thread, so that I can do:

	ContinuableThread t = new ContinuableThread() {
	  // override run or pass in Runnable to the constructor
	  void run() { ...; suspend(); ...; }
	}

	t.start();	// start running. return when suspended

	oos.writeObject(t);	// serialize
	t = ois.readObject();	// deserialize
	t = t.clone();		// AKA thread fork

	while(t.isAlive())
		t.continue();

If I wanted to do the equivalent of the ContinuationContext, I can
define a field in my derived class and set if every time before I call 
continue.

I can implement this on top of existing javaflow code without changing
them. Does this work with you?

-- 
Kohsuke Kawaguchi

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [javaflow] MethodLookup

Posted by Torsten Curdt <tc...@apache.org>.
On 27.07.2005, at 07:11, Kohsuke Kawaguchi wrote:

> I'm bit confused with MethodLookup. I could be just missing  
> something, but here it goes...
>

The MethodLookup was introduced when I added the
serialization support. Beforehand the Method was
passed in directly. But now the Method that is
stored inside the Continuation is marked transient.
After the resume the Method is null and must be
looked up.

> I wonder if it would be better if the startWith method is:
>
> Continuation startWith( Runnable target, ContinuationContext context )

I've been thinking about that, too ...but for the
use inside Cocoon I don't really want to restrict
it to the "void run()" method only.

Besides for the serialization support we would
need to pass in the Runnable/Method also for
"continueWith(Runnable/Method, Continuation)"

IIRC there were even more reasons ...will try
to remember :)

> I think developers relates to Runnable more easily, as  
> java.lang.Thread uses it. I also think there isn't any loss in the  
> expressiveness of the library, as we can emulate the old  
> functionality by using a Runnable that looks up a method name from  
> String and create a new instance.

Uh... that sounds ugly :)

...but how about the other way around. We could
also provide

  Continuation startWith(ContinuationContext) // means: use method "run"
  Continuation startWith(String, ContinuationContext)

with a standard ContinuationContext that explicitly
supports a Runnable by providing a default implementation.
So a bit simplified you could do:

   startWith(new DefaultContinuationContext(Runnable))

That would give us all the flexibility and the simplified
interface

WDYT?


> It also has some other minor benefits, like simplifying the  
> implementation and improving the performance by avoiding reflection.

As the reflection calls are cached the performance impact
is really minor.

cheers
--
Torsten