Search in sources :

Example 16 with PeerDescription

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

the class PeerSelector method fetchRemotePeerStatuses.

private Set<PeerStatus> fetchRemotePeerStatuses() throws IOException {
    final Set<PeerDescription> peersToRequestClusterInfoFrom = new HashSet<>();
    // Look at all of the peers that we fetched last time.
    final Set<PeerStatus> lastFetched = lastFetchedQueryablePeers;
    if (lastFetched != null && !lastFetched.isEmpty()) {
        lastFetched.stream().map(peer -> peer.getPeerDescription()).forEach(desc -> peersToRequestClusterInfoFrom.add(desc));
    }
    // Always add the configured node info to the list of peers to communicate with
    peersToRequestClusterInfoFrom.add(peerStatusProvider.getBootstrapPeerDescription());
    logger.debug("Fetching remote peer statuses from: {}", peersToRequestClusterInfoFrom);
    Exception lastFailure = null;
    for (final PeerDescription peerDescription : peersToRequestClusterInfoFrom) {
        try {
            final Set<PeerStatus> statuses = peerStatusProvider.fetchRemotePeerStatuses(peerDescription);
            lastFetchedQueryablePeers = statuses.stream().filter(p -> p.isQueryForPeers()).collect(Collectors.toSet());
            return statuses;
        } catch (final Exception e) {
            logger.warn("Could not communicate with {}:{} to determine which nodes exist in the remote NiFi cluster, due to {}", peerDescription.getHostname(), peerDescription.getPort(), e.toString());
            lastFailure = e;
        }
    }
    final IOException ioe = new IOException("Unable to communicate with remote NiFi cluster in order to determine which nodes exist in the remote cluster");
    if (lastFailure != null) {
        ioe.addSuppressed(lastFailure);
    }
    throw ioe;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Random(java.util.Random) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) PeerStatus(org.apache.nifi.remote.PeerStatus) Map(java.util.Map) OutputStream(java.io.OutputStream) TransferDirection(org.apache.nifi.remote.TransferDirection) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) PeerDescription(org.apache.nifi.remote.PeerDescription) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) PeerStatusCache(org.apache.nifi.remote.util.PeerStatusCache) EventReporter(org.apache.nifi.events.EventReporter) EventReportUtil.error(org.apache.nifi.remote.util.EventReportUtil.error) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Peer(org.apache.nifi.remote.Peer) Collections(java.util.Collections) InputStream(java.io.InputStream) EventReportUtil.warn(org.apache.nifi.remote.util.EventReportUtil.warn) PeerDescription(org.apache.nifi.remote.PeerDescription) PeerStatus(org.apache.nifi.remote.PeerStatus) IOException(java.io.IOException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 17 with PeerDescription

use of org.apache.nifi.remote.PeerDescription 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 18 with PeerDescription

use of org.apache.nifi.remote.PeerDescription 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

PeerDescription (org.apache.nifi.remote.PeerDescription)18 PeerStatus (org.apache.nifi.remote.PeerStatus)13 HashSet (java.util.HashSet)10 Peer (org.apache.nifi.remote.Peer)9 IOException (java.io.IOException)6 Test (org.junit.Test)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 InputStream (java.io.InputStream)4 SocketChannelCommunicationsSession (org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4 OutputStream (java.io.OutputStream)3 URI (java.net.URI)3 Set (java.util.Set)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 EventReporter (org.apache.nifi.events.EventReporter)3 TransferDirection (org.apache.nifi.remote.TransferDirection)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3