Search in sources :

Example 1 with CannotAcquireLockException

use of dvoraka.avservice.storage.CannotAcquireLockException 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 CannotAcquireLockException

use of dvoraka.avservice.storage.CannotAcquireLockException 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();
}
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) CannotAcquireLockException(dvoraka.avservice.storage.CannotAcquireLockException) ReplicationMessageList(dvoraka.avservice.client.service.response.ReplicationMessageList) FileNotFoundException(dvoraka.avservice.storage.FileNotFoundException)

Aggregations

ReplicationServiceClient (dvoraka.avservice.client.service.ReplicationServiceClient)2 ReplicationMessageList (dvoraka.avservice.client.service.response.ReplicationMessageList)2 ReplicationResponseClient (dvoraka.avservice.client.service.response.ReplicationResponseClient)2 Command (dvoraka.avservice.common.data.Command)2 FileMessage (dvoraka.avservice.common.data.FileMessage)2 MessageRouting (dvoraka.avservice.common.data.MessageRouting)2 ReplicationMessage (dvoraka.avservice.common.data.ReplicationMessage)2 ReplicationStatus (dvoraka.avservice.common.data.ReplicationStatus)2 ReplicationHelper (dvoraka.avservice.common.replication.ReplicationHelper)2 CannotAcquireLockException (dvoraka.avservice.storage.CannotAcquireLockException)2 ExistingFileException (dvoraka.avservice.storage.ExistingFileException)2 FileNotFoundException (dvoraka.avservice.storage.FileNotFoundException)2 FileServiceException (dvoraka.avservice.storage.FileServiceException)2 LockCountNotMatchException (dvoraka.avservice.storage.LockCountNotMatchException)2 FileService (dvoraka.avservice.storage.service.FileService)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)2 Executors (java.util.concurrent.Executors)2