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.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.mock;
24  
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.quotas.policies.NoWritesViolationPolicyEnforcement;
32  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
33  import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
34  import org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement;
35  import org.apache.hadoop.hbase.quotas.policies.MissingSnapshotViolationPolicyEnforcement;
36  import org.apache.hadoop.hbase.testclassification.SmallTests;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  /**
42   * Test class for {@link ActivePolicyEnforcement}.
43   */
44  @Category(SmallTests.class)
45  public class TestActivePolicyEnforcement {
46    private static final Map<TableName,SpaceViolationPolicyEnforcement> EMPTY_ENFORCEMENT_MAP =
47        Collections.emptyMap();
48    private static final Map<TableName,SpaceQuotaSnapshot> EMPTY_SNAPSHOT_MAP =
49        Collections.emptyMap();
50  
51    private RegionServerServices rss;
52  
53    @Before
54    public void setup() {
55      rss = mock(RegionServerServices.class);
56    }
57  
58    @Test
59    public void testGetter() {
60      final TableName tableName = TableName.valueOf("table");
61      Map<TableName, SpaceViolationPolicyEnforcement> map = new HashMap<>();
62      map.put(tableName, new NoWritesViolationPolicyEnforcement());
63      ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
64          map, EMPTY_SNAPSHOT_MAP, null);
65      assertEquals(map.get(tableName), ape.getPolicyEnforcement(tableName));
66    }
67  
68    @Test
69    public void testNoPolicyReturnsNoopEnforcement() {
70      ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
71          new HashMap<TableName, SpaceViolationPolicyEnforcement>(),
72          EMPTY_SNAPSHOT_MAP,
73          mock(RegionServerServices.class));
74      SpaceViolationPolicyEnforcement enforcement = ape.getPolicyEnforcement(
75          TableName.valueOf("nonexistent"));
76      assertNotNull(enforcement);
77      assertTrue(
78          "Expected an instance of MissingSnapshotViolationPolicyEnforcement, but got "
79              + enforcement.getClass(),
80          enforcement instanceof MissingSnapshotViolationPolicyEnforcement);
81    }
82  
83    @Test
84    public void testNoBulkLoadChecksOnNoSnapshot() {
85      ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
86          new HashMap<TableName, SpaceViolationPolicyEnforcement>(),
87          EMPTY_SNAPSHOT_MAP,
88          mock(RegionServerServices.class));
89      SpaceViolationPolicyEnforcement enforcement = ape.getPolicyEnforcement(
90          TableName.valueOf("nonexistent"));
91      assertFalse("Should not check bulkloads", enforcement.shouldCheckBulkLoads());
92    }
93  
94    @Test
95    public void testNoQuotaReturnsSingletonPolicyEnforcement() {
96      final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
97          EMPTY_ENFORCEMENT_MAP, EMPTY_SNAPSHOT_MAP, rss);
98      final TableName tableName = TableName.valueOf("my_table");
99      SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
100     // This should be the same exact instance, the singleton
101     assertTrue(policyEnforcement == MissingSnapshotViolationPolicyEnforcement.getInstance());
102     assertEquals(1, ape.getLocallyCachedPolicies().size());
103     Entry<TableName,SpaceViolationPolicyEnforcement> entry =
104         ape.getLocallyCachedPolicies().entrySet().iterator().next();
105     assertTrue(policyEnforcement == entry.getValue());
106   }
107 
108   @Test
109   public void testNonViolatingQuotaCachesPolicyEnforcment() {
110     final Map<TableName,SpaceQuotaSnapshot> snapshots = new HashMap<>();
111     final TableName tableName = TableName.valueOf("my_table");
112     snapshots.put(tableName, new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 0, 1024));
113     final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
114         EMPTY_ENFORCEMENT_MAP, snapshots, rss);
115     SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
116     assertTrue(
117         "Found the wrong class: " + policyEnforcement.getClass(),
118         policyEnforcement instanceof DefaultViolationPolicyEnforcement);
119     SpaceViolationPolicyEnforcement copy = ape.getPolicyEnforcement(tableName);
120     assertTrue("Expected the instance to be cached", policyEnforcement == copy);
121     Entry<TableName,SpaceViolationPolicyEnforcement> entry =
122         ape.getLocallyCachedPolicies().entrySet().iterator().next();
123     assertTrue(policyEnforcement == entry.getValue());
124   }
125 
126   @Test
127   public void testViolatingQuotaCachesNothing() {
128     final TableName tableName = TableName.valueOf("my_table");
129     SpaceViolationPolicyEnforcement policyEnforcement = mock(SpaceViolationPolicyEnforcement.class);
130     final Map<TableName,SpaceViolationPolicyEnforcement> activePolicies = new HashMap<>();
131     activePolicies.put(tableName, policyEnforcement);
132     final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
133         activePolicies, EMPTY_SNAPSHOT_MAP, rss);
134     assertTrue(ape.getPolicyEnforcement(tableName) == policyEnforcement);
135     assertEquals(0, ape.getLocallyCachedPolicies().size());
136   }
137 }