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.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import java.util.concurrent.atomic.AtomicInteger;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.fs.BlockLocation;
30 import org.apache.hadoop.fs.FileStatus;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.util.FSUtils;
34
35
36
37
38
39
40 @InterfaceAudience.Private
41 class FSRegionScanner implements Runnable {
42 static private final Log LOG = LogFactory.getLog(FSRegionScanner.class);
43
44 private Path regionPath;
45
46
47
48
49 private FileSystem fs;
50
51
52
53
54 private Map<String,String> regionToBestLocalityRSMapping;
55
56
57
58
59
60 private Map<String, Map<String, Float>> regionDegreeLocalityMapping;
61
62 FSRegionScanner(FileSystem fs, Path regionPath,
63 Map<String, String> regionToBestLocalityRSMapping,
64 Map<String, Map<String, Float>> regionDegreeLocalityMapping) {
65 this.fs = fs;
66 this.regionPath = regionPath;
67 this.regionToBestLocalityRSMapping = regionToBestLocalityRSMapping;
68 this.regionDegreeLocalityMapping = regionDegreeLocalityMapping;
69 }
70
71 @Override
72 public void run() {
73 try {
74
75 Map<String, AtomicInteger> blockCountMap = new HashMap<String, AtomicInteger>();
76
77
78 String tableName = regionPath.getParent().getName();
79 int totalBlkCount = 0;
80
81
82 FileStatus[] cfList = fs.listStatus(regionPath, new FSUtils.FamilyDirFilter(fs));
83 if (null == cfList) {
84 return;
85 }
86
87
88 for (FileStatus cfStatus : cfList) {
89 if (!cfStatus.isDirectory()) {
90
91 continue;
92 }
93
94 FileStatus[] storeFileLists = fs.listStatus(cfStatus.getPath());
95 if (null == storeFileLists) {
96 continue;
97 }
98
99 for (FileStatus storeFile : storeFileLists) {
100 BlockLocation[] blkLocations =
101 fs.getFileBlockLocations(storeFile, 0, storeFile.getLen());
102 if (null == blkLocations) {
103 continue;
104 }
105
106 totalBlkCount += blkLocations.length;
107 for(BlockLocation blk: blkLocations) {
108 for (String host: blk.getHosts()) {
109 AtomicInteger count = blockCountMap.get(host);
110 if (count == null) {
111 count = new AtomicInteger(0);
112 blockCountMap.put(host, count);
113 }
114 count.incrementAndGet();
115 }
116 }
117 }
118 }
119
120 if (regionToBestLocalityRSMapping != null) {
121 int largestBlkCount = 0;
122 String hostToRun = null;
123 for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
124 String host = entry.getKey();
125
126 int tmp = entry.getValue().get();
127 if (tmp > largestBlkCount) {
128 largestBlkCount = tmp;
129 hostToRun = host;
130 }
131 }
132
133
134 if (null == hostToRun) {
135 return;
136 }
137
138 if (hostToRun.endsWith(".")) {
139 hostToRun = hostToRun.substring(0, hostToRun.length()-1);
140 }
141 String name = tableName + ":" + regionPath.getName();
142 synchronized (regionToBestLocalityRSMapping) {
143 regionToBestLocalityRSMapping.put(name, hostToRun);
144 }
145 }
146
147 if (regionDegreeLocalityMapping != null && totalBlkCount > 0) {
148 Map<String, Float> hostLocalityMap = new HashMap<String, Float>();
149 for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
150 String host = entry.getKey();
151 if (host.endsWith(".")) {
152 host = host.substring(0, host.length() - 1);
153 }
154
155 float locality = ((float)entry.getValue().get()) / totalBlkCount;
156 hostLocalityMap.put(host, locality);
157 }
158
159
160 regionDegreeLocalityMapping.put(regionPath.getName(), hostLocalityMap);
161 }
162 } catch (IOException e) {
163 LOG.warn("Problem scanning file system", e);
164 } catch (RuntimeException e) {
165 LOG.warn("Problem scanning file system", e);
166 }
167 }
168 }