-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Add GCF Java Tips #1550
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
feat: Add GCF Java Tips #1550
Conversation
Looks like I need to fix some linting issues:
|
// [START run_tips_global_scope] | ||
// Global (instance-wide) scope | ||
// This computation runs at instance cold-start | ||
public static final int INSTANCE_VAR = heavyComputation(); |
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.
Class variables should be declared at the top of the class.
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.
Also, this might not be the way we want to showcase this. Normally, global variables aren't used with servlets due to issues with thread-safety. This example is probably okay because it's a final variable, but we probably either want to add a warning or (better yet) update the example to us a ServletContextListener
for creating start up resources.
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.
Moved to the very top. Not sure if we need the context listener.
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 should be updated to either use a ContextListener, or include a warning that global values in Servlets are not threadsafe, and a ContextListener
is preferred for mutable variables.
I'm not really sure if/how a ContextListener would work with functions, so this snippet might be a good example to showcase that pattern as well.
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 don't really know how to proceed. Do you think that GCF will use multiple threads per Function invocation?
This example is probably okay because it's a final variable
Since this is a final
variable, isn't it safe to access across multiple threads?
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.
Most servlet environments will use multiple threads to handle requests, so
Do you think that GCF will use multiple threads per Function invocation?
I don't know how GCF handles things, but it would be idiomatic to Java to remain threadsafe when using Servlets since other runtimes may use multiple threads.
Since this is a final variable, isn't it safe to access across multiple threads?
I should have been more clear - since this is a final
and a primitive it's immutable, and thus thread-safe. However, we should either point out why this is okay, or show the user how to handle these properly. If the user tried to do this with an object that isn't thread-safe (even if it's final), they'll run into issues.
private static HttpClient client = | ||
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); | ||
|
||
// [END functions_tips_scopes] |
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.
Excluding these functions breaks one of the goals of the new sample format, in that the sample isn't copy-paste-runnable. Can you leave them in (but maybe move them towards the bottom of the class since they aren't important?)
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.
Sure.
I was just following the other samples that exclude them.
Can include them at the bottom.
return numbers.stream().reduce((t, x) -> t + x).get(); | ||
} | ||
|
||
private static int heavyComputation() { |
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.
These functions are exactly the same - is this intended?
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, fixed.
// [END run_tips_global_scope] | ||
// [END functions_tips_scopes] | ||
|
||
public void TipsRetry(HttpServletRequest request, HttpServletResponse response) |
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 what this function is doing - it should be in a different file if it's for a different snippet
HttpRequest getRequest = HttpRequest.newBuilder().uri(URI.create(url)).GET().build(); | ||
|
||
// Send the TipsRetry using the client | ||
HttpResponse<String> getResponse = client.send(getRequest, BodyHandlers.ofString()); |
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.
What does TipsRetry mean? This seems like just a simple get request?
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.
This PR should be good to go.
Adds this 1 snippet:
https://cloud.google.com/functions/docs/bestpractices/tips#use_dependencies_wisely
private static HttpClient client = | ||
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); | ||
|
||
// [END functions_tips_scopes] |
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.
Sure.
I was just following the other samples that exclude them.
Can include them at the bottom.
return numbers.stream().reduce((t, x) -> t + x).get(); | ||
} | ||
|
||
private static int heavyComputation() { |
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, fixed.
// [START run_tips_global_scope] | ||
// Global (instance-wide) scope | ||
// This computation runs at instance cold-start | ||
public static final int INSTANCE_VAR = heavyComputation(); |
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.
Moved to the very top. Not sure if we need the context listener.
// [END functions_tips_scopes] | ||
// [END run_tips_global_scope] | ||
|
||
private static int lightComputation() { |
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.
These functions are still excluded, which means that this snippet isn't copy-paste-runnable (see the Sample Format Guidelines). Older samples might not be following the format, but we are trying to keep all new samples following the guidelines.
Also, both functions are exactly the same, which makes it a bit confusing to understand when reading. Perhaps it would be better to replace this with something like this:
// This function represents a heavy computation, which is only called when the instance starts.
private static int heavyComputation() {
TimeUnit.SECONDS.sleep(3);
return System.currentTimeMillis();
}
// This function represents a light computation, which might be called on every incoming request.
private static int lightComputation() {
TimeUnit.SECONDS.sleep(0);
return System.currentTimeMillis();
}
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.
- These functions are excluded like they are in the Node sample.
Also, both functions are exactly the same
- These functions are not exactly the same. They are modeled after Node.
What would you like us to do?
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.
- These functions are excluded like they are in the Node sample.
I believe that the node repo has a similar policy regarding copy-paste-runnable (since it's borrowed from the overarching Code Snippets Rubric). It looks like these samples probably predate this policy, but I don't know how the node repo is enforcing those rules.
Currently, the java-docs-samples policy is that new samples should follow the guidelines, while minor updates to existing samples can be grandfathered in. Since these samples are new, they need to follow the guidelines and include these functions so that users can copy them, paste them, and run them without having to fill out these functions.
- These functions are not exactly the same.
This is my bad, the version I was looking at shows them both using addition, but I can see at HEAD they are different.
At the very least, this version of the sample should contain the comments I posted above. (My two cents is still that the intent of the sample is significantly more clear when using sleep, because the user can see that the difference between running those functions is non-trivial. However if you strongly prefer to remain consistent with the Node version, I'm satisfied with the comments clarifying above)
// [START run_tips_global_scope] | ||
// Global (instance-wide) scope | ||
// This computation runs at instance cold-start | ||
public static final int INSTANCE_VAR = heavyComputation(); |
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 should be updated to either use a ContextListener, or include a warning that global values in Servlets are not threadsafe, and a ContextListener
is preferred for mutable variables.
I'm not really sure if/how a ContextListener would work with functions, so this snippet might be a good example to showcase that pattern as well.
Fixed with #1586 instead. |
Adds GCF Tips snippets:
https://cloud.google.com/functions/docs/bestpractices/tips