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()) {
571
572
573 if (top) {
574
575 KeyValue splitKey = KeyValue.createFirstOnRow(splitRow);
576 byte[] lastKey = f.createReader().getLastKey();
577
578 if (lastKey == null) return null;
579 if (f.getReader().getComparator().compareFlatKey(splitKey.getBuffer(),
580 splitKey.getKeyOffset(), splitKey.getKeyLength(), lastKey, 0, lastKey.length) > 0) {
581 return null;
582 }
583 } else {
584
585 KeyValue splitKey = KeyValue.createLastOnRow(splitRow);
586 byte[] firstKey = f.createReader().getFirstKey();
587
588 if (firstKey == null) return null;
589 if (f.getReader().getComparator().compareFlatKey(splitKey.getBuffer(),
590 splitKey.getKeyOffset(), splitKey.getKeyLength(), firstKey, 0, firstKey.length) < 0) {
591 return null;
592 }
593 }
594 }
595
596 f.closeReader(true);
597
598 Path splitDir = new Path(getSplitsDir(hri), familyName);
599
600 Reference r =
601 top ? Reference.createTopReference(splitRow): Reference.createBottomReference(splitRow);
602
603
604
605
606 String parentRegionName = regionInfo.getEncodedName();
607
608
609 Path p = new Path(splitDir, f.getPath().getName() + "." + parentRegionName);
610 return r.write(fs, p);
611 }
612
613
614
615
616
617 Path getMergesDir() {
618 return new Path(getRegionDir(), REGION_MERGES_DIR);
619 }
620
621 Path getMergesDir(final HRegionInfo hri) {
622 return new Path(getMergesDir(), hri.getEncodedName());
623 }
624
625
626
627
628 void cleanupMergesDir() throws IOException {
629 deleteDir(getMergesDir());
630 }
631
632
633
634
635
636
637 void cleanupMergedRegion(final HRegionInfo mergedRegion) throws IOException {
638 Path regionDir = new Path(this.tableDir, mergedRegion.getEncodedName());
639 if (this.fs.exists(regionDir) && !this.fs.delete(regionDir, true)) {
640 throw new IOException("Failed delete of " + regionDir);
641 }
642 }
643
644
645
646
647
648
649 void createMergesDir() throws IOException {
650 Path mergesdir = getMergesDir();
651 if (fs.exists(mergesdir)) {
652 LOG.info("The " + mergesdir
653 + " directory exists. Hence deleting it to recreate it");
654 if (!fs.delete(mergesdir, true)) {
655 throw new IOException("Failed deletion of " + mergesdir
656 + " before creating them again.");
657 }
658 }
659 if (!fs.mkdirs(mergesdir))
660 throw new IOException("Failed create of " + mergesdir);
661 }
662
663
664
665
666
667
668
669
670
671
672
673 Path mergeStoreFile(final HRegionInfo mergedRegion, final String familyName,
674 final StoreFile f, final Path mergedDir)
675 throws IOException {
676 Path referenceDir = new Path(new Path(mergedDir,
677 mergedRegion.getEncodedName()), familyName);
678
679 Reference r = Reference.createTopReference(regionInfo.getStartKey());
680
681
682
683
684 String mergingRegionName = regionInfo.getEncodedName();
685
686
687 Path p = new Path(referenceDir, f.getPath().getName() + "."
688 + mergingRegionName);
689 return r.write(fs, p);
690 }
691
692
693
694
695
696
697
698 void commitMergedRegion(final HRegionInfo mergedRegionInfo) throws IOException {
699 Path regionDir = new Path(this.tableDir, mergedRegionInfo.getEncodedName());
700 Path mergedRegionTmpDir = this.getMergesDir(mergedRegionInfo);
701
702 if (mergedRegionTmpDir != null && fs.exists(mergedRegionTmpDir)) {
703 if (!fs.rename(mergedRegionTmpDir, regionDir)) {
704 throw new IOException("Unable to rename " + mergedRegionTmpDir + " to "
705 + regionDir);
706 }
707 }
708 }
709
710
711
712
713
714
715
716
717
718 void logFileSystemState(final Log LOG) throws IOException {
719 FSUtils.logFileSystemState(fs, this.getRegionDir(), LOG);
720 }
721
722
723
724
725
726
727 private static byte[] getRegionInfoFileContent(final HRegionInfo hri) throws IOException {
728 return hri.toDelimitedByteArray();
729 }
730
731
732
733
734
735
736
737
738 public static HRegionInfo loadRegionInfoFileContent(final FileSystem fs, final Path regionDir)
739 throws IOException {
740 FSDataInputStream in = fs.open(new Path(regionDir, REGION_INFO_FILE));
741 try {
742 return HRegionInfo.parseFrom(in);
743 } finally {
744 in.close();
745 }
746 }
747
748
749
750
751 private static void writeRegionInfoFileContent(final Configuration conf, final FileSystem fs,
752 final Path regionInfoFile, final byte[] content) throws IOException {
753
754 FsPermission perms = FSUtils.getFilePermissions(fs, conf, HConstants.DATA_FILE_UMASK_KEY);
755
756 FSDataOutputStream out = FSUtils.create(fs, regionInfoFile, perms, null);
757 try {
758 out.write(content);
759 } finally {
760 out.close();
761 }
762 }
763
764
765
766
767
768 void checkRegionInfoOnFilesystem() throws IOException {
769
770
771
772
773
774 byte[] content = getRegionInfoFileContent(regionInfo);
775 try {
776 Path regionInfoFile = new Path(getRegionDir(), REGION_INFO_FILE);
777
778 FileStatus status = fs.getFileStatus(regionInfoFile);
779 if (status != null && status.getLen() == content.length) {
780
781
782 return;
783 }
784
785 LOG.info("Rewriting .regioninfo file at: " + regionInfoFile);
786 if (!fs.delete(regionInfoFile, false)) {
787 throw new IOException("Unable to remove existing " + regionInfoFile);
788 }
789 } catch (FileNotFoundException e) {
790 LOG.warn(REGION_INFO_FILE + " file not found for region: " + regionInfo.getEncodedName());
791 }
792
793
794 writeRegionInfoOnFilesystem(content, true);
795 }
796
797
798
799
800
801 private void writeRegionInfoOnFilesystem(boolean useTempDir) throws IOException {
802 byte[] content = getRegionInfoFileContent(regionInfo);
803 writeRegionInfoOnFilesystem(content, useTempDir);
804 }
805
806
807
808
809
810
811 private void writeRegionInfoOnFilesystem(final byte[] regionInfoContent,
812 final boolean useTempDir) throws IOException {
813 Path regionInfoFile = new Path(getRegionDir(), REGION_INFO_FILE);
814 if (useTempDir) {
815
816
817
818
819
820
821 Path tmpPath = new Path(getTempDir(), REGION_INFO_FILE);
822
823
824
825
826
827 if (FSUtils.isExists(fs, tmpPath)) {
828 FSUtils.delete(fs, tmpPath, true);
829 }
830
831
832 writeRegionInfoFileContent(conf, fs, tmpPath, regionInfoContent);
833
834
835 if (fs.exists(tmpPath) && !rename(tmpPath, regionInfoFile)) {
836 throw new IOException("Unable to rename " + tmpPath + " to " + regionInfoFile);
837 }
838 } else {
839
840 writeRegionInfoFileContent(conf, fs, regionInfoFile, regionInfoContent);
841 }
842 }
843
844
845
846
847
848
849
850
851
852 public static HRegionFileSystem createRegionOnFileSystem(final Configuration conf,
853 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo) throws IOException {
854 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
855 Path regionDir = regionFs.getRegionDir();
856
857 if (fs.exists(regionDir)) {
858 LOG.warn("Trying to create a region that already exists on disk: " + regionDir);
859 throw new IOException("The specified region already exists on disk: " + regionDir);
860 }
861
862
863 if (!createDirOnFileSystem(fs, conf, regionDir)) {
864 LOG.warn("Unable to create the region directory: " + regionDir);
865 throw new IOException("Unable to create region directory: " + regionDir);
866 }
867
868
869 regionFs.writeRegionInfoOnFilesystem(false);
870 return regionFs;
871 }
872
873
874
875
876
877
878
879
880
881
882 public static HRegionFileSystem openRegionFromFileSystem(final Configuration conf,
883 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo, boolean readOnly)
884 throws IOException {
885 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
886 Path regionDir = regionFs.getRegionDir();
887
888 if (!fs.exists(regionDir)) {
889 LOG.warn("Trying to open a region that do not exists on disk: " + regionDir);
890 throw new IOException("The specified region do not exists on disk: " + regionDir);
891 }
892
893 if (!readOnly) {
894
895 regionFs.cleanupTempDir();
896 regionFs.cleanupSplitsDir();
897 regionFs.cleanupMergesDir();
898
899
900 regionFs.checkRegionInfoOnFilesystem();
901 }
902
903 return regionFs;
904 }
905
906
907
908
909
910
911
912
913
914 public static void deleteRegionFromFileSystem(final Configuration conf,
915 final FileSystem fs, final Path tableDir, final HRegionInfo regionInfo) throws IOException {
916 HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
917 Path regionDir = regionFs.getRegionDir();
918
919 if (!fs.exists(regionDir)) {
920 LOG.warn("Trying to delete a region that do not exists on disk: " + regionDir);
921 return;
922 }
923
924 if (LOG.isDebugEnabled()) {
925 LOG.debug("DELETING region " + regionDir);
926 }
927
928
929 Path rootDir = FSUtils.getRootDir(conf);
930 HFileArchiver.archiveRegion(fs, rootDir, tableDir, regionDir);
931
932
933 if (!fs.delete(regionDir, true)) {
934 LOG.warn("Failed delete of " + regionDir);
935 }
936 }
937
938
939
940
941
942
943
944
945 boolean createDir(Path dir) throws IOException {
946 int i = 0;
947 IOException lastIOE = null;
948 do {
949 try {
950 return fs.mkdirs(dir);
951 } catch (IOException ioe) {
952 lastIOE = ioe;
953 if (fs.exists(dir)) return true;
954 sleepBeforeRetry("Create Directory", i+1);
955 }
956 } while (++i <= hdfsClientRetriesNumber);
957 throw new IOException("Exception in createDir", lastIOE);
958 }
959
960
961
962
963
964
965
966
967 boolean rename(Path srcpath, Path dstPath) throws IOException {
968 IOException lastIOE = null;
969 int i = 0;
970 do {
971 try {
972 return fs.rename(srcpath, dstPath);
973 } catch (IOException ioe) {
974 lastIOE = ioe;
975 if (!fs.exists(srcpath) && fs.exists(dstPath)) return true;
976
977 sleepBeforeRetry("Rename Directory", i+1);
978 }
979 } while (++i <= hdfsClientRetriesNumber);
980 throw new IOException("Exception in rename", lastIOE);
981 }
982
983
984
985
986
987
988
989 boolean deleteDir(Path dir) throws IOException {
990 IOException lastIOE = null;
991 int i = 0;
992 do {
993 try {
994 return fs.delete(dir, true);
995 } catch (IOException ioe) {
996 lastIOE = ioe;
997 if (!fs.exists(dir)) return true;
998
999 sleepBeforeRetry("Delete Directory", i+1);
1000 }
1001 } while (++i <= hdfsClientRetriesNumber);
1002 throw new IOException("Exception in DeleteDir", lastIOE);
1003 }
1004
1005
1006
1007
1008 private void sleepBeforeRetry(String msg, int sleepMultiplier) {
1009 sleepBeforeRetry(msg, sleepMultiplier, baseSleepBeforeRetries, hdfsClientRetriesNumber);
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022 private static boolean createDirOnFileSystem(FileSystem fs, Configuration conf, Path dir)
1023 throws IOException {
1024 int i = 0;
1025 IOException lastIOE = null;
1026 int hdfsClientRetriesNumber = conf.getInt("hdfs.client.retries.number",
1027 DEFAULT_HDFS_CLIENT_RETRIES_NUMBER);
1028 int baseSleepBeforeRetries = conf.getInt("hdfs.client.sleep.before.retries",
1029 DEFAULT_BASE_SLEEP_BEFORE_RETRIES);
1030 do {
1031 try {
1032 return fs.mkdirs(dir);
1033 } catch (IOException ioe) {
1034 lastIOE = ioe;
1035 if (fs.exists(dir)) return true;
1036 sleepBeforeRetry("Create Directory", i+1, baseSleepBeforeRetries, hdfsClientRetriesNumber);
1037 }
1038 } while (++i <= hdfsClientRetriesNumber);
1039 throw new IOException("Exception in createDir", lastIOE);
1040 }
1041
1042
1043
1044
1045
1046 private static void sleepBeforeRetry(String msg, int sleepMultiplier, int baseSleepBeforeRetries,
1047 int hdfsClientRetriesNumber) {
1048 if (sleepMultiplier > hdfsClientRetriesNumber) {
1049 LOG.debug(msg + ", retries exhausted");
1050 return;
1051 }
1052 LOG.debug(msg + ", sleeping " + baseSleepBeforeRetries + " times " + sleepMultiplier);
1053 Threads.sleep((long)baseSleepBeforeRetries * sleepMultiplier);
1054 }
1055 }