use of dvoraka.avservice.client.service.response.ReplicationMessageList 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.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultReplicationService method discoverNeighbours.
private void discoverNeighbours() {
log.debug("Discovering neighbours ({})...", nodeId);
ReplicationMessage message = createDiscoverRequest(nodeId);
serviceClient.sendMessage(message);
Optional<ReplicationMessageList> responses = responseClient.getResponseWait(message.getId(), MAX_RESPONSE_TIME, MAX_RESPONSE_TIME);
if (!responses.isPresent()) {
log.debug("Discovered ({}): none", nodeId);
return;
}
ReplicationMessageList messages = responses.orElseGet(ReplicationMessageList::new);
Set<String> newNeighbours = messages.stream().filter(msg -> msg.getReplicationStatus() == ReplicationStatus.READY).map(ReplicationMessage::getFromId).collect(Collectors.toSet());
neighbours.clear();
neighbours.addAll(newNeighbours);
log.debug("Discovered ({}): {}", nodeId, neighbourCount());
}
use of dvoraka.avservice.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultRemoteLock method unlockForFile.
@Override
public boolean unlockForFile(String filename, String owner, int lockCount) {
// send the unlock request
ReplicationMessage unlockRequest = createUnlockRequest(filename, owner, nodeId, getSequence());
serviceClient.sendMessage(unlockRequest);
// get replies
Optional<ReplicationMessageList> unlockReplies = responseClient.getResponseWaitSize(unlockRequest.getId(), MAX_RESPONSE_TIME, lockCount);
// count success unlocks
if (unlockReplies.isPresent()) {
long successUnlocks = unlockReplies.get().stream().filter(message -> message.getReplicationStatus() == ReplicationStatus.OK).count();
if (lockCount == successUnlocks) {
try {
unlockFile(filename, owner);
} catch (FileNotLockedException e) {
log.warn(UNLOCKING_FAILED, e);
}
return true;
} else {
log.warn(UNLOCKING_FAILED);
}
}
return false;
}
use of dvoraka.avservice.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultRemoteLock method initializeSequence.
private void initializeSequence() {
log.debug("Initializing sequence...");
ReplicationMessage request = createSequenceRequest(nodeId);
serviceClient.sendMessage(request);
ReplicationMessageList responses = responseClient.getResponseWait(request.getId(), MAX_RESPONSE_TIME).orElseGet(ReplicationMessageList::new);
long actualSequence = responses.stream().peek(message -> log.debug("Sequence: {}", message)).findFirst().map(ReplicationMessage::getSequence).orElse(1L);
setSequence(actualSequence);
}
use of dvoraka.avservice.client.service.response.ReplicationMessageList in project av-service by dvoraka.
the class DefaultRemoteLock method lockForFile.
@Override
public boolean lockForFile(String filename, String owner, int lockCount) throws InterruptedException {
// lock local file if possible
if (!lockFile(filename, owner)) {
return false;
}
log.debug("Locking {} nodes ({})...", lockCount, nodeId);
lockingLock.lock();
log.debug("Locked.");
ReplicationMessage lockRequest = createLockRequest(filename, owner, nodeId, getSequence());
serviceClient.sendMessage(lockRequest);
Optional<ReplicationMessageList> lockReplies = responseClient.getResponseWaitSize(lockRequest.getId(), MAX_RESPONSE_TIME, lockCount);
// count success locks
if (lockReplies.isPresent()) {
long successLocks = lockReplies.get().stream().filter(message -> message.getReplicationStatus() == ReplicationStatus.READY).count();
if (lockCount == successLocks) {
incSequence();
log.info("Remote locking success.");
lockingLock.unlock();
log.debug("Unlocked.");
return true;
} else {
lockingLock.unlock();
log.debug("Unlocked.");
try {
unlockFile(filename, owner);
} catch (FileNotLockedException e) {
log.warn(UNLOCKING_FAILED, e);
}
return false;
}
}
lockingLock.unlock();
log.debug("Unlocked.");
try {
unlockFile(filename, owner);
} catch (FileNotLockedException e) {
log.warn(UNLOCKING_FAILED, e);
}
return false;
}
Aggregations