Commit 6f2f9c54dcd7c32f0511807a66c3ec8497aa42dd

Authored by YevhenBondarenko
Committed by Andrew Shvayka
1 parent ffdef642

fixed zones in SchedulerUtils and ApiUsageStateService improvements

... ... @@ -61,6 +61,7 @@ import org.thingsboard.server.service.telemetry.InternalTelemetryService;
61 61 import javax.annotation.PostConstruct;
62 62 import javax.annotation.PreDestroy;
63 63 import java.util.ArrayList;
  64 +import java.util.Arrays;
64 65 import java.util.HashSet;
65 66 import java.util.List;
66 67 import java.util.Map;
... ... @@ -73,6 +74,7 @@ import java.util.concurrent.Executors;
73 74 import java.util.concurrent.TimeUnit;
74 75 import java.util.concurrent.locks.Lock;
75 76 import java.util.concurrent.locks.ReentrantLock;
  77 +import java.util.stream.Collectors;
76 78
77 79 @Slf4j
78 80 @Service
... ... @@ -347,16 +349,11 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
347 349 try {
348 350 long now = System.currentTimeMillis();
349 351 myTenantStates.values().forEach(state -> {
350   - if ((state.getNextCycleTs() > now) && (state.getNextCycleTs() - now < TimeUnit.HOURS.toMillis(1))) {
  352 + if ((state.getNextCycleTs() < now) && (now - state.getNextCycleTs() < TimeUnit.HOURS.toMillis(1))) {
351 353 TenantId tenantId = state.getTenantId();
352 354 state.setCycles(state.getNextCycleTs(), SchedulerUtils.getStartOfNextNextMonth());
353   - ToUsageStatsServiceMsg.Builder msg = ToUsageStatsServiceMsg.newBuilder();
354   - msg.setTenantIdMSB(tenantId.getId().getMostSignificantBits());
355   - msg.setTenantIdLSB(tenantId.getId().getLeastSignificantBits());
356   - for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
357   - msg.addValues(UsageStatsKVProto.newBuilder().setKey(key.name()).setValue(0).build());
358   - }
359   - process(new TbProtoQueueMsg<>(UUID.randomUUID(), msg.build()), TbCallback.EMPTY);
  355 + saveNewCounts(state, Arrays.asList(ApiUsageRecordKey.values()));
  356 + updateTenantState(state, tenantProfileCache.get(tenantId));
360 357 }
361 358 });
362 359 } finally {
... ... @@ -364,6 +361,14 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
364 361 }
365 362 }
366 363
  364 + private void saveNewCounts(TenantApiUsageState state, List<ApiUsageRecordKey> keys) {
  365 + List<TsKvEntry> counts = keys.stream()
  366 + .map(key -> new BasicTsKvEntry(state.getCurrentCycleTs(), new LongDataEntry(key.getApiCountKey(), 0L)))
  367 + .collect(Collectors.toList());
  368 +
  369 + tsWsService.saveAndNotifyInternal(state.getTenantId(), state.getApiUsageState().getId(), counts, VOID_CALLBACK);
  370 + }
  371 +
