Merge pull request #10072 from thingsboard/feature/integration-executor-tenant-profile
Tenant profiles support for integration executor (for compatibility with PE)
This commit is contained in:
		
						commit
						13baa7143a
					
				@ -26,7 +26,7 @@ import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.TenantProfileNotFoundException;
 | 
			
		||||
import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.exception.ThingsboardErrorResponseHandler;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -48,7 +48,7 @@ import org.thingsboard.server.common.data.security.event.UserSessionInvalidation
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.JwtPair;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.SecuritySettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.UserPasswordPolicy;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.rest.RestAuthenticationDetails;
 | 
			
		||||
import org.thingsboard.server.service.security.model.ActivateUserRequest;
 | 
			
		||||
 | 
			
		||||
@ -43,7 +43,7 @@ import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.config.WebSocketConfiguration;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.jwt.JwtAuthenticationProvider;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
@ -62,7 +62,7 @@ import org.thingsboard.server.dao.notification.NotificationService;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationSettingsService;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationTargetService;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationTemplateService;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos;
 | 
			
		||||
import org.thingsboard.server.queue.common.TbProtoQueueMsg;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.TopicService;
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,7 @@ import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor;
 | 
			
		||||
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
 | 
			
		||||
import org.thingsboard.server.common.msg.queue.ServiceType;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationRequestService;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.PartitionService;
 | 
			
		||||
import org.thingsboard.server.queue.notification.NotificationDeduplicationService;
 | 
			
		||||
import org.thingsboard.server.service.executors.NotificationExecutorService;
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFaProvi
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFaProviderType;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFaConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFaProvider;
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ import org.thingsboard.server.common.data.sync.ie.EntityImportResult;
 | 
			
		||||
import org.thingsboard.server.common.data.util.ThrowingRunnable;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationService;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.entitiy.TbNotificationEntityService;
 | 
			
		||||
import org.thingsboard.server.service.sync.ie.exporting.EntityExportService;
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,8 @@ import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.mockito.Mockito;
 | 
			
		||||
import org.mockito.junit.MockitoJUnitRunner;
 | 
			
		||||
import org.thingsboard.server.cache.limits.DefaultRateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.common.data.TenantProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.NotificationRuleId;
 | 
			
		||||
@ -28,9 +30,7 @@ import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
 | 
			
		||||
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.DefaultRateLimitService;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.DefaultTbTenantProfileCache;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
@ -46,12 +46,12 @@ import static org.mockito.Mockito.when;
 | 
			
		||||
public class RateLimitServiceTest {
 | 
			
		||||
 | 
			
		||||
    private RateLimitService rateLimitService;
 | 
			
		||||
    private TbTenantProfileCache tenantProfileCache;
 | 
			
		||||
    private DefaultTbTenantProfileCache tenantProfileCache;
 | 
			
