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.Matchers.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( 085 SpaceQuota.newBuilder() 086 .setSoftLimit(1024L) 087 .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE) 088 .build()) 089 .build(); 090 Quotas quotaWithoutSpace = Quotas.newBuilder().build(); 091 092 AtomicReference<Quotas> quotaRef = new AtomicReference<>(); 093 when(mockStore.getQuotaForNamespace(any())).then(new Answer<Quotas>() { 094 @Override 095 public Quotas answer(InvocationOnMock invocation) throws Throwable { 096 return quotaRef.get(); 097 } 098 }); 099 100 quotaRef.set(quotaWithSpace); 101 assertEquals(quotaWithSpace.getSpace(), mockStore.getSpaceQuota("ns")); 102 quotaRef.set(quotaWithoutSpace); 103 assertNull(mockStore.getSpaceQuota("ns")); 104 } 105 106 @Test 107 public void testTargetViolationState() throws IOException { 108 mockNoSnapshotSizes(); 109 final String NS = "ns"; 110 TableName tn1 = TableName.valueOf(NS, "tn1"); 111 TableName tn2 = TableName.valueOf(NS, "tn2"); 112 TableName tn3 = TableName.valueOf("tn3"); 113 SpaceQuota quota = SpaceQuota.newBuilder() 114 .setSoftLimit(ONE_MEGABYTE) 115 .setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(SpaceViolationPolicy.DISABLE)) 116 .build(); 117 118 // Create some junk data to filter. Makes sure it's so large that it would 119 // immediately violate the quota. 120 for (int i = 0; i < 3; i++) { 121 122 regionReports.put(RegionInfoBuilder.newBuilder(tn3) 123 .setStartKey(Bytes.toBytes(i)) 124 .setEndKey(Bytes.toBytes(i + 1)) 125 .build(), 126 5L * ONE_MEGABYTE); 127 } 128 129 regionReports.put(RegionInfoBuilder.newBuilder(tn1) 130 .setStartKey(Bytes.toBytes(0)) 131 .setEndKey(Bytes.toBytes(1)) 132 .build(), 1024L * 512L); 133 regionReports.put(RegionInfoBuilder.newBuilder(tn1) 134 .setStartKey(Bytes.toBytes(1)) 135 .setEndKey(Bytes.toBytes(2)) 136 .build(), 1024L * 256L); 137 138 // Below the quota 139 assertEquals(false, store.getTargetState(NS, quota).getQuotaStatus().isInViolation()); 140 141 regionReports.put(RegionInfoBuilder.newBuilder(tn2) 142 .setStartKey(Bytes.toBytes(2)) 143 .setEndKey(Bytes.toBytes(3)) 144 .build(), 1024L * 256L); 145 146 // Equal to the quota is still in observance 147 assertEquals(false, store.getTargetState(NS, quota).getQuotaStatus().isInViolation()); 148 149 regionReports.put(RegionInfoBuilder.newBuilder(tn2) 150 .setStartKey(Bytes.toBytes(3)) 151 .setEndKey(Bytes.toBytes(4)) 152 .build(), 1024L); 153 154 // Exceeds the quota, should be in violation 155 assertEquals(true, store.getTargetState(NS, quota).getQuotaStatus().isInViolation()); 156 assertEquals(SpaceViolationPolicy.DISABLE, 157 store.getTargetState(NS, quota).getQuotaStatus().getPolicy().get()); 158 } 159 160 @Test 161 public void testFilterRegionsByNamespace() { 162 TableName tn1 = TableName.valueOf("foo"); 163 TableName tn2 = TableName.valueOf("sn", "bar"); 164 TableName tn3 = TableName.valueOf("ns", "foo"); 165 TableName tn4 = TableName.valueOf("ns", "bar"); 166 167 assertEquals(0, size(store.filterBySubject("asdf"))); 168 169 for (int i = 0; i < 5; i++) { 170 regionReports.put(RegionInfoBuilder.newBuilder(tn1) 171 .setStartKey(Bytes.toBytes(i)) 172 .setEndKey(Bytes.toBytes(i + 1)) 173 .build(), 0L); 174 } 175 for (int i = 0; i < 3; i++) { 176 regionReports.put(RegionInfoBuilder.newBuilder(tn2) 177 .setStartKey(Bytes.toBytes(i)) 178 .setEndKey(Bytes.toBytes(i + 1)) 179 .build(), 0L); 180 } 181 for (int i = 0; i < 10; i++) { 182 regionReports.put(RegionInfoBuilder.newBuilder(tn3) 183 .setStartKey(Bytes.toBytes(i)) 184 .setEndKey(Bytes.toBytes(i + 1)) 185 .build(), 0L); 186 } 187 for (int i = 0; i < 8; i++) { 188 regionReports.put(RegionInfoBuilder.newBuilder(tn4) 189 .setStartKey(Bytes.toBytes(i)) 190 .setEndKey(Bytes.toBytes(i + 1)) 191 .build(), 0L); 192 } 193 assertEquals(26, regionReports.size()); 194 assertEquals(5, size(store.filterBySubject(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR))); 195 assertEquals(3, size(store.filterBySubject("sn"))); 196 assertEquals(18, size(store.filterBySubject("ns"))); 197 } 198 199 void mockNoSnapshotSizes() throws IOException { 200 Table quotaTable = mock(Table.class); 201 when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); 202 when(quotaTable.get(any(Get.class))).thenReturn(new Result()); 203 } 204}