Skip to content

Commit aab005b

Browse files
ludochgae-java-bot
authored andcommitted
Modernize JavaRuntimeParams and SessionsConfig to Java 17 records.
PiperOrigin-RevId: 873101732 Change-Id: Ib4c034a414bb1ae08e7c16bbd60db4128ffb7b04
1 parent 774386a commit aab005b

13 files changed

Lines changed: 111 additions & 133 deletions

File tree

runtime/impl/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
<artifactId>google-extensions</artifactId>
8787
<optional>true</optional>
8888
</dependency>
89+
<dependency>
90+
<groupId>com.google.code.findbugs</groupId>
91+
<artifactId>jsr305</artifactId>
92+
</dependency>
8993
<dependency>
9094
<groupId>com.google.guava</groupId>
9195
<artifactId>guava</artifactId>

runtime/impl/src/main/java/com/google/apphosting/base/AppVersionKey.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.apphosting.base.protos.AppinfoPb.AppInfo;
2020
import com.google.apphosting.base.protos.RuntimePb.UPRequest;
21-
import com.google.auto.value.AutoValue;
21+
import javax.annotation.concurrent.Immutable;
2222

2323
/**
2424
* A simple immutable data container class that identifies a single
@@ -28,8 +28,8 @@
2828
* AppInfo and UPRequests.
2929
*
3030
*/
31-
@AutoValue
32-
public abstract class AppVersionKey {
31+
@Immutable
32+
public record AppVersionKey(String appId, String versionId) {
3333
public static AppVersionKey fromAppInfo(AppInfo appInfo) {
3434
return of(appInfo.getAppId(), appInfo.getVersionId());
3535
}
@@ -39,15 +39,19 @@ public static AppVersionKey fromUpRequest(UPRequest request) {
3939
}
4040

4141
public static AppVersionKey of(String appId, String versionId) {
42-
return new AutoValue_AppVersionKey(appId, versionId);
42+
return new AppVersionKey(appId, versionId);
4343
}
4444

45-
public abstract String getAppId();
45+
public String getAppId() {
46+
return appId();
47+
}
4648

47-
public abstract String getVersionId();
49+
public String getVersionId() {
50+
return versionId();
51+
}
4852

4953
@Override
5054
public final String toString() {
51-
return getAppId() + "/" + getVersionId();
55+
return appId() + "/" + versionId();
5256
}
5357
}

runtime/impl/src/main/java/com/google/apphosting/base/VersionId.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616

1717
package com.google.apphosting.base;
1818

19-
import com.google.auto.value.AutoValue;
2019
import java.util.regex.Matcher;
2120
import java.util.regex.Pattern;
2221
import org.jspecify.annotations.Nullable;
2322

2423
/**
2524
* A parsed Version Id.
26-
*
2725
*/
28-
@AutoValue
29-
public abstract class VersionId {
26+
public record VersionId(
27+
@Nullable String versionId,
28+
String majorVersion,
29+
String engineId,
30+
String engineVersionId,
31+
@Nullable String minorVersion) {
32+
3033
// These must be kept in sync with the corresponding constants in
3134
// apphosting/base/constants.h
3235
public static final String DEFAULT_ENGINE_ID = "default";
@@ -68,38 +71,42 @@ private static VersionId from(String versionId) {
6871
throw new IllegalArgumentException("Malformed versionId: " + versionId);
6972
}
7073
String engineId = matcher.group(2) == null ? DEFAULT_ENGINE_ID : matcher.group(2);
71-
return new AutoValue_VersionId(
74+
return new VersionId(
7275
versionId, matcher.group(1), engineId, matcher.group(3), matcher.group(5));
7376
}
7477

75-
/**
76-
* Returns the versionId.
77-
*/
78-
@Nullable
79-
public abstract String getVersionId();
78+
public static VersionId parse(String versionId) {
79+
return from(versionId);
80+
}
81+
82+
/** Returns the versionId. */
83+
public @Nullable String getVersionId() {
84+
return versionId;
85+
}
8086

8187
/**
8288
* Returns the majorVersion.
8389
*/
84-
public abstract String getMajorVersion();
90+
public String getMajorVersion() {
91+
return majorVersion;
92+
}
8593

8694
/**
8795
* Returns the serverId.
8896
*/
89-
public abstract String getEngineId();
97+
public String getEngineId() {
98+
return engineId;
99+
}
90100

91101
/**
92102
* Returns the server version id.
93103
*/
94-
public abstract String getEngineVersionId();
95-
96-
/**
97-
* Returns the minorVersion or {@code null} if no minor version was present.
98-
*/
99-
@Nullable
100-
public abstract String getMinorVersion();
104+
public String getEngineVersionId() {
105+
return engineVersionId;
106+
}
101107

102-
public static VersionId parse(String versionId) {
103-
return from(versionId);
108+
/** Returns the minorVersion or {@code null} if no minor version was present. */
109+
public @Nullable String getMinorVersion() {
110+
return minorVersion;
104111
}
105112
}

runtime/impl/src/main/java/com/google/apphosting/runtime/JavaRuntimeFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void startRuntime(NullSandboxPlugin sandboxPlugin, String[] args)
5959

6060
public JavaRuntime getStartedRuntime(NullSandboxPlugin sandboxPlugin, String[] args) {
6161
JavaRuntimeParams params = JavaRuntimeParams.parseArgs(args);
62-
List<String> unknownParams = params.getUnknownParams();
62+
List<String> unknownParams = params.unknownParams();
6363
if (!unknownParams.isEmpty()) {
6464
logger.atWarning().log("Unknown command line arguments: %s", unknownParams);
6565
}
@@ -110,7 +110,7 @@ public JavaRuntime getStartedRuntime(NullSandboxPlugin sandboxPlugin, String[] a
110110
ApiProxyImpl.builder()
111111
.setApiHost(
112112
apiHostFactory.newAPIHost(
113-
params.getTrustedHost(), OptionalInt.of(params.getMaxOutstandingApiRpcs())))
113+
params.trustedHost(), OptionalInt.of(params.maxOutstandingApiRpcs())))
114114
.setDeadlineOracle(deadlineOracle)
115115
.setExternalDatacenterName("MARS")
116116
.setByteCountBeforeFlushing(BYTE_COUNT_BEFORE_FLUSHING)
@@ -125,7 +125,7 @@ public JavaRuntime getStartedRuntime(NullSandboxPlugin sandboxPlugin, String[] a
125125
.setSoftDeadlineDelay(SOFT_DEADLINE_DELAY_MS)
126126
.setRuntimeLogSink(Optional.of(logSink))
127127
.setApiProxyImpl(apiProxyImpl)
128-
.setMaxOutstandingApiRpcs(params.getMaxOutstandingApiRpcs())
128+
.setMaxOutstandingApiRpcs(params.maxOutstandingApiRpcs())
129129
.setThreadStopTerminatesClone(THREAD_STOP_TERMINATES_CLONE)
130130
.setCyclesPerSecond(CYCLES_PER_SECOND);
131131

@@ -146,16 +146,16 @@ public JavaRuntime getStartedRuntime(NullSandboxPlugin sandboxPlugin, String[] a
146146
.setDeadlineOracle(deadlineOracle)
147147
.setCoordinator(coordinator)
148148
.setForceUrlfetchUrlStreamHandler(FORCE_URLFETCH_URL_STREAM_HANDLER)
149-
.setFixedApplicationPath(params.getFixedApplicationPath());
149+
.setFixedApplicationPath(params.fixedApplicationPath());
150150

151151
JavaRuntime runtime = makeRuntime(runtimeBuilder);
152152

153153
ApiProxy.setDelegate(apiProxyImpl);
154154
ServletEngineAdapter.Config runtimeOptions =
155155
ServletEngineAdapter.Config.builder()
156156
.setApplicationRoot("notused")
157-
.setFixedApplicationPath(params.getFixedApplicationPath())
158-
.setJettyHttpAddress(HostAndPort.fromParts("0.0.0.0", params.getJettyHttpPort()))
157+
.setFixedApplicationPath(params.fixedApplicationPath())
158+
.setJettyHttpAddress(HostAndPort.fromParts("0.0.0.0", params.jettyHttpPort()))
159159
.setJettyRequestHeaderSize(JETTY_REQUEST_HEADER_SIZE)
160160
.setJettyResponseHeaderSize(JETTY_RESPONSE_HEADER_SIZE)
161161
.setEvaluationRuntimeServerInterface(runtime)

runtime/impl/src/main/java/com/google/apphosting/runtime/JavaRuntimeParams.java

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,12 @@
2020
import java.util.List;
2121

2222
/** Command line parameters for Java runtime, and its dependencies. */
23-
final class JavaRuntimeParams {
24-
25-
/** Default trusted host is empty string. */
26-
private String trustedHost = "";
27-
28-
/** Default application path is null. */
29-
private String fixedApplicationPath = null;
30-
31-
/** Default Jetty HTTP port is 8080. */
32-
private int jettyHttpPort = 8080;
33-
34-
/** Default value from {@link AppEngineConstants#DEFAULT_MAX_OUTSTANDING_API_RPCS}. */
35-
private int maxOutstandingApiRpcs = AppEngineConstants.DEFAULT_MAX_OUTSTANDING_API_RPCS;
36-
37-
private List<String> unknownParams;
38-
39-
private JavaRuntimeParams() {}
23+
record JavaRuntimeParams(
24+
String trustedHost,
25+
String fixedApplicationPath,
26+
int jettyHttpPort,
27+
int maxOutstandingApiRpcs,
28+
List<String> unknownParams) {
4029

4130
/**
4231
* Creates {@code JavaRuntimeParams} from command line arguments.
@@ -57,8 +46,12 @@ private JavaRuntimeParams() {}
5746
* @return an instance of {@code JavaRuntimeParams} with parsed values from command line
5847
*/
5948
public static JavaRuntimeParams parseArgs(String... args) {
60-
JavaRuntimeParams params = new JavaRuntimeParams();
61-
params.unknownParams = new ArrayList<>();
49+
String trustedHost = "";
50+
String fixedApplicationPath = null;
51+
int jettyHttpPort = 8080;
52+
int maxOutstandingApiRpcs = AppEngineConstants.DEFAULT_MAX_OUTSTANDING_API_RPCS;
53+
List<String> unknownParams = new ArrayList<>();
54+
6255
for (String arg : args) {
6356
String key = arg;
6457
String value = null;
@@ -70,22 +63,22 @@ public static JavaRuntimeParams parseArgs(String... args) {
7063
switch (key) {
7164
case "--trusted_host" -> {
7265
if (value != null) {
73-
params.trustedHost = value;
66+
trustedHost = value;
7467
} else {
7568
throw new IllegalArgumentException("Missing value for " + arg);
7669
}
7770
}
7871
case "--fixed_application_path" -> {
7972
if (value != null) {
80-
params.fixedApplicationPath = value;
73+
fixedApplicationPath = value;
8174
} else {
8275
throw new IllegalArgumentException("Missing value for " + arg);
8376
}
8477
}
8578
case "--jetty_http_port" -> {
8679
if (value != null) {
8780
try {
88-
params.jettyHttpPort = Integer.parseInt(value);
81+
jettyHttpPort = Integer.parseInt(value);
8982
} catch (NumberFormatException e) {
9083
// If the value is not a valid integer throw an exception.
9184
throw new IllegalArgumentException("Invalid value for " + arg, e);
@@ -97,7 +90,7 @@ public static JavaRuntimeParams parseArgs(String... args) {
9790
case "--clone_max_outstanding_api_rpcs" -> {
9891
if (value != null) {
9992
try {
100-
params.maxOutstandingApiRpcs = Integer.parseInt(value);
93+
maxOutstandingApiRpcs = Integer.parseInt(value);
10194
} catch (NumberFormatException e) {
10295
// If the value is not a valid integer throw an exception.
10396
throw new IllegalArgumentException("Invalid value for " + arg, e);
@@ -106,36 +99,10 @@ public static JavaRuntimeParams parseArgs(String... args) {
10699
throw new IllegalArgumentException("Missing value for " + arg);
107100
}
108101
}
109-
default -> params.unknownParams.add(arg);
102+
default -> unknownParams.add(arg);
110103
}
111104
}
112-
return params;
113-
}
114-
115-
/** Specification used for connecting back to the appserver. */
116-
String getTrustedHost() {
117-
return trustedHost;
118-
}
119-
120-
/** Jetty HTTP Port number to use for http access to the runtime. */
121-
int getJettyHttpPort() {
122-
return jettyHttpPort;
123-
}
124-
125-
/** Maximum number of outstanding API RPCs. */
126-
int getMaxOutstandingApiRpcs() {
127-
return maxOutstandingApiRpcs;
128-
}
129-
130-
/**
131-
* Fixed path to use for the application root directory, irrespective of the application id and
132-
* version. Ignored if empty.
133-
*/
134-
String getFixedApplicationPath() {
135-
return fixedApplicationPath;
136-
}
137-
138-
List<String> getUnknownParams() {
139-
return unknownParams;
105+
return new JavaRuntimeParams(
106+
trustedHost, fixedApplicationPath, jettyHttpPort, maxOutstandingApiRpcs, unknownParams);
140107
}
141108
}

runtime/lite/src/test/java/com/google/appengine/runtime/lite/AppEngineRuntimeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,8 @@ public void createAppVersion_noLegacy() throws Exception {
978978
assertThat(appEnv.getUseGoogleConnectorJ()).isTrue();
979979

980980
SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
981-
assertThat(sessionsConfig.isEnabled()).isFalse();
982-
assertThat(sessionsConfig.isAsyncPersistence()).isFalse();
983-
assertThat(sessionsConfig.getAsyncPersistenceQueueName()).isNull();
981+
assertThat(sessionsConfig.enabled()).isFalse();
982+
assertThat(sessionsConfig.asyncPersistence()).isFalse();
983+
assertThat(sessionsConfig.asyncPersistenceQueueName()).isNull();
984984
}
985985
}

runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/EE10AppVersionHandlerFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ private org.eclipse.jetty.server.Handler doCreateHandler(AppVersion appVersion)
170170

171171
SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
172172
EE10SessionManagerHandler.Config.Builder builder = EE10SessionManagerHandler.Config.builder();
173-
if (sessionsConfig.getAsyncPersistenceQueueName() != null) {
174-
builder.setAsyncPersistenceQueueName(sessionsConfig.getAsyncPersistenceQueueName());
173+
if (sessionsConfig.asyncPersistenceQueueName() != null) {
174+
builder.setAsyncPersistenceQueueName(sessionsConfig.asyncPersistenceQueueName());
175175
}
176176
builder
177-
.setEnableSession(sessionsConfig.isEnabled())
178-
.setAsyncPersistence(sessionsConfig.isAsyncPersistence())
177+
.setEnableSession(sessionsConfig.enabled())
178+
.setAsyncPersistence(sessionsConfig.asyncPersistence())
179179
.setServletContextHandler(context);
180180
EE10SessionManagerHandler.create(builder.build());
181181
// Pass the AppVersion on to any of our servlets (e.g. ResourceFileServlet).

runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee8/EE8AppVersionHandlerFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ private org.eclipse.jetty.server.Handler doCreateHandler(AppVersion appVersion)
189189

190190
SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
191191
SessionManagerHandler.Config.Builder builder = SessionManagerHandler.Config.builder();
192-
if (sessionsConfig.getAsyncPersistenceQueueName() != null) {
193-
builder.setAsyncPersistenceQueueName(sessionsConfig.getAsyncPersistenceQueueName());
192+
if (sessionsConfig.asyncPersistenceQueueName() != null) {
193+
builder.setAsyncPersistenceQueueName(sessionsConfig.asyncPersistenceQueueName());
194194
}
195195
builder
196-
.setEnableSession(sessionsConfig.isEnabled())
197-
.setAsyncPersistence(sessionsConfig.isAsyncPersistence())
196+
.setEnableSession(sessionsConfig.enabled())
197+
.setAsyncPersistence(sessionsConfig.asyncPersistence())
198198
.setServletContextHandler(context);
199199

200200
SessionManagerHandler.create(builder.build());

runtime/runtime_impl_jetty121/src/main/java/com/google/apphosting/runtime/jetty/ee11/EE11AppVersionHandlerFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ private org.eclipse.jetty.server.Handler doCreateHandler(AppVersion appVersion)
152152
context.setThrowUnavailableOnStartupException(true);
153153
SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
154154
EE11SessionManagerHandler.Config.Builder builder = EE11SessionManagerHandler.Config.builder();
155-
if (sessionsConfig.getAsyncPersistenceQueueName() != null) {
156-
builder.setAsyncPersistenceQueueName(sessionsConfig.getAsyncPersistenceQueueName());
155+
if (sessionsConfig.asyncPersistenceQueueName() != null) {
156+
builder.setAsyncPersistenceQueueName(sessionsConfig.asyncPersistenceQueueName());
157157
}
158158
builder
159-
.setEnableSession(sessionsConfig.isEnabled())
160-
.setAsyncPersistence(sessionsConfig.isAsyncPersistence())
159+
.setEnableSession(sessionsConfig.enabled())
160+
.setAsyncPersistence(sessionsConfig.asyncPersistence())
161161
.setServletContextHandler(context);
162162
EE11SessionManagerHandler.create(builder.build());
163163
// Pass the AppVersion on to any of our servlets (e.g. ResourceFileServlet).

runtime/runtime_impl_jetty121/src/main/java/com/google/apphosting/runtime/jetty/ee8/EE8AppVersionHandlerFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ private org.eclipse.jetty.server.Handler doCreateHandler(AppVersion appVersion)
190190

191191
SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
192192
SessionManagerHandler.Config.Builder builder = SessionManagerHandler.Config.builder();
193-
if (sessionsConfig.getAsyncPersistenceQueueName() != null) {
194-
builder.setAsyncPersistenceQueueName(sessionsConfig.getAsyncPersistenceQueueName());
193+
if (sessionsConfig.asyncPersistenceQueueName() != null) {
194+
builder.setAsyncPersistenceQueueName(sessionsConfig.asyncPersistenceQueueName());
195195
}
196196
builder
197-
.setEnableSession(sessionsConfig.isEnabled())
198-
.setAsyncPersistence(sessionsConfig.isAsyncPersistence())
197+
.setEnableSession(sessionsConfig.enabled())
198+
.setAsyncPersistence(sessionsConfig.asyncPersistence())
199199
.setServletContextHandler(context);
200200

201201
var unused = SessionManagerHandler.create(builder.build());

0 commit comments

Comments
 (0)