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().setSoftLimit(1024L * 1024L * 1024L * 50L) // 50G
057      .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE) // Disable the table
058      .build();
059    final long readLimit = 1000;
060    final long writeLimit = 500;
061    final Throttle throttle = Throttle.newBuilder()
062      // 1000 read reqs/min
063      .setReadNum(TimedQuota.newBuilder().setSoftLimit(readLimit)
064        .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build())
065      // 500 write reqs/min
066      .setWriteNum(TimedQuota.newBuilder().setSoftLimit(writeLimit)
067        .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build())
068      .build();
069    final Quotas quotas = Quotas.newBuilder().setSpace(spaceQuota) // Set the FS quotas
070      .setThrottle(throttle) // Set some RPC limits
071      .build();
072    final TableName tn = TableName.valueOf("my_table");
073    List<QuotaSettings> settings = QuotaSettingsFactory.fromTableQuotas(tn, quotas);
074    assertEquals(3, settings.size());
075    boolean seenRead = false;
076    boolean seenWrite = false;
077    boolean seenSpace = false;
078    for (QuotaSettings setting : settings) {
079      if (setting instanceof ThrottleSettings) {
080        ThrottleSettings throttleSettings = (ThrottleSettings) setting;
081        switch (throttleSettings.getThrottleType()) {
082          case READ_NUMBER:
083            assertFalse("Should not have multiple read quotas", seenRead);
084            assertEquals(readLimit, throttleSettings.getSoftLimit());
085            assertEquals(TimeUnit.MINUTES, throttleSettings.getTimeUnit());
086            assertEquals(tn, throttleSettings.getTableName());
087            assertNull("Username should be null", throttleSettings.getUserName());
088            assertNull("Namespace should be null", throttleSettings.getNamespace());
089            assertNull("RegionServer should be null", throttleSettings.getRegionServer());
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            assertNull("RegionServer should be null", throttleSettings.getRegionServer());
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        assertNull("RegionServer should be null", spaceLimit.getRegionServer());
112        assertTrue("SpaceLimitSettings should have a SpaceQuota", spaceLimit.getProto().hasQuota());
113        assertEquals(spaceQuota, spaceLimit.getProto().getQuota());
114        seenSpace = true;
115      } else {
116        fail("Unexpected QuotaSettings implementation: " + setting.getClass());
117      }
118    }
119    assertTrue("Should have seen a read quota", seenRead);
120    assertTrue("Should have seen a write quota", seenWrite);
121    assertTrue("Should have seen a space quota", seenSpace);
122  }
123
124  @Test(expected = IllegalArgumentException.class)
125  public void testNeitherTableNorNamespace() {
126    final SpaceQuota spaceQuota = SpaceQuota.newBuilder().setSoftLimit(1L)
127      .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE).build();
128    QuotaSettingsFactory.fromSpace(null, null, spaceQuota);
129  }
130
131  @Test(expected = IllegalArgumentException.class)
132  public void testBothTableAndNamespace() {
133    final SpaceQuota spaceQuota = SpaceQuota.newBuilder().setSoftLimit(1L)
134      .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.DISABLE).build();
135    QuotaSettingsFactory.fromSpace(TableName.valueOf("foo"), "bar", spaceQuota);
136  }
137
138  @Test
139  public void testSpaceLimitSettings() {
140    final TableName tableName = TableName.valueOf("foo");
141    final long sizeLimit = 1024L * 1024L * 1024L * 75; // 75GB
142    final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_INSERTS;
143    QuotaSettings settings =
144      QuotaSettingsFactory.limitTableSpace(tableName, sizeLimit, violationPolicy);
145    assertNotNull("QuotaSettings should not be null", settings);
146    assertTrue("Should be an instance of SpaceLimitSettings",
147      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",
164      nsSettings instanceof SpaceLimitSettings);
165    SpaceLimitRequest nsProto = ((SpaceLimitSettings) nsSettings).getProto();
166    assertTrue("Request should have a SpaceQuota", nsProto.hasQuota());
167    assertTrue("The remove attribute should be true", nsProto.getQuota().getRemove());
168
169    QuotaSettings tableSettings = QuotaSettingsFactory.removeTableSpaceLimit(tn);
170    assertNotNull("QuotaSettings should not be null", tableSettings);
171    assertTrue("Should be an instance of SpaceLimitSettings",
172      tableSettings instanceof SpaceLimitSettings);
173    SpaceLimitRequest tableProto = ((SpaceLimitSettings) tableSettings).getProto();
174    assertTrue("Request should have a SpaceQuota", tableProto.hasQuota());
175    assertTrue("The remove attribute should be true", tableProto.getQuota().getRemove());
176  }
177}