Skip to content

Commit 75292b5

Browse files
chenxu14chandrasekhar-188k
authored andcommitted
HBASE-22335 do add hfile ref only when replication_scope is 1
1 parent 99bd5b5 commit 75292b5

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.hadoop.conf.Configuration;
2424
import org.apache.hadoop.fs.Path;
2525
import org.apache.hadoop.hbase.HConstants;
26+
import org.apache.hadoop.hbase.TableName;
2627
import org.apache.hadoop.hbase.coprocessor.CoreCoprocessor;
2728
import org.apache.hadoop.hbase.coprocessor.HasRegionServerServices;
2829
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
@@ -31,6 +32,7 @@
3132
import org.apache.hadoop.hbase.coprocessor.RegionObserver;
3233
import org.apache.hadoop.hbase.regionserver.HRegionServer;
3334
import org.apache.hadoop.hbase.regionserver.RegionServerServices;
35+
import org.apache.hadoop.hbase.util.Bytes;
3436
import org.apache.hadoop.hbase.util.Pair;
3537
import org.apache.yetus.audience.InterfaceAudience;
3638
import org.slf4j.Logger;
@@ -65,11 +67,23 @@ public void preCommitStoreFile(final ObserverContext<? extends RegionCoprocessor
6567
+ "data replication.");
6668
return;
6769
}
70+
TableName tableName = env.getRegionInfo().getTable();
71+
if (
72+
env.getRegion().getTableDescriptor().getColumnFamily(family).getScope()
73+
!= HConstants.REPLICATION_SCOPE_GLOBAL
74+
) {
75+
LOG.debug(
76+
"Skipping recording bulk load entries in preCommitStoreFile for table:{}, family:{}, Because the replication is not enabled",
77+
tableName, Bytes.toString(family));
78+
return;
79+
}
80+
6881
// This is completely cheating AND getting a HRegionServer from a RegionServerEnvironment is
6982
// just going to break. This is all private. Not allowed. Regions shouldn't assume they are
7083
// hosted in a RegionServer. TODO: fix.
7184
RegionServerServices rss = ((HasRegionServerServices) env).getRegionServerServices();
7285
Replication rep = (Replication) ((HRegionServer) rss).getReplicationSourceService();
73-
rep.addHFileRefsToQueue(env.getRegionInfo().getTable(), family, pairs);
86+
87+
rep.addHFileRefsToQueue(tableName, family, pairs);
7488
}
7589
}

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestBulkLoadReplication.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import static org.apache.hadoop.hbase.HConstants.REPLICATION_CLUSTER_ID;
2121
import static org.apache.hadoop.hbase.HConstants.REPLICATION_CONF_DIR;
2222
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNotEquals;
2324
import static org.junit.Assert.assertTrue;
2425

2526
import java.io.File;
2627
import java.io.FileOutputStream;
2728
import java.io.IOException;
2829
import java.net.UnknownHostException;
30+
import java.util.ArrayList;
31+
import java.util.HashMap;
2932
import java.util.List;
3033
import java.util.Map;
3134
import java.util.Optional;
@@ -66,6 +69,8 @@
6669
import org.apache.hadoop.hbase.tool.BulkLoadHFilesTool;
6770
import org.apache.hadoop.hbase.util.Bytes;
6871
import org.apache.hadoop.hbase.util.Pair;
72+
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
73+
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
6974
import org.apache.hadoop.hdfs.MiniDFSCluster;
7075
import org.junit.After;
7176
import org.junit.Before;
@@ -79,6 +84,8 @@
7984
import org.slf4j.Logger;
8085
import org.slf4j.LoggerFactory;
8186

