-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
HBaseTestingUtility.java
4458 lines (4084 loc) · 166 KB
/
HBaseTestingUtility.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.BindException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.logging.impl.Jdk14Logger;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Waiter.ExplainingPredicate;
import org.apache.hadoop.hbase.Waiter.Predicate;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.AsyncClusterConnection;
import org.apache.hadoop.hbase.client.BufferedMutator;
import org.apache.hadoop.hbase.client.ClusterConnectionFactory;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Consistency;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Hbck;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.TableState;
import org.apache.hadoop.hbase.fs.HFileSystem;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.io.hfile.BlockCache;
import org.apache.hadoop.hbase.io.hfile.ChecksumUtil;
import org.apache.hadoop.hbase.io.hfile.HFile;
import org.apache.hadoop.hbase.ipc.RpcServerInterface;
import org.apache.hadoop.hbase.ipc.ServerNotRunningYetException;
import org.apache.hadoop.hbase.mapreduce.MapreduceTestingShim;
import org.apache.hadoop.hbase.master.HMaster;
import org.apache.hadoop.hbase.master.RegionState;
import org.apache.hadoop.hbase.master.ServerManager;
import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
import org.apache.hadoop.hbase.master.assignment.RegionStateStore;
import org.apache.hadoop.hbase.master.assignment.RegionStates;
import org.apache.hadoop.hbase.mob.MobFileCache;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.regionserver.ChunkCreator;
import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.hadoop.hbase.regionserver.HRegionServer;
import org.apache.hadoop.hbase.regionserver.HStore;
import org.apache.hadoop.hbase.regionserver.InternalScanner;
import org.apache.hadoop.hbase.regionserver.MemStoreLABImpl;
import org.apache.hadoop.hbase.regionserver.Region;
import org.apache.hadoop.hbase.regionserver.RegionScanner;
import org.apache.hadoop.hbase.regionserver.RegionServerServices;
import org.apache.hadoop.hbase.regionserver.RegionServerStoppedException;
import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.security.UserProvider;
import org.apache.hadoop.hbase.security.visibility.VisibilityLabelsCache;
import org.apache.hadoop.hbase.trace.TraceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CommonFSUtils;
import org.apache.hadoop.hbase.util.FSTableDescriptors;
import org.apache.hadoop.hbase.util.FSUtils;
import org.apache.hadoop.hbase.util.JVMClusterUtil;
import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.RegionSplitter;
import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
import org.apache.hadoop.hbase.util.RetryCounter;
import org.apache.hadoop.hbase.util.Threads;
import org.apache.hadoop.hbase.wal.WAL;
import org.apache.hadoop.hbase.wal.WALFactory;
import org.apache.hadoop.hbase.zookeeper.EmptyWatcher;
import org.apache.hadoop.hbase.zookeeper.ZKConfig;
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.server.namenode.EditLogFileOutputStream;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MiniMRCluster;
import org.apache.hadoop.mapred.TaskLog;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.log4j.LogManager;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeper.States;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.Log4jLoggerAdapter;
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
/**
* Facility for testing HBase. Replacement for
* old HBaseTestCase and HBaseClusterTestCase functionality.
* Create an instance and keep it around testing HBase. This class is
* meant to be your one-stop shop for anything you might need testing. Manages
* one cluster at a time only. Managed cluster can be an in-process
* {@link MiniHBaseCluster}, or a deployed cluster of type {@code DistributedHBaseCluster}.
* Not all methods work with the real cluster.
* Depends on log4j being on classpath and
* hbase-site.xml for logging and test-run configuration. It does not set
* logging levels.
* In the configuration properties, default values for master-info-port and
* region-server-port are overridden such that a random port will be assigned (thus
* avoiding port contention if another local HBase instance is already running).
* <p>To preserve test data directories, pass the system property "hbase.testing.preserve.testdir"
* setting it to true.
*/
@InterfaceAudience.Public
@SuppressWarnings("deprecation")
public class HBaseTestingUtility extends HBaseZKTestingUtility {
/**
* System property key to get test directory value. Name is as it is because mini dfs has
* hard-codings to put test data here. It should NOT be used directly in HBase, as it's a property
* used in mini dfs.
* @deprecated since 2.0.0 and will be removed in 3.0.0. Can be used only with mini dfs.
* @see <a href="https://issues.apache.org/jira/browse/HBASE-19410">HBASE-19410</a>
*/
@Deprecated
private static final String TEST_DIRECTORY_KEY = "test.build.data";
public static final String REGIONS_PER_SERVER_KEY = "hbase.test.regions-per-server";
/**
* The default number of regions per regionserver when creating a pre-split
* table.
*/
public static final int DEFAULT_REGIONS_PER_SERVER = 3;
public static final String PRESPLIT_TEST_TABLE_KEY = "hbase.test.pre-split-table";
public static final boolean PRESPLIT_TEST_TABLE = true;
private MiniDFSCluster dfsCluster = null;
private volatile HBaseCluster hbaseCluster = null;
private MiniMRCluster mrCluster = null;
/** If there is a mini cluster running for this testing utility instance. */
private volatile boolean miniClusterRunning;
private String hadoopLogDir;
/** Directory on test filesystem where we put the data for this instance of
* HBaseTestingUtility*/
private Path dataTestDirOnTestFS = null;
private volatile AsyncClusterConnection asyncConnection;
/** Filesystem URI used for map-reduce mini-cluster setup */
private static String FS_URI;
/** This is for unit tests parameterized with a single boolean. */
public static final List<Object[]> MEMSTORETS_TAGS_PARAMETRIZED = memStoreTSAndTagsCombination();
/**
* Checks to see if a specific port is available.
*
* @param port the port number to check for availability
* @return <tt>true</tt> if the port is available, or <tt>false</tt> if not
*/
public static boolean available(int port) {
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
// Do nothing
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
/* should not be thrown */
}
}
}
return false;
}
/**
* Create all combinations of Bloom filters and compression algorithms for
* testing.
*/
private static List<Object[]> bloomAndCompressionCombinations() {
List<Object[]> configurations = new ArrayList<>();
for (Compression.Algorithm comprAlgo :
HBaseCommonTestingUtility.COMPRESSION_ALGORITHMS) {
for (BloomType bloomType : BloomType.values()) {
configurations.add(new Object[] { comprAlgo, bloomType });
}
}
return Collections.unmodifiableList(configurations);
}
/**
* Create combination of memstoreTS and tags
*/
private static List<Object[]> memStoreTSAndTagsCombination() {
List<Object[]> configurations = new ArrayList<>();
configurations.add(new Object[] { false, false });
configurations.add(new Object[] { false, true });
configurations.add(new Object[] { true, false });
configurations.add(new Object[] { true, true });
return Collections.unmodifiableList(configurations);
}
public static List<Object[]> memStoreTSTagsAndOffheapCombination() {
List<Object[]> configurations = new ArrayList<>();
configurations.add(new Object[] { false, false, true });
configurations.add(new Object[] { false, false, false });
configurations.add(new Object[] { false, true, true });
configurations.add(new Object[] { false, true, false });
configurations.add(new Object[] { true, false, true });
configurations.add(new Object[] { true, false, false });
configurations.add(new Object[] { true, true, true });
configurations.add(new Object[] { true, true, false });
return Collections.unmodifiableList(configurations);
}
public static final Collection<Object[]> BLOOM_AND_COMPRESSION_COMBINATIONS =
bloomAndCompressionCombinations();
/**
* <p>Create an HBaseTestingUtility using a default configuration.
*
* <p>Initially, all tmp files are written to a local test data directory.
* Once {@link #startMiniDFSCluster} is called, either directly or via
* {@link #startMiniCluster()}, tmp data will be written to the DFS directory instead.
*
* <p>Previously, there was a distinction between the type of utility returned by
* {@link #createLocalHTU()} and this constructor; this is no longer the case. All
* HBaseTestingUtility objects will behave as local until a DFS cluster is started,
* at which point they will switch to using mini DFS for storage.
*/
public HBaseTestingUtility() {
this(HBaseConfiguration.create());
}
/**
* <p>Create an HBaseTestingUtility using a given configuration.
*
* <p>Initially, all tmp files are written to a local test data directory.
* Once {@link #startMiniDFSCluster} is called, either directly or via
* {@link #startMiniCluster()}, tmp data will be written to the DFS directory instead.
*
* <p>Previously, there was a distinction between the type of utility returned by
* {@link #createLocalHTU()} and this constructor; this is no longer the case. All
* HBaseTestingUtility objects will behave as local until a DFS cluster is started,
* at which point they will switch to using mini DFS for storage.
*
* @param conf The configuration to use for further operations
*/
public HBaseTestingUtility(@Nullable Configuration conf) {
super(conf);
// a hbase checksum verification failure will cause unit tests to fail
ChecksumUtil.generateExceptionForChecksumFailureForTest(true);
// Save this for when setting default file:// breaks things
if (this.conf.get("fs.defaultFS") != null) {
this.conf.set("original.defaultFS", this.conf.get("fs.defaultFS"));
}
if (this.conf.get(HConstants.HBASE_DIR) != null) {
this.conf.set("original.hbase.dir", this.conf.get(HConstants.HBASE_DIR));
}
// Every cluster is a local cluster until we start DFS
// Note that conf could be null, but this.conf will not be
String dataTestDir = getDataTestDir().toString();
this.conf.set("fs.defaultFS","file:///");
this.conf.set(HConstants.HBASE_DIR, "file://" + dataTestDir);
LOG.debug("Setting {} to {}", HConstants.HBASE_DIR, dataTestDir);
this.conf.setBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE,false);
// If the value for random ports isn't set set it to true, thus making
// tests opt-out for random port assignment
this.conf.setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS,
this.conf.getBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, true));
}
/**
* @deprecated since 2.0.0 and will be removed in 3.0.0. Use {@link #HBaseTestingUtility()}
* instead.
* @return a normal HBaseTestingUtility
* @see #HBaseTestingUtility()
* @see <a href="https://issues.apache.org/jira/browse/HBASE-19841">HBASE-19841</a>
*/
@Deprecated
public static HBaseTestingUtility createLocalHTU() {
return new HBaseTestingUtility();
}
/**
* @deprecated since 2.0.0 and will be removed in 3.0.0. Use
* {@link #HBaseTestingUtility(Configuration)} instead.
* @return a normal HBaseTestingUtility
* @see #HBaseTestingUtility(Configuration)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-19841">HBASE-19841</a>
*/
@Deprecated
public static HBaseTestingUtility createLocalHTU(Configuration c) {
return new HBaseTestingUtility(c);
}
/**
* Close both the region {@code r} and it's underlying WAL. For use in tests.
*/
public static void closeRegionAndWAL(final Region r) throws IOException {
closeRegionAndWAL((HRegion)r);
}
/**
* Close both the HRegion {@code r} and it's underlying WAL. For use in tests.
*/
public static void closeRegionAndWAL(final HRegion r) throws IOException {
if (r == null) return;
r.close();
if (r.getWAL() == null) return;
r.getWAL().close();
}
/**
* Returns this classes's instance of {@link Configuration}. Be careful how
* you use the returned Configuration since {@link Connection} instances
* can be shared. The Map of Connections is keyed by the Configuration. If
* say, a Connection was being used against a cluster that had been shutdown,
* see {@link #shutdownMiniCluster()}, then the Connection will no longer
* be wholesome. Rather than use the return direct, its usually best to
* make a copy and use that. Do
* <code>Configuration c = new Configuration(INSTANCE.getConfiguration());</code>
* @return Instance of Configuration.
*/
@Override
public Configuration getConfiguration() {
return super.getConfiguration();
}
public void setHBaseCluster(HBaseCluster hbaseCluster) {
this.hbaseCluster = hbaseCluster;
}
/**
* Home our data in a dir under {@link #DEFAULT_BASE_TEST_DIRECTORY}.
* Give it a random name so can have many concurrent tests running if
* we need to. It needs to amend the {@link #TEST_DIRECTORY_KEY}
* System property, as it's what minidfscluster bases
* it data dir on. Moding a System property is not the way to do concurrent
* instances -- another instance could grab the temporary
* value unintentionally -- but not anything can do about it at moment;
* single instance only is how the minidfscluster works.
*
* We also create the underlying directory for
* hadoop.log.dir, mapreduce.cluster.local.dir and hadoop.tmp.dir, and set the values
* in the conf, and as a system property for hadoop.tmp.dir
*
* @return The calculated data test build directory, if newly-created.
*/
@Override
protected Path setupDataTestDir() {
Path testPath = super.setupDataTestDir();
if (null == testPath) {
return null;
}
createSubDirAndSystemProperty(
"hadoop.log.dir",
testPath, "hadoop-log-dir");
// This is defaulted in core-default.xml to /tmp/hadoop-${user.name}, but
// we want our own value to ensure uniqueness on the same machine
createSubDirAndSystemProperty(
"hadoop.tmp.dir",
testPath, "hadoop-tmp-dir");
// Read and modified in org.apache.hadoop.mapred.MiniMRCluster
createSubDir(
"mapreduce.cluster.local.dir",
testPath, "mapred-local-dir");
return testPath;
}
private void createSubDirAndSystemProperty(
String propertyName, Path parent, String subDirName){
String sysValue = System.getProperty(propertyName);
if (sysValue != null) {
// There is already a value set. So we do nothing but hope
// that there will be no conflicts
LOG.info("System.getProperty(\""+propertyName+"\") already set to: "+
sysValue + " so I do NOT create it in " + parent);
String confValue = conf.get(propertyName);
if (confValue != null && !confValue.endsWith(sysValue)){
LOG.warn(
propertyName + " property value differs in configuration and system: "+
"Configuration="+confValue+" while System="+sysValue+
" Erasing configuration value by system value."
);
}
conf.set(propertyName, sysValue);
} else {
// Ok, it's not set, so we create it as a subdirectory
createSubDir(propertyName, parent, subDirName);
System.setProperty(propertyName, conf.get(propertyName));
}
}
/**
* @return Where to write test data on the test filesystem; Returns working directory
* for the test filesystem by default
* @see #setupDataTestDirOnTestFS()
* @see #getTestFileSystem()
*/
private Path getBaseTestDirOnTestFS() throws IOException {
FileSystem fs = getTestFileSystem();
return new Path(fs.getWorkingDirectory(), "test-data");
}
/**
* @return META table descriptor
*/
public TableDescriptorBuilder getMetaTableDescriptorBuilder() {
try {
return FSTableDescriptors.createMetaTableDescriptorBuilder(conf);
} catch (IOException e) {
throw new RuntimeException("Unable to create META table descriptor", e);
}
}
/**
* Returns a Path in the test filesystem, obtained from {@link #getTestFileSystem()}
* to write temporary test data. Call this method after setting up the mini dfs cluster
* if the test relies on it.
* @return a unique path in the test filesystem
*/
public Path getDataTestDirOnTestFS() throws IOException {
if (dataTestDirOnTestFS == null) {
setupDataTestDirOnTestFS();
}
return dataTestDirOnTestFS;
}
/**
* Returns a Path in the test filesystem, obtained from {@link #getTestFileSystem()}
* to write temporary test data. Call this method after setting up the mini dfs cluster
* if the test relies on it.
* @return a unique path in the test filesystem
* @param subdirName name of the subdir to create under the base test dir
*/
public Path getDataTestDirOnTestFS(final String subdirName) throws IOException {
return new Path(getDataTestDirOnTestFS(), subdirName);
}
/**
* Sets up a path in test filesystem to be used by tests.
* Creates a new directory if not already setup.
*/
private void setupDataTestDirOnTestFS() throws IOException {
if (dataTestDirOnTestFS != null) {
LOG.warn("Data test on test fs dir already setup in "
+ dataTestDirOnTestFS.toString());
return;
}
dataTestDirOnTestFS = getNewDataTestDirOnTestFS();
}
/**
* Sets up a new path in test filesystem to be used by tests.
*/
private Path getNewDataTestDirOnTestFS() throws IOException {
//The file system can be either local, mini dfs, or if the configuration
//is supplied externally, it can be an external cluster FS. If it is a local
//file system, the tests should use getBaseTestDir, otherwise, we can use
//the working directory, and create a unique sub dir there
FileSystem fs = getTestFileSystem();
Path newDataTestDir;
String randomStr = getRandomUUID().toString();
if (fs.getUri().getScheme().equals(FileSystem.getLocal(conf).getUri().getScheme())) {
newDataTestDir = new Path(getDataTestDir(), randomStr);
File dataTestDir = new File(newDataTestDir.toString());
if (deleteOnExit()) dataTestDir.deleteOnExit();
} else {
Path base = getBaseTestDirOnTestFS();
newDataTestDir = new Path(base, randomStr);
if (deleteOnExit()) fs.deleteOnExit(newDataTestDir);
}
return newDataTestDir;
}
/**
* Cleans the test data directory on the test filesystem.
* @return True if we removed the test dirs
* @throws IOException
*/
public boolean cleanupDataTestDirOnTestFS() throws IOException {
boolean ret = getTestFileSystem().delete(dataTestDirOnTestFS, true);
if (ret)
dataTestDirOnTestFS = null;
return ret;
}
/**
* Cleans a subdirectory under the test data directory on the test filesystem.
* @return True if we removed child
* @throws IOException
*/
public boolean cleanupDataTestDirOnTestFS(String subdirName) throws IOException {
Path cpath = getDataTestDirOnTestFS(subdirName);
return getTestFileSystem().delete(cpath, true);
}
/**
* Start a minidfscluster.
* @param servers How many DNs to start.
* @throws Exception
* @see #shutdownMiniDFSCluster()
* @return The mini dfs cluster created.
*/
public MiniDFSCluster startMiniDFSCluster(int servers) throws Exception {
return startMiniDFSCluster(servers, null);
}
/**
* Start a minidfscluster.
* This is useful if you want to run datanode on distinct hosts for things
* like HDFS block location verification.
* If you start MiniDFSCluster without host names, all instances of the
* datanodes will have the same host name.
* @param hosts hostnames DNs to run on.
* @throws Exception
* @see #shutdownMiniDFSCluster()
* @return The mini dfs cluster created.
*/
public MiniDFSCluster startMiniDFSCluster(final String hosts[])
throws Exception {
if ( hosts != null && hosts.length != 0) {
return startMiniDFSCluster(hosts.length, hosts);
} else {
return startMiniDFSCluster(1, null);
}
}
/**
* Start a minidfscluster.
* Can only create one.
* @param servers How many DNs to start.
* @param hosts hostnames DNs to run on.
* @throws Exception
* @see #shutdownMiniDFSCluster()
* @return The mini dfs cluster created.
*/
public MiniDFSCluster startMiniDFSCluster(int servers, final String hosts[])
throws Exception {
return startMiniDFSCluster(servers, null, hosts);
}
private void setFs() throws IOException {
if(this.dfsCluster == null){
LOG.info("Skipping setting fs because dfsCluster is null");
return;
}
FileSystem fs = this.dfsCluster.getFileSystem();
FSUtils.setFsDefault(this.conf, new Path(fs.getUri()));
// re-enable this check with dfs
conf.unset(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE);
}
public MiniDFSCluster startMiniDFSCluster(int servers, final String racks[], String hosts[])
throws Exception {
createDirsAndSetProperties();
EditLogFileOutputStream.setShouldSkipFsyncForTesting(true);
// Error level to skip some warnings specific to the minicluster. See HBASE-4709
org.apache.log4j.Logger.getLogger(org.apache.hadoop.metrics2.util.MBeans.class).
setLevel(org.apache.log4j.Level.ERROR);
org.apache.log4j.Logger.getLogger(org.apache.hadoop.metrics2.impl.MetricsSystemImpl.class).
setLevel(org.apache.log4j.Level.ERROR);
TraceUtil.initTracer(conf);
this.dfsCluster = new MiniDFSCluster(0, this.conf, servers, true, true,
true, null, racks, hosts, null);
// Set this just-started cluster as our filesystem.
setFs();
// Wait for the cluster to be totally up
this.dfsCluster.waitClusterUp();
//reset the test directory for test file system
dataTestDirOnTestFS = null;
String dataTestDir = getDataTestDir().toString();
conf.set(HConstants.HBASE_DIR, dataTestDir);
LOG.debug("Setting {} to {}", HConstants.HBASE_DIR, dataTestDir);
return this.dfsCluster;
}
public MiniDFSCluster startMiniDFSClusterForTestWAL(int namenodePort) throws IOException {
createDirsAndSetProperties();
dfsCluster = new MiniDFSCluster(namenodePort, conf, 5, false, true, true, null,
null, null, null);
return dfsCluster;
}
/** This is used before starting HDFS and map-reduce mini-clusters */
private void createDirsAndSetProperties() throws IOException {
setupClusterTestDir();
conf.set(TEST_DIRECTORY_KEY, clusterTestDir.getPath());
System.setProperty(TEST_DIRECTORY_KEY, clusterTestDir.getPath());
createDirAndSetProperty("cache_data", "test.cache.data");
createDirAndSetProperty("hadoop_tmp", "hadoop.tmp.dir");
hadoopLogDir = createDirAndSetProperty("hadoop_logs", "hadoop.log.dir");
createDirAndSetProperty("mapred_local", "mapreduce.cluster.local.dir");
createDirAndSetProperty("mapred_temp", "mapreduce.cluster.temp.dir");
enableShortCircuit();
Path root = getDataTestDirOnTestFS("hadoop");
conf.set(MapreduceTestingShim.getMROutputDirProp(),
new Path(root, "mapred-output-dir").toString());
conf.set("mapreduce.jobtracker.system.dir", new Path(root, "mapred-system-dir").toString());
conf.set("mapreduce.jobtracker.staging.root.dir",
new Path(root, "mapreduce-jobtracker-staging-root-dir").toString());
conf.set("mapreduce.job.working.dir", new Path(root, "mapred-working-dir").toString());
conf.set("yarn.app.mapreduce.am.staging-dir",
new Path(root, "mapreduce-am-staging-root-dir").toString());
}
/**
* Check whether the tests should assume NEW_VERSION_BEHAVIOR when creating
* new column families. Default to false.
*/
public boolean isNewVersionBehaviorEnabled(){
final String propName = "hbase.tests.new.version.behavior";
String v = System.getProperty(propName);
if (v != null){
return Boolean.parseBoolean(v);
}
return false;
}
/**
* Get the HBase setting for dfs.client.read.shortcircuit from the conf or a system property.
* This allows to specify this parameter on the command line.
* If not set, default is true.
*/
public boolean isReadShortCircuitOn(){
final String propName = "hbase.tests.use.shortcircuit.reads";
String readOnProp = System.getProperty(propName);
if (readOnProp != null){
return Boolean.parseBoolean(readOnProp);
} else {
return conf.getBoolean(propName, false);
}
}
/** Enable the short circuit read, unless configured differently.
* Set both HBase and HDFS settings, including skipping the hdfs checksum checks.
*/
private void enableShortCircuit() {
if (isReadShortCircuitOn()) {
String curUser = System.getProperty("user.name");
LOG.info("read short circuit is ON for user " + curUser);
// read short circuit, for hdfs
conf.set("dfs.block.local-path-access.user", curUser);
// read short circuit, for hbase
conf.setBoolean("dfs.client.read.shortcircuit", true);
// Skip checking checksum, for the hdfs client and the datanode
conf.setBoolean("dfs.client.read.shortcircuit.skip.checksum", true);
} else {
LOG.info("read short circuit is OFF");
}
}
private String createDirAndSetProperty(final String relPath, String property) {
String path = getDataTestDir(relPath).toString();
System.setProperty(property, path);
conf.set(property, path);
new File(path).mkdirs();
LOG.info("Setting " + property + " to " + path + " in system properties and HBase conf");
return path;
}
/**
* Shuts down instance created by call to {@link #startMiniDFSCluster(int)}
* or does nothing.
* @throws IOException
*/
public void shutdownMiniDFSCluster() throws IOException {
if (this.dfsCluster != null) {
// The below throws an exception per dn, AsynchronousCloseException.
this.dfsCluster.shutdown();
dfsCluster = null;
dataTestDirOnTestFS = null;
FSUtils.setFsDefault(this.conf, new Path("file:///"));
}
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper where WAL's walDir is created separately.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param createWALDir Whether to create a new WAL directory.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(boolean createWALDir) throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.createWALDir(createWALDir).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param createRootDir Whether to create a new root or data directory path.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numSlaves, boolean createRootDir)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numRegionServers(numSlaves).numDataNodes(numSlaves).createRootDir(createRootDir).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param createRootDir Whether to create a new root or data directory path.
* @param createWALDir Whether to create a new WAL directory.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numSlaves, boolean createRootDir,
boolean createWALDir) throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numRegionServers(numSlaves).numDataNodes(numSlaves).createRootDir(createRootDir)
.createWALDir(createWALDir).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param createRootDir Whether to create a new root or data directory path.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numSlaves, boolean createRootDir)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).numRegionServers(numSlaves).createRootDir(createRootDir)
.numDataNodes(numSlaves).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numSlaves) throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).numRegionServers(numSlaves).numDataNodes(numSlaves).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param dataNodeHosts The hostnames of DataNodes to run on. If not null, its size will overwrite
* HDFS data node number.
* @param createRootDir Whether to create a new root or data directory path.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numSlaves, String[] dataNodeHosts,
boolean createRootDir) throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).numRegionServers(numSlaves).createRootDir(createRootDir)
.numDataNodes(numSlaves).dataNodeHosts(dataNodeHosts).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param dataNodeHosts The hostnames of DataNodes to run on. If not null, its size will overwrite
* HDFS data node number.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numSlaves, String[] dataNodeHosts)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).numRegionServers(numSlaves)
.numDataNodes(numSlaves).dataNodeHosts(dataNodeHosts).build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numRegionServers Number of region servers.
* @param numDataNodes Number of datanodes.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numRegionServers, int numDataNodes)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).numRegionServers(numRegionServers).numDataNodes(numDataNodes)
.build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numSlaves Slave node number, for both HBase region server and HDFS data node.
* @param dataNodeHosts The hostnames of DataNodes to run on. If not null, its size will overwrite
* HDFS data node number.
* @param masterClass The class to use as HMaster, or null for default.
* @param rsClass The class to use as HRegionServer, or null for default.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numSlaves, String[] dataNodeHosts,
Class<? extends HMaster> masterClass,
Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> rsClass)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).masterClass(masterClass)
.numRegionServers(numSlaves).rsClass(rsClass)
.numDataNodes(numSlaves).dataNodeHosts(dataNodeHosts)
.build();
return startMiniCluster(option);
}
/**
* Start up a minicluster of hbase, dfs, and zookeeper.
* All other options will use default values, defined in {@link StartMiniClusterOption.Builder}.
* @param numMasters Master node number.
* @param numRegionServers Number of region servers.
* @param numDataNodes Number of datanodes.
* @param dataNodeHosts The hostnames of DataNodes to run on. If not null, its size will overwrite
* HDFS data node number.
* @param masterClass The class to use as HMaster, or null for default.
* @param rsClass The class to use as HRegionServer, or null for default.
* @return The mini HBase cluster created.
* @see #shutdownMiniCluster()
* @deprecated since 2.2.0 and will be removed in 4.0.0. Use
* {@link #startMiniCluster(StartMiniClusterOption)} instead.
* @see #startMiniCluster(StartMiniClusterOption)
* @see <a href="https://issues.apache.org/jira/browse/HBASE-21071">HBASE-21071</a>
*/
@Deprecated
public MiniHBaseCluster startMiniCluster(int numMasters, int numRegionServers, int numDataNodes,
String[] dataNodeHosts, Class<? extends HMaster> masterClass,
Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> rsClass)
throws Exception {
StartMiniClusterOption option = StartMiniClusterOption.builder()
.numMasters(numMasters).masterClass(masterClass)
.numRegionServers(numRegionServers).rsClass(rsClass)