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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.junit.jupiter.api.Assertions.fail;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.util.Arrays;
027import java.util.Collection;
028import java.util.Collections;
029import java.util.HashMap;
030import java.util.HashSet;
031import java.util.Map;
032import java.util.Set;
033import org.apache.hadoop.conf.Configuration;
034import org.apache.hadoop.hbase.HBaseConfiguration;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.Admin;
037import org.apache.hadoop.hbase.client.Connection;
038import org.apache.hadoop.hbase.client.RegionInfo;
039import org.apache.hadoop.hbase.quotas.QuotaObserverChore.TablesWithQuotas;
040import org.apache.hadoop.hbase.testclassification.SmallTests;
041import org.junit.jupiter.api.BeforeEach;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
044
045import org.apache.hbase.thirdparty.com.google.common.collect.Multimap;
046
047/**
048 * Non-HBase cluster unit tests for {@link TablesWithQuotas}.
049 */
050@Tag(SmallTests.TAG)
051public class TestTablesWithQuotas {
052
053  private Connection conn;
054  private Configuration conf;
055
056  @BeforeEach
057  public void setup() throws Exception {
058    conn = mock(Connection.class);
059    conf = HBaseConfiguration.create();
060  }
061
062  @Test
063  public void testImmutableGetters() {
064    Set<TableName> tablesWithTableQuotas = new HashSet<>();
065    Set<TableName> tablesWithNamespaceQuotas = new HashSet<>();
066    final TablesWithQuotas tables = new TablesWithQuotas(conn, conf);
067    for (int i = 0; i < 5; i++) {
068      TableName tn = TableName.valueOf("tn" + i);
069      tablesWithTableQuotas.add(tn);
070      tables.addTableQuotaTable(tn);
071    }
072    for (int i = 0; i < 3; i++) {
073      TableName tn = TableName.valueOf("tn_ns" + i);
074      tablesWithNamespaceQuotas.add(tn);
075      tables.addNamespaceQuotaTable(tn);
076    }
077    Set<TableName> actualTableQuotaTables = tables.getTableQuotaTables();
078    Set<TableName> actualNamespaceQuotaTables = tables.getNamespaceQuotaTables();
079    assertEquals(tablesWithTableQuotas, actualTableQuotaTables);
080    assertEquals(tablesWithNamespaceQuotas, actualNamespaceQuotaTables);
081    try {
082      actualTableQuotaTables.add(null);
083      fail("Should not be able to add an element");
084    } catch (UnsupportedOperationException e) {
085      // pass
086    }
087    try {
088      actualNamespaceQuotaTables.add(null);
089      fail("Should not be able to add an element");
090    } catch (UnsupportedOperationException e) {
091      // pass
092    }
093  }
094
095  @Test
096  public void testInsufficientlyReportedTableFiltering() throws Exception {
097    final Map<TableName, Integer> reportedRegions = new HashMap<>();
098    final Map<TableName, Integer> actualRegions = new HashMap<>();
099    final Configuration conf = HBaseConfiguration.create();
100    conf.setDouble(QuotaObserverChore.QUOTA_OBSERVER_CHORE_REPORT_PERCENT_KEY, 0.95);
101
102    TableName tooFewRegionsTable = TableName.valueOf("tn1");
103    TableName sufficientRegionsTable = TableName.valueOf("tn2");
104    TableName tooFewRegionsNamespaceTable = TableName.valueOf("ns1", "tn2");
105    TableName sufficientRegionsNamespaceTable = TableName.valueOf("ns1", "tn2");
106    final TablesWithQuotas tables = new TablesWithQuotas(conn, conf) {
107      @Override
108      Configuration getConfiguration() {
109        return conf;
110      }
111
112      @Override
113      int getNumRegions(TableName tableName) {
114        return actualRegions.get(tableName);
115      }
116
117      @Override
118      int getNumReportedRegions(TableName table, QuotaSnapshotStore<TableName> tableStore) {
119        return reportedRegions.get(table);
120      }
121    };
122    tables.addTableQuotaTable(tooFewRegionsTable);
123    tables.addTableQuotaTable(sufficientRegionsTable);
124    tables.addNamespaceQuotaTable(tooFewRegionsNamespaceTable);
125    tables.addNamespaceQuotaTable(sufficientRegionsNamespaceTable);
126
127    reportedRegions.put(tooFewRegionsTable, 5);
128    actualRegions.put(tooFewRegionsTable, 10);
129    reportedRegions.put(sufficientRegionsTable, 19);
130    actualRegions.put(sufficientRegionsTable, 20);
131    reportedRegions.put(tooFewRegionsNamespaceTable, 9);
132    actualRegions.put(tooFewRegionsNamespaceTable, 10);
133    reportedRegions.put(sufficientRegionsNamespaceTable, 98);
134    actualRegions.put(sufficientRegionsNamespaceTable, 100);
135
136    // Unused argument
137    tables.filterInsufficientlyReportedTables(null);
138    Set<TableName> filteredTablesWithTableQuotas = tables.getTableQuotaTables();
139    assertEquals(Collections.singleton(sufficientRegionsTable), filteredTablesWithTableQuotas);
140    Set<TableName> filteredTablesWithNamespaceQutoas = tables.getNamespaceQuotaTables();
141    assertEquals(Collections.singleton(sufficientRegionsNamespaceTable),
142      filteredTablesWithNamespaceQutoas);
143  }
144
145  @Test
146  public void testGetTablesByNamespace() {
147    final TablesWithQuotas tables = new TablesWithQuotas(conn, conf);
148    tables.addTableQuotaTable(TableName.valueOf("ignored1"));
149    tables.addTableQuotaTable(TableName.valueOf("ignored2"));
150    tables.addNamespaceQuotaTable(TableName.valueOf("ns1", "t1"));
151    tables.addNamespaceQuotaTable(TableName.valueOf("ns1", "t2"));
152    tables.addNamespaceQuotaTable(TableName.valueOf("ns1", "t3"));
153    tables.addNamespaceQuotaTable(TableName.valueOf("ns2", "t1"));
154    tables.addNamespaceQuotaTable(TableName.valueOf("ns2", "t2"));
155
156    Multimap<String, TableName> tablesByNamespace = tables.getTablesByNamespace();
157    Collection<TableName> tablesInNs = tablesByNamespace.get("ns1");
158    assertEquals(3, tablesInNs.size());
159    assertTrue(
160      tablesInNs.containsAll(Arrays.asList(TableName.valueOf("ns1", "t1"),
161        TableName.valueOf("ns1", "t2"), TableName.valueOf("ns1", "t3"))),
162      "Unexpected results for ns1: " + tablesInNs);
163    tablesInNs = tablesByNamespace.get("ns2");
164    assertEquals(2, tablesInNs.size());
165    assertTrue(
166      tablesInNs
167        .containsAll(Arrays.asList(TableName.valueOf("ns2", "t1"), TableName.valueOf("ns2", "t2"))),
168      "Unexpected results for ns2: " + tablesInNs);
169  }
170
171  @Test
172  public void testFilteringMissingTables() throws Exception {
173    final TableName missingTable = TableName.valueOf("doesNotExist");
174    // Set up Admin to return null (match the implementation)
175    Admin admin = mock(Admin.class);
176    when(conn.getAdmin()).thenReturn(admin);
177    when(admin.getRegions(missingTable)).thenReturn(null);
178
179    QuotaObserverChore chore = mock(QuotaObserverChore.class);
180    Map<RegionInfo, Long> regionUsage = new HashMap<>();
181    TableQuotaSnapshotStore store = new TableQuotaSnapshotStore(conn, chore, regionUsage);
182
183    // A super dirty hack to verify that, after getting no regions for our table,
184    // we bail out and start processing the next element (which there is none).
185    final TablesWithQuotas tables = new TablesWithQuotas(conn, conf) {
186      @Override
187      int getNumReportedRegions(TableName table, QuotaSnapshotStore<TableName> tableStore) {
188        throw new RuntimeException("Should should not reach here");
189      }
190    };
191    tables.addTableQuotaTable(missingTable);
192
193    tables.filterInsufficientlyReportedTables(store);
194
195    final Set<TableName> tablesWithQuotas = tables.getTableQuotaTables();
196    assertTrue(tablesWithQuotas.isEmpty(),
197      "Expected to find no tables, but found " + tablesWithQuotas);
198  }
199}