Commit 5f50fd44d2e6fbb53f4cab1a32bb52fc69748c35

Authored by Andrii Shvaika
1 parent 3518a3d9

Usage Records implementation

@@ -93,6 +93,8 @@ public class TenantController extends BaseController { @@ -93,6 +93,8 @@ public class TenantController extends BaseController {
93 } 93 }
94 tenantProfileCache.evict(tenant.getId()); 94 tenantProfileCache.evict(tenant.getId());
95 tbClusterService.onTenantChange(tenant, null); 95 tbClusterService.onTenantChange(tenant, null);
  96 + tbClusterService.onEntityStateChange(tenant.getId(), tenant.getId(),
  97 + newTenant ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
96 return tenant; 98 return tenant;
97 } catch (Exception e) { 99 } catch (Exception e) {
98 throw handleException(e); 100 throw handleException(e);
@@ -243,6 +243,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { @@ -243,6 +243,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
243 243
244 private void updateTenantState(TenantApiUsageState state, TenantProfile tenantProfile) { 244 private void updateTenantState(TenantApiUsageState state, TenantProfile tenantProfile) {
245 TenantProfileData oldProfileData = state.getTenantProfileData(); 245 TenantProfileData oldProfileData = state.getTenantProfileData();
  246 + state.setTenantProfileId(tenantProfile.getId());
246 state.setTenantProfileData(tenantProfile.getProfileData()); 247 state.setTenantProfileData(tenantProfile.getProfileData());
247 Map<ApiFeature, Boolean> result = state.checkStateUpdatedDueToThresholds(); 248 Map<ApiFeature, Boolean> result = state.checkStateUpdatedDueToThresholds();
248 if (!result.isEmpty()) { 249 if (!result.isEmpty()) {
@@ -257,8 +258,10 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { @@ -257,8 +258,10 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
257 long ts = System.currentTimeMillis(); 258 long ts = System.currentTimeMillis();
258 List<TsKvEntry> profileThresholds = new ArrayList<>(); 259 List<TsKvEntry> profileThresholds = new ArrayList<>();
259 for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) { 260 for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
260 - if (oldData == null || oldData.getProfileThreshold(key) != newData.getProfileThreshold(key)) {  
261 - profileThresholds.add(new BasicTsKvEntry(ts, new LongDataEntry(key.getApiLimitKey(), newData.getProfileThreshold(key)))); 261 + long newProfileThreshold = newData.getProfileThreshold(key);
  262 + if (oldData == null || oldData.getProfileThreshold(key) != newProfileThreshold) {
  263 + log.info("[{}] Updating profile threshold [{}]:[{}]", tenantId, key, newProfileThreshold);
  264 + profileThresholds.add(new BasicTsKvEntry(ts, new LongDataEntry(key.getApiLimitKey(), newProfileThreshold)));
262 } 265 }
263 } 266 }
264 if (!profileThresholds.isEmpty()) { 267 if (!profileThresholds.isEmpty()) {
@@ -267,6 +270,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { @@ -267,6 +270,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
267 } 270 }
268 271
269 private void persistAndNotify(TenantApiUsageState state, Map<ApiFeature, Boolean> result) { 272 private void persistAndNotify(TenantApiUsageState state, Map<ApiFeature, Boolean> result) {
  273 + log.info("[{}] Detected update of the API state: {}", state.getTenantId(), result);
270 apiUsageStateService.update(state.getApiUsageState()); 274 apiUsageStateService.update(state.getApiUsageState());
271 clusterService.onApiStateChange(state.getApiUsageState(), null); 275 clusterService.onApiStateChange(state.getApiUsageState(), null);
272 long ts = System.currentTimeMillis(); 276 long ts = System.currentTimeMillis();
@@ -320,6 +324,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService { @@ -320,6 +324,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
320 } 324 }
321 } 325 }
322 } 326 }
  327 + log.debug("[{}] Initialized state: {}", tenantId, dbStateEntity);
323 myTenantStates.put(tenantId, tenantState); 328 myTenantStates.put(tenantId, tenantState);
324 } catch (InterruptedException | ExecutionException e) { 329 } catch (InterruptedException | ExecutionException e) {
325 log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e); 330 log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e);
@@ -21,6 +21,7 @@ import lombok.Data; @@ -21,6 +21,7 @@ import lombok.Data;
21 import lombok.EqualsAndHashCode; 21 import lombok.EqualsAndHashCode;
22 import lombok.extern.slf4j.Slf4j; 22 import lombok.extern.slf4j.Slf4j;
23 import org.thingsboard.server.common.data.id.TenantProfileId; 23 import org.thingsboard.server.common.data.id.TenantProfileId;
  24 +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
24 import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; 25 import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
25 26
26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayInputStream;
@@ -79,15 +80,21 @@ public class TenantProfile extends SearchTextBased<TenantProfileId> implements H @@ -79,15 +80,21 @@ public class TenantProfile extends SearchTextBased<TenantProfileId> implements H
79 profileData = mapper.readValue(new ByteArrayInputStream(profileDataBytes), TenantProfileData.class); 80 profileData = mapper.readValue(new ByteArrayInputStream(profileDataBytes), TenantProfileData.class);
80 } catch (IOException e) { 81 } catch (IOException e) {
81 log.warn("Can't deserialize tenant profile data: ", e); 82 log.warn("Can't deserialize tenant profile data: ", e);
82 - return null; 83 + return createDefaultTenantProfileData();
83 } 84 }
84 return profileData; 85 return profileData;
85 } else { 86 } else {
86 - return null; 87 + return createDefaultTenantProfileData();
87 } 88 }
88 } 89 }
89 } 90 }
90 91
  92 + public TenantProfileData createDefaultTenantProfileData() {
  93 + TenantProfileData tpd = new TenantProfileData();
  94 + tpd.setConfiguration(new DefaultTenantProfileConfiguration());
  95 + return tpd;
  96 + }
  97 +
91 public void setProfileData(TenantProfileData data) { 98 public void setProfileData(TenantProfileData data) {
92 this.profileData = data; 99 this.profileData = data;
93 try { 100 try {