Skip to content

Commit 326a5ce

Browse files
comnetworkvirajjasani
authored andcommitted
HBASE-27539 Encapsulate and centralise access to ref count through StoreFileInfo (apache#4928)
Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
1 parent 9c15780 commit 326a5ce

File tree

10 files changed

+126
-90
lines changed

10 files changed

+126
-90
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/io/HalfStoreFileReader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.IOException;
2121
import java.nio.ByteBuffer;
2222
import java.util.Optional;
23-
import java.util.concurrent.atomic.AtomicInteger;
2423
import org.apache.hadoop.conf.Configuration;
2524
import org.apache.hadoop.hbase.Cell;
2625
import org.apache.hadoop.hbase.HConstants;
@@ -31,6 +30,7 @@
3130
import org.apache.hadoop.hbase.io.hfile.HFileInfo;
3231
import org.apache.hadoop.hbase.io.hfile.HFileScanner;
3332
import org.apache.hadoop.hbase.io.hfile.ReaderContext;
33+
import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
3434
import org.apache.hadoop.hbase.regionserver.StoreFileReader;
3535
import org.apache.hadoop.hbase.util.Bytes;
3636
import org.apache.yetus.audience.InterfaceAudience;
@@ -69,13 +69,12 @@ public class HalfStoreFileReader extends StoreFileReader {
6969
* @param fileInfo HFile info
7070
* @param cacheConf CacheConfig
7171
* @param r original reference file (contains top or bottom)
72-
* @param refCount reference count
7372
* @param conf Configuration
7473
*/
7574
public HalfStoreFileReader(final ReaderContext context, final HFileInfo fileInfo,
76-
final CacheConfig cacheConf, final Reference r, AtomicInteger refCount,
75+
final CacheConfig cacheConf, final Reference r, StoreFileInfo storeFileInfo,
7776
final Configuration conf) throws IOException {
78-
super(context, fileInfo, cacheConf, refCount, conf);
77+
super(context, fileInfo, cacheConf, storeFileInfo, conf);
7978
// This is not actual midkey for this half-file; its just border
8079
// around which we split top and bottom. Have to look in files to find
8180
// actual last and first keys for bottom and top halves. Half-files don't

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStoreFile.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,12 @@ public boolean isCompactedAway() {
343343
}
344344

345345
public int getRefCount() {
346-
return fileInfo.refCount.get();
346+
return fileInfo.getRefCount();
347347
}
348348

349349
/** Returns true if the file is still used in reads */
350350
public boolean isReferencedInReads() {
351-
int rc = fileInfo.refCount.get();
351+
int rc = fileInfo.getRefCount();
352352
assert rc >= 0; // we should not go negative.
353353
return rc > 0;
354354
}
@@ -647,11 +647,11 @@ Set<String> getCompactedStoreFiles() {
647647
}
648648

649649
long increaseRefCount() {
650-
return this.fileInfo.refCount.incrementAndGet();
650+
return this.fileInfo.increaseRefCount();
651651
}
652652

653653
long decreaseRefCount() {
654-
return this.fileInfo.refCount.decrementAndGet();
654+
return this.fileInfo.decreaseRefCount();
655655
}
656656

657657
static void increaseStoreFilesRefeCount(Collection<HStoreFile> storeFiles) {

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileInfo.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class StoreFileInfo implements Configurable {
108108
// Counter that is incremented every time a scanner is created on the
109109
// store file. It is decremented when the scan on the store file is
110110
// done.
111-
final AtomicInteger refCount = new AtomicInteger(0);
111+
private final AtomicInteger refCount = new AtomicInteger(0);
112112

113113
/**
114114
* Create a Store File Info
@@ -275,12 +275,13 @@ public HDFSBlocksDistribution getHDFSBlockDistribution() {
275275
return this.hdfsBlocksDistribution;
276276
}
277277

278-
StoreFileReader createReader(ReaderContext context, CacheConfig cacheConf) throws IOException {
278+
public StoreFileReader createReader(ReaderContext context, CacheConfig cacheConf)
279+
throws IOException {
279280
StoreFileReader reader = null;
280281
if (this.reference != null) {
281-
reader = new HalfStoreFileReader(context, hfileInfo, cacheConf, reference, refCount, conf);
282+
reader = new HalfStoreFileReader(context, hfileInfo, cacheConf, reference, this, conf);
282283
} else {
283-
reader = new StoreFileReader(context, hfileInfo, cacheConf, refCount, conf);
284+
reader = new StoreFileReader(context, hfileInfo, cacheConf, this, conf);
284285
}
285286
return reader;
286287
}
@@ -681,7 +682,7 @@ boolean isNoReadahead() {
681682
return this.noReadahead;
682683
}
683684

684-
HFileInfo getHFileInfo() {
685+
public HFileInfo getHFileInfo() {
685686
return hfileInfo;
686687
}
687688

@@ -713,4 +714,16 @@ public void initHFileInfo(ReaderContext context) throws IOException {
713714
this.hfileInfo = new HFileInfo(context, conf);
714715
}
715716

717+
int getRefCount() {
718+
return this.refCount.get();
719+
}
720+
721+
int increaseRefCount() {
722+
return this.refCount.incrementAndGet();
723+
}
724+
725+
int decreaseRefCount() {
726+
return this.refCount.decrementAndGet();
727+
}
728+
716729
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileReader.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Map;
2828
import java.util.Optional;
2929
import java.util.SortedSet;
30-
import java.util.concurrent.atomic.AtomicInteger;
3130
import org.apache.hadoop.conf.Configuration;
3231
import org.apache.hadoop.hbase.Cell;
3332
import org.apache.hadoop.hbase.CellComparator;
@@ -78,24 +77,26 @@ public class StoreFileReader {
7877
private int prefixLength = -1;
7978
protected Configuration conf;
8079

81-
// Counter that is incremented every time a scanner is created on the
82-
// store file. It is decremented when the scan on the store file is
83-
// done. All StoreFileReader for the same StoreFile will share this counter.
84-
private final AtomicInteger refCount;
80+
/**
81+
* All {@link StoreFileReader} for the same StoreFile will share the
82+
* {@link StoreFileInfo#refCount}. Counter that is incremented every time a scanner is created on
83+
* the store file. It is decremented when the scan on the store file is done.
84+
*/
85+
private final StoreFileInfo storeFileInfo;
8586
private final ReaderContext context;
8687

87-
private StoreFileReader(HFile.Reader reader, AtomicInteger refCount, ReaderContext context,
88+
private StoreFileReader(HFile.Reader reader, StoreFileInfo storeFileInfo, ReaderContext context,
8889
Configuration conf) {
8990
this.reader = reader;
9091
bloomFilterType = BloomType.NONE;
91-
this.refCount = refCount;
92+
this.storeFileInfo = storeFileInfo;
9293
this.context = context;
9394
this.conf = conf;
9495
}
9596

9697
public StoreFileReader(ReaderContext context, HFileInfo fileInfo, CacheConfig cacheConf,
97-
AtomicInteger refCount, Configuration conf) throws IOException {
98-
this(HFile.createReader(context, fileInfo, cacheConf, conf), refCount, context, conf);
98+
StoreFileInfo storeFileInfo, Configuration conf) throws IOException {
99+
this(HFile.createReader(context, fileInfo, cacheConf, conf), storeFileInfo, context, conf);
99100
}
100101

101102
void copyFields(StoreFileReader storeFileReader) throws IOException {
@@ -120,7 +121,7 @@ public boolean isPrimaryReplicaReader() {
120121
*/
121122
@InterfaceAudience.Private
122123
StoreFileReader() {
123-
this.refCount = new AtomicInteger(0);
124+
this.storeFileInfo = null;
124125
this.reader = null;
125126
this.context = null;
126127
}
@@ -151,23 +152,23 @@ public StoreFileScanner getStoreFileScanner(boolean cacheBlocks, boolean pread,
151152
* is opened.
152153
*/
153154
int getRefCount() {
154-
return refCount.get();
155+
return storeFileInfo.getRefCount();
155156
}
156157

157158
/**
158159
* Indicate that the scanner has started reading with this reader. We need to increment the ref
159160
* count so reader is not close until some object is holding the lock
160161
*/
161162
void incrementRefCount() {
162-
refCount.incrementAndGet();
163+
storeFileInfo.increaseRefCount();
163164
}
164165

165166
/**
166167
* Indicate that the scanner has finished reading with this reader. We need to decrement the ref
167168
* count, and also, if this is not the common pread reader, we should close it.
168169
*/
169170
void readCompleted() {
170-
refCount.decrementAndGet();
171+
storeFileInfo.decreaseRefCount();
171172
if (context.getReaderType() == ReaderType.STREAM) {
172173
try {
173174
reader.close(false);

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ HFile.Writer getHFileWriter() {
402402
* @param dir Directory to create file in.
403403
* @return random filename inside passed <code>dir</code>
404404
*/
405-
static Path getUniqueFile(final FileSystem fs, final Path dir) throws IOException {
405+
public static Path getUniqueFile(final FileSystem fs, final Path dir) throws IOException {
406406
if (!fs.getFileStatus(dir).isDirectory()) {
407407
throw new IOException("Expecting " + dir.toString() + " to be a directory");
408408
}

hbase-server/src/main/java/org/apache/hadoop/hbase/tool/BulkLoadHFilesTool.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,11 @@ private static void copyHFileHalf(Configuration conf, Path inFile, Path outFile,
753753
StoreFileWriter halfWriter = null;
754754
try {
755755
ReaderContext context = new ReaderContextBuilder().withFileSystemAndPath(fs, inFile).build();
756-
HFileInfo hfile = new HFileInfo(context, conf);
757-
halfReader =
758-
new HalfStoreFileReader(context, hfile, cacheConf, reference, new AtomicInteger(0), conf);
759-
hfile.initMetaAndIndex(halfReader.getHFileReader());
756+
StoreFileInfo storeFileInfo =
757+
new StoreFileInfo(conf, fs, fs.getFileStatus(inFile), reference);
758+
storeFileInfo.initHFileInfo(context);
759+
halfReader = (HalfStoreFileReader) storeFileInfo.createReader(context, cacheConf);
760+
storeFileInfo.getHFileInfo().initMetaAndIndex(halfReader.getHFileReader());
760761
Map<byte[], byte[]> fileInfo = halfReader.loadFileInfo();
761762

762763
int blocksize = familyDescriptor.getBlocksize();

hbase-server/src/test/java/org/apache/hadoop/hbase/io/TestHalfStoreFileReader.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.IOException;
2525
import java.util.ArrayList;
2626
import java.util.List;
27-
import java.util.concurrent.atomic.AtomicInteger;
2827
import org.apache.hadoop.conf.Configuration;
2928
import org.apache.hadoop.fs.FileSystem;
3029
import org.apache.hadoop.fs.Path;
@@ -39,10 +38,10 @@
3938
import org.apache.hadoop.hbase.io.hfile.HFile;
4039
import org.apache.hadoop.hbase.io.hfile.HFileContext;
4140
import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
42-
import org.apache.hadoop.hbase.io.hfile.HFileInfo;
4341
import org.apache.hadoop.hbase.io.hfile.HFileScanner;
4442
import org.apache.hadoop.hbase.io.hfile.ReaderContext;
4543
import org.apache.hadoop.hbase.io.hfile.ReaderContextBuilder;
44+
import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
4645
import org.apache.hadoop.hbase.testclassification.IOTests;
4746
import org.apache.hadoop.hbase.testclassification.SmallTests;
4847
import org.apache.hadoop.hbase.util.Bytes;
@@ -118,10 +117,12 @@ public void testHalfScanAndReseek() throws IOException {
118117
private void doTestOfScanAndReseek(Path p, FileSystem fs, Reference bottom, CacheConfig cacheConf)
119118
throws IOException {
120119
ReaderContext context = new ReaderContextBuilder().withFileSystemAndPath(fs, p).build();
121-
HFileInfo fileInfo = new HFileInfo(context, TEST_UTIL.getConfiguration());
122-
final HalfStoreFileReader halfreader = new HalfStoreFileReader(context, fileInfo, cacheConf,
123-
bottom, new AtomicInteger(0), TEST_UTIL.getConfiguration());
124-
fileInfo.initMetaAndIndex(halfreader.getHFileReader());
120+
StoreFileInfo storeFileInfo =
121+
new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, fs.getFileStatus(p), bottom);
122+
storeFileInfo.initHFileInfo(context);
123+
final HalfStoreFileReader halfreader =
124+
(HalfStoreFileReader) storeFileInfo.createReader(context, cacheConf);
125+
storeFileInfo.getHFileInfo().initMetaAndIndex(halfreader.getHFileReader());
125126
halfreader.loadFileInfo();
126127
final HFileScanner scanner = halfreader.getScanner(false, false);
127128

@@ -214,10 +215,12 @@ public void testHalfScanner() throws IOException {
214215
private Cell doTestOfSeekBefore(Path p, FileSystem fs, Reference bottom, Cell seekBefore,
215216
CacheConfig cacheConfig) throws IOException {
216217
ReaderContext context = new ReaderContextBuilder().withFileSystemAndPath(fs, p).build();
217-
HFileInfo fileInfo = new HFileInfo(context, TEST_UTIL.getConfiguration());
218-
final HalfStoreFileReader halfreader = new HalfStoreFileReader(context, fileInfo, cacheConfig,
219-
bottom, new AtomicInteger(0), TEST_UTIL.getConfiguration());
220-
fileInfo.initMetaAndIndex(halfreader.getHFileReader());
218+
StoreFileInfo storeFileInfo =
219+
new StoreFileInfo(TEST_UTIL.getConfiguration(), fs, fs.getFileStatus(p), bottom);
220+
storeFileInfo.initHFileInfo(context);
221+
final HalfStoreFileReader halfreader =
222+
(HalfStoreFileReader) storeFileInfo.createReader(context, cacheConfig);
223+
storeFileInfo.getHFileInfo().initMetaAndIndex(halfreader.getHFileReader());
221224
halfreader.loadFileInfo();
222225
final HFileScanner scanner = halfreader.getScanner(false, false);
223226
scanner.seekBefore(seekBefore);

0 commit comments

Comments
 (0)