RPC deleteOutdatedRpcByTenantId simple native query. the test added
This commit is contained in:
parent
6c6b606fdf
commit
c3e8c62ffc
@ -68,7 +68,7 @@ public class RpcCleanUpService {
|
||||
long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getRpcTtlDays());
|
||||
long expirationTime = System.currentTimeMillis() - ttl;
|
||||
|
||||
long totalRemoved = rpcDao.deleteOutdatedRpcByTenantId(tenantId, expirationTime);
|
||||
int totalRemoved = rpcDao.deleteOutdatedRpcByTenantId(tenantId, expirationTime);
|
||||
|
||||
if (totalRemoved > 0) {
|
||||
log.info("Removed {} outdated rpc(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime));
|
||||
|
||||
@ -30,5 +30,6 @@ public interface RpcDao extends Dao<Rpc> {
|
||||
|
||||
PageData<Rpc> findAllRpcByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
Long deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime);
|
||||
int deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime);
|
||||
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
@ -67,8 +68,9 @@ public class JpaRpcDao extends JpaAbstractDao<RpcEntity, Rpc> implements RpcDao
|
||||
return DaoUtil.toPageData(rpcRepository.findAllByTenantId(tenantId.getId(), DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Long deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime) {
|
||||
public int deleteOutdatedRpcByTenantId(TenantId tenantId, Long expirationTime) {
|
||||
return rpcRepository.deleteOutdatedRpcByTenantId(tenantId.getId(), expirationTime);
|
||||
}
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.server.dao.sql.rpc;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.thingsboard.server.common.data.rpc.RpcStatus;
|
||||
@ -32,7 +33,8 @@ public interface RpcRepository extends JpaRepository<RpcEntity, UUID> {
|
||||
|
||||
Page<RpcEntity> findAllByTenantId(UUID tenantId, Pageable pageable);
|
||||
|
||||
@Query(value = "WITH deleted AS (DELETE FROM rpc WHERE (tenant_id = :tenantId AND created_time < :expirationTime) IS TRUE RETURNING *) SELECT count(*) FROM deleted",
|
||||
@Modifying
|
||||
@Query(value = "DELETE FROM rpc WHERE tenant_id = :tenantId AND created_time < :expirationTime",
|
||||
nativeQuery = true)
|
||||
Long deleteOutdatedRpcByTenantId(@Param("tenantId") UUID tenantId, @Param("expirationTime") Long expirationTime);
|
||||
int deleteOutdatedRpcByTenantId(@Param("tenantId") UUID tenantId, @Param("expirationTime") Long expirationTime);
|
||||
}
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright © 2016-2024 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.dao.sql.rpc;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.rpc.Rpc;
|
||||
import org.thingsboard.server.common.data.rpc.RpcStatus;
|
||||
import org.thingsboard.server.dao.AbstractJpaDaoTest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JpaRpcDaoTest extends AbstractJpaDaoTest {
|
||||
|
||||
@Autowired
|
||||
JpaRpcDao rpcDao;
|
||||
|
||||
@Test
|
||||
public void deleteOutdated() {
|
||||
Rpc rpc = new Rpc();
|
||||
rpc.setTenantId(TenantId.SYS_TENANT_ID);
|
||||
rpc.setDeviceId(new DeviceId(UUID.randomUUID()));
|
||||
rpc.setStatus(RpcStatus.QUEUED);
|
||||
rpc.setRequest(JacksonUtil.toJsonNode("{}"));
|
||||
rpcDao.saveAndFlush(rpc.getTenantId(), rpc);
|
||||
|
||||
rpc.setId(null);
|
||||
rpcDao.saveAndFlush(rpc.getTenantId(), rpc);
|
||||
|
||||
TenantId tenantId = TenantId.fromUUID(UUID.fromString("3d193a7a-774b-4c05-84d5-f7fdcf7a37cf"));
|
||||
rpc.setId(null);
|
||||
rpc.setTenantId(tenantId);
|
||||
rpc.setDeviceId(new DeviceId(UUID.randomUUID()));
|
||||
rpcDao.saveAndFlush(rpc.getTenantId(), rpc);
|
||||
|
||||
assertThat(rpcDao.deleteOutdatedRpcByTenantId(TenantId.SYS_TENANT_ID, 0L)).isEqualTo(0);
|
||||
assertThat(rpcDao.deleteOutdatedRpcByTenantId(TenantId.SYS_TENANT_ID, Long.MAX_VALUE)).isEqualTo(2);
|
||||
assertThat(rpcDao.deleteOutdatedRpcByTenantId(tenantId, System.currentTimeMillis() + 1)).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user