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.apache.hbase.thirdparty.com.google.common.collect.Iterables.size;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNull;
023import static org.mockito.ArgumentMatchers.any;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.when;
026
027import java.io.IOException;
028import java.util.HashMap;
029import java.util.Map;
030import java.util.concurrent.atomic.AtomicReference;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.NamespaceDescriptor;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.Get;
036import org.apache.hadoop.hbase.client.RegionInfo;
037import org.apache.hadoop.hbase.client.RegionInfoBuilder;
038import org.apache.hadoop.hbase.client.Result;
039import org.apache.hadoop.hbase.client.Table;
040import org.apache.hadoop.hbase.testclassification.SmallTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.Before;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.mockito.invocation.InvocationOnMock;
047import org.mockito.stubbing.Answer;
048
049import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
051import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Quotas;
052import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceQuota;
053
054/**
055 * Test class for {@link NamespaceQuotaSnapshotStore}.
056 */
057@Category(SmallTests.class)
058public class TestNamespaceQuotaViolationStore {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062    HBaseClassTestRule.forClass(TestNamespaceQuotaViolationStore.class);
063
064  private static final long ONE_MEGABYTE = 1024L * 1024L;
065
066  private Connection conn;
067  private QuotaObserverChore chore;
068  private Map<RegionInfo, Long> regionReports;
069  private NamespaceQuotaSnapshotStore store;
070
071  @Before
072  public void setup() {
073    conn = mock(Connection.class);
074    chore = mock(QuotaObserverChore.class);
075    regionReports = new HashMap<>();
076    store = new NamespaceQuotaSnapshotStore(conn, chore, regionReports);
077  }
078
079  @Test
080  public void testGetSpaceQuota() throws Exception {
081    NamespaceQuotaSnapshotStore mockStore = mock(NamespaceQuotaSnapshotStore.class);
082    when(mockStore.getSpaceQuota(any())).thenCallRealMethod();
083
084    Quotas quotaWithSpace = Quotas.newBuilder().setSpace(SpaceQuota.newBuilder().setSoftLimit(1024L)
085      .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE).build()).build();
086    Quotas quotaWithoutSpace = Quotas.newBuilder().build();
087
088    AtomicReference<Quotas> quotaRef = new AtomicReference<>();
089    when(mockStore.getQuotaForNamespace(any())).then(new Answer<Quotas>() {
090      @Override
091      public Quotas answer(InvocationOnMock invocation) throws Throwable {
092        return quotaRef.get();
093      }
094    });
095
096    quotaRef.set(quotaWithSpace);
097    assertEquals(quotaWithSpace.getSpace(), mockStore.getSpaceQuota("ns"));
098    quotaRef.set(quotaWithoutSpace);
099    assertNull(mockStore.getSpaceQuota("ns"));
100  }
101
102  @Test
103  public void testTargetViolationState() throws IOException {
104    mockNoSnapshotSizes();
105    final String NS = "ns";
106    TableName tn1 = TableName.valueOf(NS, "tn1");
107    TableName tn2 = TableName.valueOf(NS, "tn2");
108    TableName tn3 = TableName.valueOf("tn3");
109    SpaceQuota quota = SpaceQuota.newBuilder().setSoftLimit(ONE_MEGABYTE)
110      .setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(SpaceViolationPolicy.DISABLE))
111      .build();
112
113    // Create some junk data to filter. Makes sure it's so large that it would
114    // immediately violate the quota.
115    for (int i = 0; i < 3; i++) {
116
117      regionReports.put(RegionInfoBuilder.newBuilder(tn3).setStartKey(Bytes.toBytes(i))
118        .setEndKey(Bytes.toBytes(i + 1)).build(), 5L * ONE_MEGABYTE);
119    }
120
121    regionReports.put(RegionInfoBuilder.newBuilder(tn1).setStartKey(Bytes.toBytes(0))
122      .setEndKey(Bytes.toBytes(1)).build(), 1024L * 512L);
123    regionReports.put(RegionInfoBuilder.newBuilder(tn1).setStartKey(Bytes.toBytes(1))
124      .setEndKey(Bytes.toBytes(2)).build(), 1024L * 256L);
125
126    // Below the quota
127    assertEquals(false, store.getTargetState(NS, quota).getQuotaStatus().isInViolation());
128
129    regionReports.put(RegionInfoBuilder.newBuilder(tn2).setStartKey(Bytes.toBytes(2))
130      .setEndKey(Bytes.toBytes(3)).build(), 1024L * 256L);
131
132    // Equal to the quota is still in observance
133    assertEquals(false, store.getTargetState(NS, quota).getQuotaStatus().isInViolation());
134
135    regionReports.put(RegionInfoBuilder.newBuilder(tn2).setStartKey(Bytes.toBytes(3))
136      .setEndKey(Bytes.toBytes(4)).build(), 1024L);
137
138    // Exceeds the quota, should be in violation
139    assertEquals(true, store.getTargetState(NS, quota).getQuotaStatus().isInViolation());
140    assertEquals(SpaceViolationPolicy.DISABLE,
141      store.getTargetState(NS, quota).getQuotaStatus().getPolicy().get());
142  }
143
144  @Test
145  public void testFilterRegionsByNamespace() {
146    TableName tn1 = TableName.valueOf("foo");
147    TableName tn2 = TableName.valueOf("sn", "bar");
148    TableName tn3 = TableName.valueOf("ns", "foo");
149    TableName tn4 = TableName.valueOf("ns", "bar");
150
151    assertEquals(0, size(store.filterBySubject("asdf")));
152
153    for (int i = 0; i < 5; i++) {
154      regionReports.put(RegionInfoBuilder.newBuilder(tn1).setStartKey(Bytes.toBytes(i))
155        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
156    }
157    for (int i = 0; i < 3; i++) {
158      regionReports.put(RegionInfoBuilder.newBuilder(tn2).setStartKey(Bytes.toBytes(i))
159        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
160    }
161    for (int i = 0; i < 10; i++) {
162      regionReports.put(RegionInfoBuilder.newBuilder(tn3).setStartKey(Bytes.toBytes(i))
163        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
164    }
165    for (int i = 0; i < 8; i++) {
166      regionReports.put(RegionInfoBuilder.newBuilder(tn4).setStartKey(Bytes.toBytes(i))
167        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
168    }
169    assertEquals(26, regionReports.size());
170    assertEquals(5, size(store.filterBySubject(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR)));
171    assertEquals(3, size(store.filterBySubject("sn")));
172    assertEquals(18, size(store.filterBySubject("ns")));
173  }
174
175  void mockNoSnapshotSizes() throws IOException {
176    Table quotaTable = mock(Table.class);
177    when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable);
178    when(quotaTable.get(any(Get.class))).thenReturn(new Result());
179  }
180}