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.assertNotNull;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.Mockito.mock;
025
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.Map.Entry;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
033import org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement;
034import org.apache.hadoop.hbase.quotas.policies.MissingSnapshotViolationPolicyEnforcement;
035import org.apache.hadoop.hbase.quotas.policies.NoWritesViolationPolicyEnforcement;
036import org.apache.hadoop.hbase.regionserver.RegionServerServices;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043/**
044 * Test class for {@link ActivePolicyEnforcement}.
045 */
046@Category(SmallTests.class)
047public class TestActivePolicyEnforcement {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestActivePolicyEnforcement.class);
052
053  private RegionServerServices rss;
054
055  @Before
056  public void setup() {
057    rss = mock(RegionServerServices.class);
058  }
059
060  @Test
061  public void testGetter() {
062    final TableName tableName = TableName.valueOf("table");
063    Map<TableName, SpaceViolationPolicyEnforcement> map = new HashMap<>();
064    map.put(tableName, new NoWritesViolationPolicyEnforcement());
065    ActivePolicyEnforcement ape = new ActivePolicyEnforcement(map, Collections.emptyMap(), null);
066    assertEquals(map.get(tableName), ape.getPolicyEnforcement(tableName));
067  }
068
069  @Test
070  public void testNoPolicyReturnsNoopEnforcement() {
071    ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
072        new HashMap<>(), Collections.emptyMap(), mock(RegionServerServices.class));
073    SpaceViolationPolicyEnforcement enforcement = ape.getPolicyEnforcement(
074        TableName.valueOf("nonexistent"));
075    assertNotNull(enforcement);
076    assertTrue(
077        "Expected an instance of MissingSnapshotViolationPolicyEnforcement, but got "
078            + enforcement.getClass(),
079        enforcement instanceof MissingSnapshotViolationPolicyEnforcement);
080  }
081
082  @Test
083  public void testNoBulkLoadChecksOnNoSnapshot() {
084    ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
085        new HashMap<TableName, SpaceViolationPolicyEnforcement>(),
086        Collections.<TableName,SpaceQuotaSnapshot> emptyMap(),
087        mock(RegionServerServices.class));
088    SpaceViolationPolicyEnforcement enforcement = ape.getPolicyEnforcement(
089        TableName.valueOf("nonexistent"));
090    assertFalse("Should not check bulkloads", enforcement.shouldCheckBulkLoads());
091  }
092
093  @Test
094  public void testNoQuotaReturnsSingletonPolicyEnforcement() {
095    final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
096        Collections.emptyMap(), Collections.emptyMap(), rss);
097    final TableName tableName = TableName.valueOf("my_table");
098    SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
099    // This should be the same exact instance, the singleton
100    assertTrue(policyEnforcement == MissingSnapshotViolationPolicyEnforcement.getInstance());
101    assertEquals(1, ape.getLocallyCachedPolicies().size());
102    Entry<TableName,SpaceViolationPolicyEnforcement> entry =
103        ape.getLocallyCachedPolicies().entrySet().iterator().next();
104    assertTrue(policyEnforcement == entry.getValue());
105  }
106
107  @Test
108  public void testNonViolatingQuotaCachesPolicyEnforcment() {
109    final Map<TableName,SpaceQuotaSnapshot> snapshots = new HashMap<>();
110    final TableName tableName = TableName.valueOf("my_table");
111    snapshots.put(tableName, new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 0, 1024));
112    final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
113        Collections.emptyMap(), snapshots, rss);
114    SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
115    assertTrue(
116        "Found the wrong class: " + policyEnforcement.getClass(),
117        policyEnforcement instanceof DefaultViolationPolicyEnforcement);
118    SpaceViolationPolicyEnforcement copy = ape.getPolicyEnforcement(tableName);
119    assertTrue("Expected the instance to be cached", policyEnforcement == copy);
120    Entry<TableName,SpaceViolationPolicyEnforcement> entry =
121        ape.getLocallyCachedPolicies().entrySet().iterator().next();
122    assertTrue(policyEnforcement == entry.getValue());
123  }
124
125  @Test
126  public void testViolatingQuotaCachesNothing() {
127    final TableName tableName = TableName.valueOf("my_table");
128    SpaceViolationPolicyEnforcement policyEnforcement = mock(SpaceViolationPolicyEnforcement.class);
129    final Map<TableName,SpaceViolationPolicyEnforcement> activePolicies = new HashMap<>();
130    activePolicies.put(tableName, policyEnforcement);
131    final ActivePolicyEnforcement ape = new ActivePolicyEnforcement(
132        activePolicies, Collections.emptyMap(), rss);
133    assertTrue(ape.getPolicyEnforcement(tableName) == policyEnforcement);
134    assertEquals(0, ape.getLocallyCachedPolicies().size());
135  }
136}