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.io.hfile;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.stream.Stream;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
029import org.apache.hadoop.hbase.conf.ConfigurationManager;
030import org.apache.hadoop.hbase.io.ByteBuffAllocator;
031import org.apache.hadoop.hbase.testclassification.IOTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.TestTemplate;
037import org.junit.jupiter.params.provider.Arguments;
038
039/**
040 * Tests that {@link CacheConfig#onConfigurationChange(Configuration)} keeps the column family
041 * settings applied at construction time.
042 */
043@Tag(IOTests.TAG)
044@Tag(SmallTests.TAG)
045@HBaseParameterizedTestTemplate(
046    name = "{index}: withFamily={0}, blockCache={1}, cacheDataOnWrite={2}, evictOnClose={3}")
047public class TestCacheConfigOnConfigurationChange {
048
049  private static final boolean[] BOOLEANS = { false, true };
050
051  private final boolean withFamily;
052  private final boolean blockCacheEnabled;
053  private final boolean familyCacheDataOnWrite;
054  private final boolean familyEvictOnClose;
055
056  private ConfigurationManager manager;
057  private CacheConfig[] cacheConfigs;
058
059  public static Stream<Arguments> parameters() {
060    List<Arguments> params = new ArrayList<>();
061    // Without a column family the block cache is enabled and there are no family settings.
062    params.add(Arguments.of(false, true, false, false));
063    for (boolean blockCacheEnabled : BOOLEANS) {
064      for (boolean cacheDataOnWrite : BOOLEANS) {
065        for (boolean evictOnClose : BOOLEANS) {
066          params.add(Arguments.of(true, blockCacheEnabled, cacheDataOnWrite, evictOnClose));
067        }
068      }
069    }
070    return params.stream();
071  }
072
073  public TestCacheConfigOnConfigurationChange(boolean withFamily, boolean blockCacheEnabled,
074    boolean familyCacheDataOnWrite, boolean familyEvictOnClose) {
075    this.withFamily = withFamily;
076    this.blockCacheEnabled = blockCacheEnabled;
077    this.familyCacheDataOnWrite = familyCacheDataOnWrite;
078    this.familyEvictOnClose = familyEvictOnClose;
079  }
080
081  @BeforeEach
082  public void setUp() {
083    ColumnFamilyDescriptor family = withFamily
084      ? ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf"))
085        .setBlockCacheEnabled(blockCacheEnabled).setCacheDataOnWrite(familyCacheDataOnWrite)
086        .setEvictBlocksOnClose(familyEvictOnClose).build()
087      : null;
088    CacheConfig original =
089      new CacheConfig(new Configuration(false), family, null, ByteBuffAllocator.HEAP);
090    // A copy must carry the column family settings as well.
091    cacheConfigs = new CacheConfig[] { original, new CacheConfig(original) };
092    manager = new ConfigurationManager();
093    for (CacheConfig cacheConfig : cacheConfigs) {
094      manager.registerObserver(cacheConfig);
095    }
096  }
097
098  @TestTemplate
099  public void testReloadUnchangedConfiguration() {
100    assertCacheSettings(CacheConfig.DEFAULT_CACHE_DATA_ON_READ,
101      CacheConfig.DEFAULT_CACHE_DATA_ON_WRITE, CacheConfig.DEFAULT_EVICT_ON_CLOSE);
102    manager.notifyAllObservers(new Configuration(false));
103    assertCacheSettings(CacheConfig.DEFAULT_CACHE_DATA_ON_READ,
104      CacheConfig.DEFAULT_CACHE_DATA_ON_WRITE, CacheConfig.DEFAULT_EVICT_ON_CLOSE);
105  }
106
107  @TestTemplate
108  public void testReloadChangedConfiguration() {
109    // Every global setting is enabled and disabled independently of the other two.
110    for (boolean cacheDataOnRead : BOOLEANS) {
111      for (boolean cacheDataOnWrite : BOOLEANS) {
112        for (boolean evictOnClose : BOOLEANS) {
113          Configuration conf = new Configuration(false);
114          conf.setBoolean(CacheConfig.CACHE_DATA_ON_READ_KEY, cacheDataOnRead);
115          conf.setBoolean(CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY, cacheDataOnWrite);
116          conf.setBoolean(CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY, evictOnClose);
117          manager.notifyAllObservers(conf);
118          assertCacheSettings(cacheDataOnRead, cacheDataOnWrite, evictOnClose);
119        }
120      }
121    }
122  }
123
124  private void assertCacheSettings(boolean globalCacheDataOnRead, boolean globalCacheDataOnWrite,
125    boolean globalEvictOnClose) {
126    for (CacheConfig cacheConfig : cacheConfigs) {
127      assertEquals(blockCacheEnabled && globalCacheDataOnRead, cacheConfig.shouldCacheDataOnRead());
128      assertEquals(blockCacheEnabled && (globalCacheDataOnWrite || familyCacheDataOnWrite),
129        cacheConfig.shouldCacheDataOnWrite());
130      assertEquals(blockCacheEnabled && (globalEvictOnClose || familyEvictOnClose),
131        cacheConfig.shouldEvictOnClose());
132    }
133  }
134}