Search in sources :

Example 1 with ReplicationMessage

use of dvoraka.avservice.common.data.ReplicationMessage 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 2 with ReplicationMessage

use of dvoraka.avservice.common.data.ReplicationMessage 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 3 with ReplicationMessage

use of dvoraka.avservice.common.data.ReplicationMessage in project av-service by dvoraka.

the class AmqpReplicationComponent method onMessage.

@Override
public void onMessage(Message message) {
    log.debug("Receive: " + message);
    ReplicationMessage replicationMessage;
    try {
        replicationMessage = (ReplicationMessage) messageConverter.fromMessage(message);
        log.debug("Converted ({}): {}", nodeId, replicationMessage);
    } catch (MessageConversionException e) {
        log.warn("Conversion error!", e);
        return;
    }
    listeners.forEach(listener -> listener.onMessage(replicationMessage));
}
Also used : MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) ReplicationMessage(dvoraka.avservice.common.data.ReplicationMessage)

Example 4 with ReplicationMessage

use of dvoraka.avservice.common.data.ReplicationMessage 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 ReplicationMessage

use of dvoraka.avservice.common.data.ReplicationMessage 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

ReplicationMessage (dvoraka.avservice.common.data.ReplicationMessage)6 ReplicationMessageList (dvoraka.avservice.client.service.response.ReplicationMessageList)5 ReplicationServiceClient (dvoraka.avservice.client.service.ReplicationServiceClient)3 ReplicationResponseClient (dvoraka.avservice.client.service.response.ReplicationResponseClient)3 ReplicationMessageListener (dvoraka.avservice.common.ReplicationMessageListener)3 Command (dvoraka.avservice.common.data.Command)3 MessageRouting (dvoraka.avservice.common.data.MessageRouting)3 ReplicationStatus (dvoraka.avservice.common.data.ReplicationStatus)3 ReplicationHelper (dvoraka.avservice.common.replication.ReplicationHelper)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 Optional (java.util.Optional)3 Set (java.util.Set)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 PostConstruct (javax.annotation.PostConstruct)3 LogManager (org.apache.logging.log4j.LogManager)3