@Override public SpaceQuotaSnapshot getTargetState( String subject, SpaceQuota spaceQuota) throws IOException { rlock.lock(); try { final long sizeLimitInBytes = spaceQuota.getSoftLimit(); long sum = 0L; for (Entry<RegionInfo,Long> entry : filterBySubject(subject)) { sum += entry.getValue(); } // Add in the size for any snapshots against this table sum += QuotaTableUtil.getNamespaceSnapshotSize(conn, subject); // Observance is defined as the size of the table being less than the limit SpaceQuotaStatus status = sum <= sizeLimitInBytes ? SpaceQuotaStatus.notInViolation() : new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(spaceQuota.getViolationPolicy())); return new SpaceQuotaSnapshot(status, sum, sizeLimitInBytes); } finally { rlock.unlock(); } }
@Override public SpaceQuotaSnapshot getTargetState( TableName table, SpaceQuota spaceQuota) throws IOException { rlock.lock(); try { final long sizeLimitInBytes = spaceQuota.getSoftLimit(); long sum = 0L; for (Entry<RegionInfo,Long> entry : filterBySubject(table)) { sum += entry.getValue(); } // Add in the size for any snapshots against this table sum += getSnapshotSizesForTable(table); // Observance is defined as the size of the table being less than the limit SpaceQuotaStatus status = sum <= sizeLimitInBytes ? SpaceQuotaStatus.notInViolation() : new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(spaceQuota.getViolationPolicy())); return new SpaceQuotaSnapshot(status, sum, sizeLimitInBytes); } finally { rlock.unlock(); } }
@Test public void testExceptionOnPolicyEnforcementEnable() throws Exception { final TableName tableName = TableName.valueOf("foo"); final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 2048L); RegionServerServices rss = mock(RegionServerServices.class); SpaceViolationPolicyEnforcementFactory factory = mock( SpaceViolationPolicyEnforcementFactory.class); SpaceViolationPolicyEnforcement enforcement = mock(SpaceViolationPolicyEnforcement.class); RegionServerSpaceQuotaManager realManager = new RegionServerSpaceQuotaManager(rss, factory); when(factory.create(rss, tableName, snapshot)).thenReturn(enforcement); doThrow(new IOException("Failed for test!")).when(enforcement).enable(); realManager.enforceViolationPolicy(tableName, snapshot); Map<TableName, SpaceViolationPolicyEnforcement> enforcements = realManager.copyActiveEnforcements(); assertTrue("Expected active enforcements to be empty, but were " + enforcements, enforcements.isEmpty()); }
@Test public void testSpaceQuotaViolation() throws IOException, InterruptedException { region.getRegionServerServices().getRegionServerSpaceQuotaManager().enforceViolationPolicy(NAME, new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 10L, 100L)); Tracker tracker = new Tracker(); TRACKER = tracker; region.requestCompaction("test", Store.PRIORITY_USER, false, tracker); tracker.await(); assertEquals(2, tracker.notExecutedStores.size()); tracker.notExecutedStores.sort((p1, p2) -> p1.getFirst().getColumnFamilyName() .compareTo(p2.getFirst().getColumnFamilyName())); assertEquals(Bytes.toString(CF1), tracker.notExecutedStores.get(0).getFirst().getColumnFamilyName()); assertThat(tracker.notExecutedStores.get(0).getSecond(), containsString("space quota violation")); assertEquals(Bytes.toString(CF2), tracker.notExecutedStores.get(1).getFirst().getColumnFamilyName()); assertThat(tracker.notExecutedStores.get(1).getSecond(), containsString("space quota violation")); assertTrue(tracker.beforeExecuteStores.isEmpty()); assertTrue(tracker.afterExecuteStores.isEmpty()); } }
@Test public void testExceptionOnPolicyEnforcementEnable() throws Exception { final TableName tableName = TableName.valueOf("foo"); final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 2048L); RegionServerServices rss = mock(RegionServerServices.class); SpaceViolationPolicyEnforcementFactory factory = mock( SpaceViolationPolicyEnforcementFactory.class); SpaceViolationPolicyEnforcement enforcement = mock(SpaceViolationPolicyEnforcement.class); RegionServerSpaceQuotaManager realManager = new RegionServerSpaceQuotaManager(rss, factory); when(factory.create(rss, tableName, snapshot)).thenReturn(enforcement); doThrow(new IOException("Failed for test!")).when(enforcement).enable(); realManager.enforceViolationPolicy(tableName, snapshot); Map<TableName, SpaceViolationPolicyEnforcement> enforcements = realManager.copyActiveEnforcements(); assertTrue("Expected active enforcements to be empty, but were " + enforcements, enforcements.isEmpty()); }
@Test public void testToViolation() throws Exception { final TableName tn = TableName.valueOf("inviolation"); final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 1024L, 512L); final Table quotaTable = mock(Table.class); when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); final Put expectedPut = new Put(Bytes.toBytes("t." + tn.getNameAsString())); final QuotaProtos.SpaceQuotaSnapshot protoQuota = QuotaProtos.SpaceQuotaSnapshot.newBuilder() .setQuotaStatus(QuotaProtos.SpaceQuotaStatus.newBuilder().setInViolation(true) .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.NO_INSERTS)) .setQuotaLimit(512L) .setQuotaUsage(1024L) .build(); expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("p"), protoQuota.toByteArray()); notifier.transitionTable(tn, snapshot); verify(quotaTable).put(argThat(new SingleCellMutationMatcher<Put>(expectedPut))); }
public static SpaceQuotaStatus toStatus(QuotaProtos.SpaceQuotaStatus proto) { if (proto.getInViolation()) { return new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(proto.getViolationPolicy())); } else { return NOT_IN_VIOLATION; } } }
@Test public void testQuotaSnapshotConversion() { MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl( TEST_UTIL.getHBaseCluster().getMaster()); assertEquals(new SimpleImmutableEntry<Long,Long>(1024L, 2048L), info.convertSnapshot(new SpaceQuotaSnapshot( SpaceQuotaStatus.notInViolation(), 1024L, 2048L))); assertEquals(new SimpleImmutableEntry<Long,Long>(4096L, 2048L), info.convertSnapshot(new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L))); } }
public static SpaceQuotaStatus toStatus(QuotaProtos.SpaceQuotaStatus proto) { if (proto.getInViolation()) { return new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(proto.getViolationPolicy())); } else { return NOT_IN_VIOLATION; } } }
.build(), 1024L); tn1Snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L * 1024L + 1024L, 1024L * 1024L);
new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 256L, 1024L); noInsertsPolicy.initialize(rss, TableName.valueOf("no_inserts"), noInsertsSnapshot); enforcements.put(noInsertsPolicy.getTableName(), noInsertsPolicy); new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 512L, 2048L); noWritesPolicy.initialize(rss, TableName.valueOf("no_writes"), noWritesSnapshot); enforcements.put(noWritesPolicy.getTableName(), noWritesPolicy); new NoWritesCompactionsViolationPolicyEnforcement(); SpaceQuotaSnapshot noWritesCompactionsSnapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 1024L, 4096L); noWritesCompactionsPolicy.initialize( rss, TableName.valueOf("no_writes_compactions"), noWritesCompactionsSnapshot); new DisableTableViolationPolicyEnforcement(); SpaceQuotaSnapshot disableSnapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 2048L, 8192L); disablePolicy.initialize(rss, TableName.valueOf("disable"), disableSnapshot); enforcements.put(disablePolicy.getTableName(), disablePolicy);
previousPolicies.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 4096L, 512L)); previousPolicies.put( TableName.valueOf("table4"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 8192L, 512L)); new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"),
@Test public void testSerDeViolationPolicies() throws Exception { final TableName tn1 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot1 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 512L, 1024L); final TableName tn2 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot2 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 512L, 1024L); final TableName tn3 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot3 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 512L, 1024L); List<Put> puts = new ArrayList<>(); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn1, snapshot1)); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn2, snapshot2)); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn3, snapshot3)); final Map<TableName,SpaceQuotaSnapshot> expectedPolicies = new HashMap<>(); expectedPolicies.put(tn1, snapshot1); expectedPolicies.put(tn2, snapshot2); expectedPolicies.put(tn3, snapshot3); final Map<TableName,SpaceQuotaSnapshot> actualPolicies = new HashMap<>(); try (Table quotaTable = connection.getTable(QuotaUtil.QUOTA_TABLE_NAME)) { quotaTable.put(puts); ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan()); for (Result r : scanner) { QuotaTableUtil.extractQuotaSnapshot(r, actualPolicies); } scanner.close(); } assertEquals(expectedPolicies, actualPolicies); }
@Test public void testPoliciesAreEnforced() throws IOException { // Create a number of policies that should be enforced (usage > limit) final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 4096L, 512L)); policiesToEnforce.put( TableName.valueOf("table4"), new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 8192L, 512L)); // No active enforcements when(manager.copyQuotaSnapshots()).thenReturn(Collections.emptyMap()); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { // Ensure we enforce the policy verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); // Don't disable any policies verify(manager, never()).disableViolationPolicyEnforcement(entry.getKey()); } }
@Test public void testNewPolicyOverridesOld() throws IOException { final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 512L)); final Map<TableName,SpaceQuotaSnapshot> previousPolicies = new HashMap<>(); previousPolicies.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 8192L, 512L)); // No active enforcements when(manager.getActivePoliciesAsMap()).thenReturn(previousPolicies); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); } verify(manager, never()).disableViolationPolicyEnforcement(TableName.valueOf("table1")); }
@Test public void testSpaceQuotaViolation() throws IOException, InterruptedException { region.getRegionServerServices().getRegionServerSpaceQuotaManager().enforceViolationPolicy(NAME, new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 10L, 100L)); Tracker tracker = new Tracker(); TRACKER = tracker; region.requestCompaction("test", Store.PRIORITY_USER, false, tracker); tracker.await(); assertEquals(2, tracker.notExecutedStores.size()); tracker.notExecutedStores.sort((p1, p2) -> p1.getFirst().getColumnFamilyName() .compareTo(p2.getFirst().getColumnFamilyName())); assertEquals(Bytes.toString(CF1), tracker.notExecutedStores.get(0).getFirst().getColumnFamilyName()); assertThat(tracker.notExecutedStores.get(0).getSecond(), containsString("space quota violation")); assertEquals(Bytes.toString(CF2), tracker.notExecutedStores.get(1).getFirst().getColumnFamilyName()); assertThat(tracker.notExecutedStores.get(1).getSecond(), containsString("space quota violation")); assertTrue(tracker.beforeExecuteStores.isEmpty()); assertTrue(tracker.afterExecuteStores.isEmpty()); } }
@Test public void testExceptionOnPolicyEnforcementDisable() throws Exception { final TableName tableName = TableName.valueOf("foo"); final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 2048L); RegionServerServices rss = mock(RegionServerServices.class); SpaceViolationPolicyEnforcementFactory factory = mock( SpaceViolationPolicyEnforcementFactory.class); SpaceViolationPolicyEnforcement enforcement = mock(SpaceViolationPolicyEnforcement.class); RegionServerSpaceQuotaManager realManager = new RegionServerSpaceQuotaManager(rss, factory); when(factory.create(rss, tableName, snapshot)).thenReturn(enforcement); doNothing().when(enforcement).enable(); doThrow(new IOException("Failed for test!")).when(enforcement).disable(); // Enabling should work realManager.enforceViolationPolicy(tableName, snapshot); Map<TableName, SpaceViolationPolicyEnforcement> enforcements = realManager.copyActiveEnforcements(); assertEquals(1, enforcements.size()); // If the disable fails, we should still treat it as "active" realManager.disableViolationPolicyEnforcement(tableName); enforcements = realManager.copyActiveEnforcements(); assertEquals(1, enforcements.size()); } }
@Test public void testToViolation() throws Exception { final TableName tn = TableName.valueOf("inviolation"); final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 1024L, 512L); final Table quotaTable = mock(Table.class); when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); final Put expectedPut = new Put(Bytes.toBytes("t." + tn.getNameAsString())); final QuotaProtos.SpaceQuotaSnapshot protoQuota = QuotaProtos.SpaceQuotaSnapshot.newBuilder() .setQuotaStatus(QuotaProtos.SpaceQuotaStatus.newBuilder().setInViolation(true) .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.NO_INSERTS)) .setQuotaLimit(512L) .setQuotaUsage(1024L) .build(); expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("p"), protoQuota.toByteArray()); notifier.transitionTable(tn, snapshot); verify(quotaTable).put(argThat(new SingleCellMutationMatcher<Put>(expectedPut))); }
@Test public void testQuotaSnapshotConversion() { MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl( TEST_UTIL.getHBaseCluster().getMaster()); assertEquals(new SimpleImmutableEntry<Long,Long>(1024L, 2048L), info.convertSnapshot(new SpaceQuotaSnapshot( SpaceQuotaStatus.notInViolation(), 1024L, 2048L))); assertEquals(new SimpleImmutableEntry<Long,Long>(4096L, 2048L), info.convertSnapshot(new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L))); }
public static SpaceQuotaStatus toStatus(QuotaProtos.SpaceQuotaStatus proto) { if (proto.getInViolation()) { return new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(proto.getViolationPolicy())); } else { return NOT_IN_VIOLATION; } } }