Skip to content

Commit

Permalink
ApiVersion: switch to use ClientApiVersion
Browse files Browse the repository at this point in the history
ApiVersion is a class that wrap around RemoteApi's SemVer proto.
We have been relying on ApiVersion.current to determine the current
version of RemoteApi that Bazel supports.

However, to start supporting newer versions of RemoteApi while providing
backward compatibility with different server implementations, we don't
want to reduce our support to a single version. Instead, we need a range
of versions that Bazel (client-side) supports to match with the range of
versions the server supports, which is provided via RemoteApi's
ServerCapabilities.

Create ClientApiVersion to supply this version range logic and move
support status check from ApiVersion to the new class.

Enhance ServerSupportedStatus with the inclusion of the highest
supported version, which could either be "null", in case of Unsupported
status, or is the highest supported version in case of Supported or
Deprecated status.

This work should enable future effort to enable bazelbuild#18270, which was
reverted previously because backward incompatible with older RemoteApi
versions.
  • Loading branch information
sluongng committed Jun 13, 2023
1 parent d50395d commit c913c28
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 184 deletions.
83 changes: 7 additions & 76 deletions src/main/java/com/google/devtools/build/lib/remote/ApiVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@

package com.google.devtools.build.lib.remote;

import build.bazel.remote.execution.v2.ServerCapabilities;
import build.bazel.semver.SemVer;

