|
19 | 19 | package org.apache.hadoop.hdfs.server.federation.resolver; |
20 | 20 |
|
21 | 21 | import java.io.IOException; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.IdentityHashMap; |
| 24 | +import java.util.LinkedList; |
22 | 25 | import java.util.List; |
23 | | - |
| 26 | +import java.util.Set; |
| 27 | +import java.util.SortedMap; |
| 28 | +import java.util.TreeSet; |
24 | 29 | import org.apache.hadoop.classification.InterfaceAudience; |
25 | 30 | import org.apache.hadoop.classification.InterfaceStability; |
| 31 | +import org.apache.hadoop.fs.Path; |
| 32 | +import org.apache.hadoop.hdfs.server.federation.store.records.MountTable; |
26 | 33 |
|
27 | 34 | /** |
28 | 35 | * Interface to map a file path in the global name space to a specific |
@@ -69,10 +76,125 @@ public interface FileSubclusterResolver { |
69 | 76 | */ |
70 | 77 | List<String> getMountPoints(String path) throws IOException; |
71 | 78 |
|
| 79 | + /** |
| 80 | + * Get a IdentityHashMap (child, mountTable source path) of mount points for a path. |
| 81 | + * Results are from the mount table cache. |
| 82 | + * |
| 83 | + * @param path Path to get the mount points under. |
| 84 | + * @return IdentityHashMap of mount points present at this path. Return zero-length |
| 85 | + * IdentityHashMap if the path is a mount point but there are no mount points |
| 86 | + * under the path. Return null if the path is not a mount point |
| 87 | + * and there are no mount points under the path. |
| 88 | + * @throws IOException Throws exception if the data is not available. |
| 89 | + */ |
| 90 | + IdentityHashMap<String,String> getMountPointsWithSrc(String path) throws IOException; |
| 91 | + |
| 92 | + |
72 | 93 | /** |
73 | 94 | * Get the default namespace for the cluster. |
74 | 95 | * |
75 | 96 | * @return Default namespace identifier. |
76 | 97 | */ |
77 | 98 | String getDefaultNamespace(); |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the all namespace which has mountpoints for the cluster. |
| 102 | + * |
| 103 | + * @return a set of namespace identifiers. |
| 104 | + */ |
| 105 | + Set<String> getAllNamespaces(); |
| 106 | + |
| 107 | + /** |
| 108 | + * Get a list of mount points for a path. |
| 109 | + * |
| 110 | + * @param path Path to get the mount points under. |
| 111 | + * @param mountPoints the mount points to choose. |
| 112 | + * @return Return empty list if the path is a mount point but there are no |
| 113 | + * mount points under the path. Return null if the path is not a mount |
| 114 | + * point and there are no mount points under the path. |
| 115 | + */ |
| 116 | + static List<String> getMountPoints(String path, |
| 117 | + Collection<String> mountPoints) { |
| 118 | + Set<String> children = new TreeSet<>(); |
| 119 | + boolean exists = false; |
| 120 | + for (String subPath : mountPoints) { |
| 121 | + String child = subPath; |
| 122 | + |
| 123 | + // Special case for / |
| 124 | + if (!path.equals(Path.SEPARATOR)) { |
| 125 | + // Get the children |
| 126 | + int ini = path.length(); |
| 127 | + child = subPath.substring(ini); |
| 128 | + } |
| 129 | + |
| 130 | + if (child.isEmpty()) { |
| 131 | + // This is a mount point but without children |
| 132 | + exists = true; |
| 133 | + } else if (child.startsWith(Path.SEPARATOR)) { |
| 134 | + // This is a mount point with children |
| 135 | + exists = true; |
| 136 | + child = child.substring(1); |
| 137 | + |
| 138 | + // We only return immediate children |
| 139 | + int fin = child.indexOf(Path.SEPARATOR); |
| 140 | + if (fin > -1) { |
| 141 | + child = child.substring(0, fin); |
| 142 | + } |
| 143 | + if (!child.isEmpty()) { |
| 144 | + children.add(child); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + if (!exists) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + return new LinkedList<>(children); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get a IdentityHashMap (child, mountTable source path) for a path. The child can be repetitive. |
| 156 | + * |
| 157 | + * @param path Path to get the mount points under. |
| 158 | + * @param tree the mount points tree. |
| 159 | + * @return Return empty IdentityHashMap if the path is a mount point but there are no |
| 160 | + * mount points under the path. Return null if the path is not a mount |
| 161 | + * point and there are no mount points under the path. |
| 162 | + */ |
| 163 | + static IdentityHashMap<String,String> getMountPointsWithSrc(String path, SortedMap<String, |
| 164 | + MountTable> tree) { |
| 165 | + IdentityHashMap<String,String> childWithSourcePaths = new IdentityHashMap<>(); |
| 166 | + boolean exists = false; |
| 167 | + for (String subPath : tree.keySet()) { |
| 168 | + String child = subPath; |
| 169 | + |
| 170 | + // Special case for / |
| 171 | + if (!path.equals(Path.SEPARATOR)) { |
| 172 | + // Get the children |
| 173 | + int ini = path.length(); |
| 174 | + child = subPath.substring(ini); |
| 175 | + } |
| 176 | + |
| 177 | + if (child.isEmpty()) { |
| 178 | + // This is a mount point but without children |
| 179 | + exists = true; |
| 180 | + } else if (child.startsWith(Path.SEPARATOR)) { |
| 181 | + // This is a mount point with children |
| 182 | + exists = true; |
| 183 | + child = child.substring(1); |
| 184 | + |
| 185 | + // We only return immediate children |
| 186 | + int fin = child.indexOf(Path.SEPARATOR); |
| 187 | + if (fin > -1) { |
| 188 | + child = child.substring(0, fin); |
| 189 | + } |
| 190 | + if (!child.isEmpty()) { |
| 191 | + childWithSourcePaths.put(child, tree.get(subPath).getSourcePath()); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + if (!exists) { |
| 196 | + return null; |
| 197 | + } |
| 198 | + return childWithSourcePaths; |
| 199 | + } |
78 | 200 | } |
0 commit comments