//
// write nBytes of data using randomDataGenerator to numFiles
//
ArrayList<UTF8> testfilesList = new ArrayList<UTF8>();
byte[] buffer = new byte[bufferSize];
UTF8 testFileName = null;
for (int iFileNumber = 0; iFileNumber < numFiles; iFileNumber++) {
testFileName = new UTF8("/f" + iFileNumber);
testfilesList.add(testFileName);
OutputStream nos = dfsClient.create(testFileName.toString(), false);
try {
for (long nBytesWritten = 0L;
nBytesWritten < nBytes;
nBytesWritten += buffer.length) {
if ((nBytesWritten + buffer.length) > nBytes) {
// calculate byte count needed to exactly hit nBytes in length
// to keep randomDataGenerator in sync during the verify step
int pb = (int) (nBytes - nBytesWritten);
byte[] bufferPartial = new byte[pb];
randomDataGenerator.nextBytes(bufferPartial);
nos.write(bufferPartial);
} else {
randomDataGenerator.nextBytes(buffer);
nos.write(buffer);
}
}
} finally {
nos.flush();
nos.close();
}
}
//
// No need to wait for blocks to be replicated because replication
// is supposed to be complete when the file is closed.
//
//
// take one datanode down
iDatanodeClosed =
currentTestCycleNumber % listOfDataNodeDaemons.size();
DataNode dn = (DataNode) listOfDataNodeDaemons.get(iDatanodeClosed);
msg("shutdown datanode daemon " + iDatanodeClosed +
" dn=" + dn.data);
try {
dn.shutdown();
} catch (Exception e) {
msg("ignoring datanode shutdown exception=" + e);
}
//
// verify data against a "rewound" randomDataGenerator
// that all of the data is intact
long lastLong = randomDataGenerator.nextLong();
randomDataGenerator = makeRandomDataGenerator(); // restart (make new) PRNG
ListIterator li = testfilesList.listIterator();
while (li.hasNext()) {
testFileName = (UTF8) li.next();
FSInputStream nis = dfsClient.open(testFileName.toString());
byte[] bufferGolden = new byte[bufferSize];
int m = 42;
try {
while (m != -1) {
m = nis.read(buffer);
if (m == buffer.length) {
randomDataGenerator.nextBytes(bufferGolden);
assertBytesEqual(buffer, bufferGolden, buffer.length);
} else if (m > 0) {
byte[] bufferGoldenPartial = new byte[m];
randomDataGenerator.nextBytes(bufferGoldenPartial);
assertBytesEqual(buffer, bufferGoldenPartial, bufferGoldenPartial.length);
}
}
} finally {
nis.close();
}
}
// verify last randomDataGenerator rand val to ensure last file length was checked
long lastLongAgain = randomDataGenerator.nextLong();
assertEquals(lastLong, lastLongAgain);
msg("Finished validating all file contents");
//
// now delete all the created files
msg("Delete all random test files under DFS via remaining datanodes");
li = testfilesList.listIterator();
while (li.hasNext()) {
testFileName = (UTF8) li.next();
assertTrue(dfsClient.delete(testFileName.toString()));
}
//
// wait for delete to be propagated
// (unlike writing files, delete is lazy)