private TransmissionFileSystemOutput createAndSend(String absoulutePath, int amount, Integer capacity) { TransmissionFileSystemOutput tested = null; if (capacity != null) { tested = new TransmissionFileSystemOutput(absoulutePath, String.valueOf(capacity));; } else { tested = new TransmissionFileSystemOutput(absoulutePath); } for (int i = 0; i < amount; ++i) { tested.send(new Transmission(new byte[SIZE_OF_TRANSMISSION_CONTENT], "MockContentType", "MockEncodingType")); } return tested; } }
private void fetchNext(boolean shouldDispatch) throws InterruptedException { Transmission transmission = fileSystem.fetchOldestFile(); if (transmission == null) { Thread.sleep(sleepIntervalWhenNoTransmissionsFoundInMills); } else { if (shouldDispatch) { dispatcher.dispatch(transmission); } // TODO: check if we need this as configuration value Thread.sleep(DEFAULT_SLEEP_INTERVAL_AFTER_DISPATCHING_IN_MILLS); } } }
@Override public boolean send(Transmission transmission) { long currentSizeInBytes = size.get(); if (currentSizeInBytes >= capacityInBytes) { InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.WARN, "Persistent storage max capacity has been reached; " + "currently at %.3f KB. Telemetry will be lost, " + "please consider increasing the value of MaxTransmissionStorageFilesCapacityInMB property in the configuration file.", (currentSizeInBytes / 1024.0)); return false; } Optional<File> tempTransmissionFile = createTemporaryFile(); if (!tempTransmissionFile.isPresent()) { return false; } if (!saveTransmission(tempTransmissionFile.get(), transmission)) { return false; } if (!renameToPermanentName(tempTransmissionFile.get())) { return false; } InternalLogger.INSTANCE.info("Data persisted to file. To be sent when the network is available."); return true; }
public Transmission fetchOldestFile() { try { Optional<File> oldestFile = fetchOldestFromCache(); if (!oldestFile.isPresent()) { return null; Optional<File> oldestFileAsTemp = renameToTemporaryName(oldestFile.get()); if (!oldestFileAsTemp.isPresent()) { return null; Optional<Transmission> transmission = loadTransmission(tempFile);
@Test public void testFetchOldestFiles() throws Exception { File folder = tmpFolder.newFolder(TEMP_TEST_FOLDER+"2"); try { TransmissionFileSystemOutput tested = new TransmissionFileSystemOutput(folder.getAbsolutePath()); for (int i = 1; i <= 10; ++i) { String iAsString = String.valueOf(i); String content = MOCK_CONTENT + iAsString; tested.send(new Transmission(content.getBytes(), MOCK_CONTENT_TYPE_BASE + iAsString, MOCK_ENCODING_TYPE_BASE + iAsString)); TimeUnit.MILLISECONDS.sleep(150); // sleep a bit so 2 files can never have the same timestamp. } for (int i = 1; i <= 10; ++i) { Transmission transmission = tested.fetchOldestFile(); assertNotNull(transmission); String iAsString = String.valueOf(i); assertEquals(String.format("Wrong WebContentType %s", transmission.getWebContentType()), MOCK_CONTENT_TYPE_BASE + iAsString, transmission.getWebContentType()); assertEquals(String.format("Wrong WebContentEncodingType %s", transmission.getWebContentEncodingType()), MOCK_ENCODING_TYPE_BASE + iAsString, transmission.getWebContentEncodingType()); String fetchedContent = new String(transmission.getContent()); assertEquals(String.format("Wrong content %s", fetchedContent), MOCK_CONTENT + iAsString, fetchedContent); } Transmission transmission = tested.fetchOldestFile(); assertNull(transmission); } finally { if (folder.exists()) { FileUtils.deleteDirectory(folder); } } }
private void testIllegalState(final TransmissionDispatcher dispatcher, int numberOfThreads) throws Exception { File folder = null; try { String filesPath = System.getProperty("java.io.tmpdir") + File.separator + TEMP_TEST_FOLDER; folder = new File(filesPath); if (folder.exists()) { FileUtils.deleteDirectory(folder); } if (!folder.exists()) { folder.mkdir(); } TransmissionFileSystemOutput mock = new TransmissionFileSystemOutput(filesPath); new ActiveTransmissionLoader(mock, dispatcher, mockStateFetcher(), numberOfThreads); } finally { if (folder != null && folder.exists()) { FileUtils.deleteDirectory(folder); } } }
public TransmissionFileSystemOutput(String folderPath, String maxTransmissionStorageCapacity) { if (folderPath == null) { folderPath = new File(LocalFileSystemUtils.getTempDir(), TRANSMISSION_DEFAULT_FOLDER).getPath(); } capacityEnforcer = LimitsEnforcer.createWithClosestLimitOnError(MIN_CAPACITY_MEGABYTES, MAX_CAPACITY_MEGABYTES, DEFAULT_CAPACITY_MEGABYTES, MAX_TRANSMISSION_STORAGE_CAPACITY_NAME, maxTransmissionStorageCapacity); capacityInBytes = capacityEnforcer.getCurrentValue() * 1024 * 1024; folder = new File(folderPath); if (!folder.exists()) { folder.mkdir(); } if (!folder.exists() || !folder.canRead() || !folder.canWrite()) { throw new IllegalArgumentException("Folder must exist with read and write permissions"); } long currentSize = getTotalSizeOfTransmissionFiles(); size = new AtomicLong(currentSize); }
@Override public TelemetriesTransmitter create(String endpoint, String maxTransmissionStorageCapacity, boolean throttlingIsEnabled, int maxInstanceRetries) { // An active object with the network sender TransmissionOutput actualNetworkSender = TestThreadLocalData.getTransmissionOutput(); final TransmissionPolicyManager transmissionPolicyManager = new TransmissionPolicyManager(throttlingIsEnabled); transmissionPolicyManager.addTransmissionHandler(new ErrorHandler(transmissionPolicyManager)); transmissionPolicyManager.addTransmissionHandler(new PartialSuccessHandler(transmissionPolicyManager)); transmissionPolicyManager.addTransmissionHandler(new ThrottlingHandler(transmissionPolicyManager)); transmissionPolicyManager.setMaxInstantRetries(maxInstanceRetries); TransmissionPolicyStateFetcher stateFetcher = transmissionPolicyManager.getTransmissionPolicyState(); TransmissionOutput networkSender = new ActiveTransmissionNetworkOutput(actualNetworkSender, stateFetcher); // An active object with the file system sender TransmissionFileSystemOutput fileSystemSender = new TransmissionFileSystemOutput(); TransmissionOutput activeFileSystemOutput = new ActiveTransmissionFileSystemOutput(fileSystemSender, stateFetcher); // The dispatcher works with the two active senders TransmissionDispatcher dispatcher = new NonBlockingDispatcher(new TransmissionOutput[] {networkSender, activeFileSystemOutput}); // The loader works with the file system loader as the active one does TransmissionsLoader transmissionsLoader = new ActiveTransmissionLoader(fileSystemSender, stateFetcher, dispatcher); // The Transmitter manage all TelemetriesTransmitter telemetriesTransmitter = new TransmitterImpl(dispatcher, new GzipTelemetrySerializer(), transmissionsLoader); return telemetriesTransmitter; } }
TransmissionFileSystemOutput fileSystem = new TransmissionFileSystemOutput(filesPath); TransmissionDispatcher mockDispatcher = Mockito.mock(TransmissionDispatcher.class); tested = new ActiveTransmissionLoader(fileSystem, mockDispatcher, mockStateFetcher(), 2); fileSystem.send(new Transmission(new byte[2], "MockContentType", "MockEncodingType"));
TransmissionFileSystemOutput fileSystemSender = new TransmissionFileSystemOutput(null, maxTransmissionStorageCapacity); TransmissionOutput activeFileSystemOutput = new ActiveTransmissionFileSystemOutput(fileSystemSender, stateFetcher);