Usage Records implementation

This commit is contained in:
Andrii Shvaika 2020-11-09 14:06:20 +02:00
parent 3518a3d927
commit 5f50fd44d2
3 changed files with 18 additions and 4 deletions

View File

@ -93,6 +93,8 @@ public class TenantController extends BaseController {
} }
tenantProfileCache.evict(tenant.getId()); tenantProfileCache.evict(tenant.getId());
tbClusterService.onTenantChange(tenant, null); tbClusterService.onTenantChange(tenant, null);
tbClusterService.onEntityStateChange(tenant.getId(), tenant.getId(),
newTenant ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
return tenant; return tenant;
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);

View File

@ -243,6 +243,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
private void updateTenantState(TenantApiUsageState state, TenantProfile tenantProfile) { private void updateTenantState(TenantApiUsageState state, TenantProfile tenantProfile) {
TenantProfileData oldProfileData = state.getTenantProfileData(); TenantProfileData oldProfileData = state.getTenantProfileData();
state.setTenantProfileId(tenantProfile.getId());
state.setTenantProfileData(tenantProfile.getProfileData()); state.setTenantProfileData(tenantProfile.getProfileData());
Map<ApiFeature, Boolean> result = state.checkStateUpdatedDueToThresholds(); Map<ApiFeature, Boolean> result = state.checkStateUpdatedDueToThresholds();
if (!result.isEmpty()) { if (!result.isEmpty()) {
@ -257,8 +258,10 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
List<TsKvEntry> profileThresholds = new ArrayList<>(); List<TsKvEntry> profileThresholds = new ArrayList<>();
for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) { for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
if (oldData == null || oldData.getProfileThreshold(key) != newData.getProfileThreshold(key)) { long newProfileThreshold = newData.getProfileThreshold(key);
profileThresholds.add(new BasicTsKvEntry(ts, new LongDataEntry(key.getApiLimitKey(), newData.getProfileThreshold(key)))); if (oldData == null || oldData.getProfileThreshold(key) != newProfileThreshold) {
log.info("[{}] Updating profile threshold [{}]:[{}]", tenantId, key, newProfileThreshold);
profileThresholds.add(new BasicTsKvEntry(ts, new LongDataEntry(key.getApiLimitKey(), newProfileThreshold)));
} }
} }
if (!profileThresholds.isEmpty()) { if (!profileThresholds.isEmpty()) {
@ -267,6 +270,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
} }
private void persistAndNotify(TenantApiUsageState state, Map<ApiFeature, Boolean> result) { private void persistAndNotify(TenantApiUsageState state, Map<ApiFeature, Boolean> result) {
log.info("[{}] Detected update of the API state: {}", state.getTenantId(), result);
apiUsageStateService.update(state.getApiUsageState()); apiUsageStateService.update(state.getApiUsageState());
clusterService.onApiStateChange(state.getApiUsageState(), null); clusterService.onApiStateChange(state.getApiUsageState(), null);
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
@ -320,6 +324,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
} }
} }
} }
log.debug("[{}] Initialized state: {}", tenantId, dbStateEntity);
myTenantStates.put(tenantId, tenantState); myTenantStates.put(tenantId, tenantState);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e); log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e);

View File

@ -21,6 +21,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.id.TenantProfileId; import org.thingsboard.server.common.data.id.TenantProfileId;
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -79,15 +80,21 @@ public class TenantProfile extends SearchTextBased<TenantProfileId> implements H
profileData = mapper.readValue(new ByteArrayInputStream(profileDataBytes), TenantProfileData.class); profileData = mapper.readValue(new ByteArrayInputStream(profileDataBytes), TenantProfileData.class);
} catch (IOException e) { } catch (IOException e) {
log.warn("Can't deserialize tenant profile data: ", e); log.warn("Can't deserialize tenant profile data: ", e);
return null; return createDefaultTenantProfileData();
} }
return profileData; return profileData;
} else { } else {
return null; return createDefaultTenantProfileData();
} }
} }
} }
public TenantProfileData createDefaultTenantProfileData() {
TenantProfileData tpd = new TenantProfileData();
tpd.setConfiguration(new DefaultTenantProfileConfiguration());
return tpd;
}
public void setProfileData(TenantProfileData data) { public void setProfileData(TenantProfileData data) {
this.profileData = data; this.profileData = data;
try { try {