1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver;
21
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.fs.FSDataInputStream;
35 import org.apache.hadoop.fs.FSDataOutputStream;
36 import org.apache.hadoop.fs.FileStatus;
37 import org.apache.hadoop.fs.FileSystem;
38 import org.apache.hadoop.fs.FileUtil;
39 import org.apache.hadoop.fs.Path;
40 import org.apache.hadoop.fs.permission.FsPermission;
41 import org.apache.hadoop.hbase.HColumnDescriptor;
42 import org.apache.hadoop.hbase.HConstants;
43 import org.apache.hadoop.hbase.HRegionInfo;
44 import org.apache.hadoop.hbase.HTableDescriptor;
45 import org.apache.hadoop.hbase.KeyValue;
46 import org.apache.hadoop.hbase.backup.HFileArchiver;
47 import org.apache.hadoop.hbase.fs.HFileSystem;
48 import org.apache.hadoop.hbase.io.Reference;
49 import org.apache.hadoop.hbase.util.Bytes;
50 import org.apache.hadoop.hbase.util.FSHDFSUtils;
51 import org.apache.hadoop.hbase.util.FSUtils;
52 import org.apache.hadoop.hbase.util.Threads;
53
54
55
56
57
58 @InterfaceAudience.Private
59 public class HRegionFileSystem {
60 public static final Log LOG = LogFactory.getLog(HRegionFileSystem.class);
61
62
63 public final static String REGION_INFO_FILE = ".regioninfo";
64
65
66 public static final String REGION_MERGES_DIR = ".merges";
67
68
69 public static final String REGION_SPLITS_DIR = ".splits";
70
71
72 private static final String REGION_TEMP_DIR = ".tmp";
73
74 private final HRegionInfo regionInfo;
75 private final Configuration conf;
76 private final Path tableDir;
77 private final FileSystem fs;
78
79
80
81
82
83 private final int hdfsClientRetriesNumber;
84 private final int baseSleepBeforeRetries;
85 private static final int DEFAULT_HDFS_CLIENT_RETRIES_NUMBER = 10;
86 private static final int DEFAULT_BASE_SLEEP_BEFORE_RETRIES = 1000;
87
88
89
90
91
92
93
94
95 HRegionFileSystem(final Configuration conf, final FileSystem fs, final Path tableDir,
96 final HRegionInfo regionInfo) {
97 this.fs = fs;
98 this.conf = conf;
99 this.tableDir = tableDir;
100 this.regionInfo = regionInfo;
101 this.hdfsClientRetriesNumber = conf.getInt("hdfs.client.retries.number",
102 DEFAULT_HDFS_CLIENT_RETRIES_NUMBER);
103 this.baseSleepBeforeRetries = conf.getInt("hdfs.client.sleep.before.retries",
104 DEFAULT_BASE_SLEEP_BEFORE_RETRIES);
105 }
106
107
108 public FileSystem getFileSystem() {
109 return this.fs;
110 }
111
112
113 public HRegionInfo getRegionInfo() {
114 return this.regionInfo;
115 }
116
117
118 public Path getTableDir() {
119 return this.tableDir;
120 }
121
122
123 public Path getRegionDir() {
124 return new Path(this.tableDir, this.regionInfo.getEncodedName());
125 }
126
127
128
129
130
131 Path getTempDir() {
132 return new Path(getRegionDir(), REGION_TEMP_DIR);
133 }
134
135
136
137
138 void cleanupTempDir() throws IOException {
139 deleteDir(getTempDir());
140 }
141
142
143
144
145
146
147
148
149
150 public Path getStoreDir(final String familyName) {
151 return new Path(this.getRegionDir(), familyName);
152 }
153
154
155
156
157
158
159
160 Path createStoreDir(final String familyName) throws IOException {
161 Path storeDir = getStoreDir(familyName);
162 if(!fs.exists(storeDir) && !createDir(storeDir))
163 throw new IOException("Failed creating "+storeDir);
164 return storeDir;
165 }
166
167
168
169
170
171
172
173 public Collection<StoreFileInfo> getStoreFiles(final byte[] familyName) throws IOException {
174 return getStoreFiles(Bytes.toString(familyName));
175 }
176
177 public Collection<StoreFileInfo> getStoreFiles(final String familyName) throws IOException {
178 return getStoreFiles(familyName, true);
179 }
180
181
182
183
184
185
186
187 public Collection<StoreFileInfo> getStoreFiles(final String familyName, final boolean validate)
188 throws IOException {
189 Path familyDir = getStoreDir(familyName);
190 FileStatus[] files = FSUtils.listStatus(this.fs, familyDir);
191 if (files == null) {
192 LOG.debug("No StoreFiles for: " + familyDir);
193 return null;
194 }
195
196 ArrayList<StoreFileInfo> storeFiles = new ArrayList<StoreFileInfo>(files.length);
197 for (FileStatus status: files) {
198 if (validate && !StoreFileInfo.isValid(status)) {
199 LOG.warn("Invalid StoreFile: " + status.getPath());
200 continue;
201 }
202
203 storeFiles.add(new StoreFileInfo(this.conf, this.fs, status));
204 }
205 return storeFiles;
206 }
207
208
209
210
211
212
213
214
215 Path getStoreFilePath(final String familyName, final String fileName) {
216 Path familyDir = getStoreDir(familyName);
217 return new Path(familyDir, fileName).makeQualified(this.fs);
218 }
219
220
221
222
223
224
225
226
227 StoreFileInfo getStoreFileInfo(final String familyName, final String fileName)
228 throws IOException {
229 Path familyDir = getStoreDir(familyName);
230 FileStatus status = fs.getFileStatus(new Path(familyDir, fileName));
231 return new StoreFileInfo(this.conf, this.fs, status);
232 }
233
234
235
236
237
238
239
240 public boolean hasReferences(final String familyName) throws IOException {
241 FileStatus[] files = FSUtils.listStatus(fs, getStoreDir(familyName),
242 new FSUtils.ReferenceFileFilter(fs));
243 return files != null && files.length > 0;
244 }
245
246
247
248
249
250
251
252 public boolean hasReferences(final HTableDescriptor htd) throws IOException {
253 for (HColumnDescriptor family : htd.getFamilies()) {
254 if (hasReferences(family.getNameAsString())) {
255 return true;
256 }
257 }
258 return false;
259 }
260
261
262
263
264
265 public Collection<String> getFamilies() throws IOException {
266 FileStatus[] fds = FSUtils.listStatus(fs, getRegionDir(), new FSUtils.FamilyDirFilter(fs));
267 if (fds == null) return null;
268
269 ArrayList<String> families = new ArrayList<String>(fds.length);
270 for (FileStatus status: fds) {
271 families.add(status.getPath().getName());
272 }
273
274 return families;
275 }
276
277
278
279
280
281
282 public void deleteFamily(final String familyName) throws IOException {
283
284 HFileArchiver.archiveFamily(fs, conf, regionInfo, tableDir, Bytes.toBytes(familyName));
285
286
287 Path familyDir = getStoreDir(familyName);
288 if(fs.exists(familyDir) && !deleteDir(familyDir))
289 throw new IOException("Could not delete family " + familyName
290 + " from FileSystem for region " + regionInfo.getRegionNameAsString() + "("
291 + regionInfo.getEncodedName() + ")");
292 }
293
294
295
296
297
298
299 private static String generateUniqueName(final String suffix) {
300 String name = UUID.randomUUID().toString().replaceAll("-", "");
301 if (suffix != null) name += suffix;
302 return name;
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316 public Path createTempName() {
317 return createTempName(null);
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 public Path createTempName(final String suffix) {
333 return new Path(getTempDir(), generateUniqueName(suffix));
334 }
335
336
337
338
339
340
341
342
343 public Path commitStoreFile(final String familyName, final Path buildPath) throws IOException {
344 return commitStoreFile(familyName, buildPath, -1, false);
345 }
346
347
348
349
350
351
352
353
354
355
356 private Path commitStoreFile(final String familyName, final Path buildPath,
357 final long seqNum, final boolean generateNewName) throws IOException {
358 Path storeDir = getStoreDir(familyName);
359 if(!fs.exists(storeDir) && !createDir(storeDir))
360 throw new IOException("Failed creating " + storeDir);
361
362 String name = buildPath.getName();
363 if (generateNewName) {
364 name = generateUniqueName((seqNum < 0) ? null : "_SeqId_" + seqNum + "_");
365 }
366 Path dstPath = new Path(storeDir, name);
367 if (!fs.exists(buildPath)) {
368 throw new FileNotFoundException(buildPath.toString());
369 }
370 LOG.debug("Committing store file " + buildPath + " as " + dstPath);
371
372 if (!rename(buildPath, dstPath)) {
373 throw new IOException("Failed rename of " + buildPath + " to " + dstPath);
374 }
375 return dstPath;
376 }
377
378
379
380
381
382
383
384 void commitStoreFiles(final Map<byte[], List<StoreFile>> storeFiles) throws IOException {
385 for (Map.Entry<byte[], List<StoreFile>> es: storeFiles.entrySet()) {
386 String familyName = Bytes.toString(es.getKey());
387 for (StoreFile sf: es.getValue()) {
388 commitStoreFile(familyName, sf.getPath());
389 }
390 }
391 }
392
393
394
395
396
397
398
399 public void removeStoreFile(final String familyName, final Path filePath)
400 throws IOException {
401 HFileArchiver.archiveStoreFile(this.conf, this.fs, this.regionInfo,
402 this.tableDir, Bytes.toBytes(familyName), filePath);
403 }
404
405
406
407
408
409
410
411 public void removeStoreFiles(final String familyName, final Collection<StoreFile> storeFiles)
412 throws IOException {
413 HFileArchiver.archiveStoreFiles(this.conf, this.fs, this.regionInfo,
414 this.tableDir, Bytes.toBytes(familyName), storeFiles);
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428 Path bulkLoadStoreFile(final String familyName, Path srcPath, long seqNum)
429 throws IOException {
430
431 FileSystem srcFs = srcPath.getFileSystem(conf);
432 FileSystem desFs = fs instanceof HFileSystem ? ((HFileSystem)fs).getBackingFs() : fs;
433
434
435
436
437 if (!FSHDFSUtils.isSameHdfs(conf, srcFs, desFs)) {
438 LOG.info("Bulk-load file " + srcPath + " is on different filesystem than " +
439 "the destination store. Copying file over to destination filesystem.");
440 Path tmpPath = createTempName();
441 FileUtil.copy(srcFs, srcPath, fs, tmpPath, false, conf);
442 LOG.info("Copied " + srcPath + " to temporary path on destination filesystem: " + tmpPath);
443 srcPath = tmpPath;
444 }
445
446 return commitStoreFile(familyName, srcPath, seqNum, true);
447 }
448
449
450
451
452
453 Path getSplitsDir() {
454 return new Path(getRegionDir(), REGION_SPLITS_DIR);
455 }
456
457 Path getSplitsDir(final HRegionInfo hri) {
458 return new Path(getSplitsDir(), hri.getEncodedName());
459 }
460
461
462
463
464 void cleanupSplitsDir() throws IOException {
465 deleteDir(getSplitsDir());
466 }
467
468
469
470
471
472
473
474 void cleanupAnySplitDetritus() throws IOException {
475 Path splitdir = this.getSplitsDir();
476 if (!fs.exists(splitdir)) return;
477
478
479
480
481
482
483
484 FileStatus[] daughters = FSUtils.listStatus(fs, splitdir, new FSUtils.DirFilter(fs));
485 if (daughters != null) {
486 for (FileStatus daughter: daughters) {
487 Path daughterDir = new Path(getTableDir(), daughter.getPath().getName());
488 if (fs.exists(daughterDir) && !deleteDir(daughterDir)) {
489 throw new IOException("Failed delete of " + daughterDir);
490 }
491 }
492 }
493 cleanupSplitsDir();
494 LOG.info("Cleaned up old failed split transaction detritus: " + splitdir);
495 }
496
497
498
499
500
501
502 void cleanupDaughterRegion(final HRegionInfo regionInfo) throws IOException {
503 Path regionDir = new Path(this.tableDir, regionInfo.getEncodedName());
504 if (this.fs.exists(regionDir) && !deleteDir(regionDir)) {
505 throw new IOException("Failed delete of " + regionDir);
506 }
507 }
508
509
510
511
512
513
514
515
516 Path commitDaughterRegion(final HRegionInfo regionInfo)
517 throws IOException {
518 Path regionDir = new Path(this.tableDir, regionInfo.getEncodedName());
519 Path daughterTmpDir = this.getSplitsDir(regionInfo);
520
521 if (fs.exists(daughterTmpDir)) {
522
523
524 Path regionInfoFile = new Path(daughterTmpDir, REGION_INFO_FILE);
525 byte[] regionInfoContent = getRegionInfoFileContent(regionInfo);
526 writeRegionInfoFileContent(conf, fs, regionInfoFile, regionInfoContent);
527
528
529 if (!rename(daughterTmpDir, regionDir)) {
530 throw new IOException("Unable to rename " + daughterTmpDir + " to " + regionDir);
531 }
532 }
533
534 return regionDir;
535 }
536
537
538
539
540 void createSplitsDir() throws IOException {
541 Path splitdir = getSplitsDir();
542 if (fs.exists(splitdir)) {
543 LOG.info("The " + splitdir + " directory exists. Hence deleting it to recreate it");
544 if (!deleteDir(splitdir)) {
545 throw new IOException("Failed deletion of " + splitdir
546 + " before creating them again.");
547 }
548 }
549
550 if (!createDir(splitdir)) {
551 throw new IOException("Failed create of " + splitdir);
552 }
553 }
554
555
556
557
558
559
560
561
562
563
564
565
566
567 Path splitStoreFile(final HRegionInfo hri, final String familyName, final StoreFile f,
568 final byte[] splitRow, final boolean top, RegionSplitPolicy splitPolicy) throws IOException {
569
570 if (splitPolicy == null || !splitPolicy.skipStoreFileRangeCheck(familyName)) {
571
572
573 try {
574 if (top) {
575
576 KeyValue splitKey = KeyValue.createFirstOnRow(splitRow);
577 byte[] lastKey = f.createReader().getLastKey();
578
579 if (lastKey == null) return null;
580 if (f.getReader().getComparator().compareFlatKey(splitKey.getBuffer(),
581 splitKey.getKeyOffset(), splitKey.getKeyLength(), lastKey, 0, lastKey.length) > 0) {
582 return null;
583 }
584 } else {
585
586 KeyValue splitKey = KeyValue.createLastOnRow(splitRow);
587 byte[] firstKey = f.createReader().getFirstKey();
588
589 if (firstKey == null) return null;
590 if (f.getReader().getComparator().compareFlatKey(splitKey.getBuffer(),
591 splitKey.getKeyOffset(), splitKey.getKeyLength(), firstKey, 0, firstKey.length) < 0) {
592 return null;
593 }
594 }
595 } finally {
596 f.closeReader(f.getCacheConf() != null ? f.getCacheConf().shouldEvictOnClose() : true);
597 }
598 }
599
600 Path splitDir = new Path(getSplitsDir(hri), familyName);
601
602 Reference r =
603 top ? Reference.createTopReference(splitRow): Reference.createBottomReference(splitRow);
604
605
606
607
608 String parentRegionName = regionInfo.getEncodedName();
609
610
611 Path p = new Path(splitDir, f.getPath().getName() + "." + parentRegionName);
612 return r.write(fs, p);
613 }
614
615
616
617
618
619 Path getMergesDir() {
620 return new Path(getRegionDir(), REGION_MERGES_DIR);
621 }
622
623 Path getMergesDir(final HRegionInfo hri) {
624 return new Path(getMergesDir(), hri.getEncodedName());
625 }
626
627
628
629
630 void cleanupMergesDir() throws IOException {
631 deleteDir(getMergesDir());
632 }
633
634
635
636
637
638
639 void cleanupMergedRegion(final HRegionInfo mergedRegion) throws IOException {
640 Path regionDir = new Path(this.tableDir, mergedRegion.getEncodedName());
641 if (this.fs.exists(regionDir) && !this.fs.delete(regionDir, true)) {
642 throw new IOException("Failed delete of " + regionDir);
643 }
644 }
645
646
647
648
649
650
651 void createMergesDir() throws IOException {
652 Path mergesdir = getMergesDir();
653 if (fs.exists(mergesdir)) {
654 LOG.info("The " + mergesdir
655 + " directory exists. Hence deleting it to recreate it");
656 if (!fs.delete(mergesdir, true)) {
657 throw new IOException("Failed deletion of " + mergesdir
658 + " before creating them again.");
659 }
660 }
661 if (!fs.mkdirs(mergesdir))
662 throw new IOException("Failed create of " + mergesdir);
663 }
664
665
666
667
668
669
670
671
672
673
674
675 Path mergeStoreFile(final HRegionInfo mergedRegion, final String familyName,
676 final StoreFile f, final Path mergedDir)
677 throws IOException {
678 Path referenceDir = new Path(new Path(mergedDir,
679 mergedRegion.getEncodedName()), familyName);
680
681 Reference r = Reference.createTopReference(regionInfo.getStartKey());
682
683
684
685
686 String mergingRegionName = regionInfo.getEncodedName();
687
688
689 Path p = new Path(referenceDir, f.getPath().getName() + "."
690 + mergingRegionName);
691 return r.write(fs, p);
692 }
693
694
695
696
697
698
699
700 void commitMergedRegion(final HRegionInfo mergedRegionInfo) throws IOException {
701 Path regionDir = new Path(this.tableDir, mergedRegionInfo.getEncodedName());
702 Path mergedRegionTmpDir = this.getMergesDir(mergedRegionInfo);
703
704 if (mergedRegionTmpDir != null && fs.exists(mergedRegionTmpDir)) {
705 if (!fs.rename(mergedRegionTmpDir, regionDir)) {
706 throw new IOException("Unable to rename " + mergedRegionTmpDir + " to "
707 + regionDir);
708 }
709 }
710 }
711
712
713
714
715
716
717
718
719
720 void logFileSystemState(final Log LOG) throws IOException {
721 FSUtils.logFileSystemState(fs, this.getRegionDir(), LOG);
722 }
723
724
725
726
727
728
729 private static byte[] getRegionInfoFileContent(final HRegionInfo hri) throws IOException {
730 return hri.toDelimitedByteArray();
731 }
732
733
734
735
736
737
738
739
740 public static HRegionInfo loadRegionInfoFileContent(final FileSystem fs, final Path regionDir)
741 throws IOException {
742 FSDataInputStream in = fs.open(new Path(regionDir, REGION_INFO_FILE));
743 try {
744 return HRegionInfo.parseFrom(in);
745 } finally {
746 in.close();
747 }
748 }
749
750
751
752
753 private static void writeRegionInfoFileContent(final Configuration conf, final FileSystem fs,
754 final Path regionInfoFile, final byte[] content) throws IOException {
755
756 FsPermission perms = FSUtils.getFilePermissions(fs, conf, HConstants.DATA_FILE_UMASK_KEY);
757
758 FSDataOutputStream out = FSUtils.create(conf, fs, regionInfoFile, perms, null);
759 try {
760 out.write(content);
761 } finally {
762 out.close();
763 }
764 }
765
766
767
768
769
770 void checkRegionInfoOnFilesystem() throws IOException {
771
772
773
774
775
776 byte[] content = getRegionInfoFileContent(regionInfo);
777 try {
778 Path regionInfoFile = new Path(getRegionDir(), REGION_INFO_FILE);
779
780 FileStatus status = fs.getFileStatus(regionInfoFile);
781 if (status != null && status.getLen() == content.length) {
782
783
784 return;
785 }
786
787 LOG.info("Rewriting .regioninfo file at: " + regionInfoFile);
788 if (!fs.delete(regionInfoFile, false)) {
789 throw new IOException("Unable to remove existing " + regionInfoFile);
790 }
791 } catch (FileNotFoundException e) {
792 LOG.warn(REGION_INFO_FILE + " file not found for region: " + regionInfo.getEncodedName());
793 }
794
795
796 writeRegionInfoOnFilesystem(content, true);
797 }
798
799
800
801
802
803 private void writeRegionInfoOnFilesystem(boolean useTempDir) throws IOException {
804 byte[] content = getRegionInfoFileContent(regionInfo);
805 writeRegionInfoOnFilesystem(content, useTempDir);
806 }
807
808
809
810
811
812
813 private void writeRegionInfoOnFilesystem(final byte[] regionInfoContent,
814 final boolean useTempDir) throws IOException {
815 Path regionInfoFile = new Path(getRegionDir(), REGION_INFO_FILE);
816 if (useTempDir) {
817
818
819
820
821
822
823 Path tmpPath = new Path(getTempDir(), REGION_INFO_FILE);
824
825
826
827
828
829 if (FSUtils.isExists(fs, tmpPath)) {
830 FSUtils.delete(fs, tmpPath, true);
831 }
832
833
834 writeRegionInfoFileContent(conf, fs, tmpPath, regionInfoContent);
835
836
837 if (fs.exists(tmpPath) && !rename(tmpPath, regionInfoFile)) {
838 throw new IOException("Unable to rename " + tmpPath + " to " + regionInfoFile);
839 }
840 } else {
841
842 writeRegionInfoFileContent(conf, fs, regionInfoFile, regionInfoContent);
843 }
844 }
845
846
847
848
849
850
851
852
853
854 public static HRegionFileSystem createRegionOnFileSystem(final Configuration conf,
855 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo) throws IOException {
856 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
857 Path regionDir = regionFs.getRegionDir();
858
859 if (fs.exists(regionDir)) {
860 LOG.warn("Trying to create a region that already exists on disk: " + regionDir);
861 throw new IOException("The specified region already exists on disk: " + regionDir);
862 }
863
864
865 if (!createDirOnFileSystem(fs, conf, regionDir)) {
866 LOG.warn("Unable to create the region directory: " + regionDir);
867 throw new IOException("Unable to create region directory: " + regionDir);
868 }
869
870
871 regionFs.writeRegionInfoOnFilesystem(false);
872 return regionFs;
873 }
874
875
876
877
878
879
880
881
882
883
884 public static HRegionFileSystem openRegionFromFileSystem(final Configuration conf,
885 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo, boolean readOnly)
886 throws IOException {
887 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
888 Path regionDir = regionFs.getRegionDir();
889
890 if (!fs.exists(regionDir)) {
891 LOG.warn("Trying to open a region that do not exists on disk: " + regionDir);
892 throw new IOException("The specified region do not exists on disk: " + regionDir);
893 }
894
895 if (!readOnly) {
896
897 regionFs.cleanupTempDir();
898 regionFs.cleanupSplitsDir();
899 regionFs.cleanupMergesDir();
900
901
902 regionFs.checkRegionInfoOnFilesystem();
903 }
904
905 return regionFs;
906 }
907
908
909
910
911
912
913
914
915
916 public static void deleteRegionFromFileSystem(final Configuration conf,
917 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo) throws IOException {
918 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
919 Path regionDir = regionFs.getRegionDir();
920
921 if (!fs.exists(regionDir)) {
922 LOG.warn("Trying to delete a region that do not exists on disk: " + regionDir);
923 return;
924 }
925
926 if (LOG.isDebugEnabled()) {
927 LOG.debug("DELETING region " + regionDir);
928 }
929
930
931 Path rootDir = FSUtils.getRootDir(conf);
932 HFileArchiver.archiveRegion(fs, rootDir, tableDir, regionDir);
933
934
935 if (!fs.delete(regionDir, true)) {
936 LOG.warn("Failed delete of " + regionDir);
937 }
938 }
939
940
941
942
943
944
945
946
947 boolean createDir(Path dir) throws IOException {
948 int i = 0;
949 IOException lastIOE = null;
950 do {
951 try {
952 return fs.mkdirs(dir);
953 } catch (IOException ioe) {
954 lastIOE = ioe;
955 if (fs.exists(dir)) return true;
956 sleepBeforeRetry("Create Directory", i+1);
957 }
958 } while (++i <= hdfsClientRetriesNumber);
959 throw new IOException("Exception in createDir", lastIOE);
960 }
961
962
963
964
965
966
967
968
969 boolean rename(Path srcpath, Path dstPath) throws IOException {
970 IOException lastIOE = null;
971 int i = 0;
972 do {
973 try {
974 return fs.rename(srcpath, dstPath);
975 } catch (IOException ioe) {
976 lastIOE = ioe;
977 if (!fs.exists(srcpath) && fs.exists(dstPath)) return true;
978
979 sleepBeforeRetry("Rename Directory", i+1);
980 }
981 } while (++i <= hdfsClientRetriesNumber);
982 throw new IOException("Exception in rename", lastIOE);
983 }
984
985
986
987
988
989
990
991 boolean deleteDir(Path dir) throws IOException {
992 IOException lastIOE = null;
993 int i = 0;
994 do {
995 try {
996 return fs.delete(dir, true);
997 } catch (IOException ioe) {
998 lastIOE = ioe;
999 if (!fs.exists(dir)) return true;
1000
1001 sleepBeforeRetry("Delete Directory", i+1);
1002 }
1003 } while (++i <= hdfsClientRetriesNumber);
1004 throw new IOException("Exception in DeleteDir", lastIOE);
1005 }
1006
1007
1008
1009
1010 private void sleepBeforeRetry(String msg, int sleepMultiplier) {
1011 sleepBeforeRetry(msg, sleepMultiplier, baseSleepBeforeRetries, hdfsClientRetriesNumber);
1012 }
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 private static boolean createDirOnFileSystem(FileSystem fs, Configuration conf, Path dir)
1025 throws IOException {
1026 int i = 0;
1027 IOException lastIOE = null;
1028 int hdfsClientRetriesNumber = conf.getInt("hdfs.client.retries.number",
1029 DEFAULT_HDFS_CLIENT_RETRIES_NUMBER);
1030 int baseSleepBeforeRetries = conf.getInt("hdfs.client.sleep.before.retries",
1031 DEFAULT_BASE_SLEEP_BEFORE_RETRIES);
1032 do {
1033 try {
1034 return fs.mkdirs(dir);
1035 } catch (IOException ioe) {
1036 lastIOE = ioe;
1037 if (fs.exists(dir)) return true;
1038 sleepBeforeRetry("Create Directory", i+1, baseSleepBeforeRetries, hdfsClientRetriesNumber);
1039 }
1040 } while (++i <= hdfsClientRetriesNumber);
1041 throw new IOException("Exception in createDir", lastIOE);
1042 }
1043
1044
1045
1046
1047
1048 private static void sleepBeforeRetry(String msg, int sleepMultiplier, int baseSleepBeforeRetries,
1049 int hdfsClientRetriesNumber) {
1050 if (sleepMultiplier > hdfsClientRetriesNumber) {
1051 LOG.debug(msg + ", retries exhausted");
1052 return;
1053 }
1054 LOG.debug(msg + ", sleeping " + baseSleepBeforeRetries + " times " + sleepMultiplier);
1055 Threads.sleep((long)baseSleepBeforeRetries * sleepMultiplier);
1056 }
1057 }