use of org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement in project hbase by apache.
the class SpaceViolationPolicyEnforcementFactory method createWithoutViolation.
/**
* Creates the "default" {@link SpaceViolationPolicyEnforcement} for a table that isn't in
* violation. This is used to have uniform policy checking for tables in and not quotas. This
* policy will still verify that new bulk loads do not exceed the configured quota limit.
*
* @param rss RegionServerServices instance the policy enforcement should use.
* @param tableName The target HBase table.
* @param snapshot The current quota snapshot for the {@code tableName}, can be null.
*/
public SpaceViolationPolicyEnforcement createWithoutViolation(RegionServerServices rss, TableName tableName, SpaceQuotaSnapshot snapshot) {
if (snapshot == null) {
// We should do use the (singleton instance) of this policy to do nothing.
return MissingSnapshotViolationPolicyEnforcement.getInstance();
}
// We have a snapshot which means that there is a quota set on this table, but it's not in
// violation of that quota. We need to construct a policy for this table.
SpaceQuotaStatus status = snapshot.getQuotaStatus();
if (status.isInViolation()) {
throw new IllegalArgumentException(tableName + " is in violation. Logic error. Snapshot=" + snapshot);
}
// We have a unique size snapshot to use. Create an instance for this tablename + snapshot.
DefaultViolationPolicyEnforcement enforcement = new DefaultViolationPolicyEnforcement();
enforcement.initialize(rss, tableName, snapshot);
return enforcement;
}
use of org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement in project hbase by apache.
the class TestRegionServerSpaceQuotaManager method testSpacePoliciesFromEnforcements.
@Test
public void testSpacePoliciesFromEnforcements() {
final Map<TableName, SpaceViolationPolicyEnforcement> enforcements = new HashMap<>();
final Map<TableName, SpaceQuotaSnapshot> expectedPolicies = new HashMap<>();
when(quotaManager.copyActiveEnforcements()).thenReturn(enforcements);
when(quotaManager.getActivePoliciesAsMap()).thenCallRealMethod();
NoInsertsViolationPolicyEnforcement noInsertsPolicy = new NoInsertsViolationPolicyEnforcement();
SpaceQuotaSnapshot noInsertsSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 256L, 1024L);
noInsertsPolicy.initialize(rss, TableName.valueOf("no_inserts"), noInsertsSnapshot);
enforcements.put(noInsertsPolicy.getTableName(), noInsertsPolicy);
expectedPolicies.put(noInsertsPolicy.getTableName(), noInsertsSnapshot);
NoWritesViolationPolicyEnforcement noWritesPolicy = new NoWritesViolationPolicyEnforcement();
SpaceQuotaSnapshot noWritesSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 512L, 2048L);
noWritesPolicy.initialize(rss, TableName.valueOf("no_writes"), noWritesSnapshot);
enforcements.put(noWritesPolicy.getTableName(), noWritesPolicy);
expectedPolicies.put(noWritesPolicy.getTableName(), noWritesSnapshot);
NoWritesCompactionsViolationPolicyEnforcement noWritesCompactionsPolicy = new NoWritesCompactionsViolationPolicyEnforcement();
SpaceQuotaSnapshot noWritesCompactionsSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 1024L, 4096L);
noWritesCompactionsPolicy.initialize(rss, TableName.valueOf("no_writes_compactions"), noWritesCompactionsSnapshot);
enforcements.put(noWritesCompactionsPolicy.getTableName(), noWritesCompactionsPolicy);
expectedPolicies.put(noWritesCompactionsPolicy.getTableName(), noWritesCompactionsSnapshot);
DisableTableViolationPolicyEnforcement disablePolicy = new DisableTableViolationPolicyEnforcement();
SpaceQuotaSnapshot disableSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 2048L, 8192L);
disablePolicy.initialize(rss, TableName.valueOf("disable"), disableSnapshot);
enforcements.put(disablePolicy.getTableName(), disablePolicy);
expectedPolicies.put(disablePolicy.getTableName(), disableSnapshot);
enforcements.put(TableName.valueOf("no_policy"), new DefaultViolationPolicyEnforcement());
Map<TableName, SpaceQuotaSnapshot> actualPolicies = quotaManager.getActivePoliciesAsMap();
assertEquals(expectedPolicies, actualPolicies);
}
use of org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement in project hbase by apache.
the class TestSpaceQuotaOnBulkLoad method testAtomicBulkLoadUnderQuota.
@Test
public void testAtomicBulkLoadUnderQuota() throws Exception {
// Need to verify that if the batch of hfiles cannot be loaded, none are loaded.
TableName tn = helper.createTableWithRegions(10);
final long sizeLimit = 50L * SpaceQuotaHelperForTests.ONE_KILOBYTE;
QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, SpaceViolationPolicy.NO_INSERTS);
TEST_UTIL.getAdmin().setQuota(settings);
HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
RegionServerSpaceQuotaManager spaceQuotaManager = rs.getRegionServerSpaceQuotaManager();
Map<TableName, SpaceQuotaSnapshot> snapshots = spaceQuotaManager.copyQuotaSnapshots();
Map<RegionInfo, Long> regionSizes = getReportedSizesForTable(tn);
while (true) {
SpaceQuotaSnapshot snapshot = snapshots.get(tn);
if (snapshot != null && snapshot.getLimit() > 0) {
break;
}
LOG.debug("Snapshot does not yet realize quota limit: " + snapshots + ", regionsizes: " + regionSizes);
Thread.sleep(3000);
snapshots = spaceQuotaManager.copyQuotaSnapshots();
regionSizes = getReportedSizesForTable(tn);
}
// Our quota limit should be reflected in the latest snapshot
SpaceQuotaSnapshot snapshot = snapshots.get(tn);
assertEquals(0L, snapshot.getUsage());
assertEquals(sizeLimit, snapshot.getLimit());
// We would also not have a "real" policy in violation
ActivePolicyEnforcement activePolicies = spaceQuotaManager.getActiveEnforcements();
SpaceViolationPolicyEnforcement enforcement = activePolicies.getPolicyEnforcement(tn);
assertTrue("Expected to find Noop policy, but got " + enforcement.getClass().getSimpleName(), enforcement instanceof DefaultViolationPolicyEnforcement);
// Should generate two files, each of which is over 25KB each
Map<byte[], List<Path>> family2Files = helper.generateFileToLoad(tn, 2, 525);
FileSystem fs = TEST_UTIL.getTestFileSystem();
FileStatus[] files = fs.listStatus(new Path(fs.getHomeDirectory(), testName.getMethodName() + "_files"));
for (FileStatus file : files) {
assertTrue("Expected the file, " + file.getPath() + ", length to be larger than 25KB, but was " + file.getLen(), file.getLen() > 25 * SpaceQuotaHelperForTests.ONE_KILOBYTE);
LOG.debug(file.getPath() + " -> " + file.getLen() + "B");
}
try {
BulkLoadHFiles.create(TEST_UTIL.getConfiguration()).bulkLoad(tn, family2Files);
fail("Expected the bulk load call to fail!");
} catch (IOException e) {
// Pass
assertThat(e.getCause(), instanceOf(SpaceLimitingException.class));
LOG.trace("Caught expected exception", e);
}
// Verify that we have no data in the table because neither file should have been
// loaded even though one of the files could have.
Table table = TEST_UTIL.getConnection().getTable(tn);
ResultScanner scanner = table.getScanner(new Scan());
try {
assertNull("Expected no results", scanner.next());
} finally {
scanner.close();
}
}
use of org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement in project hbase by apache.
the class TestActivePolicyEnforcement method testNonViolatingQuotaCachesPolicyEnforcment.
@Test
public void testNonViolatingQuotaCachesPolicyEnforcment() {
final Map<TableName, SpaceQuotaSnapshot> snapshots = new HashMap<>();
final TableName tableName = TableName.valueOf("my_table");
snapshots.put(tableName, new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 0, 1024));
final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(Collections.emptyMap(), snapshots, rss);
SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
assertTrue("Found the wrong class: " + policyEnforcement.getClass(), policyEnforcement instanceof DefaultViolationPolicyEnforcement);
SpaceViolationPolicyEnforcement copy = ape.getPolicyEnforcement(tableName);
assertTrue("Expected the instance to be cached", policyEnforcement == copy);
Entry<TableName, SpaceViolationPolicyEnforcement> entry = ape.getLocallyCachedPolicies().entrySet().iterator().next();
assertTrue(policyEnforcement == entry.getValue());
}
Aggregations