WatermarkDiskQuotaPolicy implements DiskQuotaPolicy { @Override public double apply(double availableDiskQuotaPercentage) { double workFactor = entryIndex == -1 ? 1.0 : this.entries.get(entryIndex).getWorkFactor(); int entryIndex = this.entryIndex; while (entryIndex >= 0 && entries.get(entryIndex).getHighWaterMarkPercent() <= availableDiskQuotaPercentage) { --entryIndex; } while (entryIndex < entries.size() - 1 && entries.get(entryIndex + 1).getLowWaterMarkPercent() > availableDiskQuotaPercentage) { ++entryIndex; } if (entryIndex != this.entryIndex) { workFactor = entryIndex == -1 ? 1.0 : entries.get(entryIndex).getWorkFactor(); this.entryIndex = entryIndex; log.info("Work factor has been updated: {}.", workFactor); } return workFactor; } WatermarkDiskQuotaPolicy(List<Entry> entries); @Override double apply(double availableDiskQuotaPercentage);  }
@Test public void testNoEntries() { WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(Collections.<WatermarkDiskQuotaPolicy.Entry>emptyList()); assertEquals(1.0, policy.apply(1.0)); assertEquals(1.0, policy.apply(0.5)); assertEquals(1.0, policy.apply(0.0)); }
@Test public void testOneEntryUntriggered() { double workFactor = 0.5f; List<WatermarkDiskQuotaPolicy.Entry> entries = Collections.singletonList(new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(1.0, policy.apply(1.0)); assertEquals(1.0, policy.apply(0.75)); assertEquals(1.0, policy.apply(0.5)); }
@Test public void testOneEntryTriggered() { double workFactor = 0.5f; List<WatermarkDiskQuotaPolicy.Entry> entries = Collections.singletonList(new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(workFactor, policy.apply(0.25)); }
@Test public void testTwoEntriesTriggered() { double workFactor1 = 0.5f; double workFactor2 = 0.25f; List<WatermarkDiskQuotaPolicy.Entry> entries = Arrays.asList( new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor1), new WatermarkDiskQuotaPolicy.Entry(0.2, 0.4, workFactor2)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(1.0, policy.apply(0.5)); assertEquals(workFactor1, policy.apply(0.4)); assertEquals(workFactor2, policy.apply(0.1)); }
@Test public void testTwoEntriesTriggeredSkipFirst() { double workFactor1 = 0.5f; double workFactor2 = 0.25f; List<WatermarkDiskQuotaPolicy.Entry> entries = Arrays.asList( new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor1), new WatermarkDiskQuotaPolicy.Entry(0.2, 0.4, workFactor2)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(workFactor2, policy.apply(0.1)); }
@Test public void testTwoEntriesReversedOrder() { double workFactor1 = 0.5f; double workFactor2 = 0.25f; List<WatermarkDiskQuotaPolicy.Entry> entries = Arrays.asList( new WatermarkDiskQuotaPolicy.Entry(0.2, 0.4, workFactor2), new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor1)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(workFactor2, policy.apply(0.1)); }
@Test public void testTriggerEntriesAndRecover() { double workFactor1 = 0.5f; double workFactor2 = 0.25f; List<WatermarkDiskQuotaPolicy.Entry> entries = Arrays.asList( new WatermarkDiskQuotaPolicy.Entry(0.5, 1.0, workFactor1), new WatermarkDiskQuotaPolicy.Entry(0.2, 0.4, workFactor2)); WatermarkDiskQuotaPolicy policy = new WatermarkDiskQuotaPolicy(entries); assertEquals(workFactor2, policy.apply(0.1)); assertEquals(workFactor1, policy.apply(0.4)); assertEquals(1.0, policy.apply(1.0)); }