forked from googleapis/java-bigtable-hbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HbaseToCloudBigtableBidirectionalReplicationEndpointTest.java
204 lines (174 loc) · 7.99 KB
/
HbaseToCloudBigtableBidirectionalReplicationEndpointTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2023 Google LLC
*
* Licensed 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 com.google.cloud.bigtable.hbase2_x.replication;
import static com.google.cloud.bigtable.hbase.replication.utils.TestUtils.CF2;
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import com.google.cloud.bigtable.hbase.replication.utils.TestUtils;
import com.google.cloud.bigtable.test.helper.BigtableEmulatorRule;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Test bidirectional replication. This test is separate from the other endpoint tests because it
* requires spinning up a cluster with additional config settings.
*/
@RunWith(JUnit4.class)
public class HbaseToCloudBigtableBidirectionalReplicationEndpointTest {
public static class TestReplicationEndpoint extends HbaseToCloudBigtableReplicationEndpoint {
static AtomicInteger replicatingEntries = new AtomicInteger();
@Override
public boolean replicate(ReplicateContext replicateContext) {
boolean result = super.replicate(replicateContext);
replicatingEntries.getAndAdd(replicateContext.getEntries().size());
return result;
}
}
private static final Logger LOG =
LoggerFactory.getLogger(HbaseToCloudBigtableBidirectionalReplicationEndpointTest.class);
private static HBaseTestingUtility hbaseTestingUtil;
private static ReplicationAdmin replicationAdmin;
@ClassRule public static final BigtableEmulatorRule bigtableEmulator = new BigtableEmulatorRule();
private static Connection cbtConnection;
private static Connection hbaseConnection;
private Table hbaseTable;
private Table cbtTable;
private static byte[] cbtQualifier = "customCbtQualifier".getBytes();
private static byte[] hbaseQualifier = "customHbaseQualifier".getBytes();
@BeforeClass
public static void setUpCluster() throws Exception {
// Prepare HBase mini cluster configuration
Configuration conf = new HBaseTestingUtility().getConfiguration();
// Set CBT related configs.
conf.set("google.bigtable.instance.id", "test-instance");
conf.set("google.bigtable.project.id", "test-project");
// This config will connect Replication endpoint to the emulator and not the prod CBT.
conf.set("google.bigtable.emulator.endpoint.host", "localhost:" + bigtableEmulator.getPort());
// Set bidirectional replication related settings
conf.set("google.bigtable.replication.enable_bidirectional_replication", "true");
conf.set("google.bigtable.replication.hbase_qualifier", new String(hbaseQualifier));
conf.set("google.bigtable.replication.cbt_qualifier", new String(cbtQualifier));
hbaseTestingUtil = new HBaseTestingUtility(conf);
hbaseTestingUtil.startMiniCluster(2);
replicationAdmin = new ReplicationAdmin(hbaseTestingUtil.getConfiguration());
cbtConnection = BigtableConfiguration.connect(conf);
hbaseConnection = hbaseTestingUtil.getConnection();
// Setup Replication in HBase mini cluster
ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
peerConfig.setReplicationEndpointImpl(
HbaseToCloudBigtableBidirectionalReplicationEndpointTest.TestReplicationEndpoint.class
.getTypeName());
// Cluster key is required, we don't really have a clusterKey for CBT.
peerConfig.setClusterKey(hbaseTestingUtil.getClusterKey());
replicationAdmin.addPeer("cbt", peerConfig);
}
@AfterClass
public static void tearDown() throws Exception {
cbtConnection.close();
hbaseConnection.close();
replicationAdmin.close();
hbaseTestingUtil.shutdownMiniCluster();
}
@After
public void tearDownTable() throws IOException {
cbtTable.close();
hbaseTable.close();
}
@Before
public void setupTestCase() throws IOException {
// Create and set the empty tables
TableName table1 = TableName.valueOf(UUID.randomUUID().toString());
createTables(table1, HConstants.REPLICATION_SCOPE_GLOBAL, HConstants.REPLICATION_SCOPE_GLOBAL);
cbtTable = cbtConnection.getTable(table1);
hbaseTable = hbaseConnection.getTable(table1);
// Reset the entry counts for TestReplicationEndpoint
TestReplicationEndpoint.replicatingEntries.set(0);
}
private void createTables(TableName tableName, int cf1Scope, int cf2Scope) throws IOException {
// Create table in HBase
HTableDescriptor htd = hbaseTestingUtil.createTableDescriptor(tableName.getNameAsString());
HColumnDescriptor cf1 = new HColumnDescriptor(TestUtils.CF1);
cf1.setMaxVersions(100);
htd.addFamily(cf1);
HColumnDescriptor cf2 = new HColumnDescriptor(CF2);
cf2.setMaxVersions(100);
htd.addFamily(cf2);
// Enables replication to all peers, including CBT
cf1.setScope(cf1Scope);
cf2.setScope(cf2Scope);
hbaseTestingUtil.getHBaseAdmin().createTable(htd);
cbtConnection.getAdmin().createTable(htd);
}
/**
* Bidirectional replication should replicate source entry and drop cbt-replicated entry to
* prevent loops from forming.
*/
@Test
public void testDropsReplicatedEntry() throws IOException, InterruptedException {
RowMutations mutationToDrop = new RowMutations(TestUtils.ROW_KEY);
mutationToDrop.add(
Arrays.asList(
new Put(TestUtils.ROW_KEY)
.addColumn(TestUtils.CF1, TestUtils.COL_QUALIFIER, 0, TestUtils.VALUE),
// Special delete mutation signifying this came from Bigtable replicator
new Delete(TestUtils.ROW_KEY).addColumns(TestUtils.CF1, cbtQualifier, 0)));
RowMutations mutationToReplicate = new RowMutations(TestUtils.ROW_KEY_2);
mutationToReplicate.add(
Arrays.asList(
new Put(TestUtils.ROW_KEY_2)
.addColumn(TestUtils.CF1, TestUtils.COL_QUALIFIER, 0, TestUtils.VALUE)));
hbaseTable.mutateRow(mutationToDrop);
hbaseTable.mutateRow(mutationToReplicate);
// Wait for replication
TestUtils.waitForReplication(
() -> {
// Both entries should've gone through the replication process
// However, one would've been filtered out during this process
return TestReplicationEndpoint.replicatingEntries.get() >= 2;
});
// Hbase table should have both mutations
Assert.assertEquals(1, hbaseTable.get(new Get(TestUtils.ROW_KEY)).size());
Assert.assertEquals(1, hbaseTable.get(new Get(TestUtils.ROW_KEY_2)).size());
// Cbt table should have only one mutation
Assert.assertTrue(cbtTable.get(new Get(TestUtils.ROW_KEY)).isEmpty());
Assert.assertEquals(1, cbtTable.get(new Get(TestUtils.ROW_KEY_2)).size());
}
}