Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2071,15 +2071,13 @@ public boolean compact(CompactionContext compaction, Store store,
* }
* Also in compactor.performCompaction():
* check periodically to see if a system stop is requested
* if (closeCheckInterval > 0) {
* bytesWritten += len;
* if (bytesWritten > closeCheckInterval) {
* bytesWritten = 0;
* if (!store.areWritesEnabled()) {
* progress.cancel();
* return false;
* }
* }
* if (closeChecker != null && closeChecker.isTimeLimit(store, now)) {
* progress.cancel();
* return false;
* }
* if (closeChecker != null && closeChecker.isSizeLimit(store, len)) {
* progress.cancel();
* return false;
* }
*/
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public class HStore implements Store {
private final CacheConfig cacheConf;
private long lastCompactSize = 0;
volatile boolean forceMajor = false;
/* how many bytes to write between status checks */
static int closeCheckInterval = 0;
private AtomicLong storeSize = new AtomicLong();
private AtomicLong totalUncompressedBytes = new AtomicLong();

Expand Down Expand Up @@ -283,11 +281,6 @@ protected HStore(final HRegion region, final HColumnDescriptor family,
this.compactionCheckMultiplier = DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER;
}

if (HStore.closeCheckInterval == 0) {
HStore.closeCheckInterval = conf.getInt(
"hbase.hstore.close.check.interval", 10*1000*1000 /* 10 MB */);
}

this.storeEngine = StoreEngine.create(this, this.conf, this.comparator);
List<StoreFile> storeFiles = loadStoreFiles();
// Move the storeSize calculation out of loadStoreFiles() method, because the secondary read
Expand Down Expand Up @@ -472,13 +465,6 @@ public static ChecksumType getChecksumType(Configuration conf) {
}
}

/**
* @return how many bytes to write between status checks
*/
public static int getCloseCheckInterval() {
return closeCheckInterval;
}

@Override
public HColumnDescriptor getFamily() {
return this.family;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.regionserver.compactions;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.regionserver.Store;

/**
* Check periodically to see if a system stop is requested
*/
@InterfaceAudience.Private
class CloseChecker {
public static final String SIZE_LIMIT_KEY = "hbase.hstore.close.check.interval";
public static final String TIME_LIMIT_KEY = "hbase.hstore.close.check.time.interval";

private final int closeCheckSizeLimit;
private final long closeCheckTimeLimit;

private long bytesWrittenProgressForCloseCheck;
private long lastCloseCheckMillis;

CloseChecker(Configuration conf, long currentTime) {
this.closeCheckSizeLimit = conf.getInt(SIZE_LIMIT_KEY, 10 * 1000 * 1000 /* 10 MB */);
this.closeCheckTimeLimit = conf.getLong(TIME_LIMIT_KEY, 10 * 1000L /* 10 s */);
this.bytesWrittenProgressForCloseCheck = 0;
this.lastCloseCheckMillis = currentTime;
}

/**
* Check periodically to see if a system stop is requested every written bytes reach size limit.
*
* @return if true, system stop.
*/
boolean isSizeLimit(Store store, long bytesWritten) {
if (closeCheckSizeLimit <= 0) {
return false;
}

bytesWrittenProgressForCloseCheck += bytesWritten;
if (bytesWrittenProgressForCloseCheck <= closeCheckSizeLimit) {
return false;
}

bytesWrittenProgressForCloseCheck = 0;
return !store.areWritesEnabled();
}

/**
* Check periodically to see if a system stop is requested every time.
*
* @return if true, system stop.
*/
boolean isTimeLimit(Store store, long now) {
if (closeCheckTimeLimit <= 0) {
return false;
}

final long elapsedMillis = now - lastCloseCheckMillis;
if (elapsedMillis <= closeCheckTimeLimit) {
return false;
}

lastCloseCheckMillis = now;
return !store.areWritesEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,16 @@ protected InternalScanner postCreateCoprocScanner(final CompactionRequest reques
protected boolean performCompaction(InternalScanner scanner, CellSink writer,
long smallestReadPoint, boolean cleanSeqId,
ThroughputController throughputController) throws IOException {
long bytesWritten = 0;
long bytesWrittenProgress = 0;
// Since scanner.next() can return 'false' but still be delivering data,
// we have to use a do/while loop.
List<Cell> cells = new ArrayList<Cell>();
long closeCheckInterval = HStore.getCloseCheckInterval();
long currentTime = EnvironmentEdgeManager.currentTime();
long lastMillis = 0;
if (LOG.isDebugEnabled()) {
lastMillis = EnvironmentEdgeManager.currentTime();
lastMillis = currentTime;
}
CloseChecker closeChecker = new CloseChecker(conf, currentTime);
String compactionName = ThroughputControlUtil.getNameForThrottling(store, "compaction");
long now = 0;
boolean hasMore;
Expand All @@ -418,8 +418,13 @@ protected boolean performCompaction(InternalScanner scanner, CellSink writer,
try {
do {
hasMore = scanner.next(cells, scannerContext);
currentTime = EnvironmentEdgeManager.currentTime();
if (LOG.isDebugEnabled()) {
now = EnvironmentEdgeManager.currentTime();
now = currentTime;
}
if (closeChecker.isTimeLimit(store, currentTime)) {
progress.cancel();
return false;
}
// output to writer:
Cell lastCleanCell = null;
Expand All @@ -441,16 +446,9 @@ protected boolean performCompaction(InternalScanner scanner, CellSink writer,
bytesWrittenProgress += len;
}
throughputController.control(compactionName, len);
// check periodically to see if a system stop is requested
if (closeCheckInterval > 0) {
bytesWritten += len;
if (bytesWritten > closeCheckInterval) {
bytesWritten = 0;
if (!store.areWritesEnabled()) {
progress.cancel();
return false;
}
}
if (closeChecker.isSizeLimit(store, len)) {
progress.cancel();
return false;
}
}
if (lastCleanCell != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.apache.hadoop.hbase.HBaseTestingUtility.START_KEY;
import static org.apache.hadoop.hbase.HBaseTestingUtility.START_KEY_BYTES;
import static org.apache.hadoop.hbase.HBaseTestingUtility.fam1;
import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.SIZE_LIMIT_KEY;
import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.TIME_LIMIT_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -144,12 +146,11 @@ public void tearDown() throws Exception {
* @throws Exception
*/
@Test
public void testInterruptCompaction() throws Exception {
public void testInterruptCompactionBySize() throws Exception {
assertEquals(0, count());

// lower the polling interval for this test
int origWI = HStore.closeCheckInterval;
HStore.closeCheckInterval = 10*1000; // 10 KB
conf.setInt(SIZE_LIMIT_KEY, 10 * 1000 /* 10 KB */);

try {
// Create a couple store files w/ 15KB (over 10KB interval)
Expand Down Expand Up @@ -193,7 +194,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
} finally {
// don't mess up future tests
r.writestate.writesEnabled = true;
HStore.closeCheckInterval = origWI;
conf.setInt(SIZE_LIMIT_KEY, 10 * 1000 * 1000 /* 10 MB */);

// Delete all Store information once done using
for (int i = 0; i < compactionThreshold; i++) {
Expand Down Expand Up @@ -222,6 +223,84 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
}

@Test
public void testInterruptCompactionByTime() throws Exception {
assertEquals(0, count());

// lower the polling interval for this test
conf.setLong(TIME_LIMIT_KEY, 1 /* 1ms */);

try {
// Create a couple store files w/ 15KB (over 10KB interval)
int jmax = (int) Math.ceil(15.0/compactionThreshold);
byte [] pad = new byte[1000]; // 1 KB chunk
for (int i = 0; i < compactionThreshold; i++) {
HRegionIncommon loader = new HRegionIncommon(r);
Put p = new Put(Bytes.add(STARTROW, Bytes.toBytes(i)));
p.setDurability(Durability.SKIP_WAL);
for (int j = 0; j < jmax; j++) {
p.add(COLUMN_FAMILY, Bytes.toBytes(j), pad);
}
HBaseTestCase.addContent(loader, Bytes.toString(COLUMN_FAMILY));
loader.put(p);
loader.flushcache();
}

HRegion spyR = spy(r);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
r.writestate.writesEnabled = false;
return invocation.callRealMethod();
}
}).when(spyR).doRegionCompactionPrep();

// force a minor compaction, but not before requesting a stop
spyR.compactStores();

// ensure that the compaction stopped, all old files are intact,
Store s = r.stores.get(COLUMN_FAMILY);
assertEquals(compactionThreshold, s.getStorefilesCount());
assertTrue(s.getStorefilesSize() > 15*1000);
// only one empty dir exists in temp dir
FileStatus[] ls = r.getFilesystem().listStatus(r.getRegionFileSystem().getTempDir());
assertEquals(1, ls.length);
Path storeTempDir = new Path(r.getRegionFileSystem().getTempDir(), Bytes.toString(COLUMN_FAMILY));
assertTrue(r.getFilesystem().exists(storeTempDir));
ls = r.getFilesystem().listStatus(storeTempDir);
assertEquals(0, ls.length);
} finally {
// don't mess up future tests
r.writestate.writesEnabled = true;
conf.setLong(TIME_LIMIT_KEY, 10 * 1000L /* 10 s */);

// Delete all Store information once done using
for (int i = 0; i < compactionThreshold; i++) {
Delete delete = new Delete(Bytes.add(STARTROW, Bytes.toBytes(i)));
byte [][] famAndQf = {COLUMN_FAMILY, null};
delete.deleteFamily(famAndQf[0]);
r.delete(delete);
}
r.flush(true);

// Multiple versions allowed for an entry, so the delete isn't enough
// Lower TTL and expire to ensure that all our entries have been wiped
final int ttl = 1000;
for (Store hstore: this.r.stores.values()) {
HStore store = (HStore)hstore;
ScanInfo old = store.getScanInfo();
ScanInfo si = new ScanInfo(old.getConfiguration(), old.getFamily(),
old.getMinVersions(), old.getMaxVersions(), ttl,
old.getKeepDeletedCells(), 0, old.getComparator());
store.setScanInfo(si);
}
Thread.sleep(ttl);

r.compact(true);
assertEquals(0, count());
}
}

private int count() throws IOException {
int count = 0;
for (StoreFile f: this.r.stores.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.regionserver.compactions;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.regionserver.Store;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.SIZE_LIMIT_KEY;
import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.TIME_LIMIT_KEY;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Category(SmallTests.class)
public class TestCloseChecker {
@Test
public void testIsClosed() {
Store enableWrite = mock(Store.class);
when(enableWrite.areWritesEnabled()).thenReturn(true);

Store disableWrite = mock(Store.class);
when(disableWrite.areWritesEnabled()).thenReturn(false);

Configuration conf = new Configuration();

long currentTime = System.currentTimeMillis();

conf.setInt(SIZE_LIMIT_KEY, 10);
conf.setLong(TIME_LIMIT_KEY, 10);

CloseChecker closeChecker = new CloseChecker(conf, currentTime);
assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime));
assertFalse(closeChecker.isSizeLimit(enableWrite, 10L));

closeChecker = new CloseChecker(conf, currentTime);
assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + 11));
assertFalse(closeChecker.isSizeLimit(enableWrite, 11L));

closeChecker = new CloseChecker(conf, currentTime);
assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 11));
assertTrue(closeChecker.isSizeLimit(disableWrite, 11L));

for (int i = 0; i < 10; i++) {
int plusTime = 5 * i;
assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + plusTime));
assertFalse(closeChecker.isSizeLimit(enableWrite, 5L));
}

closeChecker = new CloseChecker(conf, currentTime);
assertFalse(closeChecker.isTimeLimit(disableWrite, currentTime + 6));
assertFalse(closeChecker.isSizeLimit(disableWrite, 6));
assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 12));
assertTrue(closeChecker.isSizeLimit(disableWrite, 6));
}
}