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