|
| 1 | +/* |
| 2 | + * Copyright 2021 DataCanvas |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.dingodb.common.memory; |
| 18 | + |
| 19 | +import com.google.common.util.concurrent.ListenableFuture; |
| 20 | +import com.google.common.util.concurrent.SettableFuture; |
| 21 | +import io.dingodb.common.log.LogUtils; |
| 22 | +import io.dingodb.common.mysql.scope.ScopeVariables; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | + |
| 25 | +import javax.annotation.concurrent.GuardedBy; |
| 26 | + |
| 27 | +import static com.google.common.base.Preconditions.checkState; |
| 28 | + |
| 29 | +@Slf4j |
| 30 | +public abstract class BlockingMemoryPool extends MemoryPool { |
| 31 | + @GuardedBy("this") |
| 32 | + private long tryMinRequestSize = 0; |
| 33 | + |
| 34 | + @GuardedBy("this") |
| 35 | + private long minRequestSize = 0; |
| 36 | + |
| 37 | + private long maxRequestSize = 0; |
| 38 | + |
| 39 | + @GuardedBy("this") |
| 40 | + private SettableFuture<?> settableFuture; |
| 41 | + |
| 42 | + @GuardedBy("this") |
| 43 | + private SettableFuture<?> trySettableFuture; |
| 44 | + |
| 45 | + @GuardedBy("this") |
| 46 | + private boolean needMemoryRevoking = false; |
| 47 | + |
| 48 | + protected long maxElasticMemory; |
| 49 | + |
| 50 | + protected boolean blockFlag; |
| 51 | + |
| 52 | + public BlockingMemoryPool(String name, long maxLimit, MemoryPool parent, MemoryType memoryType) { |
| 53 | + super(name, maxLimit, parent, memoryType); |
| 54 | + setMaxElasticMemory(ScopeVariables.getMemoryRevokingThreshold()); |
| 55 | + } |
| 56 | + |
| 57 | + public void setMaxElasticMemory(double maxElasticThreshold) { |
| 58 | + this.maxElasticMemory = (long) (this.maxLimit * maxElasticThreshold); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setMaxLimit(long memoryLimit) { |
| 63 | + this.maxLimit = memoryLimit; |
| 64 | + setMaxElasticMemory(ScopeVariables.getMemoryRevokingThreshold()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected ListenableFuture<?> block(ListenableFuture<?> parent, long size) { |
| 69 | + if (parent != null && !parent.isDone()) { |
| 70 | + if (inheritParentFuture()) { |
| 71 | + return parent; |
| 72 | + } else { |
| 73 | + return NOT_BLOCKED; |
| 74 | + } |
| 75 | + } else { |
| 76 | + if (this.getMemoryType() == MemoryType.GLOBAL) { |
| 77 | + long maxRequestSizeTmp = reservedBytes + revocableBytes; |
| 78 | + if (maxRequestSizeTmp > maxRequestSize) { |
| 79 | + maxRequestSize = maxRequestSizeTmp; |
| 80 | + } |
| 81 | + } |
| 82 | + if (reservedBytes + revocableBytes > maxElasticMemory && ScopeVariables.enableSpill()) { |
| 83 | + log.info("The query use much more memory for the memory pool: " + name); |
| 84 | + if (minRequestSize <= 0 || minRequestSize > size) { |
| 85 | + minRequestSize = size; |
| 86 | + } |
| 87 | + if (revocableBytes >= minRequestSize) { |
| 88 | + if (settableFuture == null || settableFuture.isDone()) { |
| 89 | + settableFuture = SettableFuture.create(); |
| 90 | + this.blockFlag = true; |
| 91 | + } |
| 92 | + checkState(!settableFuture.isDone(), "future is already completed"); |
| 93 | + this.needMemoryRevoking = true; |
| 94 | + requestMemoryRevoke(); |
| 95 | + return settableFuture; |
| 96 | + } else { |
| 97 | + return NOT_BLOCKED; |
| 98 | + } |
| 99 | + } else { |
| 100 | + return NOT_BLOCKED; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected void tryBlock(MemoryAllocateFuture allocFuture, long size, boolean reserved) { |
| 107 | + if (size > maxElasticMemory) { |
| 108 | + outOfMemory(fullName, getMemoryUsage(), size, maxElasticMemory, reserved); |
| 109 | + } |
| 110 | + |
| 111 | + if (tryMinRequestSize <= 0 || tryMinRequestSize > size) { |
| 112 | + tryMinRequestSize = size; |
| 113 | + } |
| 114 | + |
| 115 | + if ((revocableBytes >= tryMinRequestSize || reserved) && ScopeVariables.enableSpill()) { |
| 116 | + log.info("The query use much more memory for the memory pool: " + name); |
| 117 | + if (trySettableFuture == null || trySettableFuture.isDone()) { |
| 118 | + trySettableFuture = SettableFuture.create(); |
| 119 | + this.blockFlag = true; |
| 120 | + } |
| 121 | + allocFuture.setAllocateFuture(trySettableFuture); |
| 122 | + this.needMemoryRevoking = true; |
| 123 | + requestMemoryRevoke(); |
| 124 | + } else { |
| 125 | + outOfMemory(fullName, getMemoryUsage(), size, maxElasticMemory, reserved); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected synchronized void notifyBlockedQuery() { |
| 131 | + if (settableFuture == null && trySettableFuture == null) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + long availableBytes = maxElasticMemory - reservedBytes - revocableBytes; |
| 136 | + if (availableBytes >= minRequestSize && settableFuture != null && !settableFuture.isDone()) { |
| 137 | + minRequestSize = 0; |
| 138 | + LogUtils.info(log, "block settable set null"); |
| 139 | + settableFuture.set(null); |
| 140 | + } |
| 141 | + if (availableBytes >= tryMinRequestSize && trySettableFuture != null && !trySettableFuture.isDone()) { |
| 142 | + tryMinRequestSize = 0; |
| 143 | + trySettableFuture.set(null); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected long getTryMaxLimit() { |
| 149 | + return maxElasticMemory; |
| 150 | + } |
| 151 | + |
| 152 | + protected boolean inheritParentFuture() { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void destroy() { |
| 158 | + if (settableFuture != null) { |
| 159 | + settableFuture.set(null); |
| 160 | + } |
| 161 | + |
| 162 | + if (trySettableFuture != null) { |
| 163 | + trySettableFuture.set(null); |
| 164 | + } |
| 165 | + super.destroy(); |
| 166 | + } |
| 167 | + |
| 168 | + public synchronized boolean isNeedMemoryRevoking() { |
| 169 | + return needMemoryRevoking; |
| 170 | + } |
| 171 | + |
| 172 | + public synchronized void resetNeedMemoryRevoking() { |
| 173 | + this.needMemoryRevoking = false; |
| 174 | + } |
| 175 | + |
| 176 | + public ListenableFuture<?> getBlockedFuture() { |
| 177 | + return settableFuture; |
| 178 | + } |
| 179 | + |
| 180 | + public SettableFuture<?> getTrySettableFuture() { |
| 181 | + return trySettableFuture; |
| 182 | + } |
| 183 | + |
| 184 | + public long getMinRequestMemory() { |
| 185 | + return tryMinRequestSize + minRequestSize; |
| 186 | + } |
| 187 | + |
| 188 | + public boolean isBlockFlag() { |
| 189 | + return blockFlag; |
| 190 | + } |
| 191 | +} |
0 commit comments