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(new HashMap<>(),
072      Collections.emptyMap(), mock(RegionServerServices.class));
073    SpaceViolationPolicyEnforcement enforcement =
074      ape.getPolicyEnforcement(TableName.valueOf("nonexistent"));
075    assertNotNull(enforcement);
076    assertTrue("Expected an instance of MissingSnapshotViolationPolicyEnforcement, but got "
077      + enforcement.getClass(), enforcement instanceof MissingSnapshotViolationPolicyEnforcement);
078  }
079
080  @Test
081  public void testNoBulkLoadChecksOnNoSnapshot() {
082    ActivePolicyEnforcement ape =
083      new ActivePolicyEnforcement(new HashMap<TableName, SpaceViolationPolicyEnforcement>(),
084        Collections.<TableName, SpaceQuotaSnapshot> emptyMap(), mock(RegionServerServices.class));
085    SpaceViolationPolicyEnforcement enforcement =
086      ape.getPolicyEnforcement(TableName.valueOf("nonexistent"));
087    assertFalse("Should not check bulkloads", enforcement.shouldCheckBulkLoads());
088  }
089
090  @Test
091  public void testNoQuotaReturnsSingletonPolicyEnforcement() {
092    final ActivePolicyEnforcement ape =
093      new ActivePolicyEnforcement(Collections.emptyMap(), Collections.emptyMap(), rss);
094    final TableName tableName = TableName.valueOf("my_table");
095    SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
096    // This should be the same exact instance, the singleton
097    assertTrue(policyEnforcement == MissingSnapshotViolationPolicyEnforcement.getInstance());
098    assertEquals(1, ape.getLocallyCachedPolicies().size());
099    Entry<TableName, SpaceViolationPolicyEnforcement> entry =
100      ape.getLocallyCachedPolicies().entrySet().iterator().next();
101    assertTrue(policyEnforcement == entry.getValue());
102  }
103
104  @Test
105  public void testNonViolatingQuotaCachesPolicyEnforcment() {
106    final Map<TableName, SpaceQuotaSnapshot> snapshots = new HashMap<>();
107    final TableName tableName = TableName.valueOf("my_table");
108    snapshots.put(tableName, new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 0, 1024));
109    final ActivePolicyEnforcement ape =
110      new ActivePolicyEnforcement(Collections.emptyMap(), snapshots, rss);
111    SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
112    assertTrue("Found the wrong class: " + policyEnforcement.getClass(),
113      policyEnforcement instanceof DefaultViolationPolicyEnforcement);
114    SpaceViolationPolicyEnforcement copy = ape.getPolicyEnforcement(tableName);
115    assertTrue("Expected the instance to be cached", policyEnforcement == copy);
116    Entry<TableName, SpaceViolationPolicyEnforcement> entry =
117      ape.getLocallyCachedPolicies().entrySet().iterator().next();
118    assertTrue(policyEnforcement == entry.getValue());
119  }
120
121  @Test
122  public void testViolatingQuotaCachesNothing() {
123    final TableName tableName = TableName.valueOf("my_table");
124    SpaceViolationPolicyEnforcement policyEnforcement = mock(SpaceViolationPolicyEnforcement.class);
125    final Map<TableName, SpaceViolationPolicyEnforcement> activePolicies = new HashMap<>();
126    activePolicies.put(tableName, policyEnforcement);
127    final ActivePolicyEnforcement ape =
128      new ActivePolicyEnforcement(activePolicies, Collections.emptyMap(), rss);
129    assertTrue(ape.getPolicyEnforcement(tableName) == policyEnforcement);
130    assertEquals(0, ape.getLocallyCachedPolicies().size());
131  }
132}