Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bitrepository.bitrepositorymessages.MessageResponse;
import org.bitrepository.common.exceptions.UnableToFinishException;

import java.time.Duration;
import java.util.LinkedList;

/**
Expand Down Expand Up @@ -54,8 +55,8 @@ protected GeneralConversationState completeState() {
}

@Override
protected long getTimeoutValue() {
return 0;
protected Duration getTimeoutValue() {
return Duration.ZERO;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import org.bitrepository.client.exceptions.UnexpectedResponseException;
import org.bitrepository.common.DefaultThreadFactory;
import org.bitrepository.common.exceptions.UnableToFinishException;
import org.bitrepository.common.utils.CountAndTimeUnit;
import org.bitrepository.common.utils.TimeUtils;
import org.bitrepository.protocol.ProtocolVersionLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -71,8 +74,9 @@ protected GeneralConversationState(Collection<String> expectedContributors) {
public void start() {
try {
if (!responseStatus.getOutstandingComponents().isEmpty()) {
if (getTimeoutValue() > 0) {
scheduledTimeout = timer.schedule(new TimeoutHandler(), getTimeoutValue(), TimeUnit.MILLISECONDS);
if (getTimeoutValue().compareTo(Duration.ZERO) > 0) { // TODO From Java 18 use: getTimeoutValue().isPositive()
CountAndTimeUnit delay = TimeUtils.durationToCountAndTimeUnit(getTimeoutValue());
scheduledTimeout = timer.schedule(new TimeoutHandler(), delay.getCount(), delay.getUnit());
}
sendRequest();
} else {
Expand Down Expand Up @@ -217,9 +221,9 @@ protected abstract boolean processMessage(MessageResponse response) throws Unexp

/**
* Gives access to the concrete timeout for the state.
* @return the number of milliseconds before timeout
* @return the duration before timeout
*/
protected abstract long getTimeoutValue();
protected abstract Duration getTimeoutValue();

