Search in sources :

Example 1 with ReplicationMessageList

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);
    }
}
Also used : ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage) ReplicationHelper(dvoraka.avservice.common.replication.ReplicationHelper) ExistingFileException(dvoraka.avservice.storage.ExistingFileException) ReplicationStatus(dvoraka.avservice.common.data.ReplicationStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) MessageRouting(dvoraka.avservice.common.data.MessageRouting) PreDestroy(javax.annotation.PreDestroy) ReplicationResponseClient(dvoraka.avservice.client.service.response.ReplicationResponseClient) FileService(dvoraka.avservice.storage.service.FileService) Service(org.springframework.stereotype.Service) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FileServiceException(dvoraka.avservice.storage.FileServiceException) LockCountNotMatchException(dvoraka.avservice.storage.LockCountNotMatchException) ReplicationServiceClient(dvoraka.avservice.client.service.ReplicationServiceClient) CannotAcquireLockException(dvoraka.avservice.storage.CannotAcquireLockException) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) Command(dvoraka.avservice.common.data.Command) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) FileMessage(dvoraka.avservice.common.data.FileMessage) TimeUnit(java.util.concurrent.TimeUnit) FileNotFoundException(dvoraka.avservice.storage.FileNotFoundException) Logger(org.apache.logging.log4j.Logger) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) ExistingFileException(dvoraka.avservice.storage.ExistingFileException) CannotAcquireLockException(dvoraka.avservice.storage.CannotAcquireLockException) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) LockCountNotMatchException(dvoraka.avservice.storage.LockCountNotMatchException)

Example 2 with ReplicationMessageList

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());
}
Also used : ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage)

Example 3 with ReplicationMessageList

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;
}
Also used : ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage) ReplicationHelper(dvoraka.avservice.common.replication.ReplicationHelper) ReplicationStatus(dvoraka.avservice.common.data.ReplicationStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) MessageRouting(dvoraka.avservice.common.data.MessageRouting) HashSet(java.util.HashSet) ReplicationResponseClient(dvoraka.avservice.client.service.response.ReplicationResponseClient) Md5HashingService(dvoraka.avservice.common.service.Md5HashingService) ReplicationServiceClient(dvoraka.avservice.client.service.ReplicationServiceClient) ReplicationMessageListener(dvoraka.avservice.common.ReplicationMessageListener) ReentrantLock(java.util.concurrent.locks.ReentrantLock) HashingService(dvoraka.avservice.common.service.HashingService) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) Command(dvoraka.avservice.common.data.Command) EventListener(org.springframework.context.event.EventListener) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) AtomicLong(java.util.concurrent.atomic.AtomicLong) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) Logger(org.apache.logging.log4j.Logger) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage)

Example 4 with ReplicationMessageList

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);
}
Also used : ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage) ReplicationHelper(dvoraka.avservice.common.replication.ReplicationHelper) ReplicationStatus(dvoraka.avservice.common.data.ReplicationStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) MessageRouting(dvoraka.avservice.common.data.MessageRouting) HashSet(java.util.HashSet) ReplicationResponseClient(dvoraka.avservice.client.service.response.ReplicationResponseClient) Md5HashingService(dvoraka.avservice.common.service.Md5HashingService) ReplicationServiceClient(dvoraka.avservice.client.service.ReplicationServiceClient) ReplicationMessageListener(dvoraka.avservice.common.ReplicationMessageListener) ReentrantLock(java.util.concurrent.locks.ReentrantLock) HashingService(dvoraka.avservice.common.service.HashingService) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) Command(dvoraka.avservice.common.data.Command) EventListener(org.springframework.context.event.EventListener) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) AtomicLong(java.util.concurrent.atomic.AtomicLong) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) Logger(org.apache.logging.log4j.Logger) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage)

Example 5 with ReplicationMessageList

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;
}
Also used : ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage) ReplicationHelper(dvoraka.avservice.common.replication.ReplicationHelper) ReplicationStatus(dvoraka.avservice.common.data.ReplicationStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) MessageRouting(dvoraka.avservice.common.data.MessageRouting) HashSet(java.util.HashSet) ReplicationResponseClient(dvoraka.avservice.client.service.response.ReplicationResponseClient) Md5HashingService(dvoraka.avservice.common.service.Md5HashingService) ReplicationServiceClient(dvoraka.avservice.client.service.ReplicationServiceClient) ReplicationMessageListener(dvoraka.avservice.common.ReplicationMessageListener) ReentrantLock(java.util.concurrent.locks.ReentrantLock) HashingService(dvoraka.avservice.common.service.HashingService) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) Command(dvoraka.avservice.common.data.Command) EventListener(org.springframework.context.event.EventListener) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) AtomicLong(java.util.concurrent.atomic.AtomicLong) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) Logger(org.apache.logging.log4j.Logger) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage)

Aggregations

ReplicationMessageList (dvoraka.avservice.client.service.response.ReplicationMessageList)7 ReplicationMessage (dvoraka.avservice.common.data.ReplicationMessage)7 ReplicationServiceClient (dvoraka.avservice.client.service.ReplicationServiceClient)5 ReplicationResponseClient (dvoraka.avservice.client.service.response.ReplicationResponseClient)5 Command (dvoraka.avservice.common.data.Command)5 MessageRouting (dvoraka.avservice.common.data.MessageRouting)5 ReplicationStatus (dvoraka.avservice.common.data.ReplicationStatus)5 ReplicationHelper (dvoraka.avservice.common.replication.ReplicationHelper)5 Optional (java.util.Optional)5 Set (java.util.Set)5 PostConstruct (javax.annotation.PostConstruct)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 ReplicationMessageListener (dvoraka.avservice.common.ReplicationMessageListener)3 HashingService (dvoraka.avservice.common.service.HashingService)3 Md5HashingService (dvoraka.avservice.common.service.Md5HashingService)3 StandardCharsets (java.nio.charset.StandardCharsets)3 HashSet (java.util.HashSet)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3