Skip to content

Commit 31eb3bc

Browse files
OmniLab Teamcopybara-github
authored andcommitted
This CL implements the StopLabServer backend API for FE v6.
PiperOrigin-RevId: 925254824
1 parent 448d3b9 commit 31eb3bc

13 files changed

Lines changed: 476 additions & 15 deletions

src/java/com/google/devtools/mobileharness/fe/v6/service/host/HostServiceLogicImpl.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.PreflightLabServerReleaseHandler;
2929
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.ReleaseLabServerHandler;
3030
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.RemoteControlDevicesHandler;
31+
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.RestartLabServerHandler;
32+
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.StartLabServerHandler;
33+
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.StopLabServerHandler;
3134
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.UpdatePassThroughFlagsHandler;
3235
import com.google.devtools.mobileharness.fe.v6.service.proto.host.CheckRemoteControlEligibilityRequest;
3336
import com.google.devtools.mobileharness.fe.v6.service.proto.host.CheckRemoteControlEligibilityResponse;
@@ -81,6 +84,9 @@ public final class HostServiceLogicImpl implements HostServiceLogic {
8184
private final DecommissionMissingDevicesHandler decommissionMissingDevicesHandler;
8285
private final PreflightLabServerReleaseHandler preflightLabServerReleaseHandler;
8386
private final ReleaseLabServerHandler releaseLabServerHandler;
87+
private final StartLabServerHandler startLabServerHandler;
88+
private final RestartLabServerHandler restartLabServerHandler;
89+
private final StopLabServerHandler stopLabServerHandler;
8490
private final UpdatePassThroughFlagsHandler updatePassThroughFlagsHandler;
8591
private final UniverseFactory universeFactory;
8692

@@ -94,6 +100,9 @@ public final class HostServiceLogicImpl implements HostServiceLogic {
94100
DecommissionMissingDevicesHandler decommissionMissingDevicesHandler,
95101
PreflightLabServerReleaseHandler preflightLabServerReleaseHandler,
96102
ReleaseLabServerHandler releaseLabServerHandler,
103+
StartLabServerHandler startLabServerHandler,
104+
RestartLabServerHandler restartLabServerHandler,
105+
StopLabServerHandler stopLabServerHandler,
97106
UpdatePassThroughFlagsHandler updatePassThroughFlagsHandler,
98107
UniverseFactory universeFactory) {
99108
this.getHostOverviewHandler = getHostOverviewHandler;
@@ -104,6 +113,9 @@ public final class HostServiceLogicImpl implements HostServiceLogic {
104113
this.decommissionMissingDevicesHandler = decommissionMissingDevicesHandler;
105114
this.preflightLabServerReleaseHandler = preflightLabServerReleaseHandler;
106115
this.releaseLabServerHandler = releaseLabServerHandler;
116+
this.startLabServerHandler = startLabServerHandler;
117+
this.restartLabServerHandler = restartLabServerHandler;
118+
this.stopLabServerHandler = stopLabServerHandler;
107119
this.updatePassThroughFlagsHandler = updatePassThroughFlagsHandler;
108120
this.universeFactory = universeFactory;
109121
}
@@ -247,30 +259,36 @@ public ListenableFuture<ReleaseLabServerResponse> releaseLabServer(
247259

248260
@Override
249261
public ListenableFuture<StartLabServerResponse> startLabServer(StartLabServerRequest request) {
250-
// TODO: Use the universe parameter.
251-
@SuppressWarnings("unused")
252-
String universe = request.getUniverse();
253-
// TODO: Implement this method.
254-
return immediateFuture(StartLabServerResponse.getDefaultInstance());
262+
UniverseScope universe;
263+
try {
264+
universe = universeFactory.create(request.getUniverse());
265+
} catch (IllegalArgumentException e) {
266+
return immediateFailedFuture(e);
267+
}
268+
return startLabServerHandler.startLabServer(request, universe);
255269
}
256270

257271
@Override
258272
public ListenableFuture<RestartLabServerResponse> restartLabServer(
259273
RestartLabServerRequest request) {
260-
// TODO: Use the universe parameter.
261-
@SuppressWarnings("unused")
262-
String universe = request.getUniverse();
263-
// TODO: Implement this method.
264-
return immediateFuture(RestartLabServerResponse.getDefaultInstance());
274+
UniverseScope universe;
275+
try {
276+
universe = universeFactory.create(request.getUniverse());
277+
} catch (IllegalArgumentException e) {
278+
return immediateFailedFuture(e);
279+
}
280+
return restartLabServerHandler.restartLabServer(request, universe);
265281
}
266282

267283
@Override
268284
public ListenableFuture<StopLabServerResponse> stopLabServer(StopLabServerRequest request) {
269-
// TODO: Use the universe parameter.
270-
@SuppressWarnings("unused")
271-
String universe = request.getUniverse();
272-
// TODO: Implement this method.
273-
return immediateFuture(StopLabServerResponse.getDefaultInstance());
285+
UniverseScope universe;
286+
try {
287+
universe = universeFactory.create(request.getUniverse());
288+
} catch (IllegalArgumentException e) {
289+
return immediateFailedFuture(e);
290+
}
291+
return stopLabServerHandler.stopLabServer(request, universe);
274292
}
275293

276294
@Override
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import com.google.common.util.concurrent.Futures;
20+
import com.google.common.util.concurrent.ListenableFuture;
21+
import com.google.common.util.concurrent.ListeningExecutorService;
22+
import com.google.devtools.mobileharness.api.query.proto.FilterProto.LabFilter;
23+
import com.google.devtools.mobileharness.api.query.proto.FilterProto.StringMatchCondition;
24+
import com.google.devtools.mobileharness.api.query.proto.FilterProto.StringMatchCondition.Include;
25+
import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabData;
26+
import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabInfo;
27+
import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabQuery;
28+
import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabQuery.Filter;
29+
import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabQuery.LabViewRequest;
30+
import com.google.devtools.mobileharness.fe.v6.service.host.provider.HostAuxiliaryInfoProvider;
31+
import com.google.devtools.mobileharness.fe.v6.service.host.provider.HostReleaseInfo;
32+
import com.google.devtools.mobileharness.fe.v6.service.host.util.HostVersionUtil;
33+
import com.google.devtools.mobileharness.fe.v6.service.shared.providers.LabInfoProvider;
34+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
35+
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoRequest;
36+
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoResponse;
37+
import java.util.Optional;
38+
import javax.inject.Inject;
39+
import javax.inject.Singleton;
40+
41+
/** Resolves the current lab server version for a host. */
42+
@Singleton
43+
public class LabServerVersionResolver {
44+
45+
private final LabInfoProvider labInfoProvider;
46+
private final HostAuxiliaryInfoProvider hostAuxiliaryInfoProvider;
47+
private final ListeningExecutorService executor;
48+
49+
@Inject
50+
LabServerVersionResolver(
51+
LabInfoProvider labInfoProvider,
52+
HostAuxiliaryInfoProvider hostAuxiliaryInfoProvider,
53+
ListeningExecutorService executor) {
54+
this.labInfoProvider = labInfoProvider;
55+
this.hostAuxiliaryInfoProvider = hostAuxiliaryInfoProvider;
56+
this.executor = executor;
57+
}
58+
59+
/** Resolves the current version of the lab server running on the specified host. */
60+
public ListenableFuture<String> resolveCurrentVersion(String hostName, UniverseScope universe) {
61+
ListenableFuture<GetLabInfoResponse> labInfoFuture =
62+
labInfoProvider.getLabInfoAsync(createGetLabInfoRequest(hostName), universe);
63+
64+
ListenableFuture<Optional<HostReleaseInfo>> hostReleaseInfoFuture =
65+
hostAuxiliaryInfoProvider.getHostReleaseInfo(hostName, universe);
66+
67+
return Futures.whenAllSucceed(labInfoFuture, hostReleaseInfoFuture)
68+
.call(
69+
() -> {
70+
GetLabInfoResponse labInfoResponse = Futures.getDone(labInfoFuture);
71+
Optional<HostReleaseInfo> hostReleaseInfoOpt = Futures.getDone(hostReleaseInfoFuture);
72+
73+
Optional<LabInfo> labInfoOpt =
74+
labInfoResponse.getLabQueryResult().getLabView().getLabDataList().stream()
75+
.map(LabData::getLabInfo)
76+
.findFirst();
77+
78+
Optional<String> currentVersionOpt =
79+
HostVersionUtil.resolveCurrentVersion(labInfoOpt, hostReleaseInfoOpt);
80+
81+
return currentVersionOpt.orElseThrow(
82+
() ->
83+
new IllegalStateException(
84+
"Failed to resolve current lab server version for host: " + hostName));
85+
},
86+
executor);
87+
}
88+
89+
private GetLabInfoRequest createGetLabInfoRequest(String hostName) {
90+
return GetLabInfoRequest.newBuilder()
91+
.setLabQuery(
92+
LabQuery.newBuilder()
93+
.setFilter(
94+
Filter.newBuilder()
95+
.setLabFilter(
96+
LabFilter.newBuilder()
97+
.addLabMatchCondition(
98+
LabFilter.LabMatchCondition.newBuilder()
99+
.setLabHostNameMatchCondition(
100+
LabFilter.LabMatchCondition.LabHostNameMatchCondition
101+
.newBuilder()
102+
.setCondition(
103+
StringMatchCondition.newBuilder()
104+
.setInclude(
105+
Include.newBuilder()
106+
.addExpected(hostName)))))))
107+
.setLabViewRequest(LabViewRequest.getDefaultInstance()))
108+
.build();
109+
}
110+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import static com.google.common.util.concurrent.Futures.immediateFuture;
20+
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerRequest;
23+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerResponse;
24+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
25+
import javax.inject.Inject;
26+
27+
/** No-op implementation of {@link RestartLabServerActionHelper}. */
28+
public final class NoOpRestartLabServerActionHelper implements RestartLabServerActionHelper {
29+
30+
@Inject
31+
NoOpRestartLabServerActionHelper() {}
32+
33+
@Override
34+
public ListenableFuture<RestartLabServerResponse> restartLabServer(
35+
RestartLabServerRequest request, UniverseScope universe) {
36+
return immediateFuture(RestartLabServerResponse.getDefaultInstance());
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import static com.google.common.util.concurrent.Futures.immediateFuture;
20+
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.StartLabServerRequest;
23+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.StartLabServerResponse;
24+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
25+
import javax.inject.Inject;
26+
27+
/** No-op implementation of {@link StartLabServerActionHelper}. */
28+
public final class NoOpStartLabServerActionHelper implements StartLabServerActionHelper {
29+
30+
@Inject
31+
NoOpStartLabServerActionHelper() {}
32+
33+
@Override
34+
public ListenableFuture<StartLabServerResponse> startLabServer(
35+
StartLabServerRequest request, UniverseScope universe) {
36+
return immediateFuture(StartLabServerResponse.getDefaultInstance());
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import static com.google.common.util.concurrent.Futures.immediateFuture;
20+
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.StopLabServerRequest;
23+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.StopLabServerResponse;
24+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
25+
import javax.inject.Inject;
26+
27+
/** No-op implementation of {@link StopLabServerActionHelper}. */
28+
public final class NoOpStopLabServerActionHelper implements StopLabServerActionHelper {
29+
30+
@Inject
31+
NoOpStopLabServerActionHelper() {}
32+
33+
@Override
34+
public ListenableFuture<StopLabServerResponse> stopLabServer(
35+
StopLabServerRequest request, UniverseScope universe) {
36+
return immediateFuture(StopLabServerResponse.getDefaultInstance());
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import com.google.common.util.concurrent.ListenableFuture;
20+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerRequest;
21+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerResponse;
22+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
23+
24+
/** Interface for helper to handle RestartLabServer RPCs. */
25+
public interface RestartLabServerActionHelper {
26+
ListenableFuture<RestartLabServerResponse> restartLabServer(
27+
RestartLabServerRequest request, UniverseScope universe);
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import com.google.common.util.concurrent.ListenableFuture;
20+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerRequest;
21+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.RestartLabServerResponse;
22+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
23+
import javax.inject.Inject;
24+
25+
/** Handler for RestartLabServer RPCs. */
26+
public final class RestartLabServerHandler {
27+
private final RestartLabServerActionHelper actionHelper;
28+
29+
@Inject
30+
RestartLabServerHandler(RestartLabServerActionHelper actionHelper) {
31+
this.actionHelper = actionHelper;
32+
}
33+
34+
public ListenableFuture<RestartLabServerResponse> restartLabServer(
35+
RestartLabServerRequest request, UniverseScope universe) {
36+
return actionHelper.restartLabServer(request, universe);
37+
}
38+
}

0 commit comments

Comments
 (0)