Browse Source
- Resolve affected tenants in PartitionChangeEvent.getAffectedTenants(), returning null (all tenants) for non rule engine events, empty diffs and shared queue changes. - HashPartitionService adds isolated tenants whose managed status changed without a change of their own queue keys. - PartitionChangeMsg: forAllTenants/forTenants factories instead of a null sentinel; affected tenants excluded from toString(). - Fold the AppActor log line into the existing DefaultActorService one. - Tests: priority ordering in ActorSystemTest, AppActorTest on a real actor system, DefaultActorServiceTest through onTbApplicationEvent, the managed-status scenario in HashPartitionServiceTest.pull/15961/head
12 changed files with 451 additions and 191 deletions
@ -1,92 +1,125 @@ |
|||||
/** |
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
||||
* Copyright © 2016-2026 The Thingsboard Authors |
// SPDX-License-Identifier: Apache-2.0
|
||||
* |
|
||||
* 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 org.thingsboard.server.actors.app; |
package org.thingsboard.server.actors.app; |
||||
|
|
||||
import org.junit.Before; |
import org.junit.jupiter.api.AfterEach; |
||||
import org.junit.Test; |
import org.junit.jupiter.api.BeforeEach; |
||||
import org.mockito.ArgumentCaptor; |
import org.junit.jupiter.api.Test; |
||||
|
import org.thingsboard.server.actors.AbstractTbActor; |
||||
import org.thingsboard.server.actors.ActorSystemContext; |
import org.thingsboard.server.actors.ActorSystemContext; |
||||
import org.thingsboard.server.actors.TbActorCtx; |
import org.thingsboard.server.actors.DefaultTbActorSystem; |
||||
|
import org.thingsboard.server.actors.TbActor; |
||||
|
import org.thingsboard.server.actors.TbActorCreator; |
||||
import org.thingsboard.server.actors.TbActorId; |
import org.thingsboard.server.actors.TbActorId; |
||||
|
import org.thingsboard.server.actors.TbActorRef; |
||||
|
import org.thingsboard.server.actors.TbActorSystem; |
||||
|
import org.thingsboard.server.actors.TbActorSystemSettings; |
||||
import org.thingsboard.server.actors.TbEntityActorId; |
import org.thingsboard.server.actors.TbEntityActorId; |
||||
import org.thingsboard.server.common.data.id.DeviceId; |
import org.thingsboard.server.actors.TbStringActorId; |
||||
import org.thingsboard.server.common.data.id.TenantId; |
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.msg.TbActorMsg; |
||||
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg; |
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg; |
||||
import org.thingsboard.server.common.msg.queue.ServiceType; |
import org.thingsboard.server.common.msg.queue.ServiceType; |
||||
import org.thingsboard.server.dao.tenant.TenantService; |
import org.thingsboard.server.dao.tenant.TenantService; |
||||
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
||||
|
|
||||
|
import java.util.List; |
||||
import java.util.Set; |
import java.util.Set; |
||||
import java.util.UUID; |
import java.util.UUID; |
||||
import java.util.function.Predicate; |
import java.util.concurrent.CopyOnWriteArrayList; |
||||
|
import java.util.concurrent.ExecutorService; |
||||
|
import java.util.concurrent.Executors; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
import static org.junit.Assert.assertFalse; |
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.junit.Assert.assertTrue; |
import static org.awaitility.Awaitility.await; |
||||
import static org.mockito.ArgumentMatchers.any; |
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean; |
|
||||
import static org.mockito.ArgumentMatchers.eq; |
|
||||
import static org.mockito.Mockito.mock; |
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.never; |
|
||||
import static org.mockito.Mockito.verify; |
|
||||
import static org.mockito.Mockito.when; |
import static org.mockito.Mockito.when; |
||||
|
|
||||
public class AppActorTest { |
class AppActorTest { |
||||
|
|
||||
AppActor appActor; |
private static final String DISPATCHER = "app-actor-test-dispatcher"; |
||||
TbActorCtx ctx; |
|
||||
TenantId tenant1 = new TenantId(UUID.fromString("11111111-1111-1111-1111-111111111111")); |
|
||||
TenantId tenant2 = new TenantId(UUID.fromString("22222222-2222-2222-2222-222222222222")); |
|
||||
|
|
||||
@Before |
private final TenantId tenant1 = TenantId.fromUUID(UUID.randomUUID()); |
||||
public void setUp() throws Exception { |
private final TenantId tenant2 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
|
||||
|
private ExecutorService executor; |
||||
|
private TbActorSystem system; |
||||
|
private TbActorRef appActor; |
||||
|
private List<TbActorMsg> tenant1Msgs; |
||||
|
private List<TbActorMsg> tenant2Msgs; |
||||
|
private List<TbActorMsg> otherChildMsgs; |
||||
|
|
||||
|
@BeforeEach |
||||
|
void setUp() { |
||||
ActorSystemContext systemContext = mock(ActorSystemContext.class); |
ActorSystemContext systemContext = mock(ActorSystemContext.class); |
||||
TenantService tenantService = mock(TenantService.class); |
when(systemContext.getTenantService()).thenReturn(mock(TenantService.class)); |
||||
when(systemContext.getTenantService()).thenReturn(tenantService); |
|
||||
when(systemContext.isTenantComponentsInitEnabled()).thenReturn(false); |
|
||||
when(systemContext.getServiceInfoProvider()).thenReturn(mock(TbServiceInfoProvider.class)); |
when(systemContext.getServiceInfoProvider()).thenReturn(mock(TbServiceInfoProvider.class)); |
||||
|
when(systemContext.isTenantComponentsInitEnabled()).thenReturn(false); |
||||
|
|
||||
appActor = (AppActor) new AppActor.ActorCreator(systemContext).createActor(); |
executor = Executors.newSingleThreadExecutor(); |
||||
ctx = mock(TbActorCtx.class); |
system = new DefaultTbActorSystem(new TbActorSystemSettings(5, 1, 42)); |
||||
appActor.init(ctx); |
system.createDispatcher(DISPATCHER, executor); |
||||
appActor.doProcess(new AppInitMsg()); |
appActor = system.createRootActor(DISPATCHER, new AppActor.ActorCreator(systemContext)); |
||||
} |
appActor.tellWithHighPriority(new AppInitMsg()); |
||||
|
|
||||
@Test |
tenant1Msgs = createRecordingChild(new TbEntityActorId(tenant1)); |
||||
public void onPartitionChangeMsgWithAffectedTenants_onlyForwardsToThoseTenants() { |
tenant2Msgs = createRecordingChild(new TbEntityActorId(tenant2)); |
||||
PartitionChangeMsg msg = new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE, Set.of(tenant1)); |
otherChildMsgs = createRecordingChild(new TbStringActorId("other")); |
||||
|
} |
||||
|
|
||||
appActor.doProcess(msg); |
@AfterEach |
||||
|
void tearDown() { |
||||
|
system.stop(); |
||||
|
executor.shutdownNow(); |
||||
|
} |
||||
|
|
||||
@SuppressWarnings("unchecked") |
@Test |
||||
ArgumentCaptor<Predicate<TbActorId>> filterCaptor = ArgumentCaptor.forClass(Predicate.class); |
void msgForTenants_isDeliveredToAffectedTenantActorsOnly() { |
||||
verify(ctx).broadcastToChildren(eq(msg), filterCaptor.capture(), eq(true)); |
PartitionChangeMsg msg = PartitionChangeMsg.forTenants(ServiceType.TB_RULE_ENGINE, Set.of(tenant1)); |
||||
Predicate<TbActorId> filter = filterCaptor.getValue(); |
appActor.tellWithHighPriority(msg); |
||||
|
// processed by the app actor after msg, so it reaches every child after msg would have
|
||||
|
PartitionChangeMsg marker = PartitionChangeMsg.forAllTenants(ServiceType.TB_RULE_ENGINE); |
||||
|
appActor.tellWithHighPriority(marker); |
||||
|
|
||||
assertTrue(filter.test(new TbEntityActorId(tenant1))); |
await().atMost(10, TimeUnit.SECONDS).until(() -> tenant2Msgs.contains(marker) && otherChildMsgs.contains(marker)); |
||||
assertFalse(filter.test(new TbEntityActorId(tenant2))); |
assertThat(tenant1Msgs).containsExactly(msg, marker); |
||||
assertFalse(filter.test(new TbEntityActorId(new DeviceId(UUID.randomUUID())))); |
assertThat(tenant2Msgs).containsExactly(marker); |
||||
|
assertThat(otherChildMsgs).containsExactly(marker); |
||||
} |
} |
||||
|
|
||||
@Test |
@Test |
||||
public void onPartitionChangeMsgWithNullAffectedTenants_broadcastsToEveryone() { |
void msgForAllTenants_isDeliveredToEveryChild() { |
||||
PartitionChangeMsg msg = new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE); |
PartitionChangeMsg msg = PartitionChangeMsg.forAllTenants(ServiceType.TB_RULE_ENGINE); |
||||
|
appActor.tellWithHighPriority(msg); |
||||
|
|
||||
appActor.doProcess(msg); |
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { |
||||
|
assertThat(tenant1Msgs).containsExactly(msg); |
||||
|
assertThat(tenant2Msgs).containsExactly(msg); |
||||
|
assertThat(otherChildMsgs).containsExactly(msg); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private List<TbActorMsg> createRecordingChild(TbActorId actorId) { |
||||
|
List<TbActorMsg> received = new CopyOnWriteArrayList<>(); |
||||
|
system.createChildActor(DISPATCHER, new TbActorCreator() { |
||||
|
@Override |
||||
|
public TbActorId createActorId() { |
||||
|
return actorId; |
||||
|
} |
||||
|
|
||||
verify(ctx).broadcastToChildren(msg, true); |
@Override |
||||
verify(ctx, never()).broadcastToChildren(any(), any(), anyBoolean()); |
public TbActor createActor() { |
||||
|
return new AbstractTbActor() { |
||||
|
@Override |
||||
|
public boolean process(TbActorMsg msg) { |
||||
|
received.add(msg); |
||||
|
return true; |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
}, appActor.getActorId()); |
||||
|
return received; |
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
|||||
@ -1,47 +1,49 @@ |
|||||
/** |
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
||||
* Copyright © 2016-2026 The Thingsboard Authors |
// SPDX-License-Identifier: Apache-2.0
|
||||
* |
|
||||
* 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 org.thingsboard.server.common.msg.queue; |
package org.thingsboard.server.common.msg.queue; |
||||
|
|
||||
import org.junit.jupiter.api.Test; |
import org.junit.jupiter.api.Test; |
||||
import org.thingsboard.server.common.data.id.TenantId; |
import org.thingsboard.server.common.data.id.TenantId; |
||||
import org.thingsboard.server.common.msg.MsgType; |
import org.thingsboard.server.common.msg.MsgType; |
||||
|
|
||||
|
import java.util.HashSet; |
||||
import java.util.Set; |
import java.util.Set; |
||||
import java.util.UUID; |
import java.util.UUID; |
||||
|
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.junit.jupiter.api.Assertions.assertNull; |
|
||||
|
|
||||
public class PartitionChangeMsgTest { |
public class PartitionChangeMsgTest { |
||||
|
|
||||
|
private final TenantId tenant1 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
private final TenantId tenant2 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
|
||||
@Test |
@Test |
||||
public void singleArgConstructor_defaultsAffectedTenantsToNull() { |
public void forAllTenants_affectsEveryTenant() { |
||||
PartitionChangeMsg msg = new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE); |
PartitionChangeMsg msg = PartitionChangeMsg.forAllTenants(ServiceType.TB_RULE_ENGINE); |
||||
|
|
||||
|
assertThat(msg.getServiceType()).isEqualTo(ServiceType.TB_RULE_ENGINE); |
||||
|
assertThat(msg.getMsgType()).isEqualTo(MsgType.PARTITION_CHANGE_MSG); |
||||
|
assertThat(msg.isForAllTenants()).isTrue(); |
||||
|
assertThat(msg.isAffected(tenant1)).isTrue(); |
||||
|
assertThat(msg.isAffected(tenant2)).isTrue(); |
||||
|
} |
||||
|
|
||||
assertEquals(ServiceType.TB_RULE_ENGINE, msg.getServiceType()); |
@Test |
||||
assertNull(msg.getAffectedTenants()); |
public void forTenants_affectsOnlyGivenTenants() { |
||||
assertEquals(MsgType.PARTITION_CHANGE_MSG, msg.getMsgType()); |
Set<TenantId> tenants = new HashSet<>(Set.of(tenant1)); |
||||
|
PartitionChangeMsg msg = PartitionChangeMsg.forTenants(ServiceType.TB_RULE_ENGINE, tenants); |
||||
|
tenants.add(tenant2); |
||||
|
|
||||
|
assertThat(msg.isForAllTenants()).isFalse(); |
||||
|
assertThat(msg.isAffected(tenant1)).isTrue(); |
||||
|
assertThat(msg.isAffected(tenant2)).isFalse(); |
||||
} |
} |
||||
|
|
||||
@Test |
@Test |
||||
public void twoArgConstructor_carriesAffectedTenants() { |
public void toString_doesNotListAffectedTenants() { |
||||
TenantId tenant = new TenantId(UUID.randomUUID()); |
PartitionChangeMsg msg = PartitionChangeMsg.forTenants(ServiceType.TB_RULE_ENGINE, Set.of(tenant1)); |
||||
PartitionChangeMsg msg = new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE, Set.of(tenant)); |
|
||||
|
|
||||
assertEquals(ServiceType.TB_RULE_ENGINE, msg.getServiceType()); |
assertThat(msg.toString()).doesNotContain(tenant1.getId().toString()); |
||||
assertEquals(Set.of(tenant), msg.getAffectedTenants()); |
|
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
|||||
@ -0,0 +1,88 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||
|
package org.thingsboard.server.queue.discovery.event; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.junit.jupiter.params.ParameterizedTest; |
||||
|
import org.junit.jupiter.params.provider.EnumSource; |
||||
|
import org.thingsboard.server.common.data.DataConstants; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.msg.queue.ServiceType; |
||||
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
||||
|
import org.thingsboard.server.queue.discovery.QueueKey; |
||||
|
|
||||
|
import java.util.Collections; |
||||
|
import java.util.Map; |
||||
|
import java.util.Set; |
||||
|
import java.util.UUID; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
|
||||
|
class PartitionChangeEventTest { |
||||
|
|
||||
|
private final TenantId tenant1 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
private final TenantId tenant2 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
private final TenantId tenant3 = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
|
||||
|
@Test |
||||
|
void isolatedTenantQueuesChanged_returnsTheirTenants() { |
||||
|
PartitionChangeEvent event = ruleEngineEvent(Set.of( |
||||
|
queueKey(DataConstants.MAIN_QUEUE_NAME, tenant1), |
||||
|
queueKey(DataConstants.HP_QUEUE_NAME, tenant1), |
||||
|
queueKey(DataConstants.MAIN_QUEUE_NAME, tenant2)), Collections.emptySet()); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).containsExactlyInAnyOrder(tenant1, tenant2); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void managedStatusChangedTenantsAreIncluded() { |
||||
|
PartitionChangeEvent event = ruleEngineEvent(Set.of(queueKey(DataConstants.MAIN_QUEUE_NAME, tenant1)), Set.of(tenant3)); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).containsExactlyInAnyOrder(tenant1, tenant3); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void sharedQueueChanged_returnsNull() { |
||||
|
PartitionChangeEvent event = ruleEngineEvent(Set.of(queueKey(DataConstants.MAIN_QUEUE_NAME, TenantId.SYS_TENANT_ID)), Collections.emptySet()); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).isNull(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void sharedAndIsolatedQueuesChanged_returnsNull() { |
||||
|
PartitionChangeEvent event = ruleEngineEvent(Set.of( |
||||
|
queueKey(DataConstants.MAIN_QUEUE_NAME, TenantId.SYS_TENANT_ID), |
||||
|
queueKey(DataConstants.MAIN_QUEUE_NAME, tenant1)), Set.of(tenant3)); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).isNull(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void noQueuesChanged_returnsNull() { |
||||
|
PartitionChangeEvent event = ruleEngineEvent(Collections.emptySet(), Set.of(tenant3)); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).isNull(); |
||||
|
} |
||||
|
|
||||
|
@ParameterizedTest |
||||
|
@EnumSource(value = ServiceType.class, names = "TB_RULE_ENGINE", mode = EnumSource.Mode.EXCLUDE) |
||||
|
void otherServiceType_returnsNull(ServiceType serviceType) { |
||||
|
QueueKey queueKey = new QueueKey(serviceType, DataConstants.MAIN_QUEUE_NAME, tenant1); |
||||
|
PartitionChangeEvent event = new PartitionChangeEvent(this, serviceType, |
||||
|
Map.of(queueKey, Collections.emptySet()), Collections.emptyMap(), Set.of(tenant3)); |
||||
|
|
||||
|
assertThat(event.getAffectedTenants()).isNull(); |
||||
|
} |
||||
|
|
||||
|
private PartitionChangeEvent ruleEngineEvent(Set<QueueKey> changedQueueKeys, Set<TenantId> managedStatusChangedTenants) { |
||||
|
Map<QueueKey, Set<TopicPartitionInfo>> newPartitions = changedQueueKeys.stream() |
||||
|
.collect(Collectors.toMap(queueKey -> queueKey, queueKey -> Collections.emptySet())); |
||||
|
return new PartitionChangeEvent(this, ServiceType.TB_RULE_ENGINE, newPartitions, Collections.emptyMap(), managedStatusChangedTenants); |
||||
|
} |
||||
|
|
||||
|
private QueueKey queueKey(String queueName, TenantId tenantId) { |
||||
|
return new QueueKey(ServiceType.TB_RULE_ENGINE, queueName, tenantId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue