Skip to content

Commit

Permalink
Remove vehicles from cache after timeout if configured to do so (fix)
Browse files Browse the repository at this point in the history
- make sure all timeout handler functions trigger the removal
- don't remove after end of block (doesn't make sense, as vehicle could still be sending AVL reports... only want to remove from cache after timeout)
  • Loading branch information
nselikoff committed Apr 10, 2019
1 parent 6c6e326 commit 3f45aec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 50 deletions.
22 changes: 4 additions & 18 deletions transitclock/src/main/java/org/transitclock/core/AvlProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,12 @@ public void makeVehicleUnpredictableAndGrabAssignment(
}

/**
* Marks the vehicle as not being predictable and that the assignment has
* been grabbed. Updates VehicleDataCache. Creates and logs a VehicleEvent
* explaining the situation.
* Removes the vehicle from the VehicleDataCache.
*
* @param vehicleState
* The vehicle to be made unpredictable
* @param eventDescription
* A longer description of why vehicle being made unpredictable
* @param vehicleEvent
* A short description from VehicleEvent class for labeling the
* event.
* @param vehicleId
* The vehicle to remove
*/
public void makeVehicleUnpredictableAndRemoveFromVehicleDataCache(
String vehicleId, String eventDescription, String vehicleEvent) {
makeVehicleUnpredictable(vehicleId, eventDescription, vehicleEvent);

// Remove vehicle from VehicleDataCache
public void removeFromVehicleDataCache(String vehicleId) {
VehicleDataCache.getInstance().removeVehicle(vehicleId);
}

Expand Down Expand Up @@ -1234,9 +1223,6 @@ private boolean handlePossibleEndOfBlock(VehicleState vehicleState) {
makeVehicleUnpredictableAndTerminateAssignment(vehicleState,
eventDescription, VehicleEvent.END_OF_BLOCK);

// Remove vehicle from VehicleDataCache
VehicleDataCache.getInstance().removeVehicle(vehicleState.getVehicleId());

// Return that end of block reached
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ public void storeAvlReport(AvlReport avlReport) {
avlReportsMap.put(avlReport.getVehicleId(), avlReport);
}
}

/**
* Removes the specified vehicle from the VehicleDataCache if configured to do so
*
* @param vehicleId
* Vehicle to remove
*/
public void removeFromVehicleDataCache(String vehicleId) {
if (removeTimedOutVehiclesFromVehicleDataCache.getValue()) {
logger.info("Removing vehicleId={} from VehicleDataCache", vehicleId);
AvlProcessor.getInstance().removeFromVehicleDataCache(vehicleId);
}
}

/**
* For regular predictable vehicle that is not a schedule based prediction
Expand All @@ -150,7 +163,7 @@ private void handlePredictablePossibleTimeout(VehicleState vehicleState, long no
+ " while allowable time without an AVL report is "
+ Time.elapsedTimeStr(maxNoAvl)
+ " and so was made unpredictable.";
makeVehicleUnpredictable(
AvlProcessor.getInstance().makeVehicleUnpredictable(
vehicleState.getVehicleId(), eventDescription,
VehicleEvent.TIMEOUT);

Expand All @@ -160,20 +173,30 @@ private void handlePredictablePossibleTimeout(VehicleState vehicleState, long no

// Remove vehicle from map for next time looking for timeouts
mapIterator.remove();


// Remove vehicle from cache if configured to do so
removeFromVehicleDataCache(vehicleState.getVehicleId());
}
}

/**
* Don't need to worry about vehicles that are not predictable. But might as
* well remove vehicle from map so don't examine vehicle every
* TimeoutHandleModule polling cycle
* For not predictable vehicle. If haven't reported in too long removes the
* vehicle from map and possibly cache
*
* @param mapIterator
* So can remove AVL report from map
*/
private void handleNotPredictablePossibleTimeout(Iterator<AvlReport> mapIterator) {
private void handleNotPredictablePossibleTimeout(VehicleState vehicleState,
long now, Iterator<AvlReport> mapIterator) {
// Log the situation
logger.info("For not predictable vehicleId={} generated timeout "
+ "event.", vehicleState.getVehicleId());

// Remove vehicle from map for next time looking for timeouts
mapIterator.remove();

// Remove vehicle from cache if configured to do so
removeFromVehicleDataCache(vehicleState.getVehicleId());
}

/**
Expand All @@ -193,7 +216,7 @@ private void handleSchedBasedPredsPossibleTimeout(VehicleState vehicleState,
String shouldTimeoutEventDescription =
SchedBasedPredsModule.shouldTimeoutVehicle(vehicleState, now);
if (shouldTimeoutEventDescription != null) {
makeVehicleUnpredictable(
AvlProcessor.getInstance().makeVehicleUnpredictable(
vehicleState.getVehicleId(), shouldTimeoutEventDescription,
VehicleEvent.TIMEOUT);

Expand All @@ -203,7 +226,10 @@ private void handleSchedBasedPredsPossibleTimeout(VehicleState vehicleState,
vehicleState.getVehicleId(), shouldTimeoutEventDescription);

// Remove vehicle from map for next time looking for timeouts
mapIterator.remove();
mapIterator.remove();

// Remove vehicle from cache if configured to do so
removeFromVehicleDataCache(vehicleState.getVehicleId());
}
}

Expand Down Expand Up @@ -266,7 +292,7 @@ private void handleWaitStopPossibleTimeout(VehicleState vehicleState, long now,
+ "time without AVL is "
+ Time.elapsedTimeStr(maxNoAvlAfterSchedDepartSecs)
+ ". Therefore vehicle was made unpredictable.";
makeVehicleUnpredictable(
AvlProcessor.getInstance().makeVehicleUnpredictable(
vehicleState.getVehicleId(), eventDescription,
VehicleEvent.TIMEOUT);

Expand All @@ -276,32 +302,13 @@ private void handleWaitStopPossibleTimeout(VehicleState vehicleState, long now,

// Remove vehicle from map for next time looking for timeouts
mapIterator.remove();

// Remove vehicle from cache if configured to do so
removeFromVehicleDataCache(vehicleState.getVehicleId());
}
}
}

/**
* Call the proper AvlProcessor method depending on the configuration
* Default behavior is to make the vehicle unpredictable
* If removeTimedOutVehiclesFromVehicleDataCache is set to true, also remove the vehicle from the cache
*
* @param vehicleId
* The vehicle to be made unpredictable
* @param eventDescription
* A longer description of why vehicle being made unpredictable
* @param vehicleEvent
* A short description from VehicleEvent class for labeling the
* event.
*/
private void makeVehicleUnpredictable(String vehicleId,
String eventDescription, String vehicleEvent) {
if (removeTimedOutVehiclesFromVehicleDataCache.getValue()) {
AvlProcessor.getInstance().makeVehicleUnpredictableAndRemoveFromVehicleDataCache(vehicleId, eventDescription, vehicleEvent);
} else {
AvlProcessor.getInstance().makeVehicleUnpredictable(vehicleId, eventDescription, vehicleEvent);
}
}

/**
* Goes through all vehicles and finds ones that have timed out
*/
Expand Down Expand Up @@ -329,7 +336,8 @@ public void handlePossibleTimeouts() {
synchronized (vehicleState) {
if (!vehicleState.isPredictable()) {
// Vehicle is not predictable
handleNotPredictablePossibleTimeout(mapIterator);
handleNotPredictablePossibleTimeout(vehicleState, now,
mapIterator);
} else if (vehicleState.isForSchedBasedPreds()) {
// Handle schedule based predictions vehicle
handleSchedBasedPredsPossibleTimeout(vehicleState, now,
Expand Down

0 comments on commit 3f45aec

Please sign in to comment.