Search in sources :

Example 16 with PeerStatus

use of org.apache.nifi.remote.PeerStatus in project nifi by apache.

the class PeerSelector method recoverPersistedPeerStatuses.

private static Set<PeerStatus> recoverPersistedPeerStatuses(final File file) throws IOException {
    if (!file.exists()) {
        return null;
    }
    final Set<PeerStatus> statuses = new HashSet<>();
    try (final InputStream fis = new FileInputStream(file);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(fis))) {
        String line;
        while ((line = reader.readLine()) != null) {
            final String[] splits = line.split(Pattern.quote(":"));
            if (splits.length != 3 && splits.length != 4) {
                continue;
            }
            final String hostname = splits[0];
            final int port = Integer.parseInt(splits[1]);
            final boolean secure = Boolean.parseBoolean(splits[2]);
            final boolean supportQueryForPeer = splits.length == 4 && Boolean.parseBoolean(splits[3]);
            statuses.add(new PeerStatus(new PeerDescription(hostname, port, secure), 1, supportQueryForPeer));
        }
    }
    return statuses;
}
Also used : InputStreamReader(java.io.InputStreamReader) PeerDescription(org.apache.nifi.remote.PeerDescription) PeerStatus(org.apache.nifi.remote.PeerStatus) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 17 with PeerStatus

use of org.apache.nifi.remote.PeerStatus in project nifi by apache.

the class PeerSelector method getPeerStatuses.

private Set<PeerStatus> getPeerStatuses() {
    final PeerStatusCache cache = this.peerStatusCache;
    if (cache == null || cache.getStatuses() == null || cache.getStatuses().isEmpty()) {
        return null;
    }
    if (cache.getTimestamp() + PEER_CACHE_MILLIS < systemTime.currentTimeMillis()) {
        final Set<PeerStatus> equalizedSet = new HashSet<>(cache.getStatuses().size());
        for (final PeerStatus status : cache.getStatuses()) {
            final PeerStatus equalizedStatus = new PeerStatus(status.getPeerDescription(), 1, status.isQueryForPeers());
            equalizedSet.add(equalizedStatus);
        }
        return equalizedSet;
    }
    return cache.getStatuses();
}
Also used : PeerStatus(org.apache.nifi.remote.PeerStatus) PeerStatusCache(org.apache.nifi.remote.util.PeerStatusCache) HashSet(java.util.HashSet)

Example 18 with PeerStatus

use of org.apache.nifi.remote.PeerStatus in project nifi by apache.

the class PeerSelector method persistPeerStatuses.

private void persistPeerStatuses(final Set<PeerStatus> statuses) {
    if (persistenceFile == null) {
        return;
    }
    try (final OutputStream fos = new FileOutputStream(persistenceFile);
        final OutputStream out = new BufferedOutputStream(fos)) {
        for (final PeerStatus status : statuses) {
            final PeerDescription description = status.getPeerDescription();
            final String line = description.getHostname() + ":" + description.getPort() + ":" + description.isSecure() + ":" + status.isQueryForPeers() + "\n";
            out.write(line.getBytes(StandardCharsets.UTF_8));
        }
    } catch (final IOException e) {
        error(logger, eventReporter, "Failed to persist list of Peers due to {}; if restarted and peer's NCM is down," + " may be unable to transfer data until communications with NCM are restored", e.toString());
        logger.error("", e);
    }
}
Also used : PeerDescription(org.apache.nifi.remote.PeerDescription) PeerStatus(org.apache.nifi.remote.PeerStatus) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

PeerStatus (org.apache.nifi.remote.PeerStatus)18 PeerDescription (org.apache.nifi.remote.PeerDescription)13 HashSet (java.util.HashSet)11 IOException (java.io.IOException)9 Peer (org.apache.nifi.remote.Peer)5 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)5 Test (org.junit.Test)5 DataInputStream (java.io.DataInputStream)3 DataOutputStream (java.io.DataOutputStream)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Set (java.util.Set)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 TransferDirection (org.apache.nifi.remote.TransferDirection)3 PeerStatusCache (org.apache.nifi.remote.util.PeerStatusCache)3 BufferedOutputStream (java.io.BufferedOutputStream)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2