Browse Source

Ignore ZK registrations with the current service id in discovery

Other servers list excludes ZK nodes carrying the current service id and
keeps a single, most recently modified, ZK node per service id, so every
node builds the same partition ring when a service has duplicate
registrations.

Cache events for ZK nodes with the current service id are ignored, with
an error logged when such a node appears. Removal of a duplicate ZK node
of another service no longer publishes a shutdown event while that
service is still registered.
pull/16263/head
Oleksandra Matviienko 2 months ago
parent
commit
afe7bdb0ae
  1. 25
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java
  2. 57
      common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java

25
common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java

@ -32,12 +32,14 @@ import org.thingsboard.server.gen.transport.TransportProtos;
import org.thingsboard.server.queue.discovery.event.OtherServiceShutdownEvent;
import org.thingsboard.server.queue.util.AfterStartUp;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.apache.curator.framework.recipes.cache.CuratorCacheAccessor.parentPathFilter;
@ -103,20 +105,29 @@ public class ZkDiscoveryService implements DiscoveryService {
@Override
public List<TransportProtos.ServiceInfo> getOtherServers() {
String currentServiceId = serviceInfoProvider.getServiceInfo().getServiceId();
return cache.stream()
.filter(parentPathFilter(zkNodesDir))
.filter(cd -> !cd.getPath().equals(nodePath))
.map(cd -> {
try {
return TransportProtos.ServiceInfo.parseFrom(cd.getData());
return new Registration(cd, TransportProtos.ServiceInfo.parseFrom(cd.getData()));
} catch (NoSuchElementException | InvalidProtocolBufferException e) {
log.error("Failed to decode ZK node", e);
throw new RuntimeException(e);
}
})
.filter(r -> !r.info().getServiceId().equals(currentServiceId))
.collect(Collectors.toMap(r -> r.info().getServiceId(), Function.identity(),
(a, b) -> b.data().getStat().getMtime() > a.data().getStat().getMtime() ? b : a,
LinkedHashMap::new))
.values().stream()
.map(Registration::info)
.collect(Collectors.toList());
}
private record Registration(ChildData data, TransportProtos.ServiceInfo info) {}
@Override
public boolean isMonolith() {
return false;
@ -329,6 +340,14 @@ public class ZkDiscoveryService implements DiscoveryService {
String serviceId = instance.getServiceId();
ProtocolStringList serviceTypesList = instance.getServiceTypesList();
if (serviceId.equals(serviceInfoProvider.getServiceInfo().getServiceId())) {
if (type == CuratorCacheListener.Type.NODE_CREATED) {
log.error("[{}] Ignoring ZK node {} registered with the current service id. " +
"It is either a stale registration of this service or another service uses the same service id", serviceId, data.getPath());
}
return;
}
log.trace("Processing [{}] event for [{}]", type, serviceId);
switch (type) {
case NODE_CREATED:
@ -349,6 +368,10 @@ public class ZkDiscoveryService implements DiscoveryService {
}
break;
case NODE_DELETED:
if (getOtherServers().stream().anyMatch(s -> s.getServiceId().equals(serviceId))) {
log.debug("[{}] Ignoring removal of duplicate ZK node {}, service is still registered", serviceId, data.getPath());
break;
}
zkExecutorService.submit(() -> applicationEventPublisher.publishEvent(new OtherServiceShutdownEvent(this, serviceId, serviceTypesList)));
ScheduledFuture<?> future = zkExecutorService.schedule(() -> {
log.debug("[{}] Going to recalculate partitions due to removed node [{}]",

57
common/queue/src/test/java/org/thingsboard/server/queue/discovery/ZkDiscoveryServiceTest.java

@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.util.ReflectionTestUtils;
import org.thingsboard.common.util.ThingsBoardExecutors;
import org.thingsboard.server.gen.transport.TransportProtos;
import org.thingsboard.server.queue.discovery.event.OtherServiceShutdownEvent;
import java.util.ArrayList;
import java.util.Collections;
@ -257,6 +258,56 @@ public class ZkDiscoveryServiceTest {
verify(client, never()).setData();
}
@Test
public void getOtherServersIgnoresNodesWithCurrentServiceIdTest() {
dataList.add(new ChildData("/thingsboard/nodes/0000000011", statWithMtime(1), currentInfo.toByteArray()));
dataList.add(childData);
assertEquals(List.of(childInfo), zkDiscoveryService.getOtherServers());
}
@Test
public void getOtherServersKeepsLatestNodeForDuplicatedServiceIdTest() {
TransportProtos.ServiceInfo latestInfo = childInfo.toBuilder().setReady(true).build();
dataList.add(new ChildData("/thingsboard/nodes/0000000020", statWithMtime(1), childInfo.toByteArray()));
dataList.add(new ChildData("/thingsboard/nodes/0000000021", statWithMtime(2), latestInfo.toByteArray()));
assertEquals(List.of(latestInfo), zkDiscoveryService.getOtherServers());
Collections.swap(dataList, 1, 2);
assertEquals(List.of(latestInfo), zkDiscoveryService.getOtherServers());
}
@Test
public void nodeWithCurrentServiceIdDoesNotTriggerRecalculationTest() {
ChildData orphanData = new ChildData("/thingsboard/nodes/0000000011", statWithMtime(1), currentInfo.toByteArray());
startNode(orphanData);
stopNode(orphanData);
assertTrue(zkDiscoveryService.delayedTasks.isEmpty());
verify(partitionService, never()).recalculatePartitions(any(), any());
verify(applicationEventPublisher, never()).publishEvent(any(OtherServiceShutdownEvent.class));
}
@Test
public void removalOfDuplicatedNodeDoesNotPublishShutdownEventTest() {
ChildData staleData = new ChildData("/thingsboard/nodes/0000000019", statWithMtime(1), childInfo.toByteArray());
ChildData liveData = new ChildData("/thingsboard/nodes/0000000020", statWithMtime(2), childInfo.toByteArray());
startNode(staleData);
startNode(liveData);
stopNode(staleData);
assertTrue(zkDiscoveryService.delayedTasks.isEmpty());
verify(applicationEventPublisher, never()).publishEvent(any(OtherServiceShutdownEvent.class));
stopNode(liveData);
assertEquals(1, zkDiscoveryService.delayedTasks.size());
}
private ExistsBuilder mockExistsBuilder() {
ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
when(client.checkExists()).thenReturn(existsBuilder);
@ -277,6 +328,12 @@ public class ZkDiscoveryServiceTest {
return stat;
}
private static Stat statWithMtime(long mtime) {
Stat stat = new Stat();
stat.setMtime(mtime);
return stat;
}
private void startNode(ChildData data) {
dataList.add(data);
zkDiscoveryService.onCacheEvent(NODE_CREATED, null, data);

Loading…
Cancel
Save