Skip to content

Commit cb63210

Browse files
authored
Check BanyanDB server compatibility against the API versions the OAP supports in code (#14267)
The OAP now knows the BanyanDB API versions it supports, 0.11 and 0.13, in the client library's BanyanDBAPIVersion enum. Each version lists the server API versions its code works with; the OAP boots only against a server the oldest version works with, and records the version it works as (the server's, or a forced one) on the client (BanyanDBClient#getServerAPIVersion), so code can branch on V0_13.isCompatibleWith(serverAPIVersion) once it uses something a newer API adds. The @APISince annotation marks such code. The compatibleServerApiVersions setting (SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS) is removed: it could list versions the OAP has no code for. For debugging only, the environment variable SW_STORAGE_BANYANDB_FORCE_SERVER_API_VERSION makes the OAP work as one of the supported versions, whatever version the server reports.
1 parent bd8f276 commit cb63210

16 files changed

Lines changed: 372 additions & 60 deletions

File tree

‎docs/en/changes/changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
* Page the DSL Debug API's record read: every record carries `captureTime`, when its node captured it, as `<millis>.<six-digit nanos>`; `GET /dsl-debugging/session/{id}` takes `capturedSince` and `pageSize` and returns `nextCapturedSince`, the point the next read passes back to get only the records captured since, from whichever OAP node serves it. The other nodes stream their records one a message over the new `CollectDebugRecords` call, and the admin channels between nodes take `admin-server.gRPCMaxMessageSize`, as the admin gRPC server does, instead of gRPC's 4 MiB, so a node whose capture outgrew 4 MiB no longer shows as unreachable. A read without the parameters starts from the first record. Each node's slice carries `instanceId`, which tells the OAP processes apart where node ids do not, and `complete` says the capture is read through; a node that reached its `recordCap` no longer holds back the records of nodes whose clocks run ahead of it.
109109
* **Breaking Change** — DSL Debug API across OAP nodes: `CollectDebugRecords` replaces `CollectDebugSamples` on the admin gRPC bus (`admin-server.gRPCPort`), which returned a node's whole capture in one reply. During a rolling upgrade, a node reads no records from a node of the other version and shows it as `unreachable` in a debug session's `nodes[]`; a session reads the whole cluster again once every node runs the same version.
110110
* Debug the network-alias pairing of OTLP calls through a new admin module, `trace-pairing-debugging` (`SW_TRACE_PAIRING_DEBUGGING`, on by default, requires `trace-analyzer`). `POST /trace-pairing-debugging/start` turns on one capture per OAP node, shared by every reader, as each node owns a share of the traces; it runs while it is read, and stops a minute after the latest read or on `POST /trace-pairing-debugging/stop`, releasing what it found; each capture from a start to its stop is a round with its own `roundId`, which every node in it shares, and each listed entry names the OAP nodes that hold it. `GET /trace-pairing-debugging/pairings` lists, over the nodes, the peers matched to a server, with the server before it when a later match moved it, the callers, the count of calls and each match's outcome (`registered`, `gateway`, `uncomposed`, `tooLong`, `noService`); and the peers and the servers whose halves left without their pair (`expired`, `evicted`, `replaced`), each with its latest five trace ids.
111+
* BanyanDB: the OAP works with BanyanDB servers reporting API version `0.11` or `0.13`, the versions it supports. The `compatibleServerApiVersions` setting in `bydb.yml` (`SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS`) is removed, as it could list versions the OAP does not support: a server reporting another API version, such as `0.10`, no longer boots even if the setting listed it, and a `bydb.yml` that still sets it boots with a warning. For debugging only, the environment variable `SW_STORAGE_BANYANDB_FORCE_SERVER_API_VERSION` makes the OAP work as one of those versions, whatever version the server reports.
111112

112113
#### UI
113114

‎docs/en/setup/backend/backend-setup.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ You should set up the database ready before starting the backend. We recommend t
2020
If you want to use other databases, please read the [storage document](backend-storage.md).
2121

2222
Use the docker mode to run BanyanDB containerized.
23-
The compatible BanyanDB API version number could be found in `/config/bydb.yml`
24-
```
25-
${SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS}
26-
```
27-
The compatible BanyanDB Server version number can be found via the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/).
23+
The BanyanDB API versions this OAP release works with are listed in [BanyanDB storage](storages/banyandb.md), and the
24+
BanyanDB Server versions of each API version in the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/).
2825