		||||
    private TenantId tenantId;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void beforeEach() {
 | 
			
		||||
        tenantProfileCache = Mockito.mock(TbTenantProfileCache.class);
 | 
			
		||||
        tenantProfileCache = Mockito.mock(DefaultTbTenantProfileCache.class);
 | 
			
		||||
        rateLimitService = new DefaultRateLimitService(tenantProfileCache, mock(NotificationRuleProcessor.class), 60, 100);
 | 
			
		||||
        tenantId = new TenantId(UUID.randomUUID());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -93,7 +93,7 @@ import org.thingsboard.server.dao.notification.DefaultNotifications;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationRequestService;
 | 
			
		||||
import org.thingsboard.server.dao.rule.RuleChainService;
 | 
			
		||||
import org.thingsboard.server.dao.service.DaoSqlTest;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.queue.notification.DefaultNotificationDeduplicationService;
 | 
			
		||||
import org.thingsboard.server.service.notification.rule.cache.DefaultNotificationRulesCache;
 | 
			
		||||
import org.thingsboard.server.service.state.DeviceStateService;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								common/cache/pom.xml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								common/cache/pom.xml
									
									
									
									
										vendored
									
									
								
							@ -40,6 +40,10 @@
 | 
			
		||||
            <groupId>org.thingsboard.common</groupId>
 | 
			
		||||
            <artifactId>data</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.thingsboard.common</groupId>
 | 
			
		||||
            <artifactId>message</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-autoconfigure</artifactId>
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.util.limits;
 | 
			
		||||
package org.thingsboard.server.cache.limits;
 | 
			
		||||
 | 
			
		||||
import com.github.benmanes.caffeine.cache.Cache;
 | 
			
		||||
import com.github.benmanes.caffeine.cache.Caffeine;
 | 
			
		||||
@ -28,25 +28,25 @@ import org.thingsboard.server.common.data.exception.TenantProfileNotFoundExcepti
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor;
 | 
			
		||||
import org.thingsboard.server.common.data.notification.rule.trigger.RateLimitsTrigger;
 | 
			
		||||
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor;
 | 
			
		||||
import org.thingsboard.server.common.msg.tools.TbRateLimits;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
@Lazy
 | 
			
		||||
@Service
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class DefaultRateLimitService implements RateLimitService {
 | 
			
		||||
 | 
			
		||||
    private final TbTenantProfileCache tenantProfileCache;
 | 
			
		||||
    private final TenantProfileProvider tenantProfileProvider;
 | 
			
		||||
    private final NotificationRuleProcessor notificationRuleProcessor;
 | 
			
		||||
 | 
			
		||||
    public DefaultRateLimitService(TbTenantProfileCache tenantProfileCache,
 | 
			
		||||
    public DefaultRateLimitService(TenantProfileProvider tenantProfileProvider,
 | 
			
		||||
                                   @Lazy NotificationRuleProcessor notificationRuleProcessor,
 | 
			
		||||
                                   @Value("${cache.rateLimits.timeToLiveInMinutes:120}") int rateLimitsTtl,
 | 
			
		||||
                                   @Value("${cache.rateLimits.maxSize:200000}") int rateLimitsCacheMaxSize) {
 | 
			
		||||
        this.tenantProfileCache = tenantProfileCache;
 | 
			
		||||
        this.tenantProfileProvider = tenantProfileProvider;
 | 
			
		||||
        this.notificationRuleProcessor = notificationRuleProcessor;
 | 
			
		||||
        this.rateLimits = Caffeine.newBuilder()
 | 
			
		||||
                .expireAfterAccess(rateLimitsTtl, TimeUnit.MINUTES)
 | 
			
		||||
@ -66,7 +66,7 @@ public class DefaultRateLimitService implements RateLimitService {
 | 
			
		||||
        if (tenantId.isSysTenantId()) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
 | 
			
		||||
        TenantProfile tenantProfile = tenantProfileProvider.get(tenantId);
 | 
			
		||||
        if (tenantProfile == null) {
 | 
			
		||||
            throw new TenantProfileNotFoundException(tenantId);
 | 
			
		||||
        }
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.util.limits;
 | 
			
		||||
package org.thingsboard.server.cache.limits;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
							
								
								
									
										25
									
								
								common/cache/src/main/java/org/thingsboard/server/cache/limits/TenantProfileProvider.java
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								common/cache/src/main/java/org/thingsboard/server/cache/limits/TenantProfileProvider.java
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 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.cache.limits;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.TenantProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
 | 
			
		||||
public interface TenantProfileProvider {
 | 
			
		||||
 | 
			
		||||
    TenantProfile get(TenantId tenantId);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -27,7 +27,7 @@ import org.thingsboard.server.dao.entity.EntityService;
 | 
			
		||||
import org.thingsboard.server.dao.util.AbstractBufferedRateExecutor;
 | 
			
		||||
import org.thingsboard.server.dao.util.AsyncTaskContext;
 | 
			
		||||
import org.thingsboard.server.dao.util.NoSqlAnyDao;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.PreDestroy;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -27,7 +27,7 @@ import org.thingsboard.server.dao.entity.EntityService;
 | 
			
		||||
import org.thingsboard.server.dao.util.AbstractBufferedRateExecutor;
 | 
			
		||||
import org.thingsboard.server.dao.util.AsyncTaskContext;
 | 
			
		||||
import org.thingsboard.server.dao.util.NoSqlAnyDao;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.PreDestroy;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,7 @@ package org.thingsboard.server.dao.tenant;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.server.cache.limits.TenantProfileProvider;
 | 
			
		||||
import org.thingsboard.server.common.data.Tenant;
 | 
			
		||||
import org.thingsboard.server.common.data.TenantProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
@ -31,7 +32,7 @@ import java.util.function.Consumer;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class DefaultTbTenantProfileCache implements TbTenantProfileCache {
 | 
			
		||||
public class DefaultTbTenantProfileCache implements TbTenantProfileCache, TenantProfileProvider {
 | 
			
		||||
 | 
			
		||||
    private final Lock tenantProfileFetchLock = new ReentrantLock();
 | 
			
		||||
    private final TenantProfileService tenantProfileService;
 | 
			
		||||
 | 
			
		||||
@ -38,7 +38,7 @@ import org.thingsboard.server.common.stats.StatsType;
 | 
			
		||||
import org.thingsboard.server.dao.entity.EntityService;
 | 
			
		||||
import org.thingsboard.server.dao.nosql.CassandraStatementTask;
 | 
			
		||||
import org.thingsboard.server.common.data.limit.LimitedApi;
 | 
			
		||||
import org.thingsboard.server.dao.util.limits.RateLimitService;
 | 
			
		||||
import org.thingsboard.server.cache.limits.RateLimitService;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user