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;
022import org.apache.hadoop.hbase.io.hfile.Cacheable;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * IO engine that stores data in pmem devices such as DCPMM. This engine also mmaps the file from
027 * the given path. But note that this path has to be a path on the pmem device so that when mmapped
028 * the file's address is mapped to the Pmem's address space and not in the DRAM. Since this address
029 * space is exclusive for the Pmem device there is no swapping out of the mmapped contents that
030 * generally happens when DRAM's free space is not enough to hold the specified file's mmapped
031 * contents. This gives us the option of using the {@code MemoryType#SHARED} type when serving the
032 * data from this pmem address space. We need not copy the blocks to the onheap space as we need to
033 * do for the case of {@code ExclusiveMemoryMmapIOEngine}.
034 */
035@InterfaceAudience.Private
036public class SharedMemoryMmapIOEngine extends FileMmapIOEngine {
037
038  // TODO this will support only one path over Pmem. To make use of multiple Pmem devices mounted,
039  // we need to support multiple paths like files IOEngine. Support later.
040  public SharedMemoryMmapIOEngine(String filePath, long capacity) throws IOException {
041    super(filePath, capacity);
042  }
043
044  @Override
045  public boolean usesSharedMemory() {
046    return true;
047  }
048
049  @Override
050  public Cacheable read(BucketEntry be) throws IOException {
051    ByteBuffer[] buffers = bufferArray.asSubByteBuffers(be.offset(), be.getLength());
052    // Here the buffer that is created directly refers to the buffer in the actual buckets.
053    // When any cell is referring to the blocks created out of these buckets then it means that
054    // those cells are referring to a shared memory area which if evicted by the BucketCache would
055    // lead to corruption of results. The readers using this block are aware of this fact and do
056    // the necessary action to prevent eviction till the results are either consumed or copied
057    return be.wrapAsCacheable(buffers);
058  }
059}