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.HColumnDescriptor;
033import org.apache.hadoop.hbase.HTableDescriptor;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.Waiter;
036import org.apache.hadoop.hbase.client.Admin;
037import org.apache.hadoop.hbase.client.Connection;
038import org.apache.hadoop.hbase.client.Put;
039import org.apache.hadoop.hbase.client.RegionInfo;
040import org.apache.hadoop.hbase.client.Result;
041import org.apache.hadoop.hbase.client.ResultScanner;
042import org.apache.hadoop.hbase.client.Table;
043import org.apache.hadoop.hbase.master.HMaster;
044import org.apache.hadoop.hbase.testclassification.LargeTests;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.junit.After;
047import org.junit.Before;
048import org.junit.ClassRule;
049import org.junit.Rule;
050import org.junit.Test;
051import org.junit.experimental.categories.Category;
052import org.junit.rules.TestName;
053import org.slf4j.Logger;
054import org.slf4j.LoggerFactory;
055
056/**
057 * A test case to verify that region reports are expired when they are not sent.
058 */
059@Category(LargeTests.class)
060public class TestQuotaObserverChoreRegionReports {
061
062  @ClassRule
063  public static final HBaseClassTestRule CLASS_RULE =
064      HBaseClassTestRule.forClass(TestQuotaObserverChoreRegionReports.class);
065
066  private static final Logger LOG =
067      LoggerFactory.getLogger(TestQuotaObserverChoreRegionReports.class);
068  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
069
070  @Rule
071  public TestName testName = new TestName();
072
073  @Before
074  public void setUp() throws Exception {
075    Configuration conf = TEST_UTIL.getConfiguration();
076    // Increase the frequency of some of the chores for responsiveness of the test
077    SpaceQuotaHelperForTests.updateConfigForQuotas(conf);
078    conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 1000);
079  }
080
081  @After
082  public void tearDown() throws Exception {
083    TEST_UTIL.shutdownMiniCluster();
084  }
085
086  @Test
087  public void testReportExpiration() throws Exception {
088    Configuration conf = TEST_UTIL.getConfiguration();
089    // Send reports every 30 seconds
090    conf.setInt(FileSystemUtilizationChore.FS_UTILIZATION_CHORE_PERIOD_KEY, 25000);
091    // Expire the reports after 5 seconds
092    conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 5000);
093    TEST_UTIL.startMiniCluster(1);
094
095    final String FAM1 = "f1";
096    final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
097    // Wait for the master to finish initialization.
098    while (master.getMasterQuotaManager() == null) {
099      LOG.debug("MasterQuotaManager is null, waiting...");
100      Thread.sleep(500);
101    }
102    final MasterQuotaManager quotaManager = master.getMasterQuotaManager();
103
104    // Create a table
105    final TableName tn = TableName.valueOf("reportExpiration");
106    HTableDescriptor tableDesc = new HTableDescriptor(tn);
107    tableDesc.addFamily(new HColumnDescriptor(FAM1));
108    TEST_UTIL.getAdmin().createTable(tableDesc);
109
110    // No reports right after we created this table.
111    assertEquals(0, getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn));
112
113    // Set a quota
114    final long sizeLimit = 100L * SpaceQuotaHelperForTests.ONE_MEGABYTE;
115    final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS;
116    QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, violationPolicy);
117    TEST_UTIL.getAdmin().setQuota(settings);
118
119    // We should get one report for the one region we have.
120    Waiter.waitFor(TEST_UTIL.getConfiguration(), 45000, 1000, new Waiter.Predicate<Exception>() {
121      @Override
122      public boolean evaluate() throws Exception {
123        int numReports = getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn);
124        LOG.debug("Saw " + numReports + " reports for " + tn + " while waiting for 1");
125        return numReports == 1;
126      }
127    });
128
129    // We should then see no reports for the single region
130    Waiter.waitFor(TEST_UTIL.getConfiguration(), 15000, 1000, new Waiter.Predicate<Exception>() {
131      @Override
132      public boolean evaluate() throws Exception {
133        int numReports = getRegionReportsForTable(quotaManager.snapshotRegionSizes(), tn);
134        LOG.debug("Saw " + numReports + " reports for " + tn + " while waiting for none");
135        return numReports == 0;
136      }
137    });
138  }
139
140  @Test
141  public void testMissingReportsRemovesQuota() throws Exception {
142    Configuration conf = TEST_UTIL.getConfiguration();
143    // Expire the reports after 5 seconds
144    conf.setInt(QuotaObserverChore.REGION_REPORT_RETENTION_DURATION_KEY, 5000);
145    TEST_UTIL.startMiniCluster(1);
146
147    final String FAM1 = "f1";
148
149    // Create a table
150    final TableName tn = TableName.valueOf("quotaAcceptanceWithoutReports");
151    HTableDescriptor tableDesc = new HTableDescriptor(tn);
152    tableDesc.addFamily(new HColumnDescriptor(FAM1));
153    TEST_UTIL.getAdmin().createTable(tableDesc);
154
155    // Set a quota
156    final long sizeLimit = 1L * SpaceQuotaHelperForTests.ONE_KILOBYTE;
157    final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS;
158    QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, violationPolicy);
159    final Admin admin = TEST_UTIL.getAdmin();
160    admin.setQuota(settings);
161    final Connection conn = TEST_UTIL.getConnection();
162
163    // Write enough data to invalidate the quota
164    Put p = new Put(Bytes.toBytes("row1"));
165    byte[] bytes = new byte[10];
166    Arrays.fill(bytes, (byte) 2);
167    for (int i = 0; i < 200; i++) {
168      p.addColumn(Bytes.toBytes(FAM1), Bytes.toBytes("qual" + i), bytes);
169    }
170    conn.getTable(tn).put(p);
171    admin.flush(tn);
172
173    // Wait for the table to move into violation
174    Waiter.waitFor(TEST_UTIL.getConfiguration(), 30000, 1000, new Waiter.Predicate<Exception>() {
175      @Override
176      public boolean evaluate() throws Exception {
177        SpaceQuotaSnapshot snapshot = getSnapshotForTable(conn, tn);
178        if (snapshot == null) {
179          return false;
180        }
181        return snapshot.getQuotaStatus().isInViolation();
182      }
183    });
184
185    // Close the region, prevent the server from sending new status reports.
186    List<RegionInfo> regions = admin.getRegions(tn);
187    assertEquals(1, regions.size());
188    RegionInfo hri = regions.get(0);
189    admin.unassign(hri.getRegionName(), true);
190
191    // We should see this table move out of violation after the report expires.
192    Waiter.waitFor(TEST_UTIL.getConfiguration(), 30000, 1000, new Waiter.Predicate<Exception>() {
193      @Override
194      public boolean evaluate() throws Exception {
195        SpaceQuotaSnapshot snapshot = getSnapshotForTable(conn, tn);
196        if (snapshot == null) {
197          return false;
198        }
199        return !snapshot.getQuotaStatus().isInViolation();
200      }
201    });
202
203    // The QuotaObserverChore's memory should also show it not in violation.
204    final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
205    QuotaSnapshotStore<TableName> tableStore =
206        master.getQuotaObserverChore().getTableSnapshotStore();
207    SpaceQuotaSnapshot snapshot = tableStore.getCurrentState(tn);
208    assertFalse("Quota should not be in violation", snapshot.getQuotaStatus().isInViolation());
209  }
210
211  private SpaceQuotaSnapshot getSnapshotForTable(
212      Connection conn, TableName tn) throws IOException {
213    try (Table quotaTable = conn.getTable(QuotaUtil.QUOTA_TABLE_NAME);
214        ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan())) {
215      Map<TableName,SpaceQuotaSnapshot> activeViolations = new HashMap<>();
216      for (Result result : scanner) {
217        try {
218          QuotaTableUtil.extractQuotaSnapshot(result, activeViolations);
219        } catch (IllegalArgumentException e) {
220          final String msg = "Failed to parse result for row " + Bytes.toString(result.getRow());
221          LOG.error(msg, e);
222          throw new IOException(msg, e);
223        }
224      }
225      return activeViolations.get(tn);
226    }
227  }
228
229  private int getRegionReportsForTable(Map<RegionInfo,Long> reports, TableName tn) {
230    int numReports = 0;
231    for (Entry<RegionInfo,Long> entry : reports.entrySet()) {
232      if (tn.equals(entry.getKey().getTable())) {
233        numReports++;
234      }
235    }
236    return numReports;
237  }
238}