|
| 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 | + |
| 20 | +package org.apache.iotdb.db.storageengine.load.memory; |
| 21 | + |
| 22 | +import org.apache.iotdb.db.exception.load.LoadRuntimeOutOfMemoryException; |
| 23 | + |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.lang.reflect.Constructor; |
| 28 | +import java.lang.reflect.Field; |
| 29 | +import java.util.concurrent.atomic.AtomicLong; |
| 30 | + |
| 31 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 32 | +import static org.mockito.Mockito.doAnswer; |
| 33 | +import static org.mockito.Mockito.spy; |
| 34 | + |
| 35 | +public class LoadTsFileMemoryManagerTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testAllocateDataCacheMemoryBlockDoesNotDoubleCountMemory() throws Exception { |
| 39 | + final long allocatedMemoryInBytes = 2L * 1024 * 1024; |
| 40 | + final LoadTsFileMemoryManager manager = spy(newMemoryManager()); |
| 41 | + |
| 42 | + doAnswer( |
| 43 | + invocation -> { |
| 44 | + setUsedMemorySize(manager, allocatedMemoryInBytes); |
| 45 | + return allocatedMemoryInBytes; |
| 46 | + }) |
| 47 | + .when(manager) |
| 48 | + .tryAllocateFromQuery(anyLong()); |
| 49 | + |
| 50 | + manager.allocateDataCacheMemoryBlock(); |
| 51 | + |
| 52 | + Assert.assertEquals(allocatedMemoryInBytes, manager.getUsedMemorySizeInBytes()); |
| 53 | + Assert.assertNotNull(getDataCacheMemoryBlock(manager)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAllocateDataCacheMemoryBlockRollsBackPartialAllocationOnFailure() |
| 58 | + throws Exception { |
| 59 | + final long allocatedMemoryInBytes = 512L; |
| 60 | + final LoadTsFileMemoryManager manager = spy(newMemoryManager()); |
| 61 | + |
| 62 | + doAnswer( |
| 63 | + invocation -> { |
| 64 | + setUsedMemorySize(manager, allocatedMemoryInBytes); |
| 65 | + return allocatedMemoryInBytes; |
| 66 | + }) |
| 67 | + .when(manager) |
| 68 | + .tryAllocateFromQuery(anyLong()); |
| 69 | + doAnswer( |
| 70 | + invocation -> { |
| 71 | + setUsedMemorySize(manager, 0L); |
| 72 | + return null; |
| 73 | + }) |
| 74 | + .when(manager) |
| 75 | + .releaseToQuery(anyLong()); |
| 76 | + |
| 77 | + try { |
| 78 | + manager.allocateDataCacheMemoryBlock(); |
| 79 | + Assert.fail("Expected LoadRuntimeOutOfMemoryException"); |
| 80 | + } catch (LoadRuntimeOutOfMemoryException e) { |
| 81 | + Assert.assertEquals(0L, manager.getUsedMemorySizeInBytes()); |
| 82 | + Assert.assertNull(getDataCacheMemoryBlock(manager)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static LoadTsFileMemoryManager newMemoryManager() throws Exception { |
| 87 | + final Constructor<LoadTsFileMemoryManager> constructor = |
| 88 | + LoadTsFileMemoryManager.class.getDeclaredConstructor(); |
| 89 | + constructor.setAccessible(true); |
| 90 | + return constructor.newInstance(); |
| 91 | + } |
| 92 | + |
| 93 | + private static void setUsedMemorySize( |
| 94 | + final LoadTsFileMemoryManager manager, final long usedMemorySizeInBytes) throws Exception { |
| 95 | + final Field field = LoadTsFileMemoryManager.class.getDeclaredField("usedMemorySizeInBytes"); |
| 96 | + field.setAccessible(true); |
| 97 | + ((AtomicLong) field.get(manager)).set(usedMemorySizeInBytes); |
| 98 | + } |
| 99 | + |
| 100 | + private static LoadTsFileDataCacheMemoryBlock getDataCacheMemoryBlock( |
| 101 | + final LoadTsFileMemoryManager manager) throws Exception { |
| 102 | + final Field field = LoadTsFileMemoryManager.class.getDeclaredField("dataCacheMemoryBlock"); |
| 103 | + field.setAccessible(true); |
| 104 | + return (LoadTsFileDataCacheMemoryBlock) field.get(manager); |
| 105 | + } |
| 106 | +} |
0 commit comments