/**
* @return The informative naming of the process this state is performing. Used for logging. Examples are 'Delete files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bitrepository.client.exceptions.UnexpectedResponseException;
import org.bitrepository.common.exceptions.UnableToFinishException;

import java.time.Duration;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -79,9 +80,8 @@ protected final GeneralConversationState completeState() throws UnableToFinishEx
}

@Override
protected long getTimeoutValue() {
return getContext().getSettings().getRepositorySettings().getClientSettings().getIdentificationTimeout()
.longValue();
protected Duration getTimeoutValue() {
return getContext().getSettings().getIdentificationTimeout();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.bitrepository.client.conversation.selector.SelectedComponentInfo;
import org.bitrepository.client.exceptions.UnexpectedResponseException;
import org.bitrepository.common.exceptions.UnableToFinishException;
import org.bitrepository.common.utils.TimeUtils;

import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -79,14 +81,15 @@ protected boolean processMessage(MessageResponse msg) throws UnableToFinishExcep

@Override
protected void logStateTimeout() throws UnableToFinishException {
throw new UnableToFinishException(
"Failed to receive responses from all contributors before timeout(" + getTimeoutValue() + "ms). Missing contributors " +
throw new UnableToFinishException("Failed to receive responses from all contributors before timeout (" +
TimeUtils.durationToHuman(getTimeoutValue()) +
"). Missing contributors " +
getOutstandingComponents());
}

@Override
protected long getTimeoutValue() {
return getContext().getSettings().getRepositorySettings().getClientSettings().getOperationTimeout().longValue();
protected Duration getTimeoutValue() {
return getContext().getSettings().getOperationTimeout();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class CollectionBasedConversationMediator implements ConversationMediator
private final Logger log = LoggerFactory.getLogger(getClass());
private final Map<String, Conversation> conversations;
private final Settings settings;
private static final Boolean TIMER_IS_DAEMON = true;
private static final boolean TIMER_IS_DAEMON = true;
private static final String NAME_OF_TIMER = "Collection based conversation timer";
/**
* The timer used to schedule cleaning of conversations.
Expand Down Expand Up @@ -96,7 +96,7 @@ public void addConversation(Conversation conversation) {
}

/**
* Will try to fail a conversation gracefully. This entitles:
* Will try to fail a conversation gracefully. This consists of:
* <ul>
* <li> Removing the conversation from the list of conversations.
* <li> Attempt to call the failConversation operation on the conversation. The call is made in a separate thread to
Expand Down Expand Up @@ -136,10 +136,10 @@ public void onMessage(Message message, MessageContext messageContext) {
}

/**
* Will clean out obsolete conversations in each run. An obsolete conversation is a conversation which satisfies on
* of the following criterias: <ol>
* Will clean out obsolete conversations in each run. An obsolete conversation is a conversation which satisfies any
* of the following criteria: <ol>
* <li> Returns true for the <code>hasEnded()</code> method.
* <li> Is older than the conversationTImeout limit allows.
* <li> Is older than the conversationTimeout limit allows.
* </ol>
* <p>
* A copy of the current conversations is created before running through the conversations to avoid having to lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.List;

/**
Expand Down Expand Up @@ -208,9 +209,8 @@ protected List<String> getPillarIDs() {
/**
* @return The timeout to use for performing the full operation.
*/
protected long getTimeout() {
return settings.getRepositorySettings().getClientSettings().getIdentificationTimeout().longValue() +
settings.getRepositorySettings().getClientSettings().getOperationTimeout().longValue();
protected Duration getTimeout() {
return settings.getIdentificationTimeout().plus(settings.getOperationTimeout());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.bitrepository.commandline.outputformatter.GetChecksumsOutputFormatter;
import org.bitrepository.commandline.resultmodel.GetChecksumsResultModel;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* Wrapper class for GetChecksumClient to handle paging through large result sets
Expand All @@ -42,13 +44,13 @@ public class PagingGetChecksumsClient {
private GetChecksumsResultModel model;
private final GetChecksumsOutputFormatter outputFormatter;
private final OutputHandler outputHandler;
private final long timeout;
private final Duration timeout;
private final int pageSize;

public PagingGetChecksumsClient(GetChecksumsClient client, long timeout, int pageSize, GetChecksumsOutputFormatter outputFormatter,
public PagingGetChecksumsClient(GetChecksumsClient client, Duration timeout, int pageSize, GetChecksumsOutputFormatter outputFormatter,
OutputHandler outputHandler) {
this.client = client;
this.timeout = timeout;
this.timeout = Objects.requireNonNull(timeout, "timeout");
this.pageSize = pageSize;
this.outputFormatter = outputFormatter;
this.outputHandler = outputHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.bitrepository.commandline.outputformatter.GetFileIDsOutputFormatter;
import org.bitrepository.commandline.resultmodel.GetFileIDsResultModel;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* Wrapper class for GetFileIDsClient to handle paging through large result sets
Expand All @@ -41,13 +43,13 @@ public class PagingGetFileIDsClient {
private GetFileIDsResultModel model;
private final GetFileIDsOutputFormatter outputFormatter;
private final OutputHandler outputHandler;
private final long timeout;
private final Duration timeout;
private final int pageSize;

public PagingGetFileIDsClient(GetFileIDsClient client, long timeout, int pageSize, GetFileIDsOutputFormatter outputFormatter,
public PagingGetFileIDsClient(GetFileIDsClient client, Duration timeout, int pageSize, GetFileIDsOutputFormatter outputFormatter,
OutputHandler outputHandler) {
this.client = client;
this.timeout = timeout;
this.timeout = Objects.requireNonNull(timeout, "timeout");
this.pageSize = pageSize;
this.outputFormatter = outputFormatter;
this.outputHandler = outputHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import org.bitrepository.client.eventhandler.OperationEvent.OperationEventType;
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.CountAndTimeUnit;
import org.bitrepository.common.utils.TimeUtils;

import java.time.Duration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand All @@ -39,7 +42,7 @@ public abstract class CompleteEventAwaiter implements EventHandler {
/**
* The amount of milliseconds before the results are required.
*/
private final Long timeout;
private final Duration timeout;
/**
* The handler of the output for this event handler.
*/
Expand All @@ -55,8 +58,7 @@ public abstract class CompleteEventAwaiter implements EventHandler {
* @param outputHandler The {@link OutputHandler} for handling outputting results
*/
public CompleteEventAwaiter(Settings settings, OutputHandler outputHandler) {
this.timeout = settings.getRepositorySettings().getClientSettings().getIdentificationTimeout().longValue()
+ settings.getRepositorySettings().getClientSettings().getOperationTimeout().longValue();
this.timeout = settings.getIdentificationTimeout().plus(settings.getOperationTimeout());
this.output = outputHandler;
}

Expand All @@ -78,14 +80,16 @@ public void handleEvent(OperationEvent event) {
public abstract void handleComponentComplete(OperationEvent event);

/**
* Retrieves the final event when the operation finishes. The final event is awaited for 'timeout' amount
* of milliseconds. If no final events has occurred, then an InterruptedException is thrown.
* Retrieves the final event when the operation finishes.
* The final event is awaited for 'timeout'. If no final events has occurred, null is returned.
*
* @return The final event.
* @return The final event or null if none has occurred.
* @throws IllegalStateException if interrupted while waiting
*/
public OperationEvent getFinish() {
try {
return finalEventQueue.poll(timeout, TimeUnit.MILLISECONDS);
CountAndTimeUnit pollTimeout = TimeUtils.durationToCountAndTimeUnit(timeout);
return finalEventQueue.poll(pollTimeout.getCount(), pollTimeout.getUnit());
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted while waiting for the final response.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.commandline.resultmodel.GetChecksumsResultModel;

import java.time.Duration;

/**
* Event handler for paging through GetChecksums results
*/
public class GetChecksumsEventHandler extends PagingEventHandler {

private final GetChecksumsResultModel model;

public GetChecksumsEventHandler(GetChecksumsResultModel model, Long timeout, OutputHandler outputHandler) {
public GetChecksumsEventHandler(GetChecksumsResultModel model, Duration timeout, OutputHandler outputHandler) {
super(timeout, outputHandler);
this.model = model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.commandline.resultmodel.GetFileIDsResultModel;

import java.time.Duration;

/**
* Event handler for paging through GetFileIDs results
*/
public class GetFileIDsEventHandler extends PagingEventHandler {
private final GetFileIDsResultModel model;

public GetFileIDsEventHandler(GetFileIDsResultModel model, Long timeout, OutputHandler outputHandler) {
public GetFileIDsEventHandler(GetFileIDsResultModel model, Duration timeout, OutputHandler outputHandler) {
super(timeout, outputHandler);
this.model = model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import org.bitrepository.client.eventhandler.OperationEvent;
import org.bitrepository.client.eventhandler.OperationEvent.OperationEventType;
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.common.utils.CountAndTimeUnit;
import org.bitrepository.common.utils.TimeUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand All @@ -36,15 +40,15 @@
* Event handler for operations that need paging functionality.
*/
public abstract class PagingEventHandler implements EventHandler {
private final Long timeout;
private final Duration timeout;
private final BlockingQueue<OperationEvent> finalEventQueue = new LinkedBlockingQueue<>(1);

protected List<String> pillarsWithPartialResults = new ArrayList<>();

private final OutputHandler outputHandler;

public PagingEventHandler(Long timeout, OutputHandler outputHandler) {
this.timeout = timeout;
public PagingEventHandler(Duration timeout, OutputHandler outputHandler) {
this.timeout = Objects.requireNonNull(timeout, "timeout");
this.outputHandler = outputHandler;
}

Expand All @@ -62,14 +66,15 @@ public void handleEvent(OperationEvent event) {
}

/**
* Retrieves the final event when the operation finishes. The final event is awaited for 'timeout' amount
* of milliseconds. If no final events has occurred, then an IllegalStateException is thrown.
* Retrieves the final event when the operation finishes.
* The final event is awaited for 'timeout'. If no final events has occurred, then null is returned.
*
* @return The final event.
* @return The final event or null if none has occurred.
*/
public OperationEvent getFinish() {
try {
return finalEventQueue.poll(timeout, TimeUnit.MILLISECONDS);
CountAndTimeUnit pollTimeout = TimeUtils.durationToCountAndTimeUnit(timeout);
return finalEventQueue.poll(pollTimeout.getCount(), pollTimeout.getUnit());
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted while waiting for the final response.", e);
}
Expand Down
Loading