Skip to content

Commit 39d7a1a

Browse files
authored
Fix Cilium node-list races, closed-node reuse and the gRPC alarm hook NPE (#14195)
Cilium fetcher (CiliumNodeManager): - The Peer notify stream, the cluster watcher and the 10-second refresh timer rebuilt the shared node list without a lock. allNodes was sorted in place while the Peer thread added or removed nodes, and two rebuilds diffing the same old usingNodes could open two flow streams for one node, or publish a node another rebuild had just closed. One nodeLock now covers node-list changes, the rebuild, listener notification and close, and start() publishes the first membership snapshot under it before registering the watcher. - A node closed when this OAP's share moved away stayed in allNodes as the same closed instance. When the share moved back (e.g. after scaling the OAP cluster out and back in), the listener got a null stub, so the OAP it moved back to could not read that node while the assignment lasted. A closed node is now replaced in allNodes by a fresh instance. - The ZooKeeper, Consul, Nacos and etcd coordinators list only registered OAPs, and only Mixed and Aggregator register, so a Receiver-role OAP never finds itself in the list. Once Cilium nodes and other OAPs were listed, findFirst().get() threw on every refresh. It now skips Cilium fetching with a debug log; the listed OAPs share every node. gRPC alarm hook (GRPCCallback): - An alarm or recovery processed while no gRPC hook was configured set the settings and stub maps to null, while the client map kept its shut-down clients. Adding a gRPC hook afterwards made every callback throw a NullPointerException at alarmSettingMap.get() until restart. The client and stub maps now stay non-null and are cleared instead. - The client of a hook removed from the configuration is now shut down instead of leaked. For an existing hook, the client is reused while its host and port are unchanged; a replacement is built before the old client is shut down, and null or missing settings are skipped on rebuild and send.
1 parent a6e853b commit 39d7a1a

5 files changed

Lines changed: 621 additions & 85 deletions

File tree

‎docs/en/changes/changes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Fix the release script on macOS: the source tarball carried an AppleDouble `._*` entry and `com.apple.provenance` xattr headers for every file (the 11.0.0 source release has them, and GNU tar extracts them as stray `._*` files with a warning per entry), the next-version PR step aborted because the single-branch release clone has no remote-tracking ref for the pushed branch, and the vote email left out the BanyanDB client proto submodule.
55

66
#### OAP Server
7+
* Fix the Cilium fetcher dropping or double-reading Cilium nodes. The Peer notify stream, the cluster watcher and the 10-second refresh timer rebuilt the shared node list without a lock, which could open two flow streams for one node or keep a closed node as in use. A node closed when this OAP's share of nodes moved away was reused, still closed, when the share moved back (e.g. after scaling the OAP cluster out and back in), so that node went unread while the assignment lasted. A `Receiver`-role OAP on a ZooKeeper, Consul, Nacos or etcd coordinator never finds itself in the cluster list and, when Cilium nodes and other OAPs were listed, threw on every refresh; it now skips Cilium fetching quietly, as the `Mixed` and `Aggregator` OAPs share all Cilium nodes among themselves.
8+
* Fix the gRPC alarm hook throwing a `NullPointerException` on every alarm until OAP restart: once an alarm fired while no gRPC hook was configured (at boot, or after the hooks were removed through dynamic configuration), adding a gRPC hook later broke it. Also shut down the client of a gRPC hook removed from the configuration.
79

810
#### UI
911

‎oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/grpc/GRPCCallback.java‎

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
import io.grpc.stub.StreamObserver;
2222

23+
import java.util.ArrayList;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Objects;
2628
import java.util.concurrent.TimeUnit;
2729

2830
import lombok.extern.slf4j.Slf4j;
@@ -35,7 +37,6 @@
3537
import org.apache.skywalking.oap.server.core.alarm.grpc.Response;
3638
import org.apache.skywalking.oap.server.core.alarm.provider.AlarmRulesWatcher;
3739
import org.apache.skywalking.oap.server.library.client.grpc.GRPCClient;
38-
import org.apache.skywalking.oap.server.library.util.CollectionUtils;
3940
import org.apache.skywalking.oap.server.library.util.GRPCStreamStatus;
4041

4142
/**
@@ -44,31 +45,17 @@
4445
@Slf4j
4546
public class GRPCCallback implements AlarmCallback {
4647

47-
private AlarmRulesWatcher alarmRulesWatcher;
48+
private final AlarmRulesWatcher alarmRulesWatcher;
4849

49-
private Map<String, GRPCAlarmSetting> alarmSettingMap;
50+
private final Map<String, AlarmServiceGrpc.AlarmServiceStub> alarmServiceStubMap;
5051

51-
private Map<String, AlarmServiceGrpc.AlarmServiceStub> alarmServiceStubMap;
52-
53-
private Map<String, GRPCClient> grpcClientMap;
52+
private final Map<String, GRPCClient> grpcClientMap;
5453

5554
public GRPCCallback(AlarmRulesWatcher alarmRulesWatcher) {
5655
this.alarmRulesWatcher = alarmRulesWatcher;
5756
this.alarmServiceStubMap = new HashMap<>();
5857
this.grpcClientMap = new HashMap<>();
59-
this.alarmSettingMap = new HashMap<>();
60-
Map<String, GRPCAlarmSetting> alarmSettingMap = alarmRulesWatcher.getGrpchookSetting();
61-
if (CollectionUtils.isNotEmpty(alarmSettingMap)) {
62-
alarmSettingMap.forEach((name, alarmSetting) -> {
63-
if (alarmSetting != null && !alarmSetting.isEmptySetting()) {
64-
GRPCClient grpcClient = new GRPCClient(alarmSetting.getTargetHost(), alarmSetting.getTargetPort());
65-
grpcClient.connect();
66-
grpcClientMap.put(name, grpcClient);
67-
alarmServiceStubMap.put(name, AlarmServiceGrpc.newStub(grpcClient.getChannel()));
68-
}
69-
});
70-
this.alarmSettingMap = alarmSettingMap;
71-
}
58+
onGRPCAlarmSettingUpdated(alarmRulesWatcher.getGrpchookSetting());
7259
}
7360

7461
@Override
@@ -92,12 +79,15 @@ private void doAlarmCallback(List<AlarmMessage> alarmMessages, boolean isRecover
9279
Map<String, List<AlarmMessage>> groupedMessages = groupMessagesByHook(alarmMessages);
9380

9481
groupedMessages.forEach((hook, messages) -> {
95-
if (alarmServiceStubMap.containsKey(hook)) {
96-
if (!isRecovery) {
97-
sendAlarmMessages(alarmServiceStubMap.get(hook), messages, settinsMap.get(hook));
98-
} else {
99-
sendAlarmRecoveryMessages(alarmServiceStubMap.get(hook), messages, settinsMap.get(hook));
100-
}
82+
final AlarmServiceGrpc.AlarmServiceStub stub = alarmServiceStubMap.get(hook);
83+
final GRPCAlarmSetting setting = settinsMap.get(hook);
84+
if (stub == null || setting == null) {
85+
return;
86+
}
87+
if (!isRecovery) {
88+
sendAlarmMessages(stub, messages, setting);
89+
} else {
90+
sendAlarmRecoveryMessages(stub, messages, setting);
10191
}
10292
});
10393

@@ -254,36 +244,56 @@ public void onCompleted() {
254244

255245
private void onGRPCAlarmSettingUpdated(Map<String, GRPCAlarmSetting> newAlarmSettingMap) {
256246
if (newAlarmSettingMap == null || newAlarmSettingMap.isEmpty()) {
257-
if (grpcClientMap != null) {
258-
grpcClientMap.forEach((name, grpcClient) -> {
259-
grpcClient.shutdown();
260-
log.debug("gRPC alarm hook target is empty, shutdown the old gRPC client.");
261-
});
262-
}
263-
alarmServiceStubMap = null;
264-
alarmSettingMap = null;
265-
247+
// Keep maps non-null so a later non-empty dynamic config update can rebuild
248+
// clients without NPE on grpcClientMap / alarmServiceStubMap access.
249+
grpcClientMap.forEach((name, grpcClient) -> {
250+
grpcClient.shutdown();
251+
log.debug("gRPC alarm hook target is empty, shutdown the old gRPC client.");
252+
});
253+
grpcClientMap.clear();
254+
alarmServiceStubMap.clear();
266255
return;
267256
}
268257

258+
// Drop clients for hooks that disappeared from the new config (e.g. {A,B} -> {A}).
259+
for (String name : new ArrayList<>(grpcClientMap.keySet())) {
260+
if (!newAlarmSettingMap.containsKey(name)) {
261+
shutdownClient(name, "removed");
262+
}
263+
}
264+
269265
newAlarmSettingMap.forEach((name, newAlarmSetting) -> {
270-
if (!newAlarmSetting.equals(alarmSettingMap.get(name))) {
271-
GRPCClient grpcClient = grpcClientMap.get(name);
272-
if (grpcClient != null) {
273-
grpcClient.shutdown();
274-
grpcClientMap.remove(name);
275-
alarmServiceStubMap.remove(name);
276-
log.debug("gRPC alarm hook target is changed, shutdown the old gRPC client.");
277-
}
278-
if (newAlarmSetting.isEmptySetting()) {
279-
return;
280-
}
281-
grpcClient = new GRPCClient(newAlarmSetting.getTargetHost(), newAlarmSetting.getTargetPort());
282-
grpcClient.connect();
283-
grpcClientMap.put(name, grpcClient);
284-
alarmServiceStubMap.put(name, AlarmServiceGrpc.newStub(grpcClient.getChannel()));
266+
if (newAlarmSetting == null || newAlarmSetting.isEmptySetting()) {
267+
shutdownClient(name, "empty");
268+
return;
269+
}
270+
final GRPCClient existing = grpcClientMap.get(name);
271+
if (sameTarget(existing, newAlarmSetting)) {
272+
return;
273+
}
274+
final GRPCClient grpcClient = new GRPCClient(newAlarmSetting.getTargetHost(), newAlarmSetting.getTargetPort());
275+
grpcClient.connect();
276+
final GRPCClient previous = grpcClientMap.put(name, grpcClient);
277+
alarmServiceStubMap.put(name, AlarmServiceGrpc.newStub(grpcClient.getChannel()));
278+
if (previous != null) {
279+
previous.shutdown();
280+
log.debug("gRPC alarm hook target is changed, shutdown the old gRPC client.");
285281
}
286282
});
287-
alarmSettingMap = newAlarmSettingMap;
283+
}
284+
285+
private static boolean sameTarget(GRPCClient existing, GRPCAlarmSetting setting) {
286+
return existing != null
287+
&& Objects.equals(existing.getHost(), setting.getTargetHost())
288+
&& existing.getPort() == setting.getTargetPort();
289+
}
290+
291+
private void shutdownClient(String name, String reason) {
292+
GRPCClient grpcClient = grpcClientMap.remove(name);
293+
alarmServiceStubMap.remove(name);
294+
if (grpcClient != null) {
295+
grpcClient.shutdown();
296+
log.debug("gRPC alarm hook target is {}, shutdown the old gRPC client.", reason);
297+
}
288298
}
289299
}

0 commit comments

Comments
 (0)