/** * Creates an object well-suited for the RegionServer to use in verifying active policies. */ public ActivePolicyEnforcement getActiveEnforcements() { return new ActivePolicyEnforcement(copyActiveEnforcements(), copyQuotaSnapshots(), rsServices); }
@Override public boolean evaluate() throws Exception { SpaceQuotaSnapshot snapshot = manager.copyQuotaSnapshots().get(tn); if (snapshot == null) { return false; } return snapshot.getUsage() >= tableSize; } });
@Override public boolean evaluate() throws Exception { Map<TableName,SpaceQuotaSnapshot> snapshots = rs.getRegionServerSpaceQuotaManager().copyQuotaSnapshots(); SpaceQuotaSnapshot snapshot = snapshots.get(tn); if (snapshot == null) { LOG.info("Found no snapshot for " + tn); return false; } LOG.info("Found snapshot " + snapshot); return snapshot.getQuotaStatus().isInViolation(); } });
getManager().copyQuotaSnapshots();
@Override public GetSpaceQuotaSnapshotsResponse getSpaceQuotaSnapshots( RpcController controller, GetSpaceQuotaSnapshotsRequest request) throws ServiceException { try { final RegionServerSpaceQuotaManager manager = regionServer.getRegionServerSpaceQuotaManager(); final GetSpaceQuotaSnapshotsResponse.Builder builder = GetSpaceQuotaSnapshotsResponse.newBuilder(); if (manager != null) { final Map<TableName,SpaceQuotaSnapshot> snapshots = manager.copyQuotaSnapshots(); for (Entry<TableName,SpaceQuotaSnapshot> snapshot : snapshots.entrySet()) { builder.addSnapshots(TableQuotaSnapshot.newBuilder() .setTableName(ProtobufUtil.toProtoTableName(snapshot.getKey())) .setSnapshot(SpaceQuotaSnapshot.toProtoSnapshot(snapshot.getValue())) .build()); } } return builder.build(); } catch (Exception e) { throw new ServiceException(e); } }
@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()); } }
when(manager.copyQuotaSnapshots()).thenReturn(previousPolicies);
Map<TableName,SpaceQuotaSnapshot> snapshots = spaceQuotaManager.copyQuotaSnapshots(); Map<RegionInfo,Long> regionSizes = getReportedSizesForTable(tn); while (true) { regionSizes); Thread.sleep(3000); snapshots = spaceQuotaManager.copyQuotaSnapshots(); regionSizes = getReportedSizesForTable(tn);
@Override public boolean evaluate() throws Exception { SpaceQuotaSnapshot snapshot = manager.copyQuotaSnapshots().get(tn); if (snapshot == null) { return false; } return snapshot.getUsage() >= tableSize; } });
@Override public boolean evaluate() throws Exception { Map<TableName,SpaceQuotaSnapshot> snapshots = rs.getRegionServerSpaceQuotaManager().copyQuotaSnapshots(); SpaceQuotaSnapshot snapshot = snapshots.get(tn); if (snapshot == null) { LOG.info("Found no snapshot for " + tn); return false; } LOG.info("Found snapshot " + snapshot); return snapshot.getQuotaStatus().isInViolation(); } });
@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()); } }
when(manager.copyQuotaSnapshots()).thenReturn(previousPolicies);
Map<TableName,SpaceQuotaSnapshot> snapshots = spaceQuotaManager.copyQuotaSnapshots(); Map<RegionInfo,Long> regionSizes = getReportedSizesForTable(tn); while (true) { regionSizes); Thread.sleep(3000); snapshots = spaceQuotaManager.copyQuotaSnapshots(); regionSizes = getReportedSizesForTable(tn);