Search in sources :

Example 6 with ReplicationMessageList

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

Example 7 with ReplicationMessageList

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();
}
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

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