use of dvoraka.avservice.storage.FileServiceException in project av-service by dvoraka.
the class FileMessageProcessor method load.
private void load(AvMessage message) {
FileMessage fileMessage;
try {
fileMessage = fileService.loadFile(message);
notifyListeners(message.createFileMessage(fileMessage.getData(), fileMessage.getType()));
} catch (FileServiceException e) {
log.warn(FILE_SERVICE_PROBLEM, e);
notifyListeners(message.createErrorResponse("Load problem"));
}
}
use of dvoraka.avservice.storage.FileServiceException in project av-service by dvoraka.
the class DefaultReplicationService method saveFile.
@Override
public void saveFile(FileMessage message) throws FileServiceException {
log.debug("Save {}: {}", idString, message);
// depends on an efficiency of the sending algorithm and
// it still must be different for "bigger" (~10 MB+) files
final int sizeTimeRatio = 2_000;
final int maxSaveTime = message.getData().length / sizeTimeRatio;
log.debug("Setting max save time to {} {}", maxSaveTime, idString);
if (localCopyExists(message)) {
throw new ExistingFileException();
}
int neighbours = neighbourCount();
try {
if (remoteLock.lockForFile(message.getFilename(), message.getOwner(), neighbours)) {
if (!exists(message)) {
log.debug("Saving locally {}...", idString);
fileService.saveFile(message);
log.debug("Saving remotely {}...", idString);
sendSaveMessage(message);
Optional<ReplicationMessageList> responses = responseClient.getResponseWaitSize(message.getId(), MAX_RESPONSE_TIME + maxSaveTime, getReplicationCount() - 1);
ReplicationMessageList messages = responses.orElseGet(ReplicationMessageList::new);
long successCount = messages.stream().filter(msg -> msg.getCommand() == Command.SAVE).filter(msg -> msg.getReplicationStatus() == ReplicationStatus.OK).count();
if (successCount != getReplicationCount() - 1) {
// rollback transaction
log.debug("Expected {}, got {} {}", getReplicationCount() - 1, successCount, idString);
throw new LockCountNotMatchException();
}
log.debug("Save success {}.", idString);
} else {
throw new ExistingFileException();
}
} else {
log.warn("Save 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);
}
}
use of dvoraka.avservice.storage.FileServiceException in project av-service by dvoraka.
the class DefaultReplicationService method handleOnMessage.
private void handleOnMessage(ReplicationMessage message) {
// broadcast and unicast messages from the replication network
log.debug("On message ({}): {}", nodeId, message);
// handle discover
if (message.getRouting() == MessageRouting.BROADCAST && message.getCommand() == Command.DISCOVER) {
serviceClient.sendMessage(createDiscoverReply(message, nodeId));
}
// handle exists
if (message.getRouting() == MessageRouting.BROADCAST && message.getCommand() == Command.EXISTS) {
if (localCopyExists(message)) {
serviceClient.sendMessage(createExistsReply(message, nodeId));
} else {
serviceClient.sendMessage(createNonExistsReply(message, nodeId));
}
}
// handle save
if (message.getRouting() == MessageRouting.UNICAST && message.getCommand() == Command.SAVE) {
try {
fileService.saveFile(message);
serviceClient.sendMessage(createSaveSuccess(message, nodeId));
} catch (FileServiceException e) {
log.warn("Saving failed (" + nodeId + ")", e);
serviceClient.sendMessage(createSaveFailed(message, nodeId));
}
}
// handle load
if (message.getRouting() == MessageRouting.UNICAST && message.getCommand() == Command.LOAD) {
try {
FileMessage fileMessage = fileService.loadFile(message);
serviceClient.sendMessage(createLoadSuccess(fileMessage, message.getFromId(), nodeId));
} catch (FileServiceException e) {
log.warn("Loading failed (" + nodeId + ")", e);
serviceClient.sendMessage(createLoadFailed(message, message.getFromId(), nodeId));
}
}
// handle delete
if (message.getRouting() == MessageRouting.UNICAST && message.getCommand() == Command.DELETE) {
try {
fileService.deleteFile(message);
serviceClient.sendMessage(createDeleteSuccess(message, nodeId, message.getFromId()));
} catch (FileServiceException e) {
log.warn("Deleting failed (" + nodeId + ")", e);
// serviceClient.sendMessage();
}
}
}
use of dvoraka.avservice.storage.FileServiceException 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