HBASE-19977 FileMmapEngine allocation of byte buffers should be
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / io / hfile / bucket / FileMmapEngine.java
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.io.RandomAccessFile;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.FileChannel;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.yetus.audience.InterfaceAudience;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.apache.hadoop.hbase.io.hfile.Cacheable;
31 import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
32 import org.apache.hadoop.hbase.io.hfile.CacheableDeserializer;
33 import org.apache.hadoop.hbase.nio.ByteBuff;
34 import org.apache.hadoop.hbase.nio.SingleByteBuff;
35 import org.apache.hadoop.hbase.util.ByteBufferAllocator;
36 import org.apache.hadoop.hbase.util.ByteBufferArray;
37 import org.apache.hadoop.util.StringUtils;
38
39 /**
40 * IO engine that stores data to a file on the local file system using memory mapping
41 * mechanism
42 */
43 @InterfaceAudience.Private
44 public class FileMmapEngine implements IOEngine {
45 static final Logger LOG = LoggerFactory.getLogger(FileMmapEngine.class);
46
47 private final String path;
48 private long size;
49 private ByteBufferArray bufferArray;
50 private final FileChannel fileChannel;
51 private RandomAccessFile raf = null;
52
53 public FileMmapEngine(String filePath, long capacity) throws IOException {
54 this.path = filePath;
55 this.size = capacity;
56 long fileSize = 0;
57 try {
58 raf = new RandomAccessFile(filePath, "rw");
59 fileSize = roundUp(capacity, ByteBufferArray.DEFAULT_BUFFER_SIZE);
60 raf.setLength(fileSize);
61 fileChannel = raf.getChannel();
62 LOG.info("Allocating " + StringUtils.byteDesc(fileSize) + ", on the path:" + filePath);
63 } catch (java.io.FileNotFoundException fex) {
64 LOG.error("Can't create bucket cache file " + filePath, fex);
65 throw fex;
66 } catch (IOException ioex) {
67 LOG.error("Can't extend bucket cache file; insufficient space for "
68 + StringUtils.byteDesc(fileSize), ioex);
69 shutdown();
70 throw ioex;
71 }
72 ByteBufferAllocator allocator = new ByteBufferAllocator() {
73 AtomicInteger pos = new AtomicInteger(0);
74 @Override
75 public ByteBuffer allocate(long size) throws IOException {
76 ByteBuffer buffer = fileChannel.map(java.nio.channels.FileChannel.MapMode.READ_WRITE,
77 pos.getAndIncrement() * size, size);
78 return buffer;
79 }
80 };
81 bufferArray = new ByteBufferArray(fileSize, allocator);
82 }
83
84 private long roundUp(long n, long to) {
85 return ((n + to - 1) / to) * to;
86 }
87
88 @Override
89 public String toString() {
90 return "ioengine=" + this.getClass().getSimpleName() + ", path=" + this.path +
91 ", size=" + String.format("%,d", this.size);
92 }
93
94 /**
95 * File IO engine is always able to support persistent storage for the cache
96 * @return true
97 */
98 @Override
99 public boolean isPersistent() {
100 return true;
101 }
102
103 @Override
104 public Cacheable read(long offset, int length, CacheableDeserializer<Cacheable> deserializer)
105 throws IOException {
106 byte[] dst = new byte[length];
107 bufferArray.getMultiple(offset, length, dst);
108 return deserializer.deserialize(new SingleByteBuff(ByteBuffer.wrap(dst)), true,
109 MemoryType.EXCLUSIVE);
110 }
111
112 /**
113 * Transfers data from the given byte buffer to file
114 * @param srcBuffer the given byte buffer from which bytes are to be read
115 * @param offset The offset in the file where the first byte to be written
116 * @throws IOException
117 */
118 @Override
119 public void write(ByteBuffer srcBuffer, long offset) throws IOException {
120 assert srcBuffer.hasArray();
121 bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
122 srcBuffer.arrayOffset());
123 }
124
125 @Override
126 public void write(ByteBuff srcBuffer, long offset) throws IOException {
127 // This singleByteBuff can be considered to be array backed
128 assert srcBuffer.hasArray();
129 bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
130 srcBuffer.arrayOffset());
131
132 }
133 /**
134 * Sync the data to file after writing
135 * @throws IOException
136 */
137 @Override
138 public void sync() throws IOException {
139 if (fileChannel != null) {
140 fileChannel.force(true);
141 }
142 }
143
144 /**
145 * Close the file
146 */
147 @Override
148 public void shutdown() {
149 try {
150 fileChannel.close();
151 } catch (IOException ex) {
152 LOG.error("Can't shutdown cleanly", ex);
153 }
154 try {
155 raf.close();
156 } catch (IOException ex) {
157 LOG.error("Can't shutdown cleanly", ex);
158 }
159 }
160 }