Skip to content

[Functions] Add lazy global snippet #1611

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

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions functions/snippets/src/main/java/Lazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_tips_lazy_globals]
// [START run_tips_global_lazy]

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Lazy {
// Always initialized (at cold-start)
// Warning: Class variables used in Servlet classes must be thread-safe,
// or else might introduce race conditions in your code.
private static final int nonLazyGlobal = fileWideComputation();
// Declared at cold-start, but only initialized if/when the function executes
private static Integer lazyGlobal = null;

public void lazyGlobal(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// This value is initialized only if (and when) the function is called
if (lazyGlobal == null) {
lazyGlobal = functionSpecificComputation();
}

PrintWriter writer = response.getWriter();
writer.write(String.format("Lazy global: %s; non-lazy global: %s", lazyGlobal, nonLazyGlobal));
}

private static int functionSpecificComputation() {
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
return Arrays.stream(numbers).sum();
}

private static int fileWideComputation() {
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
}
}
// [END run_tips_global_lazy]
// [END functions_tips_lazy_globals]
9 changes: 5 additions & 4 deletions functions/snippets/src/main/java/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

// [START functions_tips_scopes]
// [START run_tips_global_scope]

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -26,7 +27,7 @@ public class Scopes {
// Global (instance-wide) scope
// This computation runs at instance cold-start.
// Warning: Class variables used in Servlet classes must be thread-safe,
// or else might introduce race conditions in your code.
// or else might introduce race conditions in your code.
private static final int InstanceVar = heavyComputation();

public void scopeDemo(HttpServletRequest request, HttpServletResponse response)
Expand All @@ -38,16 +39,16 @@ public void scopeDemo(HttpServletRequest request, HttpServletResponse response)
PrintWriter writer = response.getWriter();
writer.write(String.format("Instance: %s; function: %s", InstanceVar, functionVar));
}

private static int lightComputation() {
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
return Arrays.stream(numbers).sum();
}

private static int heavyComputation() {
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
}
}

// [END run_tips_global_scope]
// [END functions_tips_scopes]
11 changes: 9 additions & 2 deletions functions/snippets/src/test/java/SnippetsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public void scopesTest() throws IOException {
assertThat(responseOut.toString(), containsString("Instance:"));
}

@Test
public void lazyTest() throws IOException {
new Lazy().lazyGlobal(request, response);

assertThat(responseOut.toString(), containsString("Lazy global:"));
}

@Test
public void retrieveLogsTest() throws IOException {
new RetrieveLogs().retrieveLogs(request, response);
Expand All @@ -177,7 +184,7 @@ public void helloBackgroundTest() throws IOException {
when(request.getReader()).thenReturn(bodyReader);

new HelloBackground().helloBackground(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
assertThat(responseOut.toString(), containsString("Hello John!"));
}

@Test
Expand All @@ -192,7 +199,7 @@ public void envTest() throws IOException {
new EnvVars().envVar(request, response);
assertThat(responseOut.toString(), containsString("BAR"));
}

@Test
public void helloExecutionCount() throws IOException {
new Concepts().executionCount(request, response);
Expand Down