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.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import java.util.concurrent.TimeUnit;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.ThrottleRequest;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.TimedQuota;
036
037@Category({SmallTests.class})
038public class TestThrottleSettings {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestThrottleSettings.class);
043
044  @Test
045  public void testMerge() throws IOException {
046    TimedQuota tq1 = TimedQuota.newBuilder().setSoftLimit(10)
047        .setScope(QuotaProtos.QuotaScope.MACHINE)
048        .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
049    ThrottleRequest tr1 = ThrottleRequest.newBuilder().setTimedQuota(tq1)
050        .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
051    ThrottleSettings orig = new ThrottleSettings("joe", null, null, tr1);
052
053    TimedQuota tq2 = TimedQuota.newBuilder().setSoftLimit(10)
054        .setScope(QuotaProtos.QuotaScope.MACHINE)
055        .setTimeUnit(HBaseProtos.TimeUnit.SECONDS).build();
056    ThrottleRequest tr2 = ThrottleRequest.newBuilder().setTimedQuota(tq2)
057        .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
058
059    ThrottleSettings merged = orig.merge(new ThrottleSettings("joe", null, null, tr2));
060
061    assertEquals(10, merged.getSoftLimit());
062    assertEquals(ThrottleType.REQUEST_NUMBER, merged.getThrottleType());
063    assertEquals(TimeUnit.SECONDS, merged.getTimeUnit());
064  }
065
066  @Test
067  public void testIncompatibleThrottleTypes() throws IOException {
068    TimedQuota requestsQuota = TimedQuota.newBuilder().setSoftLimit(10)
069        .setScope(QuotaProtos.QuotaScope.MACHINE)
070        .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
071    ThrottleRequest requestsQuotaReq = ThrottleRequest.newBuilder().setTimedQuota(requestsQuota)
072        .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
073    ThrottleSettings orig = new ThrottleSettings("joe", null, null, requestsQuotaReq);
074
075    TimedQuota readsQuota = TimedQuota.newBuilder().setSoftLimit(10)
076        .setScope(QuotaProtos.QuotaScope.MACHINE)
077        .setTimeUnit(HBaseProtos.TimeUnit.SECONDS).build();
078    ThrottleRequest readsQuotaReq = ThrottleRequest.newBuilder().setTimedQuota(readsQuota)
079        .setType(QuotaProtos.ThrottleType.READ_NUMBER).build();
080
081    try {
082      orig.merge(new ThrottleSettings("joe", null, null, readsQuotaReq));
083      fail("A read throttle should not be capable of being merged with a request quota");
084    } catch (IllegalArgumentException e) {
085      // Pass
086    }
087  }
088
089  @Test
090  public void testNoThrottleReturnsOriginal() throws IOException {
091    TimedQuota tq1 = TimedQuota.newBuilder().setSoftLimit(10)
092        .setScope(QuotaProtos.QuotaScope.MACHINE)
093        .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
094    ThrottleRequest tr1 = ThrottleRequest.newBuilder().setTimedQuota(tq1)
095        .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
096    ThrottleSettings orig = new ThrottleSettings("joe", null, null, tr1);
097
098    ThrottleRequest tr2 = ThrottleRequest.newBuilder()
099        .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
100
101    assertTrue(
102        "The same object should be returned by merge, but it wasn't",
103        orig == orig.merge(new ThrottleSettings("joe", null, null, tr2)));
104  }
105}