/**
* Represents a version of the Remote Execution API.
*/
// Represents a version of the Remote Execution API.
public class ApiVersion implements Comparable<ApiVersion> {
public final int major;
public final int minor;
public final int patch;
public final String prerelease;

// The current version of the Remote Execution API. This field will need to be updated
// together with all version changes.
public static final ApiVersion current = new ApiVersion(SemVer.newBuilder().setMajor(2).build());
// The current lowest/highest versions (inclusive) of the Remote Execution API that Bazel
// supports. These fields will need to be updated together with all version changes.
public static final ApiVersion low =
new ApiVersion(SemVer.newBuilder().setMajor(2).setMinor(0).build());
public static final ApiVersion high =
new ApiVersion(SemVer.newBuilder().setMajor(2).setMinor(0).build());

public ApiVersion(int major, int minor, int patch, String prerelease) {
this.major = major;
Expand Down Expand Up @@ -93,73 +93,4 @@ public int compareTo(ApiVersion other) {
}
return Integer.compare(patch, other.patch);
}

static class ServerSupportedStatus {
private enum State {
SUPPORTED,
UNSUPPORTED,
DEPRECATED,
}
private final String message;
private final State state;

private ServerSupportedStatus(State state, String message) {
this.state = state;
this.message = message;
}

public static ServerSupportedStatus supported() {
return new ServerSupportedStatus(State.SUPPORTED, "");
}

public static ServerSupportedStatus unsupported(
ApiVersion curr, ApiVersion lowApiVersion, ApiVersion highApiVersion) {
return new ServerSupportedStatus(
State.UNSUPPORTED,
String.format(
"The API version %s is not supported by the server. "
+ "Please switch to a supported version: %s to %s.",
curr, lowApiVersion, highApiVersion));
}

public static ServerSupportedStatus deprecated(
ApiVersion curr, ApiVersion lowApiVersion, ApiVersion highApiVersion) {
return new ServerSupportedStatus(
State.DEPRECATED,
String.format(
"The API version %s is deprecated by the server. "
+ "Please upgrade to a recommended version: %s to %s.",
curr, lowApiVersion, highApiVersion));
}

public String getMessage() {
return message;
}

public boolean isSupported() {
return state == State.SUPPORTED;
}

public boolean isDeprecated() {
return state == State.DEPRECATED;
}

public boolean isUnsupported() {
return state == State.UNSUPPORTED;
}
}

public ServerSupportedStatus checkServerSupportedVersions(ServerCapabilities cap) {
ApiVersion deprecated =
cap.hasDeprecatedApiVersion() ? new ApiVersion(cap.getDeprecatedApiVersion()) : null;
ApiVersion low = new ApiVersion(cap.getLowApiVersion());
ApiVersion high = new ApiVersion(cap.getHighApiVersion());
if (deprecated != null && compareTo(deprecated) >= 0 && compareTo(low) < 0) {
return ServerSupportedStatus.deprecated(this, low, high);
}
if (compareTo(low) < 0 || compareTo(high) > 0) {
return ServerSupportedStatus.unsupported(this, low, high);
}
return ServerSupportedStatus.supported();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// 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.
package com.google.devtools.build.lib.remote;

import build.bazel.remote.execution.v2.ServerCapabilities;
import javax.annotation.Nullable;

// Represents a range of the Remote Execution API that client supports.
public class ClientApiVersion {
private final ApiVersion low;
private final ApiVersion high;

public static final ClientApiVersion current =
new ClientApiVersion(ApiVersion.low, ApiVersion.high);

public ClientApiVersion(ApiVersion low, ApiVersion high) {
this.low = low;
this.high = high;
}

public ApiVersion getLow() {
return low;
}

public ApiVersion getHigh() {
return high;
}

public boolean isSupported(ApiVersion version) {
return low.compareTo(version) <= 0 && high.compareTo(version) >= 0;
}

static class ServerSupportedStatus {
private enum State {
SUPPORTED,
UNSUPPORTED,
DEPRECATED,
}

private final String message;
private final State state;
private final ApiVersion highestSupportedVersion;

private ServerSupportedStatus(State state, String message, ApiVersion highestSupportedVersion) {
this.state = state;
this.message = message;
this.highestSupportedVersion = highestSupportedVersion;
}

public static ServerSupportedStatus supported(ApiVersion highestSupportedVersion) {
return new ServerSupportedStatus(State.SUPPORTED, "", highestSupportedVersion);
}

public static ServerSupportedStatus unsupported(
ApiVersion clientLow, ApiVersion clientHigh, ApiVersion serverLow, ApiVersion serverHigh) {
return new ServerSupportedStatus(
State.UNSUPPORTED,
String.format(
"The client supported API versions, %s to %s, is not supported by the server, %s to"
+ " %s. Please switch to a different server or upgrade Bazel.",
clientLow, clientHigh, serverLow, serverHigh),
null);
}

public static ServerSupportedStatus deprecated(
ApiVersion clientHigh, ApiVersion serverLow, ApiVersion serverHigh) {
return new ServerSupportedStatus(
State.DEPRECATED,
String.format(
"The highest API version Bazel support %s is deprecated by the server. "
+ "Please upgrade to server's recommended version: %s to %s.",
clientHigh, serverLow, serverHigh),
clientHigh);
}

public String getMessage() {
return message;
}

public ApiVersion getHighestSupportedVersion() {
return highestSupportedVersion;
}

public boolean isSupported() {
return state == State.SUPPORTED;
}

public boolean isDeprecated() {
return state == State.DEPRECATED;
}

public boolean isUnsupported() {
return state == State.UNSUPPORTED;
}
}

// highestSupportedVersion compares the client's supported versions against the input low and high
// versions and returns the highest supported version. If the client's supported versions are not
// supported
// by the server, it returns null.
@Nullable
private ApiVersion highestSupportedVersion(ApiVersion serverLow, ApiVersion serverHigh) {
var higestLow = this.low.compareTo(serverLow) >= 0 ? this.low : serverLow;
var lowestHigh = this.high.compareTo(serverHigh) <= 0 ? this.high : serverHigh;

return higestLow.compareTo(lowestHigh) <= 0 ? lowestHigh : null;
}

public ServerSupportedStatus checkServerSupportedVersions(ServerCapabilities cap) {
var serverLow = new ApiVersion(cap.getLowApiVersion());
var serverHigh = new ApiVersion(cap.getHighApiVersion());

var highest = highestSupportedVersion(serverLow, serverHigh);
if (highest != null) {
return ServerSupportedStatus.supported(highest);
}

var deprecated =
cap.hasDeprecatedApiVersion() ? new ApiVersion(cap.getDeprecatedApiVersion()) : null;
if (deprecated == null) {
return ServerSupportedStatus.unsupported(this.low, this.high, serverLow, serverHigh);
}

highest = highestSupportedVersion(deprecated, serverHigh);
if (highest != null) {
return ServerSupportedStatus.deprecated(highest, serverLow, serverHigh);
}

return ServerSupportedStatus.unsupported(this.low, this.high, serverLow, serverHigh);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ public static ClientServerCompatibilityStatus checkClientServerCompatibility(
}

// Check API version.
ApiVersion.ServerSupportedStatus st =
ApiVersion.current.checkServerSupportedVersions(capabilities);
ClientApiVersion.ServerSupportedStatus st =
ClientApiVersion.current.checkServerSupportedVersions(capabilities);
if (st.isUnsupported()) {
result.addError(st.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

import static com.google.common.truth.Truth.assertThat;

import build.bazel.remote.execution.v2.ServerCapabilities;
import build.bazel.semver.SemVer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests for {@link ApiVersion}.
*/
// Tests for {@link ApiVersion}.
@RunWith(JUnit4.class)
public class ApiVersionTest {

@Test
public void testToString() throws Exception {
assertThat(new ApiVersion(0, 0, 0, "v1test").toString()).isEqualTo("v1test");
Expand All @@ -45,81 +41,25 @@ public void testCompareTo() throws Exception {
.isLessThan(0);
assertThat(new ApiVersion(0, 0, 1, "").compareTo(new ApiVersion(1, 0, 0, "v1test")))
.isGreaterThan(0);
assertThat(new ApiVersion(1, 0, 0, "").compareTo(new ApiVersion(2, 0, 0, "")))
.isLessThan(0);
assertThat(new ApiVersion(2, 0, 0, "").compareTo(new ApiVersion(1, 0, 0, "")))
.isGreaterThan(0);
assertThat(new ApiVersion(2, 1, 0, "").compareTo(new ApiVersion(2, 2, 0, "")))
.isLessThan(0);
assertThat(new ApiVersion(2, 2, 0, "").compareTo(new ApiVersion(2, 1, 0, "")))
.isGreaterThan(0);
assertThat(new ApiVersion(2, 2, 1, "").compareTo(new ApiVersion(2, 2, 2, "")))
.isLessThan(0);
assertThat(new ApiVersion(2, 2, 2, "").compareTo(new ApiVersion(2, 1, 1, "")))
.isGreaterThan(0);
assertThat(new ApiVersion(2, 2, 2, "").compareTo(new ApiVersion(2, 2, 2, "")))
.isEqualTo(0);
assertThat(new ApiVersion(1, 0, 0, "").compareTo(new ApiVersion(2, 0, 0, ""))).isLessThan(0);
assertThat(new ApiVersion(2, 0, 0, "").compareTo(new ApiVersion(1, 0, 0, ""))).isGreaterThan(0);
assertThat(new ApiVersion(2, 1, 0, "").compareTo(new ApiVersion(2, 2, 0, ""))).isLessThan(0);
assertThat(new ApiVersion(2, 2, 0, "").compareTo(new ApiVersion(2, 1, 0, ""))).isGreaterThan(0);
assertThat(new ApiVersion(2, 2, 1, "").compareTo(new ApiVersion(2, 2, 2, ""))).isLessThan(0);
assertThat(new ApiVersion(2, 2, 2, "").compareTo(new ApiVersion(2, 1, 1, ""))).isGreaterThan(0);
assertThat(new ApiVersion(2, 2, 2, "").compareTo(new ApiVersion(2, 2, 2, ""))).isEqualTo(0);
}

@Test
public void testFromToSemver() throws Exception {
SemVer[] semvers = new SemVer[] {
SemVer.newBuilder().setMajor(2).build(),
SemVer.newBuilder().setMajor(2).setMinor(1).setPatch(3).build(),
SemVer.newBuilder().setPrerelease("v1test").build(),
};
SemVer[] semvers =
new SemVer[] {
SemVer.newBuilder().setMajor(2).build(),
SemVer.newBuilder().setMajor(2).setMinor(1).setPatch(3).build(),
SemVer.newBuilder().setPrerelease("v1test").build(),
};
for (SemVer sm : semvers) {
assertThat(new ApiVersion(sm).toSemVer()).isEqualTo(sm);
}
}

@Test
public void testCheckServerSupportedVersions_isSupported() throws Exception {
assertThat(
new ApiVersion(2, 1, 1, "")
.checkServerSupportedVersions(
ServerCapabilities.newBuilder()
.setLowApiVersion(SemVer.newBuilder().setMajor(2).build())
.setHighApiVersion(SemVer.newBuilder().setMajor(3).build())
.build())
.isSupported())
.isTrue();
}

@Test
public void testCheckServerSupportedVersions_isDeprecated() throws Exception {
for (ApiVersion v :
new ApiVersion[] {
new ApiVersion(0, 0, 0, "v1test"),
new ApiVersion(0, 0, 0, "v2test"),
new ApiVersion(1, 0, 0, "")
}) {
ApiVersion.ServerSupportedStatus st =
v.checkServerSupportedVersions(
ServerCapabilities.newBuilder()
.setDeprecatedApiVersion(SemVer.newBuilder().setPrerelease("v1test").build())
.setLowApiVersion(SemVer.newBuilder().setMajor(2).build())
.setHighApiVersion(SemVer.newBuilder().setMajor(3).build())
.build());
assertThat(st.isDeprecated()).isTrue();
assertThat(st.getMessage()).contains("deprecated");
assertThat(st.getMessage()).contains("2.0 to 3.0");
}
}

@Test
public void testCheckServerSupportedVersions_isUnsupported() throws Exception {
for (ApiVersion v :
new ApiVersion[] {new ApiVersion(0, 0, 0, "v1test"), new ApiVersion(3, 1, 0, "")}) {
ApiVersion.ServerSupportedStatus st =
v.checkServerSupportedVersions(
ServerCapabilities.newBuilder()
.setLowApiVersion(SemVer.newBuilder().setMajor(2).build())
.setHighApiVersion(SemVer.newBuilder().setMajor(3).build())
.build());
assertThat(st.isUnsupported()).isTrue();
assertThat(st.getMessage()).contains("not supported");
assertThat(st.getMessage()).contains("2.0 to 3.0");
}
}
}
Loading

0 comments on commit c913c28

Please sign in to comment.