2926
```shell
3027
export BYDB_VERSION=xxx

‎docs/en/setup/backend/configuration-vocabulary.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ The global settings for the whole BanyanDB:
468468
| secretsManagementFile | Secrets management file in the properties format, including the user and password, which are managed by a 3rd party tool. When set, it overrides `user`/`password`, and a rotation of the file is applied without restarting the OAP. | SW_STORAGE_BANYANDB_SECRETS_MANAGEMENT_FILE | - |
469469
| cleanupUnusedTopNRules | Cleanup TopN rules in BanyanDB server that are not configured in the bydb-topn.yml config. | SW_STORAGE_BANYANDB_CLEANUP_UNUSED_TOPN_RULES | true |
470470
| namespace | The namespace in BanyanDB to store the data of OAP. | SW_NAMESPACE | default |
471-
| compatibleServerApiVersions | The compatible server API versions of BanyanDB. The compatible BanyanDB Server version number can be found via the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/). | SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS | 0.11 |
472471

473472
### Group Configuration
474473
The settings for each group:

‎docs/en/setup/backend/storages/banyandb.md‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
[BanyanDB](https://github.com/apache/skywalking-banyandb) is a dedicated storage implementation developed by the SkyWalking Team and the community. Activate BanyanDB as the storage by setting the storage provider to **banyandb**.
44

5-
The BanyanDB server compatibility relies on API and release versions,
6-
The compatible BanyanDB API version number could be found in `/config/bydb.yml`
7-
```
8-
${SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS}
9-
```
10-
The compatible BanyanDB Server version number can be found via the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/).
5+
The BanyanDB server compatibility relies on API versions. A BanyanDB server reports its API version, and several BanyanDB
6+
releases share one API version. This OAP release works with BanyanDB servers reporting API version `0.11` or `0.13`. The
7+
BanyanDB releases of each API version are listed in the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/).
118

129
If the BanyanDB server API version is not compatible with the OAP server, the OAP server will not start, and the following error message will be displayed:
1310
```shell
14-
... ERROR [] - ... Incompatible BanyanDB server API version: 0.x. But accepted versions: 0.y
11+
... ERROR [] - ... Incompatible BanyanDB server API version: 0.x. Supported versions: 0.11, 0.13
1512
org.apache.skywalking.oap.server.library.module.ModuleStartException: Incompatible BanyanDB server API version...
1613
```
1714

15+
For debugging only, the environment variable `SW_STORAGE_BANYANDB_FORCE_SERVER_API_VERSION` makes the OAP work as the
16+
API version it names, whatever version the server reports. The version must be one this OAP release works with, or the
17+
OAP does not start. It is not a `bydb.yml` setting, and the OAP logs a warning at boot while it is set.
18+
1819
### Configuration
1920
In the `application.yml` file, select the BanyanDB storage provider:
2021

@@ -71,9 +72,6 @@ global:
7172
# The namespace in BanyanDB to store the data of OAP, if not set, the default is "sw".
7273
# OAP will create BanyanDB Groups using the format of "{namespace}_{group name}", such as "sw_records".
7374
namespace: ${SW_NAMESPACE:"sw"}
74-
# The compatible server API versions of BanyanDB.
75-
# The compatible BanyanDB Server version number can be found via the [API versions mapping](https://skywalking.apache.org/docs/skywalking-banyandb/latest/installation/versions/).
76-
compatibleServerApiVersions: ${SW_STORAGE_BANYANDB_COMPATIBLE_SERVER_API_VERSIONS:"0.11"}
7775

7876
groups:
7977
# The group settings of record.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.library.banyandb.v1.client;
20+
21+
import java.lang.annotation.Documented;
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Marks code that sends something a BanyanDB API version introduced: a request field, an enum value, a message type or
29+
* an RPC. A server whose API version that version is not compatible with ignores or rejects it, so the marked code
30+
* runs only when {@code value().isCompatibleWith(client.getServerAPIVersion())} holds, and its caller keeps a path for
31+
* the other servers.
32+
*/
33+
@Documented
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target({
36+
ElementType.TYPE,
37+
ElementType.METHOD,
38+
ElementType.FIELD
39+
})
40+
public @interface APISince {
41+
/**
42+
* @return the API version that introduced what the marked code sends
43+
*/
44+
BanyanDBAPIVersion value();
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.library.banyandb.v1.client;
20+
21+
import java.util.List;
22+
import lombok.Getter;
23+
24+
/**
25+
* The BanyanDB API versions this client's code is written for. A BanyanDB server reports its API version through
26+
* {@code GetAPIVersion}, and several BanyanDB releases share one API version. Each constant names its version, then
27+
* the server API versions its code works with, its own included. Code that needs an API version runs only when
28+
* {@code version.isCompatibleWith(serverAPIVersion)} holds (see {@link APISince}).
29+
*
30+
* <p>Constants are declared oldest first. Code that runs without such a check is written for the {@link #oldest()}
31+
* version, so a server is supported only when the oldest version is compatible with it.
32+
*/
33+
@Getter
34+
public enum BanyanDBAPIVersion {
35+
V0_11("0.11", "0.11", "0.13"),
36+
V0_13("0.13", "0.13");
37+
38+
/**
39+
* The API version, as a server reports it.
40+
*/
41+
private final String version;
42+
/**
43+
* The server API versions this version's code works with, its own version included.
44+
*/
45+
private final List<String> compatibleServerVersions;
46+
47+
BanyanDBAPIVersion(final String version, final String... compatibleServerVersions) {
48+
this.version = version;
49+
this.compatibleServerVersions = List.of(compatibleServerVersions);
50+
}
51+
52+
/**
53+
* @param serverAPIVersion the API version a server reports, or null while it is not known, as
54+
* {@code BanyanDBClient#getServerAPIVersion()} is before the version is verified
55+
* @return true when code written for this API version works with that server; false for null
56+
*/
57+
public boolean isCompatibleWith(final String serverAPIVersion) {
58+
return serverAPIVersion != null && compatibleServerVersions.contains(serverAPIVersion);
59+
}
60+
61+
/**
62+
* @return the oldest API version, the one code without a version check is written for
63+
*/
64+
public static BanyanDBAPIVersion oldest() {
65+
return values()[0];
66+
}
67+
}

‎oap-server/server-library/library-banyandb-client/src/main/java/org/apache/skywalking/library/banyandb/v1/client/BanyanDBClient.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public class BanyanDBClient implements Closeable {
166166
* Null until {@link #connect()} runs, and for the test-only channel injection.
167167
*/
168168
private volatile ChannelManager channelManager;
169+
/**
170+
* The API version this client works as with the connected server: the one the server reported, or the one
171+
* {@link #verifyServerAPIVersion(String)} was told to force. Null until that method succeeds.
172+
*/
173+
@Getter
174+
private volatile String serverAPIVersion;
169175

170176
/**
171177
* Create a BanyanDB client instance with a default options.
@@ -1303,6 +1309,35 @@ public BanyandbCommon.APIVersion getAPIVersion() throws BanyanDBException {
13031309
});
13041310
}
13051311

1312+
/**
1313+
* Read the server's API version, check that this client supports it, and record it as
1314+
* {@link #getServerAPIVersion()}.
1315+
*
1316+
* @param forcedAPIVersion when not empty, the API version to work as instead of the one the server reports, for
1317+
* debugging; it must be a version this client supports as well
1318+
* @return the API version and revision the server reported
1319+
* @throws BanyanDBException if the server is not reachable or rejects the call
1320+
* @throws IllegalStateException if this client does not support the version it would work as
1321+
*/
1322+
public BanyandbCommon.APIVersion verifyServerAPIVersion(final String forcedAPIVersion) throws BanyanDBException {
1323+
final BanyandbCommon.APIVersion reported = getAPIVersion();
1324+
final boolean forced = !Strings.isNullOrEmpty(forcedAPIVersion);
1325+
final String version = forced ? forcedAPIVersion : reported.getVersion();
1326+
final BanyanDBAPIVersion oldest = BanyanDBAPIVersion.oldest();
1327+
if (!oldest.isCompatibleWith(version)) {
1328+
final String supported = String.join(", ", oldest.getCompatibleServerVersions());
1329+
if (forced) {
1330+
throw new IllegalStateException(
1331+
"Incompatible BanyanDB API version: " + version + ", forced in place of the server's "
1332+
+ reported.getVersion() + ". Supported versions: " + supported);
1333+
}
1334+
throw new IllegalStateException(
1335+
"Incompatible BanyanDB server API version: " + version + ". Supported versions: " + supported);
1336+
}
1337+
this.serverAPIVersion = version;
1338+
return reported;
1339+
}
1340+
13061341
@Override
13071342
public void close() throws IOException {
13081343
connectionEstablishLock.lock();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.library.banyandb.v1.client;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
/**
28+
* Holds the version lists to the rules the client relies on, whatever versions they name.
29+
*/
30+
public class BanyanDBAPIVersionTest {
31+
@Test
32+
public void everyVersionWorksWithServersOfItsOwnVersion() {
33+
for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) {
34+
assertTrue(version.isCompatibleWith(version.getVersion()), version.name());
35+
}
36+
}
37+
38+
/**
39+
* A server the oldest version does not work with fails the boot, so no newer version may list it.
40+
*/
41+
@Test
42+
public void everyCompatibleServerIsOneTheOldestVersionWorksWith() {
43+
final BanyanDBAPIVersion oldest = BanyanDBAPIVersion.oldest();
44+
for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) {
45+
for (final String server : version.getCompatibleServerVersions()) {
46+
assertTrue(oldest.isCompatibleWith(server), version.name() + " lists " + server);
47+
}
48+
}
49+
}
50+
51+
@Test
52+
public void versionsAreDeclaredOldestFirst() {
53+
final BanyanDBAPIVersion[] versions = BanyanDBAPIVersion.values();
54+
assertEquals(versions[0], BanyanDBAPIVersion.oldest());
55+
for (int i = 1; i < versions.length; i++) {
56+
assertTrue(
57+
compare(versions[i - 1].getVersion(), versions[i].getVersion()) < 0,
58+
versions[i - 1].name() + " is declared before " + versions[i].name());
59+
}
60+
}
61+
62+
/**
63+
* Code written for a version uses what that version added, which an older server does not have.
64+
*/
65+
@Test
66+
public void noVersionWorksWithAnOlderServer() {
67+
for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) {
68+
for (final String server : version.getCompatibleServerVersions()) {
69+
assertTrue(compare(server, version.getVersion()) >= 0, version.name() + " lists " + server);
70+
}
71+
}
72+
}
73+
74+
@Test
75+
public void unknownServerVersionIsNotCompatible() {
76+
for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) {
77+
assertFalse(version.isCompatibleWith("0.0"), version.name());
78+
assertFalse(version.isCompatibleWith(""), version.name());
79+
assertFalse(version.isCompatibleWith(null), version.name());
80+
}
81+
}
82+
83+
private static int compare(final String left, final String right) {
84+
final String[] l = left.split("\\.");
85+
final String[] r = right.split("\\.");
86+
final int major = Integer.compare(Integer.parseInt(l[0]), Integer.parseInt(r[0]));
87+
return major != 0 ? major : Integer.compare(Integer.parseInt(l[1]), Integer.parseInt(r[1]));
88+
}
89+
}

0 commit comments

Comments
 (0)