SQL_RELATIONS_MAX_LEVEL parameter added

This commit is contained in:
Sergey Matvienko 2021-10-15 14:36:28 +03:00 committed by Andrew Shvayka
parent 77164c3002
commit 2c743f647c
3 changed files with 41 additions and 17 deletions

View File

@ -279,6 +279,8 @@ sql:
rpc:
enabled: "${SQL_TTL_RPC_ENABLED:true}"
checking_interval: "${SQL_RPC_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours
relations:
max_level: "${SQL_RELATIONS_MAX_LEVEL:50}" # //This value has to be reasonable small to prevent infinite recursion as early as possible
# Actor system parameters
actors:

View File

@ -15,8 +15,10 @@
*/
package org.thingsboard.server.dao.sql.query;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate;
@ -223,7 +225,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
private static final String SELECT_API_USAGE_STATE = "(select aus.id, aus.created_time, aus.tenant_id, aus.entity_id, " +
"coalesce((select title from tenant where id = aus.entity_id), (select title from customer where id = aus.entity_id)) as name " +
"from api_usage_state as aus)";
static final int MAX_LEVEL_DEFAULT = 50; //This value has to be reasonable small to prevent infinite recursion as early as possible
static {
entityTableMap.put(EntityType.ASSET, "asset");
@ -268,6 +269,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from");
private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to");
@Getter
@Value("${sql.relations.max_level:50}")
int maxLevelAllowed; //This value has to be reasonable small to prevent infinite recursion as early as possible
private final NamedParameterJdbcTemplate jdbcTemplate;
private final TransactionTemplate transactionTemplate;
private final DefaultQueryLogComponent queryLog;
@ -711,7 +716,12 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
}
int getMaxLevel(int maxLevel) {
return maxLevel > 0 ? maxLevel : MAX_LEVEL_DEFAULT;
int level = maxLevel > 0 ? maxLevel : this.maxLevelAllowed;
if (level > this.maxLevelAllowed) {
log.debug("hierarchy level {} is reduced down to maxLevelAllowed {}", level, this.maxLevelAllowed);
return this.maxLevelAllowed;
}
return level;
}
private String getQueryTemplate(EntitySearchDirection direction) {

View File

@ -16,42 +16,54 @@
package org.thingsboard.server.dao.sql.query;
import org.junit.Test;
import org.thingsboard.server.common.data.id.CustomerId;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.support.TransactionTemplate;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.willCallRealMethod;
import static org.mockito.Mockito.mock;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DefaultEntityQueryRepository.class)
public class DefaultEntityQueryRepositoryTest {
@MockBean
NamedParameterJdbcTemplate jdbcTemplate;
@MockBean
TransactionTemplate transactionTemplate;
@MockBean
DefaultQueryLogComponent queryLog;
@Autowired
DefaultEntityQueryRepository repo;
/*
* This value has to be reasonable small to prevent infinite recursion as early as possible
* */
@Test
public void givenDefaultMaxLevel_whenStaticConstant_thenEqualsTo() {
assertThat(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT, equalTo(10));
assertThat(repo.getMaxLevelAllowed(), equalTo(50));
}
@Test
public void givenMaxLevelZeroOrNegative_whenGetMaxLevel_thenReturnDefaultMaxLevel() {
DefaultEntityQueryRepository repo = mock(DefaultEntityQueryRepository.class);
willCallRealMethod().given(repo).getMaxLevel(anyInt());
assertThat(repo.getMaxLevel(0), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
assertThat(repo.getMaxLevel(-1), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
assertThat(repo.getMaxLevel(-2), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
assertThat(repo.getMaxLevel(Integer.MIN_VALUE), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
assertThat(repo.getMaxLevel(0), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(-1), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(-2), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(Integer.MIN_VALUE), equalTo(repo.getMaxLevelAllowed()));
}
@Test
public void givenMaxLevelPositive_whenGetMaxLevel_thenValueTheSame() {
DefaultEntityQueryRepository repo = mock(DefaultEntityQueryRepository.class);
willCallRealMethod().given(repo).getMaxLevel(anyInt());
assertThat(repo.getMaxLevel(1), equalTo(1));
assertThat(repo.getMaxLevel(2), equalTo(2));
assertThat(repo.getMaxLevel(Integer.MAX_VALUE), equalTo(Integer.MAX_VALUE));
assertThat(repo.getMaxLevel(repo.getMaxLevelAllowed()), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(repo.getMaxLevelAllowed() + 1), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(Integer.MAX_VALUE), equalTo(repo.getMaxLevelAllowed()));
}
}