367 372 private TenantApiUsageState getOrFetchState(TenantId tenantId) {
368 373 TenantApiUsageState tenantState = myTenantStates.get(tenantId);
369 374 if (tenantState == null) {
... ... @@ -377,6 +382,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
377 382 }
378 383 TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
379 384 tenantState = new TenantApiUsageState(tenantProfile, dbStateEntity);
  385 + List<ApiUsageRecordKey> newCounts = new ArrayList<>();
380 386 try {
381 387 List<TsKvEntry> dbValues = tsService.findAllLatest(tenantId, dbStateEntity.getId()).get();
382 388 for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
... ... @@ -385,7 +391,13 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
385 391 for (TsKvEntry tsKvEntry : dbValues) {
386 392 if (tsKvEntry.getKey().equals(key.getApiCountKey())) {
387 393 cycleEntryFound = true;
388   - tenantState.put(key, tsKvEntry.getTs() == tenantState.getCurrentCycleTs() ? tsKvEntry.getLongValue().get() : 0L);
  394 +
  395 + boolean oldCount = tsKvEntry.getTs() == tenantState.getCurrentCycleTs();
  396 + tenantState.put(key, oldCount ? tsKvEntry.getLongValue().get() : 0L);
  397 +
  398 + if (!oldCount) {
  399 + newCounts.add(key);
  400 + }
389 401 } else if (tsKvEntry.getKey().equals(key.getApiCountKey() + HOURLY)) {
390 402 hourlyEntryFound = true;
391 403 tenantState.putHourly(key, tsKvEntry.getTs() == tenantState.getCurrentHourTs() ? tsKvEntry.getLongValue().get() : 0L);
... ... @@ -397,6 +409,7 @@ public class DefaultTbApiUsageStateService implements TbApiUsageStateService {
397 409 }
398 410 log.debug("[{}] Initialized state: {}", tenantId, dbStateEntity);
399 411 myTenantStates.put(tenantId, tenantState);
  412 + saveNewCounts(tenantState, newCounts);
400 413 } catch (InterruptedException | ExecutionException e) {
401 414 log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e);
402 415 }
... ...
... ... @@ -17,22 +17,19 @@ package org.thingsboard.server.common.msg.tools;
17 17
18 18 import java.time.LocalDate;
19 19 import java.time.LocalDateTime;
20   -import java.time.LocalTime;
21 20 import java.time.ZoneId;
22   -import java.time.ZoneOffset;
23 21 import java.time.temporal.ChronoUnit;
24 22 import java.time.temporal.TemporalAdjuster;
25 23 import java.time.temporal.TemporalAdjusters;
26   -import java.time.temporal.TemporalUnit;
27 24 import java.util.concurrent.ConcurrentHashMap;
28 25 import java.util.concurrent.ConcurrentMap;
29 26
  27 +import static java.time.ZoneOffset.UTC;
30 28 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
31 29 import static java.time.temporal.ChronoUnit.MONTHS;
32 30
33 31 public class SchedulerUtils {
34 32
35   - private final static ZoneId UTC = ZoneId.of("UTC");
36 33 private static final ConcurrentMap<String, ZoneId> tzMap = new ConcurrentHashMap<>();
37 34
38 35 public static ZoneId getZoneId(String tz) {
... ... @@ -44,7 +41,7 @@ public class SchedulerUtils {
44 41 }
45 42
46 43 public static long getStartOfCurrentHour(ZoneId zoneId) {
47   - return LocalDateTime.now(ZoneOffset.UTC).atZone(zoneId).truncatedTo(ChronoUnit.HOURS).toInstant().toEpochMilli();
  44 + return LocalDateTime.now(UTC).atZone(zoneId).truncatedTo(ChronoUnit.HOURS).toInstant().toEpochMilli();
48 45 }
49 46
50 47 public static long getStartOfCurrentMonth() {
... ... @@ -52,7 +49,7 @@ public class SchedulerUtils {
52 49 }
53 50
54 51 public static long getStartOfCurrentMonth(ZoneId zoneId) {
55   - return LocalDate.now().withDayOfMonth(1).atStartOfDay(zoneId).toInstant().toEpochMilli();
  52 + return LocalDate.now(UTC).withDayOfMonth(1).atStartOfDay(zoneId).toInstant().toEpochMilli();
56 53 }
57 54
58 55 public static long getStartOfNextMonth() {
... ... @@ -60,7 +57,7 @@ public class SchedulerUtils {
60 57 }
61 58
62 59 public static long getStartOfNextMonth(ZoneId zoneId) {
63   - return LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth()).atStartOfDay(zoneId).toInstant().toEpochMilli();
  60 + return LocalDate.now(UTC).with(TemporalAdjusters.firstDayOfNextMonth()).atStartOfDay(zoneId).toInstant().toEpochMilli();
64 61 }
65 62
66 63 public static long getStartOfNextNextMonth() {
... ... @@ -68,7 +65,7 @@ public class SchedulerUtils {
68 65 }
69 66
70 67 public static long getStartOfNextNextMonth(ZoneId zoneId) {
71   - return LocalDate.now().with(firstDayOfNextNextMonth()).atStartOfDay(zoneId).toInstant().toEpochMilli();
  68 + return LocalDate.now(UTC).with(firstDayOfNextNextMonth()).atStartOfDay(zoneId).toInstant().toEpochMilli();
72 69 }
73 70
74 71 public static TemporalAdjuster firstDayOfNextNextMonth() {
... ...