[ https://issues.apache.org/jira/browse/DRILL-4134?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15032808#comment-15032808 ]
ASF GitHub Bot commented on DRILL-4134: --------------------------------------- Github user julienledem commented on a diff in the pull request: https://github.com/apache/drill/pull/283#discussion_r46225897 --- Diff: exec/memory/base/src/main/java/org/apache/drill/exec/memory/AllocationReservation.java --- @@ -0,0 +1,155 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill.exec.memory; + +import io.netty.buffer.DrillBuf; + +import com.google.common.base.Preconditions; + +/** + * Supports cumulative allocation reservation. Clients may increase the size of the reservation repeatedly until they + * call for an allocation of the current total size. The reservation can only be used once, and will throw an exception + * if it is used more than once. + * <p> + * For the purposes of airtight memory accounting, the reservation must be close()d whether it is used or not. + * This is not threadsafe. + */ +public abstract class AllocationReservation implements AutoCloseable { + private int nBytes = 0; + private boolean used = false; + private boolean closed = false; + + /** + * Constructor. Prevent construction except by derived classes. + * <p>The expectation is that the derived class will be a non-static inner + * class in an allocator. + */ + AllocationReservation() { + } + + /** + * Add to the current reservation. + * + * <p>Adding may fail if the allocator is not allowed to consume any more space. + * + * @param nBytes the number of bytes to add + * @return true if the addition is possible, false otherwise + * @throws IllegalStateException if called after buffer() is used to allocate the reservation + */ + public boolean add(final int nBytes) { + Preconditions.checkArgument(nBytes >= 0, "nBytes(%d) < 0", nBytes); + Preconditions.checkState(!closed, "Attempt to increase reservation after reservation has been closed"); + Preconditions.checkState(!used, "Attempt to increase reservation after reservation has been used"); + + // we round up to next power of two since all reservations are done in powers of two. This may overestimate the + // preallocation since someone may perceive additions to be power of two. If this becomes a problem, we can look at + // modifying this behavior so that we maintain what we reserve and what the user asked for and make sure to only + // round to power of two as necessary. + final int nBytesTwo = BaseAllocator.nextPowerOfTwo(nBytes); + if (!reserve(nBytesTwo)) { + return false; + } + + this.nBytes += nBytesTwo; + return true; + } + + /** + * Requests a reservation of additional space. + * + * <p>The implementation of the allocator's inner class provides this. + * + * @param nBytes the amount to reserve + * @return true if the reservation can be satisfied, false otherwise + */ + abstract boolean reserve(int nBytes); + + /** + * Allocate a buffer whose size is the total of all the add()s made. + * + * <p>The allocation request can still fail, even if the amount of space + * requested is available, if the allocation cannot be made contiguously. + * + * @return the buffer, or null, if the request cannot be satisfied + * @throws IllegalStateException if called called more than once + */ + public DrillBuf buffer() { --- End diff -- methods should be a verb. How about allocateBuffer() ? > Incorporate remaining patches from DRILL-1942 Allocator refactor > ---------------------------------------------------------------- > > Key: DRILL-4134 > URL: https://issues.apache.org/jira/browse/DRILL-4134 > Project: Apache Drill > Issue Type: Sub-task > Components: Execution - Flow > Reporter: Jacques Nadeau > Assignee: Jacques Nadeau > Fix For: 1.4.0 > > -- This message was sent by Atlassian JIRA (v6.3.4#6332)