-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial context API #562
Initial context API #562
Conversation
* context which is already closed. | ||
* | ||
* @throws java.lang.IllegalStateException if this context is not current. | ||
* @return the current context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the current context" means "this" object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'current' means the context currently available from thread local (or root if there is nothing on the thread local)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to specify whether that is before or after the detach. If it is before, then that is 'this' object.
When would you expect to use the returned Context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It made some of the tests more succinct. E.g.
assertSame(next, root.detach());
but I could take or leave it.
@louiscryan, I think it would be useful to have some very simple examples of how an application would use the Context. |
* | ||
* @param <T> | ||
*/ | ||
public static class Key<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are having get()
on this class, maybe rename it to "Field", and "name" arguments to "key"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 key
does seem strange here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find Field more strange than Key. I have little problem with Key, although I have said that the current get()
means that if you aren't fully aware of its type you don't realize it is accessing the Context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except that it's a Key
that has a key and a value which is rather odd. If Context is essentially a map of names to values, then perhaps we should just follow the Java map naming conventions rather than inventing our own terminology. Entry
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Entry is also strange to me.
So we can treat that as one more reason to not have get(). Or maybe we should name get() something else, like lookup().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that the name of the method is the problem. The naming would even be worse for a method name lookup
that returns a Key
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??
get() returns T. lookup() would also return T. lookup() would lookup the value for the Key based on the ThreadLocal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I think I see my confusion ... Key
has a defaultValue
, not the value itself. I'm fine with the name Key
in that case. Sorry for the noise :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to lookup()
over get()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed it to lookup() though it's worth pointing out that 'get' was not going to be part of the public interface.
PTAL |
* Provide an AutoCloseable (via Closeable) for use with try-with-resource blocks that will | ||
* close a context and detach it. | ||
*/ | ||
public class ContextCloseable implements Closeable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe CloseableContext
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nm ... this isn't a context.
OK. I've now implemented the API and improved tests where there gaps. PTAL |
* notified before listeners on the children. | ||
* | ||
*/ | ||
public class Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a discussion point: do we want this exposed at the top-level of the API in io.grpc
? It doesn't seem like something that all users would necessarily use and isn't really part of the API in general. Would it make more sense in a subpackage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one issue is that the functionality here isn't really specific to GRPC, at some point it could easily move into it's own thing. Given that and the fact that the package would only have one or two classes in it I'm not sure it makes much of a difference either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, fair enough. I said that realizing that there would be nothing else in a context
package or module, which would seem rather odd. We can shelve this for now and defer to some later conversation on how to clearly identify what classes and interface compose our "public" API.
@Override | ||
public ScheduledExecutorService create() { | ||
return Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder() | ||
.setNameFormat(name + "-%d").build()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setDaemon(true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} | ||
|
||
/** | ||
* Return the cause of the cancellation or {@code null} if context was cancelled without a cause. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define behavior if context not closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
This is very different conceptually from the current way context-ish things are done in the C++/C side of gRPC. On that side, I see different context classes for client and server, and several first class components (deadline, auth context, and census context (which will include tracing context, tags etc.); the latter two are at least partially opaque), plus arbitrary key/value metadata pairs. Contexts are not immutable. Is there a desire to have the API's be similar in API/usage, or is this a non-goal? |
I think the design tradeoffs for Java are different than those for C/C++
On Wed, Jul 15, 2015 at 1:22 PM, Alistair Veitch [email protected]
|
Given that we need to make some forward progress on context binding within GRPC itself I'll be checking this in soon. The GRPC APIs are not frozen yet so there's plenty of scope for change and feedback so I encourage folks to get their hands dirty with it. |
nice work
|
…mount of state through the call stack and across thread boundaries. Strongly modeled after the Go context API https://blog.golang.org/context with support for - cancellation propagation & cancellation listeners - typed value binding - timeout/deadline The major difference with Go is that ThreadLocal is used for propagation instead of parameter passing as this is considered more idiomatic for Java.
Merged as f612116 |
@jhump @ejona86 @nmittler
This is an initial skeleton for the proposed Context API, lets use this PR to iterate on what we want.
Here are some general goals....
Fixes #262