|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iotdb.db.schemaengine.schemaregion.tag; |
| 20 | + |
| 21 | +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; |
| 22 | +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; |
| 23 | +import org.apache.iotdb.db.schemaengine.rescon.MemSchemaEngineStatistics; |
| 24 | +import org.apache.iotdb.db.schemaengine.rescon.MemSchemaRegionStatistics; |
| 25 | +import org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.mem.mnode.IMemMNode; |
| 26 | +import org.apache.iotdb.db.schemaengine.schemaregion.mtree.loader.MNodeFactoryLoader; |
| 27 | + |
| 28 | +import org.apache.tsfile.enums.TSDataType; |
| 29 | +import org.apache.tsfile.external.commons.io.FileUtils; |
| 30 | +import org.apache.tsfile.file.metadata.enums.CompressionType; |
| 31 | +import org.apache.tsfile.file.metadata.enums.TSEncoding; |
| 32 | +import org.apache.tsfile.utils.RamUsageEstimator; |
| 33 | +import org.apache.tsfile.write.schema.MeasurementSchema; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
| 42 | +import java.util.concurrent.CountDownLatch; |
| 43 | +import java.util.concurrent.ExecutorService; |
| 44 | +import java.util.concurrent.Executors; |
| 45 | +import java.util.concurrent.Future; |
| 46 | +import java.util.concurrent.TimeUnit; |
| 47 | + |
| 48 | +public class TagManagerTest { |
| 49 | + |
| 50 | + private File tempDir; |
| 51 | + private MemSchemaRegionStatistics regionStatistics; |
| 52 | + private TagManager tagManager; |
| 53 | + |
| 54 | + @After |
| 55 | + public void tearDown() throws Exception { |
| 56 | + if (tagManager != null) { |
| 57 | + tagManager.clear(); |
| 58 | + } |
| 59 | + if (regionStatistics != null) { |
| 60 | + regionStatistics.clear(); |
| 61 | + } |
| 62 | + if (tempDir != null) { |
| 63 | + FileUtils.deleteDirectory(tempDir); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void removeIndexIgnoresMissingEntriesAndReleasesOnlyExistingMemory() throws Exception { |
| 69 | + initTagManager(); |
| 70 | + final IMeasurementMNode<?> node = newMeasurementMNode("s0"); |
| 71 | + |
| 72 | + tagManager.removeIndex("missingKey", "missingValue", node); |
| 73 | + Assert.assertEquals(0, regionStatistics.getRegionMemoryUsage()); |
| 74 | + |
| 75 | + tagManager.addIndex("key", "value", node); |
| 76 | + final long expectedMemory = indexMemory("key", "value", 1); |
| 77 | + Assert.assertEquals(expectedMemory, regionStatistics.getRegionMemoryUsage()); |
| 78 | + |
| 79 | + tagManager.removeIndex("key", "missingValue", node); |
| 80 | + Assert.assertEquals(expectedMemory, regionStatistics.getRegionMemoryUsage()); |
| 81 | + |
| 82 | + tagManager.removeIndex("key", "value", newMeasurementMNode("other")); |
| 83 | + Assert.assertEquals(expectedMemory, regionStatistics.getRegionMemoryUsage()); |
| 84 | + |
| 85 | + tagManager.removeIndex("key", "value", node); |
| 86 | + Assert.assertEquals(0, regionStatistics.getRegionMemoryUsage()); |
| 87 | + |
| 88 | + tagManager.removeIndex("key", "value", node); |
| 89 | + Assert.assertEquals(0, regionStatistics.getRegionMemoryUsage()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void concurrentAddIndexRequestsMemoryForActualInsertionsOnly() throws Exception { |
| 94 | + initTagManager(); |
| 95 | + final String tagKey = "key"; |
| 96 | + final String tagValue = "value"; |
| 97 | + final int measurementCount = 128; |
| 98 | + final List<IMeasurementMNode<?>> nodes = new ArrayList<>(); |
| 99 | + for (int i = 0; i < measurementCount; i++) { |
| 100 | + nodes.add(newMeasurementMNode("s" + i)); |
| 101 | + } |
| 102 | + |
| 103 | + final int workerCount = 16; |
| 104 | + final ExecutorService executorService = Executors.newFixedThreadPool(workerCount); |
| 105 | + final CountDownLatch readyLatch = new CountDownLatch(workerCount); |
| 106 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 107 | + final List<Future<?>> futures = new ArrayList<>(); |
| 108 | + for (final IMeasurementMNode<?> node : nodes) { |
| 109 | + futures.add( |
| 110 | + executorService.submit( |
| 111 | + () -> { |
| 112 | + readyLatch.countDown(); |
| 113 | + startLatch.await(); |
| 114 | + tagManager.addIndex(tagKey, tagValue, node); |
| 115 | + return null; |
| 116 | + })); |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + Assert.assertTrue(readyLatch.await(10, TimeUnit.SECONDS)); |
| 121 | + startLatch.countDown(); |
| 122 | + for (final Future<?> future : futures) { |
| 123 | + future.get(10, TimeUnit.SECONDS); |
| 124 | + } |
| 125 | + } finally { |
| 126 | + executorService.shutdownNow(); |
| 127 | + } |
| 128 | + Assert.assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS)); |
| 129 | + |
| 130 | + final long expectedMemory = indexMemory(tagKey, tagValue, measurementCount); |
| 131 | + Assert.assertEquals(expectedMemory, regionStatistics.getRegionMemoryUsage()); |
| 132 | + |
| 133 | + for (final IMeasurementMNode<?> node : nodes) { |
| 134 | + tagManager.addIndex(tagKey, tagValue, node); |
| 135 | + } |
| 136 | + Assert.assertEquals(expectedMemory, regionStatistics.getRegionMemoryUsage()); |
| 137 | + |
| 138 | + for (final IMeasurementMNode<?> node : nodes) { |
| 139 | + tagManager.removeIndex(tagKey, tagValue, node); |
| 140 | + } |
| 141 | + Assert.assertEquals(0, regionStatistics.getRegionMemoryUsage()); |
| 142 | + } |
| 143 | + |
| 144 | + private void initTagManager() throws Exception { |
| 145 | + tempDir = Files.createTempDirectory("tag-manager").toFile(); |
| 146 | + regionStatistics = new MemSchemaRegionStatistics(0, new MemSchemaEngineStatistics()); |
| 147 | + tagManager = new TagManager(tempDir.getAbsolutePath(), regionStatistics); |
| 148 | + } |
| 149 | + |
| 150 | + private static IMeasurementMNode<IMemMNode> newMeasurementMNode(final String measurement) { |
| 151 | + final IMNodeFactory<IMemMNode> nodeFactory = |
| 152 | + MNodeFactoryLoader.getInstance().getMemMNodeIMNodeFactory(); |
| 153 | + return nodeFactory.createMeasurementMNode( |
| 154 | + null, |
| 155 | + measurement, |
| 156 | + new MeasurementSchema( |
| 157 | + measurement, TSDataType.INT64, TSEncoding.PLAIN, CompressionType.SNAPPY), |
| 158 | + null); |
| 159 | + } |
| 160 | + |
| 161 | + private static long indexMemory( |
| 162 | + final String tagKey, final String tagValue, final int measurementCount) { |
| 163 | + return RamUsageEstimator.sizeOf(tagKey) |
| 164 | + + 4 |
| 165 | + + RamUsageEstimator.sizeOf(tagValue) |
| 166 | + + 4 |
| 167 | + + (RamUsageEstimator.NUM_BYTES_OBJECT_REF + 4) * measurementCount; |
| 168 | + } |
| 169 | +} |
0 commit comments