|
|
@@ -6,7 +6,10 @@ import com.zksy.base.domain.MonitorPoint;
|
|
|
import com.zksy.base.service.EquipmentBaseService;
|
|
|
import com.zksy.base.service.EquipmentTypeService;
|
|
|
import com.zksy.base.service.MonitorPointService;
|
|
|
+import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitReadException;
|
|
|
import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotTime;
|
|
|
import com.zksy.cockpit.foundation.CockpitTypeIndustry;
|
|
|
import com.zksy.cockpit.point.BusinessCockpitDeviceFactResolver;
|
|
|
import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
|
|
|
@@ -16,6 +19,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.time.Clock;
|
|
|
+import java.time.DateTimeException;
|
|
|
import java.time.Duration;
|
|
|
import java.time.OffsetDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
@@ -99,7 +103,7 @@ public class BusinessCockpitMapSnapshotAdapter implements CockpitMapSnapshotAdap
|
|
|
@Override
|
|
|
public CockpitMapSnapshot load(String snapshotAt) {
|
|
|
String resolvedSnapshotAt = snapshotAt == null ? businessTime() : snapshotAt;
|
|
|
- OffsetDateTime requestedAt = OffsetDateTime.parse(resolvedSnapshotAt);
|
|
|
+ OffsetDateTime requestedAt = parseSnapshotAt(resolvedSnapshotAt);
|
|
|
OffsetDateTime now = OffsetDateTime.now(clock);
|
|
|
validateSnapshotWindow(requestedAt, now, "snapshot timestamp outside retention window");
|
|
|
|
|
|
@@ -113,43 +117,71 @@ public class BusinessCockpitMapSnapshotAdapter implements CockpitMapSnapshotAdap
|
|
|
|
|
|
String cacheKey = "cockpit:map-snapshot:" + resolvedSnapshotAt;
|
|
|
if (redisTemplate != null) {
|
|
|
- CockpitMapSnapshot shared = (CockpitMapSnapshot) redisTemplate.opsForValue().get(cacheKey);
|
|
|
- if (shared != null) {
|
|
|
- if (snapshots.size() < MAX_CACHED_SNAPSHOTS) {
|
|
|
- snapshots.putIfAbsent(resolvedSnapshotAt, shared);
|
|
|
+ try {
|
|
|
+ CockpitMapSnapshot shared = (CockpitMapSnapshot) redisTemplate.opsForValue().get(cacheKey);
|
|
|
+ if (shared != null) {
|
|
|
+ cacheLocally(resolvedSnapshotAt, shared);
|
|
|
+ validateSnapshotWindow(requestedAt, OffsetDateTime.now(clock),
|
|
|
+ "snapshot timestamp expired during load");
|
|
|
+ return shared;
|
|
|
}
|
|
|
- validateSnapshotWindow(requestedAt, OffsetDateTime.now(clock),
|
|
|
- "snapshot timestamp expired during load");
|
|
|
- return shared;
|
|
|
+ } catch (CockpitSnapshotExpiredException exception) {
|
|
|
+ throw exception;
|
|
|
+ } catch (RuntimeException exception) {
|
|
|
+ throw new CockpitReadException("snapshot cache unavailable");
|
|
|
}
|
|
|
}
|
|
|
if (redisTemplate == null && snapshots.size() >= MAX_CACHED_SNAPSHOTS) {
|
|
|
- throw new CockpitSnapshotExpiredException("snapshot cache capacity reached");
|
|
|
+ throw new CockpitReadException("snapshot cache capacity reached");
|
|
|
}
|
|
|
|
|
|
CockpitMapSnapshot built = buildSnapshot(resolvedSnapshotAt);
|
|
|
OffsetDateTime writeTime = OffsetDateTime.now(clock);
|
|
|
validateSnapshotWindow(requestedAt, writeTime, "snapshot timestamp expired during load");
|
|
|
if (redisTemplate != null) {
|
|
|
- Duration remaining = Duration.between(writeTime, requestedAt.plus(SNAPSHOT_RETENTION));
|
|
|
- long ttlSeconds = Math.max(1,
|
|
|
- remaining.getSeconds() + (remaining.getNano() > 0 ? 1 : 0));
|
|
|
- Boolean stored = redisTemplate.opsForValue()
|
|
|
- .setIfAbsent(cacheKey, built, ttlSeconds, TimeUnit.SECONDS);
|
|
|
- if (!Boolean.TRUE.equals(stored)) {
|
|
|
- CockpitMapSnapshot shared = (CockpitMapSnapshot) redisTemplate.opsForValue().get(cacheKey);
|
|
|
- if (shared == null) {
|
|
|
- throw new CockpitSnapshotExpiredException("snapshot cache unavailable");
|
|
|
+ try {
|
|
|
+ Duration remaining = Duration.between(writeTime, requestedAt.plus(SNAPSHOT_RETENTION));
|
|
|
+ long ttlSeconds = Math.max(1,
|
|
|
+ remaining.getSeconds() + (remaining.getNano() > 0 ? 1 : 0));
|
|
|
+ Boolean stored = redisTemplate.opsForValue()
|
|
|
+ .setIfAbsent(cacheKey, built, ttlSeconds, TimeUnit.SECONDS);
|
|
|
+ if (!Boolean.TRUE.equals(stored)) {
|
|
|
+ CockpitMapSnapshot shared = (CockpitMapSnapshot) redisTemplate.opsForValue().get(cacheKey);
|
|
|
+ if (shared == null) {
|
|
|
+ throw new CockpitReadException("snapshot cache unavailable");
|
|
|
+ }
|
|
|
+ validateSnapshotWindow(requestedAt, OffsetDateTime.now(clock),
|
|
|
+ "snapshot timestamp expired during load");
|
|
|
+ built = shared;
|
|
|
}
|
|
|
- built = shared;
|
|
|
+ } catch (CockpitSnapshotExpiredException | CockpitReadException exception) {
|
|
|
+ throw exception;
|
|
|
+ } catch (RuntimeException exception) {
|
|
|
+ throw new CockpitReadException("snapshot cache unavailable");
|
|
|
}
|
|
|
}
|
|
|
- if (snapshots.size() < MAX_CACHED_SNAPSHOTS) {
|
|
|
- snapshots.putIfAbsent(resolvedSnapshotAt, built);
|
|
|
+ cacheLocally(resolvedSnapshotAt, built);
|
|
|
+ CockpitMapSnapshot result = snapshots.getOrDefault(resolvedSnapshotAt, built);
|
|
|
+ validateSnapshotWindow(requestedAt, OffsetDateTime.now(clock),
|
|
|
+ "snapshot timestamp expired during load");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static OffsetDateTime parseSnapshotAt(String value) {
|
|
|
+ try {
|
|
|
+ return CockpitSnapshotTime.parseBusinessOffset(value);
|
|
|
+ } catch (DateTimeException exception) {
|
|
|
+ throw new CockpitInvalidRequestException("snapshot timestamp must use ISO-8601 +08:00");
|
|
|
}
|
|
|
- return snapshots.getOrDefault(resolvedSnapshotAt, built);
|
|
|
}
|
|
|
|
|
|
+ private void cacheLocally(String snapshotAt, CockpitMapSnapshot snapshot) {
|
|
|
+ synchronized (snapshots) {
|
|
|
+ if (snapshots.containsKey(snapshotAt) || snapshots.size() < MAX_CACHED_SNAPSHOTS) {
|
|
|
+ snapshots.putIfAbsent(snapshotAt, snapshot);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
private static void validateSnapshotWindow(
|
|
|
OffsetDateTime requestedAt, OffsetDateTime now, String message) {
|
|
|
if (!requestedAt.isAfter(now.minus(SNAPSHOT_RETENTION))
|