View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile.bucket;
20  
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
26  import org.apache.hadoop.hbase.nio.ByteBuff;
27  import org.apache.hadoop.hbase.nio.SingleByteBuff;
28  import org.apache.hadoop.hbase.util.ByteBufferArray;
29  import org.apache.hadoop.hbase.util.Pair;
30  
31  /**
32   * IO engine that stores data in memory using an array of ByteBuffers
33   * {@link ByteBufferArray}
34   */
35  @InterfaceAudience.Private
36  public class ByteBufferIOEngine implements IOEngine {
37    private ByteBufferArray bufferArray;
38    private final long capacity;
39    private final boolean direct;
40  
41    /**
42     * Construct the ByteBufferIOEngine with the given capacity
43     * @param capacity
44     * @param direct true if allocate direct buffer
45     * @throws IOException
46     */
47    public ByteBufferIOEngine(long capacity, boolean direct)
48        throws IOException {
49      this.capacity = capacity;
50      this.direct = direct;
51      bufferArray = new ByteBufferArray(capacity, direct);
52    }
53  
54    @Override
55    public String toString() {
56      return "ioengine=" + this.getClass().getSimpleName() + ", capacity=" +
57        String.format("%,d", this.capacity) + ", direct=" + this.direct;
58    }
59  
60    /**
61     * Memory IO engine is always unable to support persistent storage for the
62     * cache
63     * @return false
64     */
65    @Override
66    public boolean isPersistent() {
67      return false;
68    }
69  
70    @Override
71    public Pair<ByteBuff, MemoryType> read(long offset, int length) throws IOException {
72      // TODO : this allocate and copy will go away once we create BB backed cells
73      ByteBuffer dstBuffer = ByteBuffer.allocate(length);
74      bufferArray.getMultiple(offset, dstBuffer.remaining(), dstBuffer.array(),
75        dstBuffer.arrayOffset());
76      // Here the buffer that is created directly refers to the buffer in the actual buckets.
77      // When any cell is referring to the blocks created out of these buckets then it means that
78      // those cells are referring to a shared memory area which if evicted by the BucketCache would
79      // lead to corruption of results. Hence we set the type of the buffer as SHARED_MEMORY
80      // so that the readers using this block are aware of this fact and do the necessary action
81      // to prevent eviction till the results are either consumed or copied
82      if (dstBuffer.limit() != length) {
83        throw new RuntimeException("Only " + dstBuffer.limit() + " bytes read, " + length
84            + " expected");
85      }
86      // TODO : to be removed - make it conditional
87      return new Pair<ByteBuff, MemoryType>(new SingleByteBuff(dstBuffer), MemoryType.SHARED);
88    }
89  
90    /**
91     * Transfers data from the given byte buffer to the buffer array
92     * @param srcBuffer the given byte buffer from which bytes are to be read
93     * @param offset The offset in the ByteBufferArray of the first byte to be
94     *          written
95     * @throws IOException
96     */
97    @Override
98    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
99      assert srcBuffer.hasArray();
100     bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
101         srcBuffer.arrayOffset());
102   }
103 
104   @Override
105   public void write(ByteBuff srcBuffer, long offset) throws IOException {
106     // When caching block into BucketCache there will be single buffer backing for this HFileBlock.
107     // This will work for now. But from the DFS itself if we get DBB then this may not hold true.
108     assert srcBuffer.hasArray();
109     bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
110         srcBuffer.arrayOffset());
111   }
112   /**
113    * No operation for the sync in the memory IO engine
114    */
115   @Override
116   public void sync() {
117     // Nothing to do.
118   }
119 
120   /**
121    * No operation for the shutdown in the memory IO engine
122    */
123   @Override
124   public void shutdown() {
125     // Nothing to do.
126   }
127 }