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