1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.replication.regionserver;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.Stoppable;
34  import org.apache.hadoop.hbase.client.Get;
35  import org.apache.hadoop.hbase.client.HTable;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.client.ResultScanner;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.regionserver.wal.HLog;
40  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
41  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.junit.AfterClass;
44  import org.junit.Before;
45  import org.junit.BeforeClass;
46  import org.junit.Test;
47  
48  public class TestReplicationSink {
49    private static final Log LOG = LogFactory.getLog(TestReplicationSink.class);
50    private static final int BATCH_SIZE = 10;
51  
52    private final static HBaseTestingUtility TEST_UTIL =
53        new HBaseTestingUtility();
54  
55    private static ReplicationSink SINK;
56  
57    private static final byte[] TABLE_NAME1 =
58        Bytes.toBytes("table1");
59    private static final byte[] TABLE_NAME2 =
60        Bytes.toBytes("table2");
61  
62    private static final byte[] FAM_NAME1 = Bytes.toBytes("info1");
63    private static final byte[] FAM_NAME2 = Bytes.toBytes("info2");
64  
65    private static HTable table1;
66    private static Stoppable STOPPABLE = new Stoppable() {
67      final AtomicBoolean stop = new AtomicBoolean(false);
68  
69      @Override
70      public boolean isStopped() {
71        return this.stop.get();
72      }
73  
74      @Override
75      public void stop(String why) {
76        LOG.info("STOPPING BECAUSE: " + why);
77        this.stop.set(true);
78      }
79      
80    };
81  
82    private static HTable table2;
83  
84     /**
85     * @throws java.lang.Exception
86     */
87    @BeforeClass
88    public static void setUpBeforeClass() throws Exception {
89      TEST_UTIL.getConfiguration().setBoolean("dfs.support.append", true);
90      TEST_UTIL.getConfiguration().setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
91      TEST_UTIL.startMiniCluster(3);
92      SINK =
93        new ReplicationSink(new Configuration(TEST_UTIL.getConfiguration()), STOPPABLE);
94      table1 = TEST_UTIL.createTable(TABLE_NAME1, FAM_NAME1);
95      table2 = TEST_UTIL.createTable(TABLE_NAME2, FAM_NAME2);
96    }
97  
98    /**
99     * @throws java.lang.Exception
100    */
101   @AfterClass
102   public static void tearDownAfterClass() throws Exception {
103     STOPPABLE.stop("Shutting down");
104     TEST_UTIL.shutdownMiniCluster();
105   }
106 
107   /**
108    * @throws java.lang.Exception
109    */
110   @Before
111   public void setUp() throws Exception {
112     table1 = TEST_UTIL.truncateTable(TABLE_NAME1);
113     table2 = TEST_UTIL.truncateTable(TABLE_NAME2);
114   }
115 
116   /**
117    * Insert a whole batch of entries
118    * @throws Exception
119    */
120   @Test
121   public void testBatchSink() throws Exception {
122     HLog.Entry[] entries = new HLog.Entry[BATCH_SIZE];
123     for(int i = 0; i < BATCH_SIZE; i++) {
124       entries[i] = createEntry(TABLE_NAME1, i, KeyValue.Type.Put);
125     }
126     SINK.replicateEntries(entries);
127     Scan scan = new Scan();
128     ResultScanner scanRes = table1.getScanner(scan);
129     assertEquals(BATCH_SIZE, scanRes.next(BATCH_SIZE).length);
130   }
131 
132   /**
133    * Insert a mix of puts and deletes
134    * @throws Exception
135    */
136   @Test
137   public void testMixedPutDelete() throws Exception {
138     HLog.Entry[] entries = new HLog.Entry[BATCH_SIZE/2];
139     for(int i = 0; i < BATCH_SIZE/2; i++) {
140       entries[i] = createEntry(TABLE_NAME1, i, KeyValue.Type.Put);
141     }
142     SINK.replicateEntries(entries);
143 
144     entries = new HLog.Entry[BATCH_SIZE];
145     for(int i = 0; i < BATCH_SIZE; i++) {
146       entries[i] = createEntry(TABLE_NAME1, i,
147           i % 2 != 0 ? KeyValue.Type.Put: KeyValue.Type.DeleteColumn);
148     }
149 
150     SINK.replicateEntries(entries);
151     Scan scan = new Scan();
152     ResultScanner scanRes = table1.getScanner(scan);
153     assertEquals(BATCH_SIZE/2, scanRes.next(BATCH_SIZE).length);
154   }
155 
156   /**
157    * Insert to 2 different tables
158    * @throws Exception
159    */
160   @Test
161   public void testMixedPutTables() throws Exception {
162     HLog.Entry[] entries = new HLog.Entry[BATCH_SIZE];
163     for(int i = 0; i < BATCH_SIZE; i++) {
164       entries[i] =
165           createEntry( i % 2 == 0 ? TABLE_NAME2 : TABLE_NAME1,
166               i, KeyValue.Type.Put);
167     }
168 
169     SINK.replicateEntries(entries);
170     Scan scan = new Scan();
171     ResultScanner scanRes = table2.getScanner(scan);
172     for(Result res : scanRes) {
173       assertTrue(Bytes.toInt(res.getRow()) % 2 == 0);
174     }
175   }
176 
177   /**
178    * Insert then do different types of deletes
179    * @throws Exception
180    */
181   @Test
182   public void testMixedDeletes() throws Exception {
183     HLog.Entry[] entries = new HLog.Entry[3];
184     for(int i = 0; i < 3; i++) {
185       entries[i] = createEntry(TABLE_NAME1, i, KeyValue.Type.Put);
186     }
187     SINK.replicateEntries(entries);
188     entries = new HLog.Entry[3];
189 
190     entries[0] = createEntry(TABLE_NAME1, 0, KeyValue.Type.DeleteColumn);
191     entries[1] = createEntry(TABLE_NAME1, 1, KeyValue.Type.DeleteFamily);
192     entries[2] = createEntry(TABLE_NAME1, 2, KeyValue.Type.DeleteColumn);
193 
194     SINK.replicateEntries(entries);
195 
196     Scan scan = new Scan();
197     ResultScanner scanRes = table1.getScanner(scan);
198     assertEquals(0, scanRes.next(3).length);
199   }
200 
201   /**
202    * Puts are buffered, but this tests when a delete (not-buffered) is applied
203    * before the actual Put that creates it.
204    * @throws Exception
205    */
206   @Test
207   public void testApplyDeleteBeforePut() throws Exception {
208     HLog.Entry[] entries = new HLog.Entry[5];
209     for(int i = 0; i < 2; i++) {
210       entries[i] = createEntry(TABLE_NAME1, i, KeyValue.Type.Put);
211     }
212     entries[2] = createEntry(TABLE_NAME1, 1, KeyValue.Type.DeleteFamily);
213     for(int i = 3; i < 5; i++) {
214       entries[i] = createEntry(TABLE_NAME1, i, KeyValue.Type.Put);
215     }
216     SINK.replicateEntries(entries);
217     Get get = new Get(Bytes.toBytes(1));
218     Result res = table1.get(get);
219     assertEquals(0, res.size());
220   }
221 
222   private HLog.Entry createEntry(byte [] table, int row,  KeyValue.Type type) {
223     byte[] fam = Bytes.equals(table, TABLE_NAME1) ? FAM_NAME1 : FAM_NAME2;
224     byte[] rowBytes = Bytes.toBytes(row);
225     // Just make sure we don't get the same ts for two consecutive rows with
226     // same key
227     try {
228       Thread.sleep(1);
229     } catch (InterruptedException e) {
230       LOG.info("Was interrupted while sleep, meh", e);
231     }
232     final long now = System.currentTimeMillis();
233     KeyValue kv = null;
234     if(type.getCode() == KeyValue.Type.Put.getCode()) {
235       kv = new KeyValue(rowBytes, fam, fam, now,
236           KeyValue.Type.Put, Bytes.toBytes(row));
237     } else if (type.getCode() == KeyValue.Type.DeleteColumn.getCode()) {
238         kv = new KeyValue(rowBytes, fam, fam,
239             now, KeyValue.Type.DeleteColumn);
240     } else if (type.getCode() == KeyValue.Type.DeleteFamily.getCode()) {
241         kv = new KeyValue(rowBytes, fam, null,
242             now, KeyValue.Type.DeleteFamily);
243     }
244 
245     HLogKey key = new HLogKey(table, table, now, now,
246         HConstants.DEFAULT_CLUSTER_ID);
247 
248     WALEdit edit = new WALEdit();
249     edit.add(kv);
250 
251     return new HLog.Entry(key, edit);
252   }
253 }