default state service: update active/inactive state after fetching device on initPartition
This commit is contained in:
parent
254c87e29f
commit
a9c655b179
@ -163,7 +163,7 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
Runtime.getRuntime().availableProcessors(), ThingsBoardThreadFactory.forName("device-state"));
|
Runtime.getRuntime().availableProcessors(), ThingsBoardThreadFactory.forName("device-state"));
|
||||||
// Should be always single threaded due to absence of locks.
|
// Should be always single threaded due to absence of locks.
|
||||||
scheduledExecutor = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("device-state-scheduled")));
|
scheduledExecutor = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("device-state-scheduled")));
|
||||||
scheduledExecutor.scheduleAtFixedRate(this::updateState, new Random().nextInt(defaultStateCheckIntervalInSec), defaultStateCheckIntervalInSec, TimeUnit.SECONDS);
|
scheduledExecutor.scheduleAtFixedRate(this::updateInactivityStateIfExpired, new Random().nextInt(defaultStateCheckIntervalInSec), defaultStateCheckIntervalInSec, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
@ -207,19 +207,25 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
log.trace("on Device Activity [{}], lastReportedActivity [{}]", deviceId.getId(), lastReportedActivity);
|
log.trace("on Device Activity [{}], lastReportedActivity [{}]", deviceId.getId(), lastReportedActivity);
|
||||||
long lastSavedActivity = deviceLastSavedActivity.getOrDefault(deviceId, 0L);
|
long lastSavedActivity = deviceLastSavedActivity.getOrDefault(deviceId, 0L);
|
||||||
if (lastReportedActivity > 0 && lastReportedActivity > lastSavedActivity) {
|
if (lastReportedActivity > 0 && lastReportedActivity > lastSavedActivity) {
|
||||||
DeviceStateData stateData = getOrFetchDeviceStateData(deviceId);
|
final DeviceStateData stateData = getOrFetchDeviceStateData(deviceId);
|
||||||
log.trace("on Device Activity - fetched state {} for device {}", stateData, deviceId);
|
updateActivityState(deviceId, stateData, lastReportedActivity);
|
||||||
if (stateData != null) {
|
}
|
||||||
save(deviceId, LAST_ACTIVITY_TIME, lastReportedActivity);
|
}
|
||||||
deviceLastSavedActivity.put(deviceId, lastReportedActivity);
|
|
||||||
DeviceState state = stateData.getState();
|
void updateActivityState(DeviceId deviceId, DeviceStateData stateData, long lastReportedActivity) {
|
||||||
state.setLastActivityTime(lastReportedActivity);
|
log.trace("updateActivityState - fetched state {} for device {}, lastReportedActivity {}", stateData, deviceId, lastReportedActivity);
|
||||||
if (!state.isActive()) {
|
if (stateData != null) {
|
||||||
state.setActive(true);
|
save(deviceId, LAST_ACTIVITY_TIME, lastReportedActivity);
|
||||||
save(deviceId, ACTIVITY_STATE, state.isActive());
|
deviceLastSavedActivity.put(deviceId, lastReportedActivity);
|
||||||
pushRuleEngineMessage(stateData, ACTIVITY_EVENT);
|
DeviceState state = stateData.getState();
|
||||||
}
|
state.setLastActivityTime(lastReportedActivity);
|
||||||
|
if (!state.isActive()) {
|
||||||
|
state.setActive(true);
|
||||||
|
save(deviceId, ACTIVITY_STATE, true);
|
||||||
|
pushRuleEngineMessage(stateData, ACTIVITY_EVENT);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.warn("updateActivityState - fetched state IN NULL for device {}, lastReportedActivity {}", deviceId, lastReportedActivity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +253,7 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
DeviceState state = stateData.getState();
|
DeviceState state = stateData.getState();
|
||||||
state.setInactivityTimeout(inactivityTimeout);
|
state.setInactivityTimeout(inactivityTimeout);
|
||||||
boolean oldActive = state.isActive();
|
boolean oldActive = state.isActive();
|
||||||
state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout());
|
state.setActive(isActive(ts, state));
|
||||||
if (!oldActive && state.isActive() || oldActive && !state.isActive()) {
|
if (!oldActive && state.isActive() || oldActive && !state.isActive()) {
|
||||||
save(deviceId, ACTIVITY_STATE, state.isActive());
|
save(deviceId, ACTIVITY_STATE, state.isActive());
|
||||||
}
|
}
|
||||||
@ -405,7 +411,15 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
public Void apply(@Nullable DeviceStateData state) {
|
public Void apply(@Nullable DeviceStateData state) {
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
addDeviceUsingState(tpi, state);
|
addDeviceUsingState(tpi, state);
|
||||||
//updateState(device.getId()); //TODO update activity or inactivity state
|
|
||||||
|
if (state.getState().isActive()) {
|
||||||
|
updateInactivityStateIfExpired(System.currentTimeMillis(), device.getId(), state);
|
||||||
|
} else {
|
||||||
|
//trying to fix activity state
|
||||||
|
if (isActive(System.currentTimeMillis(), state.getState())) {
|
||||||
|
updateActivityState(device.getId(), state, state.getState().getLastActivityTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn("{}][{}] Fetched null state from DB", device.getName(), device.getId());
|
log.warn("{}][{}] Fetched null state from DB", device.getName(), device.getId());
|
||||||
}
|
}
|
||||||
@ -432,30 +446,30 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
deviceStates.put(state.getDeviceId(), state);
|
deviceStates.put(state.getDeviceId(), state);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateState() {
|
void updateInactivityStateIfExpired() {
|
||||||
final long ts = System.currentTimeMillis();
|
final long ts = System.currentTimeMillis();
|
||||||
log.debug("Calculating state updates for {} devices", deviceStates.size());
|
log.debug("Calculating state updates for {} devices", deviceStates.size());
|
||||||
Set<DeviceId> deviceIds = new HashSet<>(deviceStates.keySet());
|
Set<DeviceId> deviceIds = new HashSet<>(deviceStates.keySet());
|
||||||
for (DeviceId deviceId : deviceIds) {
|
for (DeviceId deviceId : deviceIds) {
|
||||||
updateState(ts, deviceId);
|
updateInactivityStateIfExpired(ts, deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateState(DeviceId deviceId) {
|
void updateInactivityStateIfExpired(long ts, DeviceId deviceId) {
|
||||||
updateState(System.currentTimeMillis(), deviceId);
|
DeviceStateData stateData = getOrFetchDeviceStateData(deviceId);
|
||||||
|
updateInactivityStateIfExpired(ts, deviceId, stateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateState(long ts, DeviceId deviceId) {
|
void updateInactivityStateIfExpired(long ts, DeviceId deviceId, DeviceStateData stateData) {
|
||||||
DeviceStateData stateData = getOrFetchDeviceStateData(deviceId);
|
|
||||||
log.trace("Processing state {} for device {}", stateData, deviceId);
|
log.trace("Processing state {} for device {}", stateData, deviceId);
|
||||||
if (stateData != null) {
|
if (stateData != null) {
|
||||||
DeviceState state = stateData.getState();
|
DeviceState state = stateData.getState();
|
||||||
state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout());
|
if (!isActive(ts, state) && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime()) && stateData.getDeviceCreationTime() + state.getInactivityTimeout() < ts) {
|
||||||
if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime()) && stateData.getDeviceCreationTime() + state.getInactivityTimeout() < ts) {
|
state.setActive(false);
|
||||||
state.setLastInactivityAlarmTime(ts);
|
state.setLastInactivityAlarmTime(ts);
|
||||||
pushRuleEngineMessage(stateData, INACTIVITY_EVENT);
|
pushRuleEngineMessage(stateData, INACTIVITY_EVENT);
|
||||||
save(deviceId, INACTIVITY_ALARM_TIME, ts);
|
save(deviceId, INACTIVITY_ALARM_TIME, ts);
|
||||||
save(deviceId, ACTIVITY_STATE, state.isActive());
|
save(deviceId, ACTIVITY_STATE, false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.debug("[{}] Device that belongs to other server is detected and removed.", deviceId);
|
log.debug("[{}] Device that belongs to other server is detected and removed.", deviceId);
|
||||||
@ -464,18 +478,29 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean isActive(long ts, DeviceState state) {
|
||||||
|
return ts < state.getLastActivityTime() + state.getInactivityTimeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
private DeviceStateData getOrFetchDeviceStateData(DeviceId deviceId) {
|
private DeviceStateData getOrFetchDeviceStateData(DeviceId deviceId) {
|
||||||
DeviceStateData deviceStateData = deviceStates.get(deviceId);
|
DeviceStateData deviceStateData = deviceStates.get(deviceId);
|
||||||
if (deviceStateData == null) {
|
if (deviceStateData != null) {
|
||||||
Device device = deviceService.findDeviceById(TenantId.SYS_TENANT_ID, deviceId);
|
return deviceStateData;
|
||||||
if (device != null) {
|
}
|
||||||
try {
|
|
||||||
deviceStateData = fetchDeviceState(device).get();
|
Device device = deviceService.findDeviceById(TenantId.SYS_TENANT_ID, deviceId);
|
||||||
deviceStates.putIfAbsent(deviceId, deviceStateData);
|
if (device != null) {
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
try {
|
||||||
log.debug("[{}] Failed to fetch device state!", deviceId, e);
|
deviceStateData = fetchDeviceState(device).get();
|
||||||
}
|
deviceStates.putIfAbsent(deviceId, deviceStateData);
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
log.warn("[{}] Failed to fetch device state!", deviceId, e);
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.warn("[{}] Failed to fetch device by Id!", deviceId);
|
||||||
|
throw new RuntimeException("Failed to fetch device by Id " + deviceId);
|
||||||
}
|
}
|
||||||
return deviceStateData;
|
return deviceStateData;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user