Skip to content

Commit

Permalink
HBASE-13082 Coarsen StoreScanner locks to RegionScanner.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhofhansl committed Mar 6, 2015
1 parent 6d4a4a4 commit ec1eff9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5233,6 +5233,8 @@ public HRegionInfo getRegionInfo() {
scan.getFamilyMap().entrySet()) {
Store store = stores.get(entry.getKey());
KeyValueScanner scanner = store.getScanner(scan, entry.getValue(), this.readPt);
// pass the RegionScanner object to lock out concurrent changes to set of readers
scanner.setReaderLock(this);
if (this.filter == null || !scan.doLoadColumnFamiliesOnDemand()
|| this.filter.isFamilyEssential(entry.getKey())) {
scanners.add(scanner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ boolean requestSeek(Cell kv, boolean forward, boolean useBloom)
* if known, or null otherwise
*/
public Cell getNextIndexedKey();

/**
* Set the object to lock when the scanner's readers (if any) are updated (this is here so that the
* coprocessors creating {@link StoreScanner}'s do not have to change)
* @param obj lock object to use
*/
public void setReaderLock(Object obj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ public boolean isFileScanner() {
public Cell getNextIndexedKey() {
return null;
}

@Override
public void setReaderLock(Object obj) {/* NO OP */}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,13 @@ public boolean seek(Cell key) throws IOException {

@Override
public boolean seekToPreviousRow(Cell key) throws IOException {
lock.lock();
try {
checkReseek();
return this.heap.seekToPreviousRow(key);
} finally {
lock.unlock();
}

checkReseek();
return this.heap.seekToPreviousRow(key);
}

@Override
public boolean backwardSeek(Cell key) throws IOException {
lock.lock();
try {
checkReseek();
return this.heap.backwardSeek(key);
} finally {
lock.unlock();
}
checkReseek();
return this.heap.backwardSeek(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,7 @@ public boolean backwardSeek(Cell key) throws IOException {
public Cell getNextIndexedKey() {
return hfs.getNextIndexedKey();
}

@Override
public void setReaderLock(Object obj) {/* NO OP */}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.NavigableSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -103,10 +102,17 @@ public class StoreScanner extends NonReversedNonLazyKeyValueScanner

// A flag whether use pread for scan
private boolean scanUsePread = false;
protected ReentrantLock lock = new ReentrantLock();

private final long readPt;

// lock to use for updateReaders
// creator needs to ensure that:
// 1. *all* calls to public methods (except updateReaders and close) are locked with this lock
// (this can be done by passing the RegionScannerImpl object down)
// OR
// 2. updateReader is *never* called (such as in flushes or compactions)
private Object readerLock;

// used by the injection framework to test race between StoreScanner construction and compaction
enum StoreScannerCompactionRace {
BEFORE_SEEK,
Expand Down Expand Up @@ -395,15 +401,10 @@ protected List<KeyValueScanner> selectScannersFrom(

@Override
public Cell peek() {
lock.lock();
try {
if (this.heap == null) {
return this.lastTop;
}
return this.heap.peek();
} finally {
lock.unlock();
}
}

@Override
Expand All @@ -414,8 +415,6 @@ public KeyValue next() {

@Override
public void close() {
lock.lock();
try {
if (this.closing) return;
this.closing = true;
// under test, we dont have a this.store
Expand All @@ -425,21 +424,13 @@ public void close() {
this.heap.close();
this.heap = null; // CLOSED!
this.lastTop = null; // If both are null, we are closed.
} finally {
lock.unlock();
}
}

@Override
public boolean seek(Cell key) throws IOException {
lock.lock();
try {
// reset matcher state, in case that underlying store changed
checkReseek();
return this.heap.seek(key);
} finally {
lock.unlock();
}
}

/**
Expand All @@ -464,8 +455,6 @@ public NextState next(List<Cell> outResult, int limit) throws IOException {
@Override
public NextState next(List<Cell> outResult, int limit, long remainingResultSize)
throws IOException {
lock.lock();
try {
if (checkReseek()) {
return NextState.makeState(NextState.State.MORE_VALUES, 0);
}
Expand Down Expand Up @@ -617,9 +606,6 @@ public NextState next(List<Cell> outResult, int limit, long remainingResultSize)
// No more keys
close();
return NextState.makeState(NextState.State.NO_MORE_VALUES, totalHeapSize);
} finally {
lock.unlock();
}
}

/*
Expand Down Expand Up @@ -663,8 +649,7 @@ public NextState next(List<Cell> outResult) throws IOException {
// Implementation of ChangedReadersObserver
@Override
public void updateReaders() throws IOException {
lock.lock();
try {
synchronized(readerLock) {
if (this.closing) return;

// All public synchronized API calls will call 'checkReseek' which will cause
Expand All @@ -684,8 +669,6 @@ public void updateReaders() throws IOException {
this.heap = null; // the re-seeks could be slow (access HDFS) free up memory ASAP

// Let the next() call handle re-creating and seeking
} finally {
lock.unlock();
}
}

Expand Down Expand Up @@ -776,8 +759,6 @@ protected boolean seekAsDirection(Cell kv)

@Override
public boolean reseek(Cell kv) throws IOException {
lock.lock();
try {
//Heap will not be null, if this is called from next() which.
//If called from RegionScanner.reseek(...) make sure the scanner
//stack is reset if needed.
Expand All @@ -786,9 +767,6 @@ public boolean reseek(Cell kv) throws IOException {
return heap.requestSeek(kv, true, useRowColBloom);
}
return heap.reseek(kv);
} finally {
lock.unlock();
}
}

@Override
Expand Down Expand Up @@ -863,5 +841,9 @@ public long getEstimatedNumberOfKvsScanned() {
public Cell getNextIndexedKey() {
return this.heap.getNextIndexedKey();
}
}

@Override
public void setReaderLock(Object obj) {
this.readerLock = obj;
}
}

0 comments on commit ec1eff9

Please sign in to comment.