Stats: do not persist empty stats (reducing event table size and disk IOPS) (#5554)

* stats: do not persist empty stats

* fixed license headers for stat actor and stat msg tests
This commit is contained in:
Sergey Matvienko 2021-11-15 10:03:01 +02:00 committed by GitHub
parent f2974532c6
commit c0514ca3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 0 deletions

View File

@ -51,6 +51,9 @@ public class StatsActor extends ContextAwareActor {
} }
public void onStatsPersistMsg(StatsPersistMsg msg) { public void onStatsPersistMsg(StatsPersistMsg msg) {
if (msg.isEmpty()) {
return;
}
Event event = new Event(); Event event = new Event();
event.setEntityId(msg.getEntityId()); event.setEntityId(msg.getEntityId());
event.setTenantId(msg.getTenantId()); event.setTenantId(msg.getTenantId());

View File

@ -37,4 +37,9 @@ public final class StatsPersistMsg implements TbActorMsg {
public MsgType getMsgType() { public MsgType getMsgType() {
return MsgType.STATS_PERSIST_MSG; return MsgType.STATS_PERSIST_MSG;
} }
public boolean isEmpty() {
return messagesProcessed == 0 && errorsOccurred == 0;
}
} }

View File

@ -0,0 +1,69 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* 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.stats;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.actors.ActorSystemContext;
import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.event.EventService;
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class StatsActorTest {
StatsActor statsActor;
ActorSystemContext actorSystemContext;
EventService eventService;
TbServiceInfoProvider serviceInfoProvider;
@BeforeEach
void setUp() {
actorSystemContext = mock(ActorSystemContext.class);
eventService = mock(EventService.class);
willReturn(eventService).given(actorSystemContext).getEventService();
serviceInfoProvider = mock(TbServiceInfoProvider.class);
willReturn(serviceInfoProvider).given(actorSystemContext).getServiceInfoProvider();
statsActor = new StatsActor(actorSystemContext);
}
@Test
void givenEmptyStatMessage_whenOnStatsPersistMsg_thenNoAction() {
StatsPersistMsg emptyStats = new StatsPersistMsg(0, 0, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID);
statsActor.onStatsPersistMsg(emptyStats);
verify(actorSystemContext, never()).getEventService();
}
@Test
void givenNonEmptyStatMessage_whenOnStatsPersistMsg_thenNoAction() {
statsActor.onStatsPersistMsg(new StatsPersistMsg(0, 1, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID));
verify(eventService, times(1)).save(any(Event.class));
statsActor.onStatsPersistMsg(new StatsPersistMsg(1, 0, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID));
verify(eventService, times(2)).save(any(Event.class));
statsActor.onStatsPersistMsg(new StatsPersistMsg(1, 1, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID));
verify(eventService, times(3)).save(any(Event.class));
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* 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.stats;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.common.data.id.TenantId;
import static org.assertj.core.api.Assertions.assertThat;
class StatsPersistMsgTest {
@Test
void testIsEmpty() {
StatsPersistMsg emptyStats = new StatsPersistMsg(0, 0, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID);
assertThat(emptyStats.isEmpty()).isTrue();
}
@Test
void testNotEmpty() {
assertThat(new StatsPersistMsg(1, 0, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isEmpty()).isFalse();
assertThat(new StatsPersistMsg(0, 1, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isEmpty()).isFalse();
assertThat(new StatsPersistMsg(1, 1, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isEmpty()).isFalse();
}
}