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; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025import static org.junit.Assert.fail; 026 027import java.util.List; 028import java.util.concurrent.TimeUnit; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.junit.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 037import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; 038import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos; 039import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Quotas; 040import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceLimitRequest; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceQuota; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Throttle; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.TimedQuota; 044 045/** 046 * Test class for {@link QuotaSettingsFactory}. 047 */ 048@Category(SmallTests.class) 049public class TestQuotaSettingsFactory { 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestQuotaSettingsFactory.class); 053 054 @Test 055 public void testAllQuotasAddedToList() { 056 final SpaceQuota spaceQuota = SpaceQuota.newBuilder() 057 .setSoftLimit(1024L * 1024L * 1024L * 50L) // 50G 058 .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE) // Disable the table 059 .build(); 060 final long readLimit = 1000; 061 final long writeLimit = 500; 062 final Throttle throttle = Throttle.newBuilder() 063 // 1000 read reqs/min 064 .setReadNum(TimedQuota.newBuilder().setSoftLimit(readLimit) 065 .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build()) 066 // 500 write reqs/min 067 .setWriteNum(TimedQuota.newBuilder().setSoftLimit(writeLimit) 068 .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build()) 069 .build(); 070 final Quotas quotas = Quotas.newBuilder() 071 .setSpace(spaceQuota) // Set the FS quotas 072 .setThrottle(throttle) // Set some RPC limits 073 .build(); 074 final TableName tn = TableName.valueOf("my_table"); 075 List<QuotaSettings> settings = QuotaSettingsFactory.fromTableQuotas(tn, quotas); 076 assertEquals(3, settings.size()); 077 boolean seenRead = false; 078 boolean seenWrite = false; 079 boolean seenSpace = false; 080 for (QuotaSettings setting : settings) { 081 if (setting instanceof ThrottleSettings) { 082 ThrottleSettings throttleSettings = (ThrottleSettings) setting; 083 switch (throttleSettings.getThrottleType()) { 084 case READ_NUMBER: 085 assertFalse("Should not have multiple read quotas", seenRead); 086 assertEquals(readLimit, throttleSettings.getSoftLimit()); 087 assertEquals(TimeUnit.MINUTES, throttleSettings.getTimeUnit()); 088 assertEquals(tn, throttleSettings.getTableName()); 089 assertNull("Username should be null", throttleSettings.getUserName()); 090 assertNull("Namespace should be null", throttleSettings.getNamespace()); 091 seenRead = true; 092 break; 093 case WRITE_NUMBER: 094 assertFalse("Should not have multiple write quotas", seenWrite); 095 assertEquals(writeLimit, throttleSettings.getSoftLimit()); 096 assertEquals(TimeUnit.MINUTES, throttleSettings.getTimeUnit()); 097 assertEquals(tn, throttleSettings.getTableName()); 098 assertNull("Username should be null", throttleSettings.getUserName()); 099 assertNull("Namespace should be null", throttleSettings.getNamespace()); 100 seenWrite = true; 101 break; 102 default: 103 fail("Unexpected throttle type: " + throttleSettings.getThrottleType()); 104 } 105 } else if (setting instanceof SpaceLimitSettings) { 106 assertFalse("Should not have multiple space quotas", seenSpace); 107 SpaceLimitSettings spaceLimit = (SpaceLimitSettings) setting; 108 assertEquals(tn, spaceLimit.getTableName()); 109 assertNull("Username should be null", spaceLimit.getUserName()); 110 assertNull("Namespace should be null", spaceLimit.getNamespace()); 111 assertTrue("SpaceLimitSettings should have a SpaceQuota", spaceLimit.getProto().hasQuota()); 112 assertEquals(spaceQuota, spaceLimit.getProto().getQuota()); 113 seenSpace = true; 114 } else { 115 fail("Unexpected QuotaSettings implementation: " + setting.getClass()); 116 } 117 } 118 assertTrue("Should have seen a read quota", seenRead); 119 assertTrue("Should have seen a write quota", seenWrite); 120 assertTrue("Should have seen a space quota", seenSpace); 121 } 122 123 @Test(expected = IllegalArgumentException.class) 124 public void testNeitherTableNorNamespace() { 125 final SpaceQuota spaceQuota = SpaceQuota.newBuilder() 126 .setSoftLimit(1L) 127 .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE) 128 .build(); 129 QuotaSettingsFactory.fromSpace(null, null, spaceQuota); 130 } 131 132 @Test(expected = IllegalArgumentException.class) 133 public void testBothTableAndNamespace() { 134 final SpaceQuota spaceQuota = SpaceQuota.newBuilder() 135 .setSoftLimit(1L) 136 .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE) 137 .build(); 138 QuotaSettingsFactory.fromSpace(TableName.valueOf("foo"), "bar", spaceQuota); 139 } 140 141 @Test 142 public void testSpaceLimitSettings() { 143 final TableName tableName = TableName.valueOf("foo"); 144 final long sizeLimit = 1024L * 1024L * 1024L * 75; // 75GB 145 final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS; 146 QuotaSettings settings = 147 QuotaSettingsFactory.limitTableSpace(tableName, sizeLimit, violationPolicy); 148 assertNotNull("QuotaSettings should not be null", settings); 149 assertTrue("Should be an instance of SpaceLimitSettings", 150 settings instanceof SpaceLimitSettings); 151 SpaceLimitSettings spaceLimitSettings = (SpaceLimitSettings) settings; 152 SpaceLimitRequest protoRequest = spaceLimitSettings.getProto(); 153 assertTrue("Request should have a SpaceQuota", protoRequest.hasQuota()); 154 SpaceQuota quota = protoRequest.getQuota(); 155 assertEquals(sizeLimit, quota.getSoftLimit()); 156 assertEquals(violationPolicy, ProtobufUtil.toViolationPolicy(quota.getViolationPolicy())); 157 assertFalse("The remove attribute should be false", quota.getRemove()); 158 } 159 160 @Test 161 public void testSpaceLimitSettingsForDeletes() { 162 final String ns = "ns1"; 163 final TableName tn = TableName.valueOf("tn1"); 164 QuotaSettings nsSettings = QuotaSettingsFactory.removeNamespaceSpaceLimit(ns); 165 assertNotNull("QuotaSettings should not be null", nsSettings); 166 assertTrue("Should be an instance of SpaceLimitSettings", 167 nsSettings instanceof SpaceLimitSettings); 168 SpaceLimitRequest nsProto = ((SpaceLimitSettings) nsSettings).getProto(); 169 assertTrue("Request should have a SpaceQuota", nsProto.hasQuota()); 170 assertTrue("The remove attribute should be true", nsProto.getQuota().getRemove()); 171 172 QuotaSettings tableSettings = QuotaSettingsFactory.removeTableSpaceLimit(tn); 173 assertNotNull("QuotaSettings should not be null", tableSettings); 174 assertTrue("Should be an instance of SpaceLimitSettings", 175 tableSettings instanceof SpaceLimitSettings); 176 SpaceLimitRequest tableProto = ((SpaceLimitSettings) tableSettings).getProto(); 177 assertTrue("Request should have a SpaceQuota", tableProto.hasQuota()); 178 assertTrue("The remove attribute should be true", tableProto.getQuota().getRemove()); 179 } 180}