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.assertTrue;
023
024import java.util.HashMap;
025import java.util.Map;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
035
036/**
037 * Tests of QuotaCache that don't require a minicluster, unlike in TestQuotaCache
038 */
039@Category({ RegionServerTests.class, SmallTests.class })
040public class TestQuotaCache2 {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestQuotaCache2.class);
045
046  @Test
047  public void testPreserveLimiterAvailability() throws Exception {
048    // establish old cache with a limiter for 100 read bytes per second
049    QuotaState oldState = new QuotaState();
050    Map<String, QuotaState> oldCache = new HashMap<>();
051    oldCache.put("my_table", oldState);
052    QuotaProtos.Throttle throttle1 = QuotaProtos.Throttle.newBuilder()
053      .setReadSize(QuotaProtos.TimedQuota.newBuilder().setTimeUnit(HBaseProtos.TimeUnit.SECONDS)
054        .setSoftLimit(100).setScope(QuotaProtos.QuotaScope.MACHINE).build())
055      .build();
056    QuotaLimiter limiter1 = TimeBasedLimiter.fromThrottle(throttle1);
057    oldState.setGlobalLimiter(limiter1);
058
059    // consume one byte from the limiter, so 99 will be left
060    limiter1.consumeRead(1, 1, false);
061
062    // establish new cache, also with a limiter for 100 read bytes per second
063    QuotaState newState = new QuotaState();
064    Map<String, QuotaState> newCache = new HashMap<>();
065    newCache.put("my_table", newState);
066    QuotaProtos.Throttle throttle2 = QuotaProtos.Throttle.newBuilder()
067      .setReadSize(QuotaProtos.TimedQuota.newBuilder().setTimeUnit(HBaseProtos.TimeUnit.SECONDS)
068        .setSoftLimit(100).setScope(QuotaProtos.QuotaScope.MACHINE).build())
069      .build();
070    QuotaLimiter limiter2 = TimeBasedLimiter.fromThrottle(throttle2);
071    newState.setGlobalLimiter(limiter2);
072
073    // update new cache from old cache
074    QuotaCache.updateNewCacheFromOld(oldCache, newCache);
075
076    // verify that the 99 available bytes from the limiter was carried over
077    TimeBasedLimiter updatedLimiter =
078      (TimeBasedLimiter) newCache.get("my_table").getGlobalLimiter();
079    assertEquals(99, updatedLimiter.getReadAvailable());
080  }
081
082  @Test
083  public void testClobberLimiterLimit() throws Exception {
084    // establish old cache with a limiter for 100 read bytes per second
085    QuotaState oldState = new QuotaState();
086    Map<String, QuotaState> oldCache = new HashMap<>();
087    oldCache.put("my_table", oldState);
088    QuotaProtos.Throttle throttle1 = QuotaProtos.Throttle.newBuilder()
089      .setReadSize(QuotaProtos.TimedQuota.newBuilder().setTimeUnit(HBaseProtos.TimeUnit.SECONDS)
090        .setSoftLimit(100).setScope(QuotaProtos.QuotaScope.MACHINE).build())
091      .build();
092    QuotaLimiter limiter1 = TimeBasedLimiter.fromThrottle(throttle1);
093    oldState.setGlobalLimiter(limiter1);
094
095    // establish new cache, also with a limiter for 100 read bytes per second
096    QuotaState newState = new QuotaState();
097    Map<String, QuotaState> newCache = new HashMap<>();
098    newCache.put("my_table", newState);
099    QuotaProtos.Throttle throttle2 = QuotaProtos.Throttle.newBuilder()
100      .setReadSize(QuotaProtos.TimedQuota.newBuilder().setTimeUnit(HBaseProtos.TimeUnit.SECONDS)
101        .setSoftLimit(50).setScope(QuotaProtos.QuotaScope.MACHINE).build())
102      .build();
103    QuotaLimiter limiter2 = TimeBasedLimiter.fromThrottle(throttle2);
104    newState.setGlobalLimiter(limiter2);
105
106    // update new cache from old cache
107    QuotaCache.updateNewCacheFromOld(oldCache, newCache);
108
109    // verify that the 99 available bytes from the limiter was carried over
110    TimeBasedLimiter updatedLimiter =
111      (TimeBasedLimiter) newCache.get("my_table").getGlobalLimiter();
112    assertEquals(50, updatedLimiter.getReadLimit());
113  }
114
115  @Test
116  public void testForgetsDeletedQuota() {
117    QuotaState oldState = new QuotaState();
118    Map<String, QuotaState> oldCache = new HashMap<>();
119    oldCache.put("my_table1", oldState);
120
121    QuotaState newState = new QuotaState();
122    Map<String, QuotaState> newCache = new HashMap<>();
123    newCache.put("my_table2", newState);
124
125    QuotaCache.updateNewCacheFromOld(oldCache, newCache);
126
127    assertTrue(newCache.containsKey("my_table2"));
128    assertFalse(newCache.containsKey("my_table1"));
129  }
130}