HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / quotas / TestGlobalQuotaSettingsImpl.java
blob55938dc5f5d5b02301824442eb39ff7da18ee7e2
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.quotas;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.TableName;
26 import org.apache.hadoop.hbase.testclassification.SmallTests;
27 import org.junit.ClassRule;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
31 import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
32 import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
34 @Category(SmallTests.class)
35 public class TestGlobalQuotaSettingsImpl {
37 @ClassRule
38 public static final HBaseClassTestRule CLASS_RULE =
39 HBaseClassTestRule.forClass(TestGlobalQuotaSettingsImpl.class);
41 QuotaProtos.TimedQuota REQUEST_THROTTLE = QuotaProtos.TimedQuota.newBuilder()
42 .setScope(QuotaProtos.QuotaScope.MACHINE).setSoftLimit(100)
43 .setTimeUnit(HBaseProtos.TimeUnit.MINUTES).build();
44 QuotaProtos.Throttle THROTTLE = QuotaProtos.Throttle.newBuilder()
45 .setReqNum(REQUEST_THROTTLE).build();
47 QuotaProtos.SpaceQuota SPACE_QUOTA = QuotaProtos.SpaceQuota.newBuilder()
48 .setSoftLimit(1024L * 1024L).setViolationPolicy(QuotaProtos.SpaceViolationPolicy.NO_WRITES)
49 .build();
51 @Test
52 public void testMergeThrottle() throws IOException {
53 QuotaProtos.Quotas quota = QuotaProtos.Quotas.newBuilder()
54 .setThrottle(THROTTLE).build();
55 QuotaProtos.TimedQuota writeQuota = REQUEST_THROTTLE.toBuilder()
56 .setSoftLimit(500).build();
57 // Unset the req throttle, set a write throttle
58 QuotaProtos.ThrottleRequest writeThrottle = QuotaProtos.ThrottleRequest.newBuilder()
59 .setTimedQuota(writeQuota).setType(QuotaProtos.ThrottleType.WRITE_NUMBER).build();
61 GlobalQuotaSettingsImpl settings = new GlobalQuotaSettingsImpl("joe", null, null, null, quota);
62 GlobalQuotaSettingsImpl merged = settings.merge(
63 new ThrottleSettings("joe", null, null, null, writeThrottle));
65 QuotaProtos.Throttle mergedThrottle = merged.getThrottleProto();
66 // Verify the request throttle is in place
67 assertTrue(mergedThrottle.hasReqNum());
68 QuotaProtos.TimedQuota actualReqNum = mergedThrottle.getReqNum();
69 assertEquals(REQUEST_THROTTLE.getSoftLimit(), actualReqNum.getSoftLimit());
71 // Verify the write throttle is in place
72 assertTrue(mergedThrottle.hasWriteNum());
73 QuotaProtos.TimedQuota actualWriteNum = mergedThrottle.getWriteNum();
74 assertEquals(writeQuota.getSoftLimit(), actualWriteNum.getSoftLimit());
77 @Test
78 public void testMergeSpace() throws IOException {
79 TableName tn = TableName.valueOf("foo");
80 QuotaProtos.Quotas quota = QuotaProtos.Quotas.newBuilder()
81 .setSpace(SPACE_QUOTA).build();
83 GlobalQuotaSettingsImpl settings = new GlobalQuotaSettingsImpl(null, tn, null, null, quota);
84 // Switch the violation policy to DISABLE
85 GlobalQuotaSettingsImpl merged = settings.merge(
86 new SpaceLimitSettings(tn, SPACE_QUOTA.getSoftLimit(), SpaceViolationPolicy.DISABLE));
88 QuotaProtos.SpaceQuota mergedSpaceQuota = merged.getSpaceProto();
89 assertEquals(SPACE_QUOTA.getSoftLimit(), mergedSpaceQuota.getSoftLimit());
90 assertEquals(
91 QuotaProtos.SpaceViolationPolicy.DISABLE, mergedSpaceQuota.getViolationPolicy());
94 @Test
95 public void testMergeThrottleAndSpace() throws IOException {
96 final String ns = "org1";
97 QuotaProtos.Quotas quota = QuotaProtos.Quotas.newBuilder()
98 .setThrottle(THROTTLE).setSpace(SPACE_QUOTA).build();
99 GlobalQuotaSettingsImpl settings = new GlobalQuotaSettingsImpl(null, null, ns, null, quota);
101 QuotaProtos.TimedQuota writeQuota = REQUEST_THROTTLE.toBuilder()
102 .setSoftLimit(500).build();
103 // Add a write throttle
104 QuotaProtos.ThrottleRequest writeThrottle = QuotaProtos.ThrottleRequest.newBuilder()
105 .setTimedQuota(writeQuota).setType(QuotaProtos.ThrottleType.WRITE_NUMBER).build();
107 GlobalQuotaSettingsImpl merged = settings.merge(
108 new ThrottleSettings(null, null, ns, null, writeThrottle));
109 GlobalQuotaSettingsImpl finalQuota = merged.merge(new SpaceLimitSettings(
110 ns, SPACE_QUOTA.getSoftLimit(), SpaceViolationPolicy.NO_WRITES_COMPACTIONS));
112 // Verify both throttle quotas
113 QuotaProtos.Throttle throttle = finalQuota.getThrottleProto();
114 assertTrue(throttle.hasReqNum());
115 QuotaProtos.TimedQuota reqNumQuota = throttle.getReqNum();
116 assertEquals(REQUEST_THROTTLE.getSoftLimit(), reqNumQuota.getSoftLimit());
118 assertTrue(throttle.hasWriteNum());
119 QuotaProtos.TimedQuota writeNumQuota = throttle.getWriteNum();
120 assertEquals(writeQuota.getSoftLimit(), writeNumQuota.getSoftLimit());
122 // Verify space quota
123 QuotaProtos.SpaceQuota finalSpaceQuota = finalQuota.getSpaceProto();
124 assertEquals(SPACE_QUOTA.getSoftLimit(), finalSpaceQuota.getSoftLimit());
125 assertEquals(
126 QuotaProtos.SpaceViolationPolicy.NO_WRITES_COMPACTIONS,
127 finalSpaceQuota.getViolationPolicy());