Skip to content

Commit cac75c1

Browse files
committed
feat: introduce timeout and retry parameter for BM executor shutdown
The awaitBinaryStorageExecutorTermination() in MonitoredClusteringBuilderState is extended with parameters that allow the timeout for the shutdown and the number of attempts to make to be specified. (also implement 2 small cleanups of code causing a compile error depending on settings and a FB warning suppression that is no longer necessary)
1 parent ec47fce commit cac75c1

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

com.avaloq.tools.ddk.xtext.builder/src/com/avaloq/tools/ddk/xtext/builder/MonitoredClusteringBuilderState.java

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -778,38 +778,87 @@ protected void deleteBinaryResources(final Set<URI> toBeDeleted) {
778778

779779
/**
780780
* Waits until binary models are stored.
781+
* Uses default parameters for timeout and retries, kept for backward compatibility.
781782
*/
782-
@SuppressFBWarnings("AT_STALE_THREAD_WRITE_OF_PRIMITIVE")
783783
protected void awaitBinaryStorageExecutorTermination() {
784-
LOGGER.info("Waiting for binary resource storage tasks to complete"); //$NON-NLS-1$
784+
awaitBinaryStorageExecutorTermination(1, TimeUnit.MINUTES, 0);
785+
}
786+
787+
/**
788+
* Waits until binary models are stored. Waits for a given time and makes a given number of attempts.
789+
*
790+
* @param timeout
791+
* time to wait for shutdown
792+
* @param unit
793+
* {@link TimeUnit} for timeout
794+
* @param retryCount
795+
* number of retries to attempt
796+
*/
797+
@SuppressWarnings("nls")
798+
protected void awaitBinaryStorageExecutorTermination(final int timeout, final TimeUnit unit, final int retryCount) {
799+
LOGGER.info("Waiting for binary resource storage tasks to complete: timeout {} {}, retryCount {}", timeout, unit, retryCount); //$NON-NLS-1$
785800
if (hwmTimeStamp != null) {
786801
LOGGER.info("high water mark was {} at {}", binaryStorageHighWaterMark, hwmTimeStamp); //$NON-NLS-1$
787802
}
788803

789804
// Stop accepting additional work
790805
binaryStorageExecutor.shutdown();
791-
long queuedTaskCount = binaryStorageExecutor.getQueue().size();
792-
long activeTaskCount = binaryStorageExecutor.getActiveCount();
793806

794-
// Attempt to wait for queued work to complete
807+
int retries = 0;
808+
boolean terminated = false;
809+
boolean stuck = false;
810+
811+
long prevQueuedTaskCount = binaryStorageExecutor.getQueue().size();
812+
long prevActiveTaskCount = binaryStorageExecutor.getActiveCount();
813+
795814
try {
796-
if (!binaryStorageExecutor.awaitTermination(1, TimeUnit.MINUTES)) {
797-
throw new InterruptedException();
815+
do {
816+
terminated = binaryStorageExecutor.awaitTermination(timeout, unit);
817+
if (!terminated) {
818+
// check whether if there is progress
819+
long currQueuedTaskCount = binaryStorageExecutor.getQueue().size();
820+
long currActiveTaskCount = binaryStorageExecutor.getActiveCount();
821+
822+
if (currQueuedTaskCount < prevQueuedTaskCount || currActiveTaskCount < prevActiveTaskCount) {
823+
LOGGER.warn("Binary resource storage tasks not completed in time, start with {} queued / {} active; now have {} / {}", prevQueuedTaskCount, prevActiveTaskCount, currQueuedTaskCount, currActiveTaskCount);
824+
if (retries < retryCount) {
825+
retries += 1;
826+
LOGGER.warn("retrying shutdown, attempt {} of {}", retries, retryCount);
827+
prevQueuedTaskCount = currQueuedTaskCount;
828+
prevActiveTaskCount = currActiveTaskCount;
829+
}
830+
} else {
831+
LOGGER.warn("Binary resource storage tasks not completed in time, not making progress, stuck on {} / {} queued / active tasks", currQueuedTaskCount, currActiveTaskCount);
832+
stuck = true;
833+
}
834+
}
835+
836+
} while (!terminated && !stuck && retries < retryCount);
837+
838+
if (terminated) {
839+
LOGGER.info("Binary resource storage executor completed.");
840+
} else {
841+
LOGGER.warn("Binary resource storage executor shutdown not successful, terminating");
842+
terminateBinaryStorageExecutor();
798843
}
799-
} catch (InterruptedException e) { // NOPMD ExceptionAsFlowControl
800-
LOGGER.warn(String.format("Binary resource storage tasks not completed in time, start with %d queued / %d active; now have %d / %d", //$NON-NLS-1$
801-
queuedTaskCount, activeTaskCount, binaryStorageExecutor.getQueue().size(), binaryStorageExecutor.getActiveCount()));
802-
binaryStorageExecutor.shutdownNow();
844+
} catch (InterruptedException e) {
845+
LOGGER.warn("Interrupted waiting for binaryStorageExecutor shutdown, terminating");
846+
terminateBinaryStorageExecutor();
803847
}
804848

805-
LOGGER.info("Binary resource storage executor completed."); //$NON-NLS-1$
806-
807849
// Be ready to accept additional work
808850
binaryStorageExecutor = makeBinaryStorageExecutor();
809851
binaryStorageHighWaterMark = 0;
810852
hwmTimeStamp = null;
811853
}
812854

855+
@SuppressWarnings("nls")
856+
private void terminateBinaryStorageExecutor() {
857+
LOGGER.warn("Terminating binaryStorageExecutor");
858+
List<Runnable> tasks = binaryStorageExecutor.shutdownNow();
859+
LOGGER.warn("{} tasks not processed", tasks.size());
860+
}
861+
813862
/**
814863
* Updates the markers on a single resource.
815864
*

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/util/ParseTreeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ public static String getParsedString(final EObject object, final String featureN
138138
public static void ensureNodeModelLoaded(final Resource resource) {
139139
if (resource instanceof LazyLinkingResource2 lazyLinkinResource && lazyLinkinResource.isLoadedFromStorage()) {
140140
EObject root = resource.getContents().get(0);
141-
if (root != null && NodeModelUtils.getNode(root) instanceof ICompositeNode composite) {
142-
composite.getText(); // side-effect on removing com.avaloq.tools.ddk.xtext.resource.persistence.ProxyCompositeNode
141+
if (root != null) {
142+
NodeModelUtils.getNode(root).getText(); // side-effect on removing com.avaloq.tools.ddk.xtext.resource.persistence.ProxyCompositeNode
143143
}
144144
}
145145
}

0 commit comments

Comments
 (0)