|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.ozone.container.common; |
| 20 | + |
| 21 | +import org.apache.hadoop.fs.FileSystemTestHelper; |
| 22 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 23 | +import org.apache.hadoop.ozone.OzoneConfigKeys; |
| 24 | +import org.apache.hadoop.ozone.container.common.utils.ContainerCache; |
| 25 | +import org.apache.hadoop.ozone.container.common.utils.ReferenceCountedDB; |
| 26 | +import org.apache.hadoop.utils.MetadataStore; |
| 27 | +import org.apache.hadoop.utils.MetadataStoreBuilder; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.rules.ExpectedException; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Test ContainerCache with evictions. |
| 38 | + */ |
| 39 | +public class TestContainerCache { |
| 40 | + private static String testRoot = new FileSystemTestHelper().getTestRootDir(); |
| 41 | + |
| 42 | + @Rule |
| 43 | + public ExpectedException thrown = ExpectedException.none(); |
| 44 | + |
| 45 | + private void createContainerDB(OzoneConfiguration conf, File dbFile) |
| 46 | + throws Exception { |
| 47 | + MetadataStore store = MetadataStoreBuilder.newBuilder().setConf(conf) |
| 48 | + .setCreateIfMissing(true).setDbFile(dbFile).build(); |
| 49 | + |
| 50 | + // we close since the SCM pre-creates containers. |
| 51 | + // we will open and put Db handle into a cache when keys are being created |
| 52 | + // in a container. |
| 53 | + |
| 54 | + store.close(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testContainerCacheEviction() throws Exception { |
| 59 | + File root = new File(testRoot); |
| 60 | + root.mkdirs(); |
| 61 | + |
| 62 | + OzoneConfiguration conf = new OzoneConfiguration(); |
| 63 | + conf.setInt(OzoneConfigKeys.OZONE_CONTAINER_CACHE_SIZE, 2); |
| 64 | + |
| 65 | + ContainerCache cache = ContainerCache.getInstance(conf); |
| 66 | + File containerDir1 = new File(root, "cont1"); |
| 67 | + File containerDir2 = new File(root, "cont2"); |
| 68 | + File containerDir3 = new File(root, "cont3"); |
| 69 | + File containerDir4 = new File(root, "cont4"); |
| 70 | + |
| 71 | + |
| 72 | + createContainerDB(conf, containerDir1); |
| 73 | + createContainerDB(conf, containerDir2); |
| 74 | + createContainerDB(conf, containerDir3); |
| 75 | + createContainerDB(conf, containerDir4); |
| 76 | + |
| 77 | + // Get 2 references out of the same db and verify the objects are same. |
| 78 | + ReferenceCountedDB db1 = cache.getDB(1, "RocksDB", |
| 79 | + containerDir1.getPath(), conf); |
| 80 | + Assert.assertEquals(1, db1.getReferenceCount()); |
| 81 | + ReferenceCountedDB db2 = cache.getDB(1, "RocksDB", |
| 82 | + containerDir1.getPath(), conf); |
| 83 | + Assert.assertEquals(2, db2.getReferenceCount()); |
| 84 | + Assert.assertEquals(2, db1.getReferenceCount()); |
| 85 | + Assert.assertEquals(db1, db2); |
| 86 | + |
| 87 | + // add one more references to ContainerCache. |
| 88 | + ReferenceCountedDB db3 = cache.getDB(2, "RocksDB", |
| 89 | + containerDir2.getPath(), conf); |
| 90 | + Assert.assertEquals(1, db3.getReferenceCount()); |
| 91 | + |
| 92 | + // and close the reference |
| 93 | + db3.close(); |
| 94 | + Assert.assertEquals(0, db3.getReferenceCount()); |
| 95 | + |
| 96 | + Assert.assertTrue(cache.isFull()); |
| 97 | + |
| 98 | + // add one more reference to ContainerCache and verify that it will not |
| 99 | + // evict the least recent entry as it has reference. |
| 100 | + ReferenceCountedDB db4 = cache.getDB(3, "RocksDB", |
| 101 | + containerDir3.getPath(), conf); |
| 102 | + Assert.assertEquals(1, db4.getReferenceCount()); |
| 103 | + |
| 104 | + Assert.assertEquals(2, cache.size()); |
| 105 | + Assert.assertNotNull(cache.get(containerDir1.getPath())); |
| 106 | + Assert.assertNull(cache.get(containerDir2.getPath())); |
| 107 | + |
| 108 | + // Now close both the references for container1 |
| 109 | + db1.close(); |
| 110 | + db2.close(); |
| 111 | + Assert.assertEquals(0, db1.getReferenceCount()); |
| 112 | + Assert.assertEquals(0, db2.getReferenceCount()); |
| 113 | + |
| 114 | + |
| 115 | + // The reference count for container1 is 0 but it is not evicted. |
| 116 | + ReferenceCountedDB db5 = cache.getDB(1, "RocksDB", |
| 117 | + containerDir1.getPath(), conf); |
| 118 | + Assert.assertEquals(1, db5.getReferenceCount()); |
| 119 | + Assert.assertEquals(db1, db5); |
| 120 | + db5.close(); |
| 121 | + db4.close(); |
| 122 | + |
| 123 | + |
| 124 | + // Decrementing reference count below zero should fail. |
| 125 | + thrown.expect(IllegalArgumentException.class); |
| 126 | + db5.close(); |
| 127 | + } |
| 128 | +} |
0 commit comments