use of dvoraka.avservice.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultReplicationService method whoHas.
/**
* Returns a set of neighbour IDs with a given file.
*
* @param filename the filename
* @param owner the owner
* @return the set of IDs
*/
public Set<String> whoHas(String filename, String owner) {
ReplicationMessage query = createExistsRequest(filename, owner, nodeId);
serviceClient.sendMessage(query);
Optional<ReplicationMessageList> response;
response = responseClient.getResponseWaitSize(query.getId(), MAX_RESPONSE_TIME, neighbourCount());
ReplicationMessageList messages = response.orElseGet(ReplicationMessageList::new);
return messages.stream().filter(message -> message.getReplicationStatus() == ReplicationStatus.OK).map(ReplicationMessage::getFromId).collect(Collectors.toSet());
}
use of dvoraka.avservice.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultReplicationService method loadFile.
@Override
public FileMessage loadFile(FileMessage message) throws FileServiceException {
log.debug("Load ({}): {}", nodeId, message);
int neighbours = neighbourCount();
try {
if (remoteLock.lockForFile(message.getFilename(), message.getOwner(), neighbours)) {
if (localCopyExists(message)) {
log.debug("Loading locally...");
return fileService.loadFile(message);
}
if (exists(message)) {
log.debug("Loading remotely {}...", idString);
sendLoadMessage(message);
Optional<ReplicationMessageList> replicationMessages = responseClient.getResponseWait(message.getId(), MAX_RESPONSE_TIME);
ReplicationMessageList messages = replicationMessages.orElseGet(ReplicationMessageList::new);
return messages.stream().filter(msg -> msg.getReplicationStatus() == ReplicationStatus.OK).peek(m -> log.debug("Load success {}.", idString)).findFirst().orElseThrow(FileNotFoundException::new);
}
log.debug("Loading failed {}.", idString);
throw new FileNotFoundException();
} else {
log.warn("Load lock problem for {}: {}", idString, message);
throw new CannotAcquireLockException();
}
} catch (InterruptedException e) {
log.warn("Locking interrupted!", e);
Thread.currentThread().interrupt();
} finally {
remoteLock.unlockForFile(message.getFilename(), message.getOwner(), neighbours);
}
throw new FileNotFoundException();
}
Aggregations