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).setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
048    ThrottleRequest tr1 = ThrottleRequest.newBuilder().setTimedQuota(tq1)
049      .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
050    ThrottleSettings orig = new ThrottleSettings("joe", null, null, null, tr1);
051
052    TimedQuota tq2 = TimedQuota.newBuilder().setSoftLimit(10)
053      .setScope(QuotaProtos.QuotaScope.MACHINE).setTimeUnit(HBaseProtos.TimeUnit.SECONDS).build();
054    ThrottleRequest tr2 = ThrottleRequest.newBuilder().setTimedQuota(tq2)
055      .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
056
057    ThrottleSettings merged = orig.merge(new ThrottleSettings("joe", null, null, null, tr2));
058
059    assertEquals(10, merged.getSoftLimit());
060    assertEquals(ThrottleType.REQUEST_NUMBER, merged.getThrottleType());
061    assertEquals(TimeUnit.SECONDS, merged.getTimeUnit());
062  }
063
064  @Test
065  public void testIncompatibleThrottleTypes() throws IOException {
066    TimedQuota requestsQuota = TimedQuota.newBuilder().setSoftLimit(10)
067      .setScope(QuotaProtos.QuotaScope.MACHINE).setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
068    ThrottleRequest requestsQuotaReq = ThrottleRequest.newBuilder().setTimedQuota(requestsQuota)
069      .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
070    ThrottleSettings orig = new ThrottleSettings("joe", null, null, null, requestsQuotaReq);
071
072    TimedQuota readsQuota = TimedQuota.newBuilder().setSoftLimit(10)
073      .setScope(QuotaProtos.QuotaScope.MACHINE).setTimeUnit(HBaseProtos.TimeUnit.SECONDS).build();
074    ThrottleRequest readsQuotaReq = ThrottleRequest.newBuilder().setTimedQuota(readsQuota)
075      .setType(QuotaProtos.ThrottleType.READ_NUMBER).build();
076
077    try {
078      orig.merge(new ThrottleSettings("joe", null, null, null, readsQuotaReq));
079      fail("A read throttle should not be capable of being merged with a request quota");
080    } catch (IllegalArgumentException e) {
081      // Pass
082    }
083  }
084
085  @Test
086  public void testNoThrottleReturnsOriginal() throws IOException {
087    TimedQuota tq1 = TimedQuota.newBuilder().setSoftLimit(10)
088      .setScope(QuotaProtos.QuotaScope.MACHINE).setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
089    ThrottleRequest tr1 = ThrottleRequest.newBuilder().setTimedQuota(tq1)
090      .setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
091    ThrottleSettings orig = new ThrottleSettings("joe", null, null, null, tr1);
092
093    ThrottleRequest tr2 =
094      ThrottleRequest.newBuilder().setType(QuotaProtos.ThrottleType.REQUEST_NUMBER).build();
095
096    assertTrue("The same object should be returned by merge, but it wasn't",
097      orig == orig.merge(new ThrottleSettings("joe", null, null, null, tr2)));
098  }
099}