Commit 5f50fd44d2e6fbb53f4cab1a32bb52fc69748c35

Authored by Andrii Shvaika
1 parent 3518a3d9

Usage Records implementation

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