SimulatedFSDataset.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hdfs.server.datanode;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.Random;
  26. import javax.management.NotCompliantMBeanException;
  27. import javax.management.ObjectName;
  28. import javax.management.StandardMBean;
  29. import org.apache.hadoop.conf.Configurable;
  30. import org.apache.hadoop.conf.Configuration;
  31. import org.apache.hadoop.hdfs.protocol.Block;
  32. import org.apache.hadoop.hdfs.protocol.BlockLocalPathInfo;
  33. import org.apache.hadoop.hdfs.protocol.FSConstants;
  34. import org.apache.hadoop.hdfs.server.datanode.metrics.FSDatasetMBean;
  35. import org.apache.hadoop.hdfs.server.protocol.BlockRecoveryInfo;
  36. import org.apache.hadoop.metrics2.util.MBeans;
  37. import org.apache.hadoop.util.DataChecksum;
  38. import org.apache.hadoop.util.DiskChecker.DiskErrorException;
  39. /**
  40. * This class implements a simulated FSDataset.
  41. *
  42. * Blocks that are created are recorded but their data (plus their CRCs) are
  43. * discarded.
  44. * Fixed data is returned when blocks are read; a null CRC meta file is
  45. * created for such data.
  46. *
  47. * This FSDataset does not remember any block information across its
  48. * restarts; it does however offer an operation to inject blocks
  49. * (See the TestInectionForSImulatedStorage()
  50. * for a usage example of injection.
  51. *
  52. * Note the synchronization is coarse grained - it is at each method.
  53. */
  54. public class SimulatedFSDataset implements FSConstants, FSDatasetInterface, Configurable{
  55. public static final String CONFIG_PROPERTY_SIMULATED =
  56. "dfs.datanode.simulateddatastorage";
  57. public static final String CONFIG_PROPERTY_CAPACITY =
  58. "dfs.datanode.simulateddatastorage.capacity";
  59. public static final long DEFAULT_CAPACITY = 2L<<40; // 1 terabyte
  60. public static final byte DEFAULT_DATABYTE = 9; // 1 terabyte
  61. byte simulatedDataByte = DEFAULT_DATABYTE;
  62. Configuration conf = null;
  63. static byte[] nullCrcFileData;
  64. {
  65. DataChecksum checksum = DataChecksum.newDataChecksum( DataChecksum.
  66. CHECKSUM_NULL, 16*1024 );
  67. byte[] nullCrcHeader = checksum.getHeader();
  68. nullCrcFileData = new byte[2 + nullCrcHeader.length];
  69. nullCrcFileData[0] = (byte) ((FSDataset.METADATA_VERSION >>> 8) & 0xff);
  70. nullCrcFileData[1] = (byte) (FSDataset.METADATA_VERSION & 0xff);
  71. for (int i = 0; i < nullCrcHeader.length; i++) {
  72. nullCrcFileData[i+2] = nullCrcHeader[i];
  73. }
  74. }
  75. private class BInfo { // information about a single block
  76. Block theBlock;
  77. private boolean finalized = false; // if not finalized => ongoing creation
  78. SimulatedOutputStream oStream = null;
  79. BInfo(Block b, boolean forWriting) throws IOException {
  80. theBlock = new Block(b);
  81. if (theBlock.getNumBytes() < 0) {
  82. theBlock.setNumBytes(0);
  83. }
  84. if (!storage.alloc(theBlock.getNumBytes())) { // expected length - actual length may
  85. // be more - we find out at finalize
  86. DataNode.LOG.warn("Lack of free storage on a block alloc");
  87. throw new IOException("Creating block, no free space available");
  88. }
  89. if (forWriting) {
  90. finalized = false;
  91. oStream = new SimulatedOutputStream();
  92. } else {
  93. finalized = true;
  94. oStream = null;
  95. }
  96. }
  97. synchronized long getGenerationStamp() {
  98. return theBlock.getGenerationStamp();
  99. }
  100. synchronized void updateBlock(Block b) {
  101. theBlock.setGenerationStamp(b.getGenerationStamp());
  102. setlength(b.getNumBytes());
  103. }
  104. synchronized long getlength() {
  105. if (!finalized) {
  106. return oStream.getLength();
  107. } else {
  108. return theBlock.getNumBytes();
  109. }
  110. }
  111. synchronized void setlength(long length) {
  112. if (!finalized) {
  113. oStream.setLength(length);
  114. } else {
  115. theBlock.setNumBytes(length);
  116. }
  117. }
  118. synchronized SimulatedInputStream getIStream() throws IOException {
  119. if (!finalized) {
  120. // throw new IOException("Trying to read an unfinalized block");
  121. return new SimulatedInputStream(oStream.getLength(), DEFAULT_DATABYTE);
  122. } else {
  123. return new SimulatedInputStream(theBlock.getNumBytes(), DEFAULT_DATABYTE);
  124. }
  125. }
  126. synchronized void finalizeBlock(long finalSize) throws IOException {
  127. if (finalized) {
  128. throw new IOException(
  129. "Finalizing a block that has already been finalized" +
  130. theBlock.getBlockId());
  131. }
  132. if (oStream == null) {
  133. DataNode.LOG.error("Null oStream on unfinalized block - bug");
  134. throw new IOException("Unexpected error on finalize");
  135. }
  136. if (oStream.getLength() != finalSize) {
  137. DataNode.LOG.warn("Size passed to finalize (" + finalSize +
  138. ")does not match what was written:" + oStream.getLength());
  139. throw new IOException(
  140. "Size passed to finalize does not match the amount of data written");
  141. }
  142. // We had allocated the expected length when block was created;
  143. // adjust if necessary
  144. long extraLen = finalSize - theBlock.getNumBytes();
  145. if (extraLen > 0) {
  146. if (!storage.alloc(extraLen)) {
  147. DataNode.LOG.warn("Lack of free storage on a block alloc");
  148. throw new IOException("Creating block, no free space available");
  149. }
  150. } else {
  151. storage.free(-extraLen);
  152. }
  153. theBlock.setNumBytes(finalSize);
  154. finalized = true;
  155. oStream = null;
  156. return;
  157. }
  158. SimulatedInputStream getMetaIStream() {
  159. return new SimulatedInputStream(nullCrcFileData);
  160. }
  161. synchronized boolean isFinalized() {
  162. return finalized;
  163. }
  164. }
  165. static private class SimulatedStorage {
  166. private long capacity; // in bytes
  167. private long used; // in bytes
  168. synchronized long getFree() {
  169. return capacity - used;
  170. }
  171. synchronized long getCapacity() {
  172. return capacity;
  173. }
  174. synchronized long getUsed() {
  175. return used;
  176. }
  177. synchronized boolean alloc(long amount) {
  178. if (getFree() >= amount) {
  179. used += amount;
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. synchronized void free(long amount) {
  186. used -= amount;
  187. }
  188. SimulatedStorage(long cap) {
  189. capacity = cap;
  190. used = 0;
  191. }
  192. }
  193. private HashMap<Block, BInfo> blockMap = null;
  194. private SimulatedStorage storage = null;
  195. private String storageId;
  196. public SimulatedFSDataset(Configuration conf) throws IOException {
  197. setConf(conf);
  198. }
  199. private SimulatedFSDataset() { // real construction when setConf called.. Uggg
  200. }
  201. public Configuration getConf() {
  202. return conf;
  203. }
  204. public void setConf(Configuration iconf) {
  205. conf = iconf;
  206. storageId = conf.get("StorageId", "unknownStorageId" +
  207. new Random().nextInt());
  208. registerMBean(storageId);
  209. storage = new SimulatedStorage(
  210. conf.getLong(CONFIG_PROPERTY_CAPACITY, DEFAULT_CAPACITY));
  211. //DataNode.LOG.info("Starting Simulated storage; Capacity = " + getCapacity() +
  212. // "Used = " + getDfsUsed() + "Free =" + getRemaining());
  213. blockMap = new HashMap<Block,BInfo>();
  214. }
  215. public synchronized void injectBlocks(Block[] injectBlocks)
  216. throws IOException {
  217. if (injectBlocks != null) {
  218. for (Block b: injectBlocks) { // if any blocks in list is bad, reject list
  219. if (b == null) {
  220. throw new NullPointerException("Null blocks in block list");
  221. }
  222. if (isValidBlock(b)) {
  223. throw new IOException("Block already exists in block list");
  224. }
  225. }
  226. HashMap<Block, BInfo> oldBlockMap = blockMap;
  227. blockMap =
  228. new HashMap<Block,BInfo>(injectBlocks.length + oldBlockMap.size());
  229. blockMap.putAll(oldBlockMap);
  230. for (Block b: injectBlocks) {
  231. BInfo binfo = new BInfo(b, false);
  232. blockMap.put(b, binfo);
  233. }
  234. }
  235. }
  236. @Override
  237. public void finalizeBlock(Block b) throws IOException {
  238. finalizeBlockInternal(b, false);
  239. }
  240. @Override
  241. public void finalizeBlockIfNeeded(Block b) throws IOException {
  242. finalizeBlockInternal(b, true);
  243. }
  244. private synchronized void finalizeBlockInternal(Block b, boolean refinalizeOk)
  245. throws IOException {
  246. BInfo binfo = blockMap.get(b);
  247. if (binfo == null) {
  248. throw new IOException("Finalizing a non existing block " + b);
  249. }
  250. binfo.finalizeBlock(b.getNumBytes());
  251. }
  252. public synchronized void unfinalizeBlock(Block b) throws IOException {
  253. if (isBeingWritten(b)) {
  254. blockMap.remove(b);
  255. }
  256. }
  257. public synchronized Block[] getBlockReport() {
  258. Block[] blockTable = new Block[blockMap.size()];
  259. int count = 0;
  260. for (BInfo b : blockMap.values()) {
  261. if (b.isFinalized()) {
  262. blockTable[count++] = b.theBlock;
  263. }
  264. }
  265. if (count != blockTable.length) {
  266. blockTable = Arrays.copyOf(blockTable, count);
  267. }
  268. return blockTable;
  269. }
  270. @Override
  271. public void requestAsyncBlockReport() {
  272. }
  273. @Override
  274. public boolean isAsyncBlockReportReady() {
  275. return true;
  276. }
  277. @Override
  278. public Block[] retrieveAsyncBlockReport() {
  279. return getBlockReport();
  280. }
  281. public long getCapacity() throws IOException {
  282. return storage.getCapacity();
  283. }
  284. public long getDfsUsed() throws IOException {
  285. return storage.getUsed();
  286. }
  287. public long getRemaining() throws IOException {
  288. return storage.getFree();
  289. }
  290. public synchronized long getLength(Block b) throws IOException {
  291. BInfo binfo = blockMap.get(b);
  292. if (binfo == null) {
  293. throw new IOException("Finalizing a non existing block " + b);
  294. }
  295. return binfo.getlength();
  296. }
  297. @Override
  298. public long getVisibleLength(Block b) throws IOException {
  299. return getLength(b);
  300. }
  301. @Override
  302. public void setVisibleLength(Block b, long length) throws IOException {
  303. //no-op
  304. }
  305. /** {@inheritDoc} */
  306. public Block getStoredBlock(long blkid) throws IOException {
  307. Block b = new Block(blkid);
  308. BInfo binfo = blockMap.get(b);
  309. if (binfo == null) {
  310. return null;
  311. }
  312. b.setGenerationStamp(binfo.getGenerationStamp());
  313. b.setNumBytes(binfo.getlength());
  314. return b;
  315. }
  316. /** {@inheritDoc} */
  317. public void updateBlock(Block oldblock, Block newblock) throws IOException {
  318. BInfo binfo = blockMap.get(newblock);
  319. if (binfo == null) {
  320. throw new IOException("BInfo not found, b=" + newblock);
  321. }
  322. binfo.updateBlock(newblock);
  323. }
  324. public synchronized void invalidate(Block[] invalidBlks) throws IOException {
  325. boolean error = false;
  326. if (invalidBlks == null) {
  327. return;
  328. }
  329. for (Block b: invalidBlks) {
  330. if (b == null) {
  331. continue;
  332. }
  333. BInfo binfo = blockMap.get(b);
  334. if (binfo == null) {
  335. error = true;
  336. DataNode.LOG.warn("Invalidate: Missing block");
  337. continue;
  338. }
  339. storage.free(binfo.getlength());
  340. blockMap.remove(b);
  341. }
  342. if (error) {
  343. throw new IOException("Invalidate: Missing blocks.");
  344. }
  345. }
  346. public synchronized boolean isValidBlock(Block b) {
  347. // return (blockMap.containsKey(b));
  348. BInfo binfo = blockMap.get(b);
  349. if (binfo == null) {
  350. return false;
  351. }
  352. return binfo.isFinalized();
  353. }
  354. /* check if a block is created but not finalized */
  355. private synchronized boolean isBeingWritten(Block b) {
  356. BInfo binfo = blockMap.get(b);
  357. if (binfo == null) {
  358. return false;
  359. }
  360. return !binfo.isFinalized();
  361. }
  362. public String toString() {
  363. return getStorageInfo();
  364. }
  365. public synchronized BlockWriteStreams writeToBlock(Block b,
  366. boolean isRecovery,
  367. boolean isReplicationRequest)
  368. throws IOException {
  369. if (isValidBlock(b)) {
  370. throw new BlockAlreadyExistsException("Block " + b +
  371. " is valid, and cannot be written to.");
  372. }
  373. if (isBeingWritten(b)) {
  374. throw new BlockAlreadyExistsException("Block " + b +
  375. " is being written, and cannot be written to.");
  376. }
  377. BInfo binfo = new BInfo(b, true);
  378. blockMap.put(b, binfo);
  379. SimulatedOutputStream crcStream = new SimulatedOutputStream();
  380. return new BlockWriteStreams(binfo.oStream, crcStream);
  381. }
  382. public synchronized InputStream getBlockInputStream(Block b)
  383. throws IOException {
  384. BInfo binfo = blockMap.get(b);
  385. if (binfo == null) {
  386. throw new IOException("No such Block " + b );
  387. }
  388. //DataNode.LOG.info("Opening block(" + b.blkid + ") of length " + b.len);
  389. return binfo.getIStream();
  390. }
  391. public synchronized InputStream getBlockInputStream(Block b, long seekOffset)
  392. throws IOException {
  393. InputStream result = getBlockInputStream(b);
  394. result.skip(seekOffset);
  395. return result;
  396. }
  397. /** Not supported */
  398. public BlockInputStreams getTmpInputStreams(Block b, long blkoff, long ckoff
  399. ) throws IOException {
  400. throw new IOException("Not supported");
  401. }
  402. /** No-op */
  403. public void validateBlockMetadata(Block b) {
  404. }
  405. /**
  406. * Returns metaData of block b as an input stream
  407. * @param b - the block for which the metadata is desired
  408. * @return metaData of block b as an input stream
  409. * @throws IOException - block does not exist or problems accessing
  410. * the meta file
  411. */
  412. private synchronized InputStream getMetaDataInStream(Block b)
  413. throws IOException {
  414. BInfo binfo = blockMap.get(b);
  415. if (binfo == null) {
  416. throw new IOException("No such Block " + b );
  417. }
  418. if (!binfo.finalized) {
  419. throw new IOException("Block " + b +
  420. " is being written, its meta cannot be read");
  421. }
  422. return binfo.getMetaIStream();
  423. }
  424. public synchronized long getMetaDataLength(Block b) throws IOException {
  425. BInfo binfo = blockMap.get(b);
  426. if (binfo == null) {
  427. throw new IOException("No such Block " + b );
  428. }
  429. if (!binfo.finalized) {
  430. throw new IOException("Block " + b +
  431. " is being written, its metalength cannot be read");
  432. }
  433. return binfo.getMetaIStream().getLength();
  434. }
  435. public MetaDataInputStream getMetaDataInputStream(Block b)
  436. throws IOException {
  437. return new MetaDataInputStream(getMetaDataInStream(b),
  438. getMetaDataLength(b));
  439. }
  440. public synchronized boolean metaFileExists(Block b) throws IOException {
  441. if (!isValidBlock(b)) {
  442. throw new IOException("Block " + b +
  443. " is valid, and cannot be written to.");
  444. }
  445. return true; // crc exists for all valid blocks
  446. }
  447. public void checkDataDir() throws DiskErrorException {
  448. // nothing to check for simulated data set
  449. }
  450. public synchronized long getChannelPosition(Block b,
  451. BlockWriteStreams stream)
  452. throws IOException {
  453. BInfo binfo = blockMap.get(b);
  454. if (binfo == null) {
  455. throw new IOException("No such Block " + b );
  456. }
  457. return binfo.getlength();
  458. }
  459. public synchronized void setChannelPosition(Block b, BlockWriteStreams stream,
  460. long dataOffset, long ckOffset)
  461. throws IOException {
  462. BInfo binfo = blockMap.get(b);
  463. if (binfo == null) {
  464. throw new IOException("No such Block " + b );
  465. }
  466. binfo.setlength(dataOffset);
  467. }
  468. /**
  469. * Simulated input and output streams
  470. *
  471. */
  472. static private class SimulatedInputStream extends java.io.InputStream {
  473. byte theRepeatedData = 7;
  474. long length; // bytes
  475. int currentPos = 0;
  476. byte[] data = null;
  477. /**
  478. * An input stream of size l with repeated bytes
  479. * @param l
  480. * @param iRepeatedData
  481. */
  482. SimulatedInputStream(long l, byte iRepeatedData) {
  483. length = l;
  484. theRepeatedData = iRepeatedData;
  485. }
  486. /**
  487. * An input stream of of the supplied data
  488. *
  489. * @param iData
  490. */
  491. SimulatedInputStream(byte[] iData) {
  492. data = iData;
  493. length = data.length;
  494. }
  495. /**
  496. *
  497. * @return the lenght of the input stream
  498. */
  499. long getLength() {
  500. return length;
  501. }
  502. @Override
  503. public int read() throws IOException {
  504. if (currentPos >= length)
  505. return -1;
  506. if (data !=null) {
  507. return data[currentPos++];
  508. } else {
  509. currentPos++;
  510. return theRepeatedData;
  511. }
  512. }
  513. @Override
  514. public int read(byte[] b) throws IOException {
  515. if (b == null) {
  516. throw new NullPointerException();
  517. }
  518. if (b.length == 0) {
  519. return 0;
  520. }
  521. if (currentPos >= length) { // EOF
  522. return -1;
  523. }
  524. int bytesRead = (int) Math.min(b.length, length-currentPos);
  525. if (data != null) {
  526. System.arraycopy(data, currentPos, b, 0, bytesRead);
  527. } else { // all data is zero
  528. for (int i : b) {
  529. b[i] = theRepeatedData;
  530. }
  531. }
  532. currentPos += bytesRead;
  533. return bytesRead;
  534. }
  535. }
  536. /**
  537. * This class implements an output stream that merely throws its data away, but records its
  538. * length.
  539. *
  540. */
  541. static private class SimulatedOutputStream extends OutputStream {
  542. long length = 0;
  543. /**
  544. * constructor for Simulated Output Steram
  545. */
  546. SimulatedOutputStream() {
  547. }
  548. /**
  549. *
  550. * @return the length of the data created so far.
  551. */
  552. long getLength() {
  553. return length;
  554. }
  555. /**
  556. */
  557. void setLength(long length) {
  558. this.length = length;
  559. }
  560. @Override
  561. public void write(int arg0) throws IOException {
  562. length++;
  563. }
  564. @Override
  565. public void write(byte[] b) throws IOException {
  566. length += b.length;
  567. }
  568. @Override
  569. public void write(byte[] b,
  570. int off,
  571. int len) throws IOException {
  572. length += len;
  573. }
  574. }
  575. private ObjectName mbeanName;
  576. /**
  577. * Register the FSDataset MBean using the name
  578. * "hadoop:service=DataNode,name=FSDatasetState-<storageid>"
  579. * We use storage id for MBean name since a minicluster within a single
  580. * Java VM may have multiple Simulated Datanodes.
  581. */
  582. void registerMBean(final String storageId) {
  583. // We wrap to bypass standard mbean naming convetion.
  584. // This wraping can be removed in java 6 as it is more flexible in
  585. // package naming for mbeans and their impl.
  586. StandardMBean bean;
  587. try {
  588. bean = new StandardMBean(this,FSDatasetMBean.class);
  589. mbeanName = MBeans.register("DataNode",
  590. "FSDatasetState-" + storageId, bean);
  591. } catch (NotCompliantMBeanException e) {
  592. e.printStackTrace();
  593. }
  594. DataNode.LOG.info("Registered FSDatasetStatusMBean");
  595. }
  596. public void shutdown() {
  597. if (mbeanName != null)
  598. MBeans.unregister(mbeanName);
  599. }
  600. public String getStorageInfo() {
  601. return "Simulated FSDataset-" + storageId;
  602. }
  603. public boolean hasEnoughResource() {
  604. return true;
  605. }
  606. @Override
  607. public Block[] getBlocksBeingWrittenReport() {
  608. return null;
  609. }
  610. @Override
  611. public BlockRecoveryInfo startBlockRecovery(long blockId)
  612. throws IOException {
  613. Block stored = getStoredBlock(blockId);
  614. return new BlockRecoveryInfo(stored, false);
  615. }
  616. @Override
  617. public BlockLocalPathInfo getBlockLocalPathInfo(Block blk) throws IOException {
  618. throw new IOException("getBlockLocalPathInfo not supported.");
  619. }
  620. }