1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.DataInputStream;
23 import java.io.EOFException;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InterruptedIOException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.net.InetSocketAddress;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.concurrent.ArrayBlockingQueue;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.concurrent.ThreadPoolExecutor;
42 import java.util.concurrent.TimeUnit;
43 import java.util.regex.Pattern;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.apache.hadoop.hbase.classification.InterfaceAudience;
48 import org.apache.hadoop.HadoopIllegalArgumentException;
49 import org.apache.hadoop.conf.Configuration;
50 import org.apache.hadoop.fs.BlockLocation;
51 import org.apache.hadoop.fs.FSDataInputStream;
52 import org.apache.hadoop.fs.FSDataOutputStream;
53 import org.apache.hadoop.fs.FileStatus;
54 import org.apache.hadoop.fs.FileSystem;
55 import org.apache.hadoop.fs.Path;
56 import org.apache.hadoop.fs.PathFilter;
57 import org.apache.hadoop.fs.permission.FsAction;
58 import org.apache.hadoop.fs.permission.FsPermission;
59 import org.apache.hadoop.hbase.ClusterId;
60 import org.apache.hadoop.hbase.HColumnDescriptor;
61 import org.apache.hadoop.hbase.HConstants;
62 import org.apache.hadoop.hbase.HDFSBlocksDistribution;
63 import org.apache.hadoop.hbase.HRegionInfo;
64 import org.apache.hadoop.hbase.RemoteExceptionHandler;
65 import org.apache.hadoop.hbase.TableName;
66 import org.apache.hadoop.hbase.exceptions.DeserializationException;
67 import org.apache.hadoop.hbase.fs.HFileSystem;
68 import org.apache.hadoop.hbase.master.HMaster;
69 import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
70 import org.apache.hadoop.hbase.security.AccessDeniedException;
71 import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter;
72 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
73 import org.apache.hadoop.hbase.protobuf.generated.FSProtos;
74 import org.apache.hadoop.hbase.regionserver.HRegion;
75 import org.apache.hadoop.hdfs.DistributedFileSystem;
76 import org.apache.hadoop.io.IOUtils;
77 import org.apache.hadoop.io.SequenceFile;
78 import org.apache.hadoop.ipc.RemoteException;
79 import org.apache.hadoop.security.UserGroupInformation;
80 import org.apache.hadoop.util.Progressable;
81 import org.apache.hadoop.util.ReflectionUtils;
82 import org.apache.hadoop.util.StringUtils;
83
84 import com.google.common.primitives.Ints;
85
86
87
88
89 @InterfaceAudience.Private
90 public abstract class FSUtils {
91 private static final Log LOG = LogFactory.getLog(FSUtils.class);
92
93
94 public static final String FULL_RWX_PERMISSIONS = "777";
95 private static final String THREAD_POOLSIZE = "hbase.client.localityCheck.threadPoolSize";
96 private static final int DEFAULT_THREAD_POOLSIZE = 2;
97
98
99 public static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
100
101 protected FSUtils() {
102 super();
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public static void setStoragePolicy(final FileSystem fs, final Configuration conf,
122 final Path path, final String policyKey, final String defaultPolicy) {
123 String storagePolicy = conf.get(policyKey, defaultPolicy).toUpperCase();
124 if (storagePolicy.equals(defaultPolicy)) {
125 if (LOG.isTraceEnabled()) {
126 LOG.trace("default policy of " + defaultPolicy + " requested, exiting early.");
127 }
128 return;
129 }
130 if (fs instanceof DistributedFileSystem) {
131 DistributedFileSystem dfs = (DistributedFileSystem)fs;
132
133 Class<? extends DistributedFileSystem> dfsClass = dfs.getClass();
134 Method m = null;
135 try {
136 m = dfsClass.getDeclaredMethod("setStoragePolicy",
137 new Class<?>[] { Path.class, String.class });
138 m.setAccessible(true);
139 } catch (NoSuchMethodException e) {
140 LOG.info("FileSystem doesn't support"
141 + " setStoragePolicy; --HDFS-6584 not available");
142 } catch (SecurityException e) {
143 LOG.info("Doesn't have access to setStoragePolicy on "
144 + "FileSystems --HDFS-6584 not available", e);
145 m = null;
146 }
147 if (m != null) {
148 try {
149 m.invoke(dfs, path, storagePolicy);
150 LOG.info("set " + storagePolicy + " for " + path);
151 } catch (Exception e) {
152
153 boolean probablyBadPolicy = false;
154 if (e instanceof InvocationTargetException) {
155 final Throwable exception = e.getCause();
156 if (exception instanceof RemoteException &&
157 HadoopIllegalArgumentException.class.getName().equals(
158 ((RemoteException)exception).getClassName())) {
159 LOG.warn("Given storage policy, '" + storagePolicy + "', was rejected and probably " +
160 "isn't a valid policy for the version of Hadoop you're running. I.e. if you're " +
161 "trying to use SSD related policies then you're likely missing HDFS-7228. For " +
162 "more information see the 'ArchivalStorage' docs for your Hadoop release.");
163 LOG.debug("More information about the invalid storage policy.", exception);
164 probablyBadPolicy = true;
165 }
166 }
167 if (!probablyBadPolicy) {
168
169
170 LOG.warn("Unable to set " + storagePolicy + " for " + path, e);
171 }
172 }
173 }
174 } else {
175 LOG.info("FileSystem isn't an instance of DistributedFileSystem; presuming it doesn't " +
176 "support setStoragePolicy.");
177 }
178 }
179
180
181
182
183
184
185
186
187
188 public static boolean isStartingWithPath(final Path rootPath, final String path) {
189 String uriRootPath = rootPath.toUri().getPath();
190 String tailUriPath = (new Path(path)).toUri().getPath();
191 return tailUriPath.startsWith(uriRootPath);
192 }
193
194
195
196
197
198
199
200
201
202 public static boolean isMatchingTail(final Path pathToSearch, String pathTail) {
203 return isMatchingTail(pathToSearch, new Path(pathTail));
204 }
205
206
207
208
209
210
211
212
213
214 public static boolean isMatchingTail(final Path pathToSearch, final Path pathTail) {
215 if (pathToSearch.depth() != pathTail.depth()) return false;
216 Path tailPath = pathTail;
217 String tailName;
218 Path toSearch = pathToSearch;
219 String toSearchName;
220 boolean result = false;
221 do {
222 tailName = tailPath.getName();
223 if (tailName == null || tailName.length() <= 0) {
224 result = true;
225 break;
226 }
227 toSearchName = toSearch.getName();
228 if (toSearchName == null || toSearchName.length() <= 0) break;
229
230 tailPath = tailPath.getParent();
231 toSearch = toSearch.getParent();
232 } while(tailName.equals(toSearchName));
233 return result;
234 }
235
236 public static FSUtils getInstance(FileSystem fs, Configuration conf) {
237 String scheme = fs.getUri().getScheme();
238 if (scheme == null) {
239 LOG.warn("Could not find scheme for uri " +
240 fs.getUri() + ", default to hdfs");
241 scheme = "hdfs";
242 }
243 Class<?> fsUtilsClass = conf.getClass("hbase.fsutil." +
244 scheme + ".impl", FSHDFSUtils.class);
245 FSUtils fsUtils = (FSUtils)ReflectionUtils.newInstance(fsUtilsClass, conf);
246 return fsUtils;
247 }
248
249
250
251
252
253
254
255
256 public static boolean deleteDirectory(final FileSystem fs, final Path dir)
257 throws IOException {
258 return fs.exists(dir) && fs.delete(dir, true);
259 }
260
261
262
263
264
265
266
267
268 public static boolean deleteRegionDir(final Configuration conf, final HRegionInfo hri)
269 throws IOException {
270 Path rootDir = getRootDir(conf);
271 FileSystem fs = rootDir.getFileSystem(conf);
272 return deleteDirectory(fs,
273 new Path(getTableDir(rootDir, hri.getTable()), hri.getEncodedName()));
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287 public static long getDefaultBlockSize(final FileSystem fs, final Path path) throws IOException {
288 Method m = null;
289 Class<? extends FileSystem> cls = fs.getClass();
290 try {
291 m = cls.getMethod("getDefaultBlockSize", new Class<?>[] { Path.class });
292 } catch (NoSuchMethodException e) {
293 LOG.info("FileSystem doesn't support getDefaultBlockSize");
294 } catch (SecurityException e) {
295 LOG.info("Doesn't have access to getDefaultBlockSize on FileSystems", e);
296 m = null;
297 }
298 if (m == null) {
299 return fs.getDefaultBlockSize(path);
300 } else {
301 try {
302 Object ret = m.invoke(fs, path);
303 return ((Long)ret).longValue();
304 } catch (Exception e) {
305 throw new IOException(e);
306 }
307 }
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 public static short getDefaultReplication(final FileSystem fs, final Path path) throws IOException {
322 Method m = null;
323 Class<? extends FileSystem> cls = fs.getClass();
324 try {
325 m = cls.getMethod("getDefaultReplication", new Class<?>[] { Path.class });
326 } catch (NoSuchMethodException e) {
327 LOG.info("FileSystem doesn't support getDefaultReplication");
328 } catch (SecurityException e) {
329 LOG.info("Doesn't have access to getDefaultReplication on FileSystems", e);
330 m = null;
331 }
332 if (m == null) {
333 return fs.getDefaultReplication(path);
334 } else {
335 try {
336 Object ret = m.invoke(fs, path);
337 return ((Number)ret).shortValue();
338 } catch (Exception e) {
339 throw new IOException(e);
340 }
341 }
342 }
343
344
345
346
347
348
349
350
351
352
353
354 public static int getDefaultBufferSize(final FileSystem fs) {
355 return fs.getConf().getInt("io.file.buffer.size", 4096);
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 public static FSDataOutputStream create(Configuration conf, FileSystem fs, Path path,
378 FsPermission perm, InetSocketAddress[] favoredNodes) throws IOException {
379 if (fs instanceof HFileSystem) {
380 FileSystem backingFs = ((HFileSystem)fs).getBackingFs();
381 if (backingFs instanceof DistributedFileSystem) {
382
383
384 short replication = Short.parseShort(conf.get(HColumnDescriptor.DFS_REPLICATION,
385 String.valueOf(HColumnDescriptor.DEFAULT_DFS_REPLICATION)));
386 try {
387 return (FSDataOutputStream) (DistributedFileSystem.class.getDeclaredMethod("create",
388 Path.class, FsPermission.class, boolean.class, int.class, short.class, long.class,
389 Progressable.class, InetSocketAddress[].class).invoke(backingFs, path, perm, true,
390 getDefaultBufferSize(backingFs),
391 replication > 0 ? replication : getDefaultReplication(backingFs, path),
392 getDefaultBlockSize(backingFs, path), null, favoredNodes));
393 } catch (InvocationTargetException ite) {
394
395 throw new IOException(ite.getCause());
396 } catch (NoSuchMethodException e) {
397 LOG.debug("DFS Client does not support most favored nodes create; using default create");
398 if (LOG.isTraceEnabled()) LOG.trace("Ignoring; use default create", e);
399 } catch (IllegalArgumentException e) {
400 LOG.debug("Ignoring (most likely Reflection related exception) " + e);
401 } catch (SecurityException e) {
402 LOG.debug("Ignoring (most likely Reflection related exception) " + e);
403 } catch (IllegalAccessException e) {
404 LOG.debug("Ignoring (most likely Reflection related exception) " + e);
405 }
406 }
407 }
408 return create(fs, path, perm, true);
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428 public static FSDataOutputStream create(FileSystem fs, Path path,
429 FsPermission perm, boolean overwrite) throws IOException {
430 if (LOG.isTraceEnabled()) {
431 LOG.trace("Creating file=" + path + " with permission=" + perm + ", overwrite=" + overwrite);
432 }
433 return fs.create(path, perm, overwrite, getDefaultBufferSize(fs),
434 getDefaultReplication(fs, path), getDefaultBlockSize(fs, path), null);
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450 public static FsPermission getFilePermissions(final FileSystem fs,
451 final Configuration conf, final String permssionConfKey) {
452 boolean enablePermissions = conf.getBoolean(
453 HConstants.ENABLE_DATA_FILE_UMASK, false);
454
455 if (enablePermissions) {
456 try {
457 FsPermission perm = new FsPermission(FULL_RWX_PERMISSIONS);
458
459 String mask = conf.get(permssionConfKey);
460 if (mask == null)
461 return FsPermission.getFileDefault();
462
463 FsPermission umask = new FsPermission(mask);
464 return perm.applyUMask(umask);
465 } catch (IllegalArgumentException e) {
466 LOG.warn(
467 "Incorrect umask attempted to be created: "
468 + conf.get(permssionConfKey)
469 + ", using default file permissions.", e);
470 return FsPermission.getFileDefault();
471 }
472 }
473 return FsPermission.getFileDefault();
474 }
475
476
477
478
479
480
481
482 public static void checkFileSystemAvailable(final FileSystem fs)
483 throws IOException {
484 if (!(fs instanceof DistributedFileSystem)) {
485 return;
486 }
487 IOException exception = null;
488 DistributedFileSystem dfs = (DistributedFileSystem) fs;
489 try {
490 if (dfs.exists(new Path("/"))) {
491 return;
492 }
493 } catch (IOException e) {
494 exception = RemoteExceptionHandler.checkIOException(e);
495 }
496 try {
497 fs.close();
498 } catch (Exception e) {
499 LOG.error("file system close failed: ", e);
500 }
501 IOException io = new IOException("File system is not available");
502 io.initCause(exception);
503 throw io;
504 }
505
506
507
508
509
510
511
512
513
514 private static boolean isInSafeMode(DistributedFileSystem dfs) throws IOException {
515 boolean inSafeMode = false;
516 try {
517 Method m = DistributedFileSystem.class.getMethod("setSafeMode", new Class<?> []{
518 org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction.class, boolean.class});
519 inSafeMode = (Boolean) m.invoke(dfs,
520 org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction.SAFEMODE_GET, true);
521 } catch (Exception e) {
522 if (e instanceof IOException) throw (IOException) e;
523
524
525 inSafeMode = dfs.setSafeMode(
526 org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction.SAFEMODE_GET);
527 }
528 return inSafeMode;
529 }
530
531
532
533
534
535
536 public static void checkDfsSafeMode(final Configuration conf)
537 throws IOException {
538 boolean isInSafeMode = false;
539 FileSystem fs = FileSystem.get(conf);
540 if (fs instanceof DistributedFileSystem) {
541 DistributedFileSystem dfs = (DistributedFileSystem)fs;
542 isInSafeMode = isInSafeMode(dfs);
543 }
544 if (isInSafeMode) {
545 throw new IOException("File system is in safemode, it can't be written now");
546 }
547 }
548
549
550
551
552
553
554
555
556
557
558 public static String getVersion(FileSystem fs, Path rootdir)
559 throws IOException, DeserializationException {
560 Path versionFile = new Path(rootdir, HConstants.VERSION_FILE_NAME);
561 FileStatus[] status = null;
562 try {
563
564
565 status = fs.listStatus(versionFile);
566 } catch (FileNotFoundException fnfe) {
567 return null;
568 }
569 if (status == null || status.length == 0) return null;
570 String version = null;
571 byte [] content = new byte [(int)status[0].getLen()];
572 FSDataInputStream s = fs.open(versionFile);
573 try {
574 IOUtils.readFully(s, content, 0, content.length);
575 if (ProtobufUtil.isPBMagicPrefix(content)) {
576 version = parseVersionFrom(content);
577 } else {
578
579 InputStream is = new ByteArrayInputStream(content);
580 DataInputStream dis = new DataInputStream(is);
581 try {
582 version = dis.readUTF();
583 } finally {
584 dis.close();
585 }
586 }
587 } catch (EOFException eof) {
588 LOG.warn("Version file was empty, odd, will try to set it.");
589 } finally {
590 s.close();
591 }
592 return version;
593 }
594
595
596
597
598
599
600
601 static String parseVersionFrom(final byte [] bytes)
602 throws DeserializationException {
603 ProtobufUtil.expectPBMagicPrefix(bytes);
604 int pblen = ProtobufUtil.lengthOfPBMagic();
605 FSProtos.HBaseVersionFileContent.Builder builder =
606 FSProtos.HBaseVersionFileContent.newBuilder();
607 try {
608 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
609 return builder.getVersion();
610 } catch (IOException e) {
611
612 throw new DeserializationException(e);
613 }
614 }
615
616
617
618
619
620
621 static byte [] toVersionByteArray(final String version) {
622 FSProtos.HBaseVersionFileContent.Builder builder =
623 FSProtos.HBaseVersionFileContent.newBuilder();
624 return ProtobufUtil.prependPBMagic(builder.setVersion(version).build().toByteArray());
625 }
626
627
628
629
630
631
632
633
634
635
636
637 public static void checkVersion(FileSystem fs, Path rootdir, boolean message)
638 throws IOException, DeserializationException {
639 checkVersion(fs, rootdir, message, 0, HConstants.DEFAULT_VERSION_FILE_WRITE_ATTEMPTS);
640 }
641
642
643
644
645
646
647
648
649
650
651
652
653
654 public static void checkVersion(FileSystem fs, Path rootdir,
655 boolean message, int wait, int retries)
656 throws IOException, DeserializationException {
657 String version = getVersion(fs, rootdir);
658 if (version == null) {
659 if (!metaRegionExists(fs, rootdir)) {
660
661
662 setVersion(fs, rootdir, wait, retries);
663 return;
664 }
665 } else if (version.compareTo(HConstants.FILE_SYSTEM_VERSION) == 0) return;
666
667
668
669 String msg = "HBase file layout needs to be upgraded."
670 + " You have version " + version
671 + " and I want version " + HConstants.FILE_SYSTEM_VERSION
672 + ". Consult http://hbase.apache.org/book.html for further information about upgrading HBase."
673 + " Is your hbase.rootdir valid? If so, you may need to run "
674 + "'hbase hbck -fixVersionFile'.";
675 if (message) {
676 System.out.println("WARNING! " + msg);
677 }
678 throw new FileSystemVersionException(msg);
679 }
680
681
682
683
684
685
686
687
688 public static void setVersion(FileSystem fs, Path rootdir)
689 throws IOException {
690 setVersion(fs, rootdir, HConstants.FILE_SYSTEM_VERSION, 0,
691 HConstants.DEFAULT_VERSION_FILE_WRITE_ATTEMPTS);
692 }
693
694
695
696
697
698
699
700
701
702
703 public static void setVersion(FileSystem fs, Path rootdir, int wait, int retries)
704 throws IOException {
705 setVersion(fs, rootdir, HConstants.FILE_SYSTEM_VERSION, wait, retries);
706 }
707
708
709
710
711
712
713
714
715
716
717
718
719 public static void setVersion(FileSystem fs, Path rootdir, String version,
720 int wait, int retries) throws IOException {
721 Path versionFile = new Path(rootdir, HConstants.VERSION_FILE_NAME);
722 Path tempVersionFile = new Path(rootdir, HConstants.HBASE_TEMP_DIRECTORY + Path.SEPARATOR +
723 HConstants.VERSION_FILE_NAME);
724 while (true) {
725 try {
726
727 FSDataOutputStream s = fs.create(tempVersionFile);
728 try {
729 s.write(toVersionByteArray(version));
730 s.close();
731 s = null;
732
733
734 if (!fs.rename(tempVersionFile, versionFile)) {
735 throw new IOException("Unable to move temp version file to " + versionFile);
736 }
737 } finally {
738
739
740
741
742
743 try {
744 if (s != null) s.close();
745 } catch (IOException ignore) { }
746 }
747 LOG.info("Created version file at " + rootdir.toString() + " with version=" + version);
748 return;
749 } catch (IOException e) {
750 if (retries > 0) {
751 LOG.debug("Unable to create version file at " + rootdir.toString() + ", retrying", e);
752 fs.delete(versionFile, false);
753 try {
754 if (wait > 0) {
755 Thread.sleep(wait);
756 }
757 } catch (InterruptedException ie) {
758 throw (InterruptedIOException)new InterruptedIOException().initCause(ie);
759 }
760 retries--;
761 } else {
762 throw e;
763 }
764 }
765 }
766 }
767
768
769
770
771
772
773
774
775
776 public static boolean checkClusterIdExists(FileSystem fs, Path rootdir,
777 int wait) throws IOException {
778 while (true) {
779 try {
780 Path filePath = new Path(rootdir, HConstants.CLUSTER_ID_FILE_NAME);
781 return fs.exists(filePath);
782 } catch (IOException ioe) {
783 if (wait > 0) {
784 LOG.warn("Unable to check cluster ID file in " + rootdir.toString() +
785 ", retrying in "+wait+"msec: "+StringUtils.stringifyException(ioe));
786 try {
787 Thread.sleep(wait);
788 } catch (InterruptedException e) {
789 throw (InterruptedIOException)new InterruptedIOException().initCause(e);
790 }
791 } else {
792 throw ioe;
793 }
794 }
795 }
796 }
797
798
799
800
801
802
803
804
805 public static ClusterId getClusterId(FileSystem fs, Path rootdir)
806 throws IOException {
807 Path idPath = new Path(rootdir, HConstants.CLUSTER_ID_FILE_NAME);
808 ClusterId clusterId = null;
809 FileStatus status = fs.exists(idPath)? fs.getFileStatus(idPath): null;
810 if (status != null) {
811 int len = Ints.checkedCast(status.getLen());
812 byte [] content = new byte[len];
813 FSDataInputStream in = fs.open(idPath);
814 try {
815 in.readFully(content);
816 } catch (EOFException eof) {
817 LOG.warn("Cluster ID file " + idPath.toString() + " was empty");
818 } finally{
819 in.close();
820 }
821 try {
822 clusterId = ClusterId.parseFrom(content);
823 } catch (DeserializationException e) {
824 throw new IOException("content=" + Bytes.toString(content), e);
825 }
826
827 if (!ProtobufUtil.isPBMagicPrefix(content)) {
828 String cid = null;
829 in = fs.open(idPath);
830 try {
831 cid = in.readUTF();
832 clusterId = new ClusterId(cid);
833 } catch (EOFException eof) {
834 LOG.warn("Cluster ID file " + idPath.toString() + " was empty");
835 } finally {
836 in.close();
837 }
838 rewriteAsPb(fs, rootdir, idPath, clusterId);
839 }
840 return clusterId;
841 } else {
842 LOG.warn("Cluster ID file does not exist at " + idPath.toString());
843 }
844 return clusterId;
845 }
846
847
848
849
850
851 private static void rewriteAsPb(final FileSystem fs, final Path rootdir, final Path p,
852 final ClusterId cid)
853 throws IOException {
854
855
856 Path movedAsideName = new Path(p + "." + System.currentTimeMillis());
857 if (!fs.rename(p, movedAsideName)) throw new IOException("Failed rename of " + p);
858 setClusterId(fs, rootdir, cid, 100);
859 if (!fs.delete(movedAsideName, false)) {
860 throw new IOException("Failed delete of " + movedAsideName);
861 }
862 LOG.debug("Rewrote the hbase.id file as pb");
863 }
864
865
866
867
868
869
870
871
872
873
874 public static void setClusterId(FileSystem fs, Path rootdir, ClusterId clusterId,
875 int wait) throws IOException {
876 while (true) {
877 try {
878 Path idFile = new Path(rootdir, HConstants.CLUSTER_ID_FILE_NAME);
879 Path tempIdFile = new Path(rootdir, HConstants.HBASE_TEMP_DIRECTORY +
880 Path.SEPARATOR + HConstants.CLUSTER_ID_FILE_NAME);
881
882 FSDataOutputStream s = fs.create(tempIdFile);
883 try {
884 s.write(clusterId.toByteArray());
885 s.close();
886 s = null;
887
888
889 if (!fs.rename(tempIdFile, idFile)) {
890 throw new IOException("Unable to move temp version file to " + idFile);
891 }
892 } finally {
893
894 try {
895 if (s != null) s.close();
896 } catch (IOException ignore) { }
897 }
898 if (LOG.isDebugEnabled()) {
899 LOG.debug("Created cluster ID file at " + idFile.toString() + " with ID: " + clusterId);
900 }
901 return;
902 } catch (IOException ioe) {
903 if (wait > 0) {
904 LOG.warn("Unable to create cluster ID file in " + rootdir.toString() +
905 ", retrying in " + wait + "msec: " + StringUtils.stringifyException(ioe));
906 try {
907 Thread.sleep(wait);
908 } catch (InterruptedException e) {
909 throw (InterruptedIOException)new InterruptedIOException().initCause(e);
910 }
911 } else {
912 throw ioe;
913 }
914 }
915 }
916 }
917
918
919
920
921
922
923
924
925 public static Path validateRootPath(Path root) throws IOException {
926 try {
927 URI rootURI = new URI(root.toString());
928 String scheme = rootURI.getScheme();
929 if (scheme == null) {
930 throw new IOException("Root directory does not have a scheme");
931 }
932 return root;
933 } catch (URISyntaxException e) {
934 IOException io = new IOException("Root directory path is not a valid " +
935 "URI -- check your " + HConstants.HBASE_DIR + " configuration");
936 io.initCause(e);
937 throw io;
938 }
939 }
940
941
942
943
944
945
946
947
948
949 public static String removeRootPath(Path path, final Configuration conf) throws IOException {
950 Path root = FSUtils.getRootDir(conf);
951 String pathStr = path.toString();
952
953 if (!pathStr.startsWith(root.toString())) return pathStr;
954
955 return pathStr.substring(root.toString().length() + 1);
956 }
957
958
959
960
961
962
963
964 public static void waitOnSafeMode(final Configuration conf,
965 final long wait)
966 throws IOException {
967 FileSystem fs = FileSystem.get(conf);
968 if (!(fs instanceof DistributedFileSystem)) return;
969 DistributedFileSystem dfs = (DistributedFileSystem)fs;
970
971 while (isInSafeMode(dfs)) {
972 LOG.info("Waiting for dfs to exit safe mode...");
973 try {
974 Thread.sleep(wait);
975 } catch (InterruptedException e) {
976 throw (InterruptedIOException)new InterruptedIOException().initCause(e);
977 }
978 }
979 }
980
981
982
983
984
985
986
987
988
989
990
991 public static String getPath(Path p) {
992 return p.toUri().getPath();
993 }
994
995
996
997
998
999
1000
1001 public static Path getRootDir(final Configuration c) throws IOException {
1002 Path p = new Path(c.get(HConstants.HBASE_DIR));
1003 FileSystem fs = p.getFileSystem(c);
1004 return p.makeQualified(fs);
1005 }
1006
1007 public static void setRootDir(final Configuration c, final Path root) throws IOException {
1008 c.set(HConstants.HBASE_DIR, root.toString());
1009 }
1010
1011 public static void setFsDefault(final Configuration c, final Path root) throws IOException {
1012 c.set("fs.defaultFS", root.toString());
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023 @SuppressWarnings("deprecation")
1024 public static boolean metaRegionExists(FileSystem fs, Path rootdir)
1025 throws IOException {
1026 Path metaRegionDir =
1027 HRegion.getRegionDir(rootdir, HRegionInfo.FIRST_META_REGIONINFO);
1028 return fs.exists(metaRegionDir);
1029 }
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 static public HDFSBlocksDistribution computeHDFSBlocksDistribution(
1040 final FileSystem fs, FileStatus status, long start, long length)
1041 throws IOException {
1042 HDFSBlocksDistribution blocksDistribution = new HDFSBlocksDistribution();
1043 BlockLocation [] blockLocations =
1044 fs.getFileBlockLocations(status, start, length);
1045 for(BlockLocation bl : blockLocations) {
1046 String [] hosts = bl.getHosts();
1047 long len = bl.getLength();
1048 blocksDistribution.addHostsAndBlockWeight(hosts, len);
1049 }
1050
1051 return blocksDistribution;
1052 }
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 public static int getTotalTableFragmentation(final HMaster master)
1064 throws IOException {
1065 Map<String, Integer> map = getTableFragmentation(master);
1066 return map != null && map.size() > 0 ? map.get("-TOTAL-") : -1;
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 public static Map<String, Integer> getTableFragmentation(
1080 final HMaster master)
1081 throws IOException {
1082 Path path = getRootDir(master.getConfiguration());
1083
1084 FileSystem fs = path.getFileSystem(master.getConfiguration());
1085 return getTableFragmentation(fs, path);
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 public static Map<String, Integer> getTableFragmentation(
1099 final FileSystem fs, final Path hbaseRootDir)
1100 throws IOException {
1101 Map<String, Integer> frags = new HashMap<String, Integer>();
1102 int cfCountTotal = 0;
1103 int cfFragTotal = 0;
1104 PathFilter regionFilter = new RegionDirFilter(fs);
1105 PathFilter familyFilter = new FamilyDirFilter(fs);
1106 List<Path> tableDirs = getTableDirs(fs, hbaseRootDir);
1107 for (Path d : tableDirs) {
1108 int cfCount = 0;
1109 int cfFrag = 0;
1110 FileStatus[] regionDirs = fs.listStatus(d, regionFilter);
1111 for (FileStatus regionDir : regionDirs) {
1112 Path dd = regionDir.getPath();
1113
1114 FileStatus[] familyDirs = fs.listStatus(dd, familyFilter);
1115 for (FileStatus familyDir : familyDirs) {
1116 cfCount++;
1117 cfCountTotal++;
1118 Path family = familyDir.getPath();
1119
1120 FileStatus[] familyStatus = fs.listStatus(family);
1121 if (familyStatus.length > 1) {
1122 cfFrag++;
1123 cfFragTotal++;
1124 }
1125 }
1126 }
1127
1128 frags.put(FSUtils.getTableName(d).getNameAsString(),
1129 cfCount == 0? 0: Math.round((float) cfFrag / cfCount * 100));
1130 }
1131
1132 frags.put("-TOTAL-",
1133 cfCountTotal == 0? 0: Math.round((float) cfFragTotal / cfCountTotal * 100));
1134 return frags;
1135 }
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145 public static Path getTableDir(Path rootdir, final TableName tableName) {
1146 return new Path(getNamespaceDir(rootdir, tableName.getNamespaceAsString()),
1147 tableName.getQualifierAsString());
1148 }
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 public static TableName getTableName(Path tablePath) {
1159 return TableName.valueOf(tablePath.getParent().getName(), tablePath.getName());
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 public static Path getNamespaceDir(Path rootdir, final String namespace) {
1171 return new Path(rootdir, new Path(HConstants.BASE_NAMESPACE_DIR,
1172 new Path(namespace)));
1173 }
1174
1175
1176
1177
1178 static class FileFilter implements PathFilter {
1179 private final FileSystem fs;
1180
1181 public FileFilter(final FileSystem fs) {
1182 this.fs = fs;
1183 }
1184
1185 @Override
1186 public boolean accept(Path p) {
1187 try {
1188 return fs.isFile(p);
1189 } catch (IOException e) {
1190 LOG.debug("unable to verify if path=" + p + " is a regular file", e);
1191 return false;
1192 }
1193 }
1194 }
1195
1196
1197
1198
1199 public static class BlackListDirFilter implements PathFilter {
1200 private final FileSystem fs;
1201 private List<String> blacklist;
1202
1203
1204
1205
1206
1207
1208
1209 @SuppressWarnings("unchecked")
1210 public BlackListDirFilter(final FileSystem fs, final List<String> directoryNameBlackList) {
1211 this.fs = fs;
1212 blacklist =
1213 (List<String>) (directoryNameBlackList == null ? Collections.emptyList()
1214 : directoryNameBlackList);
1215 }
1216
1217 @Override
1218 public boolean accept(Path p) {
1219 boolean isValid = false;
1220 try {
1221 if (isValidName(p.getName())) {
1222 isValid = fs.getFileStatus(p).isDirectory();
1223 } else {
1224 isValid = false;
1225 }
1226 } catch (IOException e) {
1227 LOG.warn("An error occurred while verifying if [" + p.toString()
1228 + "] is a valid directory. Returning 'not valid' and continuing.", e);
1229 }
1230 return isValid;
1231 }
1232
1233 protected boolean isValidName(final String name) {
1234 return !blacklist.contains(name);
1235 }
1236 }
1237
1238
1239
1240
1241 public static class DirFilter extends BlackListDirFilter {
1242
1243 public DirFilter(FileSystem fs) {
1244 super(fs, null);
1245 }
1246 }
1247
1248
1249
1250
1251
1252 public static class UserTableDirFilter extends BlackListDirFilter {
1253 public UserTableDirFilter(FileSystem fs) {
1254 super(fs, HConstants.HBASE_NON_TABLE_DIRS);
1255 }
1256
1257 protected boolean isValidName(final String name) {
1258 if (!super.isValidName(name))
1259 return false;
1260
1261 try {
1262 TableName.isLegalTableQualifierName(Bytes.toBytes(name));
1263 } catch (IllegalArgumentException e) {
1264 LOG.info("INVALID NAME " + name);
1265 return false;
1266 }
1267 return true;
1268 }
1269 }
1270
1271
1272
1273
1274
1275
1276
1277
1278 public static boolean isAppendSupported(final Configuration conf) {
1279 boolean append = conf.getBoolean("dfs.support.append", false);
1280 if (append) {
1281 try {
1282
1283
1284
1285 SequenceFile.Writer.class.getMethod("syncFs", new Class<?> []{});
1286 append = true;
1287 } catch (SecurityException e) {
1288 } catch (NoSuchMethodException e) {
1289 append = false;
1290 }
1291 }
1292 if (!append) {
1293
1294 try {
1295 FSDataOutputStream.class.getMethod("hflush", new Class<?> []{});
1296 append = true;
1297 } catch (NoSuchMethodException e) {
1298 append = false;
1299 }
1300 }
1301 return append;
1302 }
1303
1304
1305
1306
1307
1308
1309 public static boolean isHDFS(final Configuration conf) throws IOException {
1310 FileSystem fs = FileSystem.get(conf);
1311 String scheme = fs.getUri().getScheme();
1312 return scheme.equalsIgnoreCase("hdfs");
1313 }
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323 public abstract void recoverFileLease(final FileSystem fs, final Path p,
1324 Configuration conf, CancelableProgressable reporter) throws IOException;
1325
1326 public static List<Path> getTableDirs(final FileSystem fs, final Path rootdir)
1327 throws IOException {
1328 List<Path> tableDirs = new LinkedList<Path>();
1329
1330 for(FileStatus status :
1331 fs.globStatus(new Path(rootdir,
1332 new Path(HConstants.BASE_NAMESPACE_DIR, "*")))) {
1333 tableDirs.addAll(FSUtils.getLocalTableDirs(fs, status.getPath()));
1334 }
1335 return tableDirs;
1336 }
1337
1338
1339
1340
1341
1342
1343
1344
1345 public static List<Path> getLocalTableDirs(final FileSystem fs, final Path rootdir)
1346 throws IOException {
1347
1348 FileStatus[] dirs = fs.listStatus(rootdir, new UserTableDirFilter(fs));
1349 List<Path> tabledirs = new ArrayList<Path>(dirs.length);
1350 for (FileStatus dir: dirs) {
1351 tabledirs.add(dir.getPath());
1352 }
1353 return tabledirs;
1354 }
1355
1356
1357
1358
1359
1360
1361 public static boolean isRecoveredEdits(Path path) {
1362 return path.toString().contains(HConstants.RECOVERED_EDITS_DIR);
1363 }
1364
1365
1366
1367
1368 public static class RegionDirFilter implements PathFilter {
1369
1370 final public static Pattern regionDirPattern = Pattern.compile("^[0-9a-f]*$");
1371 final FileSystem fs;
1372
1373 public RegionDirFilter(FileSystem fs) {
1374 this.fs = fs;
1375 }
1376
1377 @Override
1378 public boolean accept(Path rd) {
1379 if (!regionDirPattern.matcher(rd.getName()).matches()) {
1380 return false;
1381 }
1382
1383 try {
1384 return fs.getFileStatus(rd).isDirectory();
1385 } catch (IOException ioe) {
1386
1387 LOG.warn("Skipping file " + rd +" due to IOException", ioe);
1388 return false;
1389 }
1390 }
1391 }
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401 public static List<Path> getRegionDirs(final FileSystem fs, final Path tableDir) throws IOException {
1402
1403 FileStatus[] rds = fs.listStatus(tableDir, new RegionDirFilter(fs));
1404 List<Path> regionDirs = new ArrayList<Path>(rds.length);
1405 for (FileStatus rdfs: rds) {
1406 Path rdPath = rdfs.getPath();
1407 regionDirs.add(rdPath);
1408 }
1409 return regionDirs;
1410 }
1411
1412
1413
1414
1415
1416 public static class FamilyDirFilter implements PathFilter {
1417 final FileSystem fs;
1418
1419 public FamilyDirFilter(FileSystem fs) {
1420 this.fs = fs;
1421 }
1422
1423 @Override
1424 public boolean accept(Path rd) {
1425 try {
1426
1427 HColumnDescriptor.isLegalFamilyName(Bytes.toBytes(rd.getName()));
1428 } catch (IllegalArgumentException iae) {
1429
1430 return false;
1431 }
1432
1433 try {
1434 return fs.getFileStatus(rd).isDirectory();
1435 } catch (IOException ioe) {
1436
1437 LOG.warn("Skipping file " + rd +" due to IOException", ioe);
1438 return false;
1439 }
1440 }
1441 }
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451 public static List<Path> getFamilyDirs(final FileSystem fs, final Path regionDir) throws IOException {
1452
1453 FileStatus[] fds = fs.listStatus(regionDir, new FamilyDirFilter(fs));
1454 List<Path> familyDirs = new ArrayList<Path>(fds.length);
1455 for (FileStatus fdfs: fds) {
1456 Path fdPath = fdfs.getPath();
1457 familyDirs.add(fdPath);
1458 }
1459 return familyDirs;
1460 }
1461
1462 public static List<Path> getReferenceFilePaths(final FileSystem fs, final Path familyDir) throws IOException {
1463 FileStatus[] fds = fs.listStatus(familyDir, new ReferenceFileFilter(fs));
1464 List<Path> referenceFiles = new ArrayList<Path>(fds.length);
1465 for (FileStatus fdfs: fds) {
1466 Path fdPath = fdfs.getPath();
1467 referenceFiles.add(fdPath);
1468 }
1469 return referenceFiles;
1470 }
1471
1472
1473
1474
1475 public static class HFileFilter implements PathFilter {
1476 final FileSystem fs;
1477
1478 public HFileFilter(FileSystem fs) {
1479 this.fs = fs;
1480 }
1481
1482 @Override
1483 public boolean accept(Path rd) {
1484 try {
1485
1486 return !fs.getFileStatus(rd).isDirectory() && StoreFileInfo.isHFile(rd);
1487 } catch (IOException ioe) {
1488
1489 LOG.warn("Skipping file " + rd +" due to IOException", ioe);
1490 return false;
1491 }
1492 }
1493 }
1494
1495 public static class ReferenceFileFilter implements PathFilter {
1496
1497 private final FileSystem fs;
1498
1499 public ReferenceFileFilter(FileSystem fs) {
1500 this.fs = fs;
1501 }
1502
1503 @Override
1504 public boolean accept(Path rd) {
1505 try {
1506
1507 return !fs.getFileStatus(rd).isDirectory() && StoreFileInfo.isReference(rd);
1508 } catch (IOException ioe) {
1509
1510 LOG.warn("Skipping file " + rd +" due to IOException", ioe);
1511 return false;
1512 }
1513 }
1514 }
1515
1516
1517
1518
1519
1520
1521
1522 public static FileSystem getCurrentFileSystem(Configuration conf)
1523 throws IOException {
1524 return getRootDir(conf).getFileSystem(conf);
1525 }
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543 public static Map<String, Path> getTableStoreFilePathMap(Map<String, Path> map,
1544 final FileSystem fs, final Path hbaseRootDir, TableName tableName)
1545 throws IOException {
1546 return getTableStoreFilePathMap(map, fs, hbaseRootDir, tableName, null);
1547 }
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565 public static Map<String, Path> getTableStoreFilePathMap(Map<String, Path> map,
1566 final FileSystem fs, final Path hbaseRootDir, TableName tableName, ErrorReporter errors)
1567 throws IOException {
1568 if (map == null) {
1569 map = new HashMap<String, Path>();
1570 }
1571
1572
1573 Path tableDir = FSUtils.getTableDir(hbaseRootDir, tableName);
1574
1575
1576 PathFilter familyFilter = new FamilyDirFilter(fs);
1577 FileStatus[] regionDirs = fs.listStatus(tableDir, new RegionDirFilter(fs));
1578 for (FileStatus regionDir : regionDirs) {
1579 if (null != errors) {
1580 errors.progress();
1581 }
1582 Path dd = regionDir.getPath();
1583
1584 FileStatus[] familyDirs = fs.listStatus(dd, familyFilter);
1585 for (FileStatus familyDir : familyDirs) {
1586 if (null != errors) {
1587 errors.progress();
1588 }
1589 Path family = familyDir.getPath();
1590 if (family.getName().equals(HConstants.RECOVERED_EDITS_DIR)) {
1591 continue;
1592 }
1593
1594
1595 FileStatus[] familyStatus = fs.listStatus(family);
1596 for (FileStatus sfStatus : familyStatus) {
1597 if (null != errors) {
1598 errors.progress();
1599 }
1600 Path sf = sfStatus.getPath();
1601 map.put( sf.getName(), sf);
1602 }
1603 }
1604 }
1605 return map;
1606 }
1607
1608 public static int getRegionReferenceFileCount(final FileSystem fs, final Path p) {
1609 int result = 0;
1610 try {
1611 for (Path familyDir:getFamilyDirs(fs, p)){
1612 result += getReferenceFilePaths(fs, familyDir).size();
1613 }
1614 } catch (IOException e) {
1615 LOG.warn("Error Counting reference files.", e);
1616 }
1617 return result;
1618 }
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633 public static Map<String, Path> getTableStoreFilePathMap(
1634 final FileSystem fs, final Path hbaseRootDir)
1635 throws IOException {
1636 return getTableStoreFilePathMap(fs, hbaseRootDir, null);
1637 }
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653 public static Map<String, Path> getTableStoreFilePathMap(
1654 final FileSystem fs, final Path hbaseRootDir, ErrorReporter errors)
1655 throws IOException {
1656 Map<String, Path> map = new HashMap<String, Path>();
1657
1658
1659
1660
1661
1662 for (Path tableDir : FSUtils.getTableDirs(fs, hbaseRootDir)) {
1663 getTableStoreFilePathMap(map, fs, hbaseRootDir,
1664 FSUtils.getTableName(tableDir), errors);
1665 }
1666 return map;
1667 }
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680 public static FileStatus [] listStatus(final FileSystem fs,
1681 final Path dir, final PathFilter filter) throws IOException {
1682 FileStatus [] status = null;
1683 try {
1684 status = filter == null ? fs.listStatus(dir) : fs.listStatus(dir, filter);
1685 } catch (FileNotFoundException fnfe) {
1686
1687 if (LOG.isTraceEnabled()) {
1688 LOG.trace(dir + " doesn't exist");
1689 }
1690 }
1691 if (status == null || status.length < 1) return null;
1692 return status;
1693 }
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703 public static FileStatus[] listStatus(final FileSystem fs, final Path dir) throws IOException {
1704 return listStatus(fs, dir, null);
1705 }
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716 public static boolean delete(final FileSystem fs, final Path path, final boolean recursive)
1717 throws IOException {
1718 return fs.delete(path, recursive);
1719 }
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729 public static boolean isExists(final FileSystem fs, final Path path) throws IOException {
1730 return fs.exists(path);
1731 }
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743 public static void checkAccess(UserGroupInformation ugi, FileStatus file,
1744 FsAction action) throws AccessDeniedException {
1745 if (ugi.getShortUserName().equals(file.getOwner())) {
1746 if (file.getPermission().getUserAction().implies(action)) {
1747 return;
1748 }
1749 } else if (contains(ugi.getGroupNames(), file.getGroup())) {
1750 if (file.getPermission().getGroupAction().implies(action)) {
1751 return;
1752 }
1753 } else if (file.getPermission().getOtherAction().implies(action)) {
1754 return;
1755 }
1756 throw new AccessDeniedException("Permission denied:" + " action=" + action
1757 + " path=" + file.getPath() + " user=" + ugi.getShortUserName());
1758 }
1759
1760 private static boolean contains(String[] groups, String user) {
1761 for (String group : groups) {
1762 if (group.equals(user)) {
1763 return true;
1764 }
1765 }
1766 return false;
1767 }
1768
1769
1770
1771
1772
1773
1774
1775
1776 public static void logFileSystemState(final FileSystem fs, final Path root, Log LOG)
1777 throws IOException {
1778 LOG.debug("Current file system:");
1779 logFSTree(LOG, fs, root, "|-");
1780 }
1781
1782
1783
1784
1785
1786
1787 private static void logFSTree(Log LOG, final FileSystem fs, final Path root, String prefix)
1788 throws IOException {
1789 FileStatus[] files = FSUtils.listStatus(fs, root, null);
1790 if (files == null) return;
1791
1792 for (FileStatus file : files) {
1793 if (file.isDirectory()) {
1794 LOG.debug(prefix + file.getPath().getName() + "/");
1795 logFSTree(LOG, fs, file.getPath(), prefix + "---");
1796 } else {
1797 LOG.debug(prefix + file.getPath().getName());
1798 }
1799 }
1800 }
1801
1802 public static boolean renameAndSetModifyTime(final FileSystem fs, final Path src, final Path dest)
1803 throws IOException {
1804
1805 fs.setTimes(src, EnvironmentEdgeManager.currentTime(), -1);
1806 return fs.rename(src, dest);
1807 }
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822 public static Map<String, Map<String, Float>> getRegionDegreeLocalityMappingFromFS(
1823 final Configuration conf) throws IOException {
1824 return getRegionDegreeLocalityMappingFromFS(
1825 conf, null,
1826 conf.getInt(THREAD_POOLSIZE, DEFAULT_THREAD_POOLSIZE));
1827
1828 }
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846 public static Map<String, Map<String, Float>> getRegionDegreeLocalityMappingFromFS(
1847 final Configuration conf, final String desiredTable, int threadPoolSize)
1848 throws IOException {
1849 Map<String, Map<String, Float>> regionDegreeLocalityMapping =
1850 new ConcurrentHashMap<String, Map<String, Float>>();
1851 getRegionLocalityMappingFromFS(conf, desiredTable, threadPoolSize, null,
1852 regionDegreeLocalityMapping);
1853 return regionDegreeLocalityMapping;
1854 }
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876 private static void getRegionLocalityMappingFromFS(
1877 final Configuration conf, final String desiredTable,
1878 int threadPoolSize,
1879 Map<String, String> regionToBestLocalityRSMapping,
1880 Map<String, Map<String, Float>> regionDegreeLocalityMapping)
1881 throws IOException {
1882 FileSystem fs = FileSystem.get(conf);
1883 Path rootPath = FSUtils.getRootDir(conf);
1884 long startTime = EnvironmentEdgeManager.currentTime();
1885 Path queryPath;
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986 public static void setupShortCircuitRead(final Configuration conf) {
1987
1988 boolean shortCircuitSkipChecksum =
1989 conf.getBoolean("dfs.client.read.shortcircuit.skip.checksum", false);
1990 boolean useHBaseChecksum = conf.getBoolean(HConstants.HBASE_CHECKSUM_VERIFICATION, true);
1991 if (shortCircuitSkipChecksum) {
1992 LOG.warn("Configuration \"dfs.client.read.shortcircuit.skip.checksum\" should not " +
1993 "be set to true." + (useHBaseChecksum ? " HBase checksum doesn't require " +
1994 "it, see https://issues.apache.org/jira/browse/HBASE-6868." : ""));
1995 assert !shortCircuitSkipChecksum;
1996 }
1997 checkShortCircuitReadBufferSize(conf);
1998 }
1999
2000
2001
2002
2003
2004 public static void checkShortCircuitReadBufferSize(final Configuration conf) {
2005 final int defaultSize = HConstants.DEFAULT_BLOCKSIZE * 2;
2006 final int notSet = -1;
2007
2008 final String dfsKey = "dfs.client.read.shortcircuit.buffer.size";
2009 int size = conf.getInt(dfsKey, notSet);
2010
2011 if (size != notSet) return;
2012
2013 int hbaseSize = conf.getInt("hbase." + dfsKey, defaultSize);
2014 conf.setIfUnset(dfsKey, Integer.toString(hbaseSize));
2015 }
2016 }