001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile.bucket;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022
023import org.apache.hadoop.hbase.io.hfile.Cacheable;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * IO engine that stores data in pmem devices such as DCPMM. This engine also mmaps the file from
028 * the given path. But note that this path has to be a path on the pmem device so that when mmapped
029 * the file's address is mapped to the Pmem's address space and not in the DRAM. Since this address
030 * space is exclusive for the Pmem device there is no swapping out of the mmapped contents that
031 * generally happens when DRAM's free space is not enough to hold the specified file's mmapped
032 * contents. This gives us the option of using the {@code MemoryType#SHARED} type when serving the
033 * data from this pmem address space. We need not copy the blocks to the onheap space as we need to
034 * do for the case of {@code ExclusiveMemoryMmapIOEngine}.
035 */
036@InterfaceAudience.Private
037public class SharedMemoryMmapIOEngine extends FileMmapIOEngine {
038
039  // TODO this will support only one path over Pmem. To make use of multiple Pmem devices mounted,
040  // we need to support multiple paths like files IOEngine. Support later.
041  public SharedMemoryMmapIOEngine(String filePath, long capacity) throws IOException {
042    super(filePath, capacity);
043  }
044
045  @Override
046  public boolean usesSharedMemory() {
047    return true;
048  }
049
050  @Override
051  public Cacheable read(BucketEntry be) throws IOException {
052    ByteBuffer[] buffers = bufferArray.asSubByteBuffers(be.offset(), be.getLength());
053    // Here the buffer that is created directly refers to the buffer in the actual buckets.
054    // When any cell is referring to the blocks created out of these buckets then it means that
055    // those cells are referring to a shared memory area which if evicted by the BucketCache would
056    // lead to corruption of results. The readers using this block are aware of this fact and do
057    // the necessary action to prevent eviction till the results are either consumed or copied
058    return be.wrapAsCacheable(buffers);
059  }
060}