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