-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
TestDistributedFileSystemWithECFile.java
197 lines (175 loc) · 7.5 KB
/
TestDistributedFileSystemWithECFile.java
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
/**
* 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;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertTrue;
/**
* Testing correctness of FileSystem.getFileBlockLocations and
* FileSystem.listFiles for erasure coded files.
*/
public class TestDistributedFileSystemWithECFile {
private ErasureCodingPolicy ecPolicy;
private int cellSize;
private short dataBlocks;
private short parityBlocks;
private int numDNs;
private int stripesPerBlock;
private int blockSize;
private int blockGroupSize;
private MiniDFSCluster cluster;
private FileContext fileContext;
private DistributedFileSystem fs;
private Configuration conf = new HdfsConfiguration();
public ErasureCodingPolicy getEcPolicy() {
return StripedFileTestUtil.getDefaultECPolicy();
}
@Before
public void setup() throws IOException {
ecPolicy = getEcPolicy();
cellSize = ecPolicy.getCellSize();
dataBlocks = (short) ecPolicy.getNumDataUnits();
parityBlocks = (short) ecPolicy.getNumParityUnits();
numDNs = dataBlocks + parityBlocks;
stripesPerBlock = 4;
blockSize = stripesPerBlock * cellSize;
blockGroupSize = blockSize * dataBlocks;
conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, blockSize);
conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_REDUNDANCY_CONSIDERLOAD_KEY,
false);
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(numDNs).build();
fileContext = FileContext.getFileContext(cluster.getURI(0), conf);
fs = cluster.getFileSystem();
fs.enableErasureCodingPolicy(ecPolicy.getName());
fs.mkdirs(new Path("/ec"));
cluster.getFileSystem().getClient().setErasureCodingPolicy("/ec",
ecPolicy.getName());
}
@After
public void tearDown() throws IOException {
if (cluster != null) {
cluster.shutdown();
cluster = null;
}
}
private void createFile(String path, int size) throws Exception {
byte[] expected = StripedFileTestUtil.generateBytes(size);
Path src = new Path(path);
DFSTestUtil.writeFile(fs, src, new String(expected));
StripedFileTestUtil.waitBlockGroupsReported(fs, src.toString());
StripedFileTestUtil.verifyLength(fs, src, size);
}
@Test(timeout=60000)
public void testListECFilesSmallerThanOneCell() throws Exception {
createFile("/ec/smallcell", 1);
final List<LocatedFileStatus> retVal = new ArrayList<>();
final RemoteIterator<LocatedFileStatus> iter =
cluster.getFileSystem().listFiles(new Path("/ec"), true);
while (iter.hasNext()) {
retVal.add(iter.next());
}
assertTrue(retVal.size() == 1);
LocatedFileStatus fileStatus = retVal.get(0);
assertSmallerThanOneCell(fileStatus.getBlockLocations());
BlockLocation[] locations = cluster.getFileSystem().getFileBlockLocations(
fileStatus, 0, fileStatus.getLen());
assertSmallerThanOneCell(locations);
//Test FileContext
fileStatus = fileContext.listLocatedStatus(new Path("/ec")).next();
assertSmallerThanOneCell(fileStatus.getBlockLocations());
locations = fileContext.getFileBlockLocations(new Path("/ec/smallcell"),
0, fileStatus.getLen());
assertSmallerThanOneCell(locations);
}
private void assertSmallerThanOneCell(BlockLocation[] locations)
throws IOException {
assertTrue(locations.length == 1);
BlockLocation blockLocation = locations[0];
assertTrue(blockLocation.getOffset() == 0);
assertTrue(blockLocation.getLength() == 1);
assertTrue(blockLocation.getHosts().length == 1 + parityBlocks);
}
@Test(timeout=60000)
public void testListECFilesSmallerThanOneStripe() throws Exception {
int dataBlocksNum = dataBlocks;
createFile("/ec/smallstripe", cellSize * dataBlocksNum);
RemoteIterator<LocatedFileStatus> iter =
cluster.getFileSystem().listFiles(new Path("/ec"), true);
LocatedFileStatus fileStatus = iter.next();
assertSmallerThanOneStripe(fileStatus.getBlockLocations(), dataBlocksNum);
BlockLocation[] locations = cluster.getFileSystem().getFileBlockLocations(
fileStatus, 0, fileStatus.getLen());
assertSmallerThanOneStripe(locations, dataBlocksNum);
//Test FileContext
fileStatus = fileContext.listLocatedStatus(new Path("/ec")).next();
assertSmallerThanOneStripe(fileStatus.getBlockLocations(), dataBlocksNum);
locations = fileContext.getFileBlockLocations(new Path("/ec/smallstripe"),
0, fileStatus.getLen());
assertSmallerThanOneStripe(locations, dataBlocksNum);
}
private void assertSmallerThanOneStripe(BlockLocation[] locations,
int dataBlocksNum) throws IOException {
int expectedHostNum = dataBlocksNum + parityBlocks;
assertTrue(locations.length == 1);
BlockLocation blockLocation = locations[0];
assertTrue(blockLocation.getHosts().length == expectedHostNum);
assertTrue(blockLocation.getOffset() == 0);
assertTrue(blockLocation.getLength() == dataBlocksNum * cellSize);
}
@Test(timeout=60000)
public void testListECFilesMoreThanOneBlockGroup() throws Exception {
createFile("/ec/group", blockGroupSize + 123);
RemoteIterator<LocatedFileStatus> iter =
cluster.getFileSystem().listFiles(new Path("/ec"), true);
LocatedFileStatus fileStatus = iter.next();
assertMoreThanOneBlockGroup(fileStatus.getBlockLocations(), 123);
BlockLocation[] locations = cluster.getFileSystem().getFileBlockLocations(
fileStatus, 0, fileStatus.getLen());
assertMoreThanOneBlockGroup(locations, 123);
//Test FileContext
iter = fileContext.listLocatedStatus(new Path("/ec"));
fileStatus = iter.next();
assertMoreThanOneBlockGroup(fileStatus.getBlockLocations(), 123);
locations = fileContext.getFileBlockLocations(new Path("/ec/group"),
0, fileStatus.getLen());
assertMoreThanOneBlockGroup(locations, 123);
}
private void assertMoreThanOneBlockGroup(BlockLocation[] locations,
int lastBlockSize) throws IOException {
assertTrue(locations.length == 2);
BlockLocation fistBlockGroup = locations[0];
assertTrue(fistBlockGroup.getHosts().length == numDNs);
assertTrue(fistBlockGroup.getOffset() == 0);
assertTrue(fistBlockGroup.getLength() == blockGroupSize);
BlockLocation lastBlock = locations[1];
assertTrue(lastBlock.getHosts().length == 1 + parityBlocks);
assertTrue(lastBlock.getOffset() == blockGroupSize);
assertTrue(lastBlock.getLength() == lastBlockSize);
}
}