bug 4527: allow for synchronous RaceListener registrations

Change-Id: Ibb6a064222d9cb1ef0944df5fee9dd0e3675ab8e
This commit is contained in:
Axel Uhl committed 2018-05-14 15:36:04 +02:00
1 parent 0ce7609b62
commit b095a063a8
20 files changed
+79 -274

No files matched your search

@@ -1772,7 +1772,10 @@ public class RacingEventServiceImpl implements RacingEventService, ClearStateTes
DynamicTrackedRegatta trackedRegatta) {
if (regattasObservedForDefaultLeaderboard.add(trackedRegatta)) {
trackedRegatta.addRaceListener(new RaceAdditionListener(),
Optional.of(this.getThreadLocalTransporterForCurrentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster()));
/* ThreadLocalTransporter */ Optional.empty(), // registering for synchronous callbacks; no thread locals need to be transported
/* register for synchronous execution in order to ensure that any replication-related effects happen before
* any subsequent replication operations referring to a new race hit the outbound replication queue
*/ true);
}
}
@@ -1,69 +1,13 @@
package com.sap.sailing.server.operationaltransformation;
import java.util.Optional;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Logger;
import com.sap.sailing.domain.common.RegattaAndRaceIdentifier;
import com.sap.sailing.domain.tracking.DynamicTrackedRace;
import com.sap.sailing.domain.tracking.RaceListener;
import com.sap.sailing.domain.tracking.TrackedRace;
import com.sap.sailing.domain.tracking.TrackedRegatta;
import com.sap.sailing.server.RacingEventService;
import com.sap.sse.common.Duration;
import com.sap.sse.util.ThreadPoolUtil;
public abstract class AbstractRaceOperation<ResultType> extends AbstractRacingEventServiceOperation<ResultType> {
private static final long serialVersionUID = -1162468486451355784L;
private static final Logger logger = Logger.getLogger(AbstractRaceOperation.class.getName());
private RegattaAndRaceIdentifier raceIdentifier;
/**
* Used as a race listener on the {@link TrackedRegatta} in case the {@link TrackedRace} identified by the
* {@link AbstractRaceOperation#getRaceIdentifier() race identifier} isn't found when the operation is to be
* applied. Such listeners will be removed by a scheduled task after some timeout that can be configured through the
* constructor. The listener is also removed after having received the tracked race.
*
* @author Axel Uhl (d043530)
*
*/
private class DelayedOperationRunnerWaitingForTrackedRace implements RaceListener {
private final Consumer<DynamicTrackedRace> taskWithTrackedRace;
private final TrackedRegatta whereToRegisterAndUnregisterListener;
private final ScheduledFuture<?> timeoutTask;
public DelayedOperationRunnerWaitingForTrackedRace(TrackedRegatta whereToRegisterAndUnregisterListener,
Consumer<DynamicTrackedRace> taskWithTrackedRace, Duration timeoutForReceivingTrackedRace) {
super();
this.whereToRegisterAndUnregisterListener = whereToRegisterAndUnregisterListener;
this.taskWithTrackedRace = taskWithTrackedRace;
whereToRegisterAndUnregisterListener.addRaceListener(this, /* thread local transporter */ Optional.empty());
timeoutTask = ThreadPoolUtil.INSTANCE.getDefaultForegroundTaskThreadPoolExecutor().schedule(
()->{
whereToRegisterAndUnregisterListener.removeRaceListener(DelayedOperationRunnerWaitingForTrackedRace.this);
logger.warning("Waiting for tracked race for race identifier "+getRaceIdentifier()+
" timed out. Not waiting any longer. Discarding operation "+AbstractRaceOperation.this);
},
timeoutForReceivingTrackedRace.asMillis(), TimeUnit.MILLISECONDS);
}
@Override
public void raceAdded(TrackedRace trackedRace) {
logger.info("Tracked race "+trackedRace+" showed up. Now we can apply "+AbstractRaceOperation.this);
timeoutTask.cancel(/* mayInterruptIfRunning */ false);
// the following cast is valid because the tracked regatta is always a DynamicTrackedRegatta which
// holds DynamicTrackedRace objects
taskWithTrackedRace.accept((DynamicTrackedRace) trackedRace);
whereToRegisterAndUnregisterListener.removeRaceListener(this);
}
@Override
public void raceRemoved(TrackedRace trackedRace) {
}
}
public AbstractRaceOperation(RegattaAndRaceIdentifier raceIdentifier) {
super();
this.raceIdentifier = raceIdentifier;
@@ -90,24 +34,6 @@ public abstract class AbstractRaceOperation<ResultType> extends AbstractRacingEv
return trackedRace;
}
protected void doWithTrackedRace(RacingEventService racingEventService, Consumer<DynamicTrackedRace> taskWithTrackedRace) {
final Duration timeout = Duration.ONE_MINUTE;
final DynamicTrackedRace trackedRace = getTrackedRace(racingEventService);
if (trackedRace != null) {
taskWithTrackedRace.accept(trackedRace);
} else {
logger.info("Tracked race for race identifier "+getRaceIdentifier()+" not found. Waiting for it for "+timeout);
final TrackedRegatta trackedRegatta = racingEventService.getTrackedRegatta(racingEventService.getRegatta(getRaceIdentifier()));
if (trackedRegatta != null) {
new DelayedOperationRunnerWaitingForTrackedRace(/* whereToUnregisterListener */ trackedRegatta,
taskWithTrackedRace, /* timeout for waiting for tracked race */ timeout);
} else {
logger.warning("Couldn't find tracked regatta for race identifier "+getRaceIdentifier()+
". Ignoring operation "+this);
}
}
}
@Override
public String toString() {
return super.toString()+" [raceIdentifier=" + raceIdentifier + "]";
@@ -3,6 +3,7 @@ package com.sap.sailing.server.operationaltransformation;
import com.sap.sailing.domain.base.Mark;
import com.sap.sailing.domain.common.RegattaAndRaceIdentifier;
import com.sap.sailing.domain.common.tracking.GPSFix;
import com.sap.sailing.domain.tracking.DynamicTrackedRace;
import com.sap.sailing.server.RacingEventService;
/**
@@ -23,8 +24,10 @@ public class RecordMarkGPSFixForNewMarkTrack extends RecordMarkGPSFix {
@Override
public Void internalApplyTo(RacingEventService toState) throws Exception {
// FIXME bug 4527 this is a problem: we cannot easily push execution of this task to an executor when this operation needs to be executed synchronously; it has to precede the other RecordMarkGPSFixForExistingTrack operations for the same mark at least
doWithTrackedRace(toState, trackedRace->trackedRace.recordFix(mark, getFix(), /* onlyWhenInTrackingTimeInterval */ false)); // record the fix in any case
DynamicTrackedRace trackedRace = getTrackedRace(toState);
if (trackedRace != null) {
trackedRace.recordFix(mark, getFix(), /* onlyWhenInTrackingTimeInterval */ false); // record the fix in any case
}
return null;
}
}
@@ -284,7 +284,7 @@ public class SimulationServiceImpl implements SimulationService {
DynamicTrackedRegatta trackedRegatta = racingEventService.getTrackedRegatta(regatta);
SimulationRaceListener raceListener = new SimulationRaceListener();
raceListeners.put(legIdentifier.getRegattaName(), raceListener);
trackedRegatta.addRaceListener(raceListener, /* Not replicated */ Optional.empty());
trackedRegatta.addRaceListener(raceListener, /* Not replicated */ Optional.empty(), /* synchronous */ false);
}
if (!legListeners.containsKey(legIdentifier.getRaceIdentifier())) {
TrackedRace trackedRace = racingEventService.getTrackedRace(legIdentifier);