void updateFrom(final UdpStatsEntry entry) { changed = (this.receiveQueueDepth != entry.getReceiveQueueDepth()) || (this.drops != entry.getDrops()) || (this.transmitQueueDepth != entry.getTransmitQueueDepth()); this.receiveQueueDepth = entry.getReceiveQueueDepth(); this.transmitQueueDepth = entry.getTransmitQueueDepth(); this.drops = entry.getDrops(); }
@Override public void complete() { if(!headerRow && currentColumn != 0) { bufferStatsEntryConsumer.accept(entry); } headerRow = false; currentColumn = 0; entry.reset(); }
@Override public void onStatisticsUpdated(final InetAddress inetAddress, final int port, final long socketIdentifier, final long inode, final long receiveQueueDepth, final long transmitQueueDepth, final long drops) { recordedEntries.add(new MonitoredEntry(inetAddress, port, socketIdentifier, inode, receiveQueueDepth, transmitQueueDepth, drops)); }
@Test public void shouldSampleMonitoredSockets() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(3)); assertEntry(recordedEntries.get(0), "0.0.0.0", 20048, 0, 0, 0, 21682); assertEntry(recordedEntries.get(1), "0.0.0.0", 56150, 0, 0, 4, 13597); assertEntry(recordedEntries.get(2), "192.168.122.1", 53, 166, 144, 0, 15292); }
protected static void assertEntry(final MonitoredEntry monitoredEntry, final String address, final int port, final long receiveQueueDepth, final long transmitQueueDepth, final long dropCount, final long inode) throws UnknownHostException { final long socketIdentifier = monitoredEntry.getSocketIdentifier(); assertThat(extractHostIpAddress(socketIdentifier), is(address)); assertThat(SocketIdentifier.extractPortNumber(socketIdentifier), is(port)); assertThat(monitoredEntry.getReceiverQueueDepth(), is(receiveQueueDepth)); assertThat(monitoredEntry.getTransmitQueueDepth(), is(transmitQueueDepth)); assertThat(monitoredEntry.getDrops(), is(dropCount)); assertThat(monitoredEntry.getInode(), is(inode)); assertThat(monitoredEntry.getSocketAddress(), is(new InetSocketAddress(address, port))); }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); } }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53).getAddress(), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); }
@Test public void shouldNotifyLifecycleListener() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); endMonitoring(requestSpecFor(getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<InetSocketAddress> monitoringStartedList = lifecycleListener.getMonitoringStartedList(); assertThat(monitoringStartedList.size(), is(2)); assertThat(monitoringStartedList.get(0), is(getSocketAddress("0.0.0.0", 20048))); assertThat(monitoringStartedList.get(1), is(getSocketAddress("0.0.0.0", 56150))); final List<InetSocketAddress> monitoringStoppedList = lifecycleListener.getMonitoringStoppedList(); assertThat(monitoringStoppedList.size(), is(1)); assertThat(monitoringStoppedList.get(0), is(getSocketAddress("0.0.0.0", 56150))); }
@Test public void shouldNotifyLifecycleListenerWhenMonitoredSocketBecomesUnavailable() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("0.0.0.0", 20048))); monitor.poll(recordingUdpSocketStatisticsHandler); ResourceUtil.writeDataFile("proc_net_udp_socket_removed_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<InetSocketAddress> monitoringStoppedList = lifecycleListener.getMonitoringStoppedList(); assertThat(monitoringStoppedList.size(), is(1)); assertThat(monitoringStoppedList.get(0), is(getSocketAddress("0.0.0.0", 20048))); }
/** * {@inheritDoc} */ @Override public void onStatisticsUpdated(final InetAddress inetAddress, final int port, final long socketIdentifier, final long inode, final long receiveQueueDepth, final long transmitQueueDepth, final long drops) { if(socketInodesNotOwnedByThisProcess.contains(inode)) { return; } if(!socketInodesOwnedByThisProcess.contains(inode)) { socketInodeRetriever.accept(socketInodesOwnedByThisProcess); if(!socketInodesOwnedByThisProcess.contains(inode)) { clearNotOwnedInodeCacheIfTooLarge(); socketInodesNotOwnedByThisProcess.add(inode); } } if(socketInodesOwnedByThisProcess.contains(inode)) { delegate.onStatisticsUpdated(inetAddress, port, socketIdentifier, inode, receiveQueueDepth, transmitQueueDepth, drops); } }
@Test public void shouldClearNotOwnedInodeCacheToPreventMemoryLeak() throws Exception { final int initialNotOwnedInode = 0; for(int i = 0; i < SocketOwnedByCurrentProcessFilter.MAX_NOT_OWNED_INODE_CACHE_SIZE + 1; i++) { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, i, 0L, 0L, 0L); } socketInodeRequestCount = 0; filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, initialNotOwnedInode, 0L, 0L, 0L); assertThat(socketInodeRequestCount, is(1)); }
void endMonitoring(final Collection<T> requestSpec) { requestSpec.stream().forEach(getEndMonitoringRequestMethod()); }
void beginMonitoring(final Collection<T> requestSpec) { requestSpec.stream().forEach(getBeginMonitoringRequestMethod()); }
@Before public void before() throws Exception { inputPath = Files.createTempFile("proc-net-udp", "txt"); ResourceUtil.writeDataFile("proc_net_udp_sample.txt", inputPath); monitor = new UdpSocketMonitor(lifecycleListener, inputPath); }
@Test public void shouldMonitorIpAddressOnLocalhost() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("127.0.0.1", 32770))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "127.0.0.1", 32770, 0, 0, 0, 15293); }
@Test public void shouldNotRequestInodeUpdateOfNotOwnedInodeWhenInodeHasBeenRequestedBefore() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(0)); assertThat(socketInodeRequestCount, is(1)); }
@Test public void shouldNotNotifyHandlerOfChangeWhenSocketIsNoLongerMonitored() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); recordingUdpSocketStatisticsHandler.getRecordedEntries().clear(); ResourceUtil.writeDataFile("proc_net_udp_updated_sample.txt", inputPath); endMonitoring(requestSpecFor(getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "0.0.0.0", 56150, 1, 0, 4, 13597); }
@Test public void shouldNotNotifyDelegateIfInodeIsNotSocketOwnedByCurrentProcess() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(0)); }
@Test public void shouldMonitorIpAddressWhoseFirstOctetJavaSignedByteValueIsNegative() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("239.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); ResourceUtil.writeDataFile("proc_net_udp_signed_first_octet_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "239.168.122.1", 53, 166, 0, 0, 15292); }
@Test public void shouldNotNotifyHandlerOfUnchangedEntries() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); recordingUdpSocketStatisticsHandler.getRecordedEntries().clear(); ResourceUtil.writeDataFile("proc_net_udp_updated_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(2)); assertEntry(recordedEntries.get(0), "0.0.0.0", 56150, 1, 0, 4, 13597); assertEntry(recordedEntries.get(1), "192.168.122.1", 53, 166, 0, 2, 15292); }