87+
import org.apache.hbase.thirdparty.org.apache.commons.collections4.CollectionUtils;
88+
8289
/**
8390
* Integration test for bulk load replication. Defines three clusters, with the following
8491
* replication topology: "1 <-> 2 <-> 3" (active-active between 1 and 2, and active-active between 2
@@ -322,4 +329,84 @@ public void postBulkLoadHFile(ObserverContext<? extends RegionCoprocessorEnviron
322329
});
323330
}
324331
}
332+
333+
@Test
334+
public void testBulkloadReplicationActiveActiveForNoRepFamily() throws Exception {
335+
Table peer1TestTable = UTIL1.getConnection().getTable(TestReplicationBase.tableName);
336+
Table peer2TestTable = UTIL2.getConnection().getTable(TestReplicationBase.tableName);
337+
Table peer3TestTable = UTIL3.getConnection().getTable(TestReplicationBase.tableName);
338+
byte[] row = Bytes.toBytes("004");
339+
byte[] value = Bytes.toBytes("v4");
340+
assertBulkLoadConditionsForNoRepFamily(row, value, UTIL1, peer1TestTable, peer2TestTable,
341+
peer3TestTable);
342+
// additional wait to make sure no extra bulk load happens
343+
Thread.sleep(400);
344+
assertEquals(1, BULK_LOADS_COUNT.get());
345+
ZKWatcher zkw = UTIL1.getZooKeeperWatcher();
346+
List<String> znodes = ZKUtil.listChildrenNoWatch(zkw, "/1/replication/hfile-refs/2");
347+
assertTrue(CollectionUtils.isEmpty(znodes));
348+
349+
}
350+
351+
private void assertBulkLoadConditionsForNoRepFamily(byte[] row, byte[] value,
352+
HBaseTestingUtil utility, Table... tables) throws Exception {
353+
BULK_LOAD_LATCH = new CountDownLatch(1);
354+
bulkLoadOnClusterForNoRepFamily(row, value, utility);
355+
assertTrue(BULK_LOAD_LATCH.await(1, TimeUnit.MINUTES));
356+
assertTableHasValue(tables[0], row, value);
357+
assertTableNotHasValue(tables[1], row, value);
358+
assertTableNotHasValue(tables[2], row, value);
359+
}
360+
361+
private void bulkLoadOnClusterForNoRepFamily(byte[] row, byte[] value, HBaseTestingUtil cluster)
362+
throws Exception {
363+
String bulkloadFile = createHFileForNoRepFamilies(row, value, cluster.getConfiguration());
364+
Path bulkLoadFilePath = new Path(bulkloadFile);
365+
copyToHdfsForNoRepFamily(bulkloadFile, cluster.getDFSCluster());
366+
BulkLoadHFilesTool bulkLoadHFilesTool = new BulkLoadHFilesTool(cluster.getConfiguration());
367+
Map<byte[], List<Path>> family2Files = new HashMap<>();
368+
List<Path> files = new ArrayList<>();
369+
files.add(new Path(
370+
BULK_LOAD_BASE_DIR + "/" + Bytes.toString(noRepfamName) + "/" + bulkLoadFilePath.getName()));
371+
family2Files.put(noRepfamName, files);
372+
bulkLoadHFilesTool.bulkLoad(tableName, family2Files);
373+
}
374+
375+
private String createHFileForNoRepFamilies(byte[] row, byte[] value, Configuration clusterConfig)
376+
throws IOException {
377+
ExtendedCellBuilder cellBuilder = ExtendedCellBuilderFactory.create(CellBuilderType.DEEP_COPY);
378+
cellBuilder.setRow(row).setFamily(TestReplicationBase.noRepfamName)
379+
.setQualifier(Bytes.toBytes("1")).setValue(value).setType(Cell.Type.Put);
380+
381+
HFile.WriterFactory hFileFactory = HFile.getWriterFactoryNoCache(clusterConfig);
382+
// TODO We need a way to do this without creating files
383+
File hFileLocation = testFolder.newFile();
384+
FSDataOutputStream out = new FSDataOutputStream(new FileOutputStream(hFileLocation), null);
385+
try {
386+
hFileFactory.withOutputStream(out);
387+
hFileFactory.withFileContext(new HFileContextBuilder().build());
388+
HFile.Writer writer = hFileFactory.create();
389+
try {
390+
writer.append(new KeyValue(cellBuilder.build()));
391+
} finally {
392+
writer.close();
393+
}
394+
} finally {
395+
out.close();
396+
}
397+
return hFileLocation.getAbsoluteFile().getAbsolutePath();
398+
}
399+
400+
private void copyToHdfsForNoRepFamily(String bulkLoadFilePath, MiniDFSCluster cluster)
401+
throws Exception {
402+
Path bulkLoadDir = new Path(BULK_LOAD_BASE_DIR + "/" + Bytes.toString(noRepfamName) + "/");
403+
cluster.getFileSystem().mkdirs(bulkLoadDir);
404+
cluster.getFileSystem().copyFromLocalFile(new Path(bulkLoadFilePath), bulkLoadDir);
405+
}
406+
407+
private void assertTableNotHasValue(Table table, byte[] row, byte[] value) throws IOException {
408+
Get get = new Get(row);
409+
Result result = table.get(get);
410+
assertNotEquals(Bytes.toString(value), Bytes.toString(result.value()));
411+
}
325412
}

0 commit comments

Comments
 (0)