001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.quotas; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022 023import java.io.IOException; 024import java.util.Arrays; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Map.Entry; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.Waiter; 034import org.apache.hadoop.hbase.client.Admin; 035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 036import org.apache.hadoop.hbase.client.Connection; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.client.Result; 040import org.apache.hadoop.hbase.client.ResultScanner; 041import org.apache.hadoop.hbase.client.Table; 042import org.apache.hadoop.hbase.client.TableDescriptor; 043import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 044import org.apache.hadoop.hbase.master.HMaster; 045import org.apache.hadoop.hbase.testclassification.LargeTests; 046import org.apache.hadoop.hbase.util.Bytes; 047import org.junit.After; 048import org.junit.Before; 049import org.junit.ClassRule; 050import org.junit.Rule; 051import org.junit.Test; 052import org.junit.experimental.categories.Category; 053import org.junit.rules.TestName; 054import org.slf4j.Logger; 055import org.slf4j.LoggerFactory; 056 057/** 058 * A test case to verify that region reports are expired when they are not sent. 059 */ 060@Category(LargeTests.class) 061public class TestQuotaObserverChoreRegionReports { 062 063 @ClassRule 064 public static final HBaseClassTestRule CLASS_RULE = 065 HBaseClassTestRule.forClass(TestQuotaObserverChoreRegionReports.class); 066 067 private static final Logger LOG = 068 LoggerFactory.getLogger(TestQuotaObserverChoreRegionReports.class); 069 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 070 071 @Rule 072 public TestName testName = new TestName(); 073 074 @Before 075 public void setUp() throws Exception { 076 Configuration conf = TEST_UTIL.getConfiguration(); 077 // Increase the frequency of some of the chores for responsiveness of the test 078 SpaceQuotaHelperForTests.updateConfigForQuotas(conf); 079 conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 1000); 080 } 081 082 @After 083 public void tearDown() throws Exception { 084 TEST_UTIL.shutdownMiniCluster(); 085 } 086 087 @Test 088 public void testReportExpiration() throws Exception { 089 Configuration conf = TEST_UTIL.getConfiguration(); 090 // Send reports every 25 seconds 091 conf.setInt(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_KEY, 25000); 092 // Expire the reports after 5 seconds 093 conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 5000); 094 TEST_UTIL.startMiniCluster(1); 095 096 final String FAM1 = "f1"; 097 final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); 098 // Wait for the master to finish initialization. 099 while (master.getMasterQuotaManager() == null) { 100 LOG.debug("MasterQuotaManager is null, waiting..."); 101 Thread.sleep(500); 102 } 103 final MasterQuotaManager quotaManager = master.getMasterQuotaManager(); 104 105 // Create a table 106 final TableName tn = TableName.valueOf("reportExpiration"); 107 TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tn).addColumnFamily( 108 ColumnFamilyDescriptorBuilder.of(FAM1)).build(); 109 TEST_UTIL.getAdmin().createTable(tableDesc); 110 111 // No reports right after we created this table. 112 assertEquals(0, getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn)); 113 114 // Set a quota 115 final long sizeLimit = 100L * SpaceQuotaHelperForTests.ONE_MEGABYTE; 116 final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS; 117 QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, violationPolicy); 118 TEST_UTIL.getAdmin().setQuota(settings); 119 120 // We should get one report for the one region we have. 121 Waiter.waitFor(TEST_UTIL.getConfiguration(), 45000, 1000, new Waiter.Predicate<Exception>() { 122 @Override 123 public boolean evaluate() throws Exception { 124 int numReports = getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn); 125 LOG.debug("Saw " + numReports + " reports for " + tn + " while waiting for 1"); 126 return numReports == 1; 127 } 128 }); 129 130 // We should then see no reports for the single region 131 Waiter.waitFor(TEST_UTIL.getConfiguration(), 15000, 1000, new Waiter.Predicate<Exception>() { 132 @Override 133 public boolean evaluate() throws Exception { 134 int numReports = getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn); 135 LOG.debug("Saw " + numReports + " reports for " + tn + " while waiting for none"); 136 return numReports == 0; 137 } 138 }); 139 } 140 141 @Test 142 public void testMissingReportsRemovesQuota() throws Exception { 143 Configuration conf = TEST_UTIL.getConfiguration(); 144 // Expire the reports after 5 seconds 145 conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 5000); 146 TEST_UTIL.startMiniCluster(1); 147 148 final String FAM1 = "f1"; 149 150 // Create a table 151 final TableName tn = TableName.valueOf("quotaAcceptanceWithoutReports"); 152 TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tn).addColumnFamily( 153 ColumnFamilyDescriptorBuilder.of(FAM1)).build(); 154 TEST_UTIL.getAdmin().createTable(tableDesc); 155 156 // Set a quota 157 final long sizeLimit = 1L * SpaceQuotaHelperForTests.ONE_KILOBYTE; 158 final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS; 159 QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, violationPolicy); 160 final Admin admin = TEST_UTIL.getAdmin(); 161 admin.setQuota(settings); 162 final Connection conn = TEST_UTIL.getConnection(); 163 164 // Write enough data to invalidate the quota 165 Put p = new Put(Bytes.toBytes("row1")); 166 byte[] bytes = new byte[10]; 167 Arrays.fill(bytes, (byte) 2); 168 for (int i = 0; i < 200; i++) { 169 p.addColumn(Bytes.toBytes(FAM1), Bytes.toBytes("qual" + i), bytes); 170 } 171 conn.getTable(tn).put(p); 172 admin.flush(tn); 173 174 // Wait for the table to move into violation 175 Waiter.waitFor(TEST_UTIL.getConfiguration(), 30000, 1000, new Waiter.Predicate<Exception>() { 176 @Override 177 public boolean evaluate() throws Exception { 178 SpaceQuotaSnapshot snapshot = getSnapshotForTable(conn, tn); 179 if (snapshot == null) { 180 return false; 181 } 182 return snapshot.getQuotaStatus().isInViolation(); 183 } 184 }); 185 186 // Close the region, prevent the server from sending new status reports. 187 List<RegionInfo> regions = admin.getRegions(tn); 188 assertEquals(1, regions.size()); 189 RegionInfo hri = regions.get(0); 190 admin.unassign(hri.getRegionName(), true); 191 192 // We should see this table move out of violation after the report expires. 193 Waiter.waitFor(TEST_UTIL.getConfiguration(), 30000, 1000, new Waiter.Predicate<Exception>() { 194 @Override 195 public boolean evaluate() throws Exception { 196 SpaceQuotaSnapshot snapshot = getSnapshotForTable(conn, tn); 197 if (snapshot == null) { 198 return false; 199 } 200 return !snapshot.getQuotaStatus().isInViolation(); 201 } 202 }); 203 204 // The QuotaObserverChore's memory should also show it not in violation. 205 final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); 206 QuotaSnapshotStore<TableName> tableStore = 207 master.getQuotaObserverChore().getTableSnapshotStore(); 208 SpaceQuotaSnapshot snapshot = tableStore.getCurrentState(tn); 209 assertFalse("Quota should not be in violation", snapshot.getQuotaStatus().isInViolation()); 210 } 211 212 private SpaceQuotaSnapshot getSnapshotForTable( 213 Connection conn, TableName tn) throws IOException { 214 try (Table quotaTable = conn.getTable(QuotaUtil.QUOTA_TABLE_NAME); 215 ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan())) { 216 Map<TableName,SpaceQuotaSnapshot> activeViolations = new HashMap<>(); 217 for (Result result : scanner) { 218 try { 219 QuotaTableUtil.extractQuotaSnapshot(result, activeViolations); 220 } catch (IllegalArgumentException e) { 221 final String msg = "Failed to parse result for row " + Bytes.toString(result.getRow()); 222 LOG.error(msg, e); 223 throw new IOException(msg, e); 224 } 225 } 226 return activeViolations.get(tn); 227 } 228 } 229 230 private int getRegionReportsForTable(Map<RegionInfo,Long> reports, TableName tn) { 231 int numReports = 0; 232 for (Entry<RegionInfo,Long> entry : reports.entrySet()) { 233 if (tn.equals(entry.getKey().getTable())) { 234 numReports++; 235 } 236 } 237 return numReports; 238 } 239}