View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.mockito.Mockito.doNothing;
22  import static org.mockito.Mockito.doThrow;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.IOException;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
32  import org.apache.hadoop.hbase.quotas.policies.DisableTableViolationPolicyEnforcement;
33  import org.apache.hadoop.hbase.quotas.policies.NoInsertsViolationPolicyEnforcement;
34  import org.apache.hadoop.hbase.quotas.policies.NoWritesCompactionsViolationPolicyEnforcement;
35  import org.apache.hadoop.hbase.quotas.policies.NoWritesViolationPolicyEnforcement;
36  import org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement;
37  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
38  import org.apache.hadoop.hbase.testclassification.SmallTests;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  /**
44   * Test class for {@link RegionServerSpaceQuotaManager}.
45   */
46  @Category(SmallTests.class)
47  public class TestRegionServerSpaceQuotaManager {
48  
49    private RegionServerSpaceQuotaManager quotaManager;
50    private RegionServerServices rss;
51  
52    @Before
53    public void setup() throws Exception {
54      quotaManager = mock(RegionServerSpaceQuotaManager.class);
55      rss = mock(RegionServerServices.class);
56    }
57  
58    @Test
59    public void testSpacePoliciesFromEnforcements() {
60      final Map<TableName, SpaceViolationPolicyEnforcement> enforcements = new HashMap<>();
61      final Map<TableName, SpaceQuotaSnapshot> expectedPolicies = new HashMap<>();
62      when(quotaManager.copyActiveEnforcements()).thenReturn(enforcements);
63      when(quotaManager.getActivePoliciesAsMap()).thenCallRealMethod();
64  
65      NoInsertsViolationPolicyEnforcement noInsertsPolicy = new NoInsertsViolationPolicyEnforcement();
66      SpaceQuotaSnapshot noInsertsSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 256L, 1024L);
67      noInsertsPolicy.initialize(rss, TableName.valueOf("no_inserts"), noInsertsSnapshot);
68      enforcements.put(noInsertsPolicy.getTableName(), noInsertsPolicy);
69      expectedPolicies.put(noInsertsPolicy.getTableName(), noInsertsSnapshot);
70  
71      NoWritesViolationPolicyEnforcement noWritesPolicy = new NoWritesViolationPolicyEnforcement();
72      SpaceQuotaSnapshot noWritesSnapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 512L, 2048L);
73      noWritesPolicy.initialize(rss, TableName.valueOf("no_writes"), noWritesSnapshot);
74      enforcements.put(noWritesPolicy.getTableName(), noWritesPolicy);
75      expectedPolicies.put(noWritesPolicy.getTableName(), noWritesSnapshot);
76  
77      NoWritesCompactionsViolationPolicyEnforcement noWritesCompactionsPolicy =
78          new NoWritesCompactionsViolationPolicyEnforcement();
79      SpaceQuotaSnapshot noWritesCompactionsSnapshot = new SpaceQuotaSnapshot(
80          new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 1024L, 4096L);
81      noWritesCompactionsPolicy.initialize(rss, TableName.valueOf("no_writes_compactions"), noWritesCompactionsSnapshot);
82      enforcements.put(noWritesCompactionsPolicy.getTableName(), noWritesCompactionsPolicy);
83      expectedPolicies.put(noWritesCompactionsPolicy.getTableName(),
84          noWritesCompactionsSnapshot);
85  
86      DisableTableViolationPolicyEnforcement disablePolicy = new DisableTableViolationPolicyEnforcement();
87      SpaceQuotaSnapshot disableSnapshot = new SpaceQuotaSnapshot(
88          new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 2048L, 8192L);
89      disablePolicy.initialize(rss, TableName.valueOf("disable"), disableSnapshot);
90      enforcements.put(disablePolicy.getTableName(), disablePolicy);
91      expectedPolicies.put(disablePolicy.getTableName(), disableSnapshot);
92  
93      enforcements.put(
94          TableName.valueOf("no_policy"), new DefaultViolationPolicyEnforcement());
95  
96      Map<TableName, SpaceQuotaSnapshot> actualPolicies = quotaManager.getActivePoliciesAsMap();
97      assertEquals(expectedPolicies, actualPolicies);
98    }
99  
100   @Test
101   public void testExceptionOnPolicyEnforcementEnable() throws Exception {
102     final TableName tableName = TableName.valueOf("foo");
103     final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 2048L);
104     RegionServerServices rss = mock(RegionServerServices.class);
105     SpaceViolationPolicyEnforcementFactory factory = mock(
106         SpaceViolationPolicyEnforcementFactory.class);
107     SpaceViolationPolicyEnforcement enforcement = mock(SpaceViolationPolicyEnforcement.class);
108     RegionServerSpaceQuotaManager realManager = new RegionServerSpaceQuotaManager(rss, factory);
109 
110     when(factory.create(rss, tableName, snapshot)).thenReturn(enforcement);
111     doThrow(new IOException("Failed for test!")).when(enforcement).enable();
112 
113     realManager.enforceViolationPolicy(tableName, snapshot);
114     Map<TableName, SpaceViolationPolicyEnforcement> enforcements =
115         realManager.copyActiveEnforcements();
116     assertTrue("Expected active enforcements to be empty, but were " + enforcements,
117         enforcements.isEmpty());
118   }
119 
120   @Test
121   public void testExceptionOnPolicyEnforcementDisable() throws Exception {
122     final TableName tableName = TableName.valueOf("foo");
123     final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 2048L);
124     RegionServerServices rss = mock(RegionServerServices.class);
125     SpaceViolationPolicyEnforcementFactory factory = mock(
126         SpaceViolationPolicyEnforcementFactory.class);
127     SpaceViolationPolicyEnforcement enforcement = mock(SpaceViolationPolicyEnforcement.class);
128     RegionServerSpaceQuotaManager realManager = new RegionServerSpaceQuotaManager(rss, factory);
129 
130     when(factory.create(rss, tableName, snapshot)).thenReturn(enforcement);
131     doNothing().when(enforcement).enable();
132     doThrow(new IOException("Failed for test!")).when(enforcement).disable();
133 
134     // Enabling should work
135     realManager.enforceViolationPolicy(tableName, snapshot);
136     Map<TableName, SpaceViolationPolicyEnforcement> enforcements =
137         realManager.copyActiveEnforcements();
138     assertEquals(1, enforcements.size());
139 
140     // If the disable fails, we should still treat it as "active"
141     realManager.disableViolationPolicyEnforcement(tableName);
142     enforcements = realManager.copyActiveEnforcements();
143     assertEquals(1, enforcements.size());
144   }
145 }