Improvement to device state service

This commit is contained in:
Andrew Shvayka 2019-11-28 18:37:01 +02:00
parent 866d7c4a2c
commit 93a8c90882
3 changed files with 15 additions and 30 deletions

View File

@ -30,13 +30,6 @@ public interface ClusterRoutingService extends DiscoveryServiceListener {
ServerAddress getCurrentServer(); ServerAddress getCurrentServer();
Optional<ServerAddress> resolveByUuid(UUID uuid);
Optional<ServerAddress> resolveById(EntityId entityId); Optional<ServerAddress> resolveById(EntityId entityId);
Optional<ServerAddress> resolveByUuid(ServerType server, UUID uuid);
Optional<ServerAddress> resolveById(ServerType server, EntityId entityId);
} }

View File

@ -88,21 +88,6 @@ public class ConsistentClusterRoutingService implements ClusterRoutingService {
return resolveByUuid(rootCircle, entityId.getId()); return resolveByUuid(rootCircle, entityId.getId());
} }
@Override
public Optional<ServerAddress> resolveByUuid(UUID uuid) {
return resolveByUuid(rootCircle, uuid);
}
@Override
public Optional<ServerAddress> resolveByUuid(ServerType server, UUID uuid) {
return resolveByUuid(circles[server.ordinal()], uuid);
}
@Override
public Optional<ServerAddress> resolveById(ServerType server, EntityId entityId) {
return resolveByUuid(circles[server.ordinal()], entityId.getId());
}
private Optional<ServerAddress> resolveByUuid(ConsistentHashCircle circle, UUID uuid) { private Optional<ServerAddress> resolveByUuid(ConsistentHashCircle circle, UUID uuid) {
Assert.notNull(uuid); Assert.notNull(uuid);
if (circle.isEmpty()) { if (circle.isEmpty()) {

View File

@ -296,8 +296,10 @@ public class DefaultDeviceStateService implements DeviceStateService {
private void updateState() { private void updateState() {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
Set<DeviceId> deviceIds = new HashSet<>(deviceStates.keySet()); Set<DeviceId> deviceIds = new HashSet<>(deviceStates.keySet());
log.info("Calculating state updates for {} devices", deviceStates.size());
for (DeviceId deviceId : deviceIds) { for (DeviceId deviceId : deviceIds) {
DeviceStateData stateData = deviceStates.get(deviceId); DeviceStateData stateData = getOrFetchDeviceStateData(deviceId);
if (stateData != null) {
DeviceState state = stateData.getState(); DeviceState state = stateData.getState();
state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout()); state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout());
if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime())) { if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime())) {
@ -306,6 +308,10 @@ public class DefaultDeviceStateService implements DeviceStateService {
save(deviceId, INACTIVITY_ALARM_TIME, ts); save(deviceId, INACTIVITY_ALARM_TIME, ts);
save(deviceId, ACTIVITY_STATE, state.isActive()); save(deviceId, ACTIVITY_STATE, state.isActive());
} }
} else {
log.debug("[{}] Device that belongs to other server is detected and removed.", deviceId);
deviceStates.remove(deviceId);
}
} }
} }
@ -353,6 +359,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
if (device != null) { if (device != null) {
try { try {
deviceStateData = fetchDeviceState(device).get(); deviceStateData = fetchDeviceState(device).get();
deviceStates.putIfAbsent(deviceId, deviceStateData);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
log.debug("[{}] Failed to fetch device state!", deviceId, e); log.debug("[{}] Failed to fetch device state!", deviceId, e);
} }