protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; if (status.getConsumedVolume() >= volumeThreshold) { LOG.info("Need to upload logs - current size: {}, threshold: {}", status.getConsumedVolume(), volumeThreshold); decision = LogUploadStrategyDecision.UPLOAD; } else if (status.getRecordCount() >= countThreshold) { LOG.info("Need to upload logs - current count: {}, threshold: {}", status.getRecordCount(), countThreshold); decision = LogUploadStrategyDecision.UPLOAD; } return decision; }
@Override protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { long currentRecordCount = status.getRecordCount(); LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; if (currentRecordCount >= countThreshold) { LOG.info("Need to upload logs - current count: {}, threshold: {}", currentRecordCount, countThreshold); decision = LogUploadStrategyDecision.UPLOAD; } return decision; } }
@Override protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { long currentConsumedVolume = status.getConsumedVolume(); LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; if (currentConsumedVolume >= volumeThreshold) { LOG.info("Need to upload logs - current size: {}, threshold: {}", currentConsumedVolume, volumeThreshold); decision = LogUploadStrategyDecision.UPLOAD; } return decision; } }
@Override protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; long currentTime = System.currentTimeMillis(); if (((currentTime - lastUploadTime) / 1000) >= uploadCheckPeriod) { LOG.info("Need to upload logs - current count: {}, lastUploadedTime: {}, timeLimit: {} sec", status.getRecordCount(), lastUploadTime, uploadCheckPeriod); decision = LogUploadStrategyDecision.UPLOAD; lastUploadTime = currentTime; } return decision; } }
@Test public void testLessThanRecordThresholdCount() { int thresholdVolume = 5; int uploadCheckPeriod = 2000; // 2 Sec LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) (thresholdVolume - 1)); StorageSizeWithTimeLimitLogUploadStrategy strategy = new StorageSizeWithTimeLimitLogUploadStrategy(thresholdVolume, uploadCheckPeriod, TimeUnit.MILLISECONDS); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.NOOP); }
@Override protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; long currentTime = System.currentTimeMillis(); long currentConsumedVolume = status.getConsumedVolume(); if (currentConsumedVolume >= volumeThreshold) { LOG.info("Need to upload logs - current size: {}, threshold: {}", currentConsumedVolume, volumeThreshold); decision = LogUploadStrategyDecision.UPLOAD; lastUploadTime = currentTime; } else if (((currentTime - lastUploadTime) / 1000) >= uploadCheckPeriod) { LOG.info("Need to upload logs - current count: {}, lastUploadedTime: {}, timeLimit: {}", status.getRecordCount(), lastUploadTime, uploadCheckPeriod); decision = LogUploadStrategyDecision.UPLOAD; lastUploadTime = currentTime; } return decision; } }
@Override protected LogUploadStrategyDecision checkUploadNeeded(LogStorageStatus status) { LogUploadStrategyDecision decision = LogUploadStrategyDecision.NOOP; long currentTime = System.currentTimeMillis(); long currentRecordCount = status.getRecordCount(); if (currentRecordCount >= countThreshold) { LOG.info("Need to upload logs - current count: {}, threshold: {}", currentRecordCount, countThreshold); decision = LogUploadStrategyDecision.UPLOAD; lastUploadTime = currentTime; } else if (((currentTime - lastUploadTime) / 1000) >= uploadCheckPeriod) { LOG.info("Need to upload logs - current count: {}, lastUploadedTime: {}, timeLimit: {}", currentRecordCount, lastUploadTime, uploadCheckPeriod); decision = LogUploadStrategyDecision.UPLOAD; lastUploadTime = currentTime; } return decision; } }
@Test public void testEqualToRecordThresholdCount() { int thresholdVolume = 5; int uploadCheckPeriod = 2000; // 2 Sec LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) thresholdVolume); StorageSizeWithTimeLimitLogUploadStrategy strategy = new StorageSizeWithTimeLimitLogUploadStrategy(thresholdVolume, uploadCheckPeriod, TimeUnit.MILLISECONDS); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }
Assert.assertEquals(insertionCount - receivedCount, storage.getStatus().getRecordCount()); Assert.assertEquals((insertionCount - receivedCount) * 3, storage.getStatus().getConsumedVolume()); Assert.assertEquals(insertionCount - receivedCount, storage.getStatus().getRecordCount()); Assert.assertEquals((insertionCount - receivedCount) * 3, storage.getStatus().getConsumedVolume()); Assert.assertEquals(insertionCount - receivedCount, storage.getStatus().getRecordCount()); Assert.assertEquals((insertionCount - receivedCount) * 3, storage.getStatus().getConsumedVolume()); Assert.assertEquals(insertionCount - receivedCount, storage.getStatus().getRecordCount()); Assert.assertEquals((insertionCount - receivedCount) * 3, storage.getStatus().getConsumedVolume()); storage.close();
private void processUploadDecision(LogUploadStrategyDecision decision) { switch (decision) { case UPLOAD: if (isUploadAllowed()) { LOG.debug("Going to upload logs"); transport.sync(); } break; case NOOP: if (strategy.getUploadCheckPeriod() > 0 && storage.getStatus().getRecordCount() > 0) { scheduleUploadCheck(); } break; default: break; } }
@Test public void testGreaterThanVolumeThreshold() { int thresholdVolume = 5; LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) (thresholdVolume + 1)); StorageSizeLogUploadStrategy strategy = new StorageSizeLogUploadStrategy(thresholdVolume); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); } }
@Test public void testRecordCountAndConsumedBytes() { long bucketSize = 3; int recordCount = 3; LogStorage storage = (LogStorage) getStorage(bucketSize, recordCount); LogRecord record = new LogRecord(); int insertionCount = 3; /* * Size of each record is 3B */ int iter = insertionCount; while (iter-- > 0) { storage.addLogRecord(record); } Assert.assertTrue(storage.getStatus().getRecordCount() == insertionCount); Assert.assertTrue(storage.getStatus().getConsumedVolume() == (insertionCount * record.getSize())); storage.close(); }
@Test public void testLessThanRecordThresholdCount() { int thresholdCount = 5; LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getRecordCount()).thenReturn((long) (thresholdCount - 1)); RecordCountLogUploadStrategy strategy = new RecordCountLogUploadStrategy(thresholdCount); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.NOOP); }
@Test public void testGreaterThanRecordThresholdCount() { int thresholdVolume = 5; int uploadCheckPeriod = 2000; // 2 Sec LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) (thresholdVolume + 1)); StorageSizeWithTimeLimitLogUploadStrategy strategy = new StorageSizeWithTimeLimitLogUploadStrategy(thresholdVolume, uploadCheckPeriod, TimeUnit.MILLISECONDS); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }
@Test public void testPersistDBState() { LogStorage storage = (LogStorage) getStorage(BUCKET_SIZE, RECORD_COUNT); LogRecord record = new LogRecord(); int insertionCount = 7; /* * Size of each record is 3B */ int iter = insertionCount; while (iter-- > 0) { storage.addLogRecord(record); } LogBucket beforePersist = storage.getNextBucket(); storage.close(); storage = (LogStorage) getStorage(BUCKET_SIZE, RECORD_COUNT); LogStorageStatus storageStatus = (LogStorageStatus) storage; Assert.assertEquals(insertionCount, storageStatus.getRecordCount()); Assert.assertEquals(insertionCount * 3, storageStatus.getConsumedVolume()); LogBucket afterPersist = storage.getNextBucket(); Assert.assertEquals(beforePersist.getRecords().size(), afterPersist.getRecords().size()); storage.close(); }
@Test public void testEqualToRecordThresholdCount() { int thresholdCount = 5; int uploadCheckPeriod = 2000; // 2 Sec LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getRecordCount()).thenReturn((long) thresholdCount); RecordCountWithTimeLimitLogUploadStrategy strategy = new RecordCountWithTimeLimitLogUploadStrategy(thresholdCount, uploadCheckPeriod, TimeUnit.MILLISECONDS); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }
@Test public void testLessThanVolumeThreshold() { int thresholdVolume = 5; LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) (thresholdVolume - 1)); StorageSizeLogUploadStrategy strategy = new StorageSizeLogUploadStrategy(thresholdVolume); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.NOOP); }
@Test public void testGreaterThanRecordThresholdCount() { int thresholdCount = 5; int uploadCheckPeriod = 2000; // 2 Sec LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getRecordCount()).thenReturn((long) (thresholdCount + 1)); RecordCountWithTimeLimitLogUploadStrategy strategy = new RecordCountWithTimeLimitLogUploadStrategy(thresholdCount, uploadCheckPeriod, TimeUnit.MILLISECONDS); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }
@Test public void testEqualToVolumeThreshold() { int thresholdVolume = 5; LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getConsumedVolume()).thenReturn((long) thresholdVolume); StorageSizeLogUploadStrategy strategy = new StorageSizeLogUploadStrategy(thresholdVolume); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }
@Test public void testEqualToRecordThresholdCount() { int thresholdCount = 5; LogStorageStatus logStorageStatus = Mockito.mock(LogStorageStatus.class); Mockito.when(logStorageStatus.getRecordCount()).thenReturn((long) thresholdCount); RecordCountLogUploadStrategy strategy = new RecordCountLogUploadStrategy(thresholdCount); Assert.assertEquals(strategy.checkUploadNeeded(logStorageStatus), LogUploadStrategyDecision.UPLOAD); }