-
Notifications
You must be signed in to change notification settings - Fork 13
/
HDFS-001.patch
359 lines (352 loc) · 15.3 KB
/
HDFS-001.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java
index 330c381..a491699 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java
@@ -18,6 +18,8 @@
package org.apache.hadoop.hdfs;
+import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_AVAILABLE_SPACE_VOLUME_CHOOSING_POLICY_BALANCED_SPACE_THRESHOLD_KEY;
+
import java.util.concurrent.TimeUnit;
import org.apache.hadoop.classification.InterfaceAudience;
@@ -176,6 +178,11 @@
public static final long DFS_DN_CACHED_DFSUSED_CHECK_INTERVAL_DEFAULT_MS =
600000;
+ public static final String DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_KEY =
+ "dfs.datanode.reference-count-volume-choosing-policy.reference-count-threshold";
+ public static final int DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_DEFAULT =
+ 10;
+
public static final String DFS_NAMENODE_PATH_BASED_CACHE_BLOCK_MAP_ALLOCATION_PERCENT =
"dfs.namenode.path.based.cache.block.map.allocation.percent";
public static final float DFS_NAMENODE_PATH_BASED_CACHE_BLOCK_MAP_ALLOCATION_PERCENT_DEFAULT = 0.25f;
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/FsVolumeSpi.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/FsVolumeSpi.java
index e6ace44..ce9fe75 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/FsVolumeSpi.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/FsVolumeSpi.java
@@ -179,4 +179,9 @@ public BlockIterator loadBlockIterator(String bpid, String name)
* Get the FSDatasetSpi which this volume is a part of.
*/
public FsDatasetSpi getDataset();
+
+ /**
+ * Get the reference count of this volume
+ */
+ public int getReferenceCount();
}
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/ReferenceCountVolumeChoosingPolicy.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/ReferenceCountVolumeChoosingPolicy.java
new file mode 100644
index 0000000..1b3caa8
--- /dev/null
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/ReferenceCountVolumeChoosingPolicy.java
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdfs.server.datanode.fsdataset;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.util.DiskChecker.DiskOutOfSpaceException;
+import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_DEFAULT;
+import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_KEY;
+/**
+ * Choose volumes by fsVolume reference count
+ */
+public class ReferenceCountVolumeChoosingPolicy<V extends FsVolumeSpi>
+ implements VolumeChoosingPolicy<V>, Configurable{
+ public static final Log LOG = LogFactory
+ .getLog(ReferenceCountVolumeChoosingPolicy.class);
+
+ private int referenceThreshold =
+ DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_DEFAULT;
+ private final VolumeChoosingPolicy<V> roundRobinPolicyLowReferences =
+ new RoundRobinVolumeChoosingPolicy<V>();
+ private final VolumeChoosingPolicy<V> roundRobinPolicyHighReferences =
+ new RoundRobinVolumeChoosingPolicy<V>();
+
+ @Override
+ public synchronized void setConf(Configuration conf) {
+ referenceThreshold =
+ conf.getInt(
+ DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_KEY,
+ DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_DEFAULT);
+ }
+
+ @Override
+ public synchronized Configuration getConf() {
+ return null;
+ }
+
+ @Override
+ public synchronized V chooseVolume(final List<V> volumes, long blockSize)
+ throws IOException {
+
+ if (volumes.size() < 1) {
+ throw new DiskOutOfSpaceException("No more available volumes");
+ }
+
+ V volume = null;
+
+ int minReferenceCount = getMinReferenceCountOfVolumes(volumes);
+ List<V> lowReferencesVolumes =
+ getLowReferencesCountVolume(volumes, minReferenceCount);
+ List<V> highReferencesVolumes =
+ getHighReferencesCountVolume(volumes, minReferenceCount);
+
+ if (isExistVolumeHasFreeSpaceForBlock(lowReferencesVolumes, blockSize)) {
+ volume =
+ roundRobinPolicyLowReferences.chooseVolume(lowReferencesVolumes,
+ blockSize);
+ } else {
+ volume =
+ roundRobinPolicyHighReferences.chooseVolume(highReferencesVolumes,
+ blockSize);
+ }
+
+ return volume;
+ }
+
+ private List<V> getHighReferencesCountVolume(final List<V> volumes,
+ int minReferenceCount) {
+ List<V> newVolumes = new ArrayList<V>();
+
+ for (V v : volumes) {
+ if (v.getReferenceCount() > (minReferenceCount + referenceThreshold)) {
+ newVolumes.add(v);
+ }
+ }
+
+ return newVolumes;
+ }
+
+ private List<V> getLowReferencesCountVolume(final List<V> volumes,
+ int minReferenceCount) {
+ List<V> newVolumes = new ArrayList<V>();
+
+ for (V v : volumes) {
+ if (v.getReferenceCount() <= (minReferenceCount + referenceThreshold)) {
+ newVolumes.add(v);
+ }
+ }
+
+ return newVolumes;
+ }
+
+ private int getMinReferenceCountOfVolumes(final List<V> volumes) {
+ int minReferenceCount = Integer.MAX_VALUE;
+
+ int curReferenceCount;
+ for (V v : volumes) {
+ curReferenceCount = v.getReferenceCount();
+ if (curReferenceCount < minReferenceCount) {
+ minReferenceCount = curReferenceCount;
+ }
+ }
+
+ return minReferenceCount;
+ }
+
+ private boolean isExistVolumeHasFreeSpaceForBlock(final List<V> volumes,
+ long blockSize) throws IOException {
+ boolean isExist = false;
+
+ for (V v : volumes) {
+ if (v.getAvailable() >= blockSize) {
+ isExist = true;
+ break;
+ }
+ }
+
+ return isExist;
+ }
+}
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeImpl.java
index bd20db9..418dc97 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeImpl.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeImpl.java
@@ -932,5 +932,10 @@ public StorageType getStorageType() {
DatanodeStorage toDatanodeStorage() {
return new DatanodeStorage(storageID, DatanodeStorage.State.NORMAL, storageType);
}
+
+ @Override
+ public int getReferenceCount() {
+ return this.reference.getReferenceCount();
+ }
}
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/SimulatedFSDataset.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/SimulatedFSDataset.java
index bebf371..558f414 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/SimulatedFSDataset.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/SimulatedFSDataset.java
@@ -506,6 +506,11 @@ public BlockIterator loadBlockIterator(String bpid, String name)
public FsDatasetSpi getDataset() {
throw new UnsupportedOperationException();
}
+
+ @Override
+ public int getReferenceCount() {
+ return 0;
+ }
}
private final Map<String, Map<Block, BInfo>> blockMap
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java
index 62b2ca9..fc5723b 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java
@@ -613,6 +613,11 @@ public BlockIterator loadBlockIterator(String bpid, String name)
public FsDatasetSpi getDataset() {
throw new UnsupportedOperationException();
}
+
+ @Override
+ public int getReferenceCount() {
+ return 0;
+ }
}
private final static TestFsVolumeSpi TEST_VOLUME = new TestFsVolumeSpi();
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/extdataset/ExternalVolumeImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/extdataset/ExternalVolumeImpl.java
index b00d78f..7549f7e 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/extdataset/ExternalVolumeImpl.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/extdataset/ExternalVolumeImpl.java
@@ -97,4 +97,9 @@ public BlockIterator loadBlockIterator(String bpid, String name)
public FsDatasetSpi getDataset() {
return null;
}
+
+ @Override
+ public int getReferenceCount() {
+ return 0;
+ }
}
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestReferenceCountVolumeChoosingPolicy.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestReferenceCountVolumeChoosingPolicy.java
new file mode 100644
index 0000000..ab0c9a2
--- /dev/null
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestReferenceCountVolumeChoosingPolicy.java
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdfs.server.datanode.fsdataset;
+
+import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_KEY;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.util.ReflectionUtils;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestReferenceCountVolumeChoosingPolicy {
+ private static final int REFERENCE_COUNT_THRESHOLD = 2;
+
+ private static void initPolicy(VolumeChoosingPolicy<FsVolumeSpi> policy) {
+ Configuration conf = new Configuration();
+ conf.setLong(
+ DFS_DATANODE_REFERENCE_COUNT_VOLUME_CHOOSING_POLICY_REFERENCE_COUNT_THRESHOLD_KEY,
+ REFERENCE_COUNT_THRESHOLD);
+ ((Configurable) policy).setConf(conf);
+ }
+
+ @Test
+ public void testReferenceCountVolumeChoosingPolicy() throws Exception {
+ @SuppressWarnings("unchecked")
+ final ReferenceCountVolumeChoosingPolicy<FsVolumeSpi> policy =
+ ReflectionUtils.newInstance(ReferenceCountVolumeChoosingPolicy.class,
+ null);
+
+ initPolicy(policy);
+ final List<FsVolumeSpi> volumes = new ArrayList<FsVolumeSpi>();
+
+ // Add two low references count volumes.
+ // First volume, with 1 reference.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(0).getReferenceCount()).thenReturn(1);
+ Mockito.when(volumes.get(0).getAvailable()).thenReturn(100L);
+
+ // First volume, with 2 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(1).getReferenceCount()).thenReturn(2);
+ Mockito.when(volumes.get(1).getAvailable()).thenReturn(100L);
+
+ // Add two high references count volumes.
+ // First volume, with 4 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(2).getReferenceCount()).thenReturn(4);
+ Mockito.when(volumes.get(2).getAvailable()).thenReturn(100L);
+
+ // First volume, with 5 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(3).getReferenceCount()).thenReturn(5);
+ Mockito.when(volumes.get(3).getAvailable()).thenReturn(100L);
+
+ // initPolicy(policy, 1.0f);
+ Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 50));
+
+ volumes.clear();
+
+ // Test when the low-references volumes has not enough available space for
+ // block
+ // First volume, with 1 reference.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(0).getReferenceCount()).thenReturn(1);
+ Mockito.when(volumes.get(0).getAvailable()).thenReturn(50L);
+
+ // First volume, with 2 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(1).getReferenceCount()).thenReturn(2);
+ Mockito.when(volumes.get(1).getAvailable()).thenReturn(50L);
+
+ // Add two high references count volumes.
+ // First volume, with 4 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(2).getReferenceCount()).thenReturn(4);
+ Mockito.when(volumes.get(2).getAvailable()).thenReturn(200L);
+
+ // First volume, with 5 references.
+ volumes.add(Mockito.mock(FsVolumeSpi.class));
+ Mockito.when(volumes.get(3).getReferenceCount()).thenReturn(5);
+ Mockito.when(volumes.get(3).getAvailable()).thenReturn(200L);
+
+ Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100));
+ }
+
+}