1 /*
2    Copyright 2013-2016 Skytechnology sp. z o.o.
3 
4    This file is part of LizardFS.
5 
6    LizardFS is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, version 3.
9 
10    LizardFS is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include "common/platform.h"
22 
23 #include <atomic>
24 #include <cstdint>
25 #include <map>
26 #include <mutex>
27 #include <string>
28 #include <vector>
29 
30 #include "common/chunk_connector.h"
31 #include "common/chunk_read_planner.h"
32 #include "common/chunk_type_with_address.h"
33 #include "common/connection_pool.h"
34 #include "common/massert.h"
35 #include "common/network_address.h"
36 #include "common/read_plan_executor.h"
37 #include "common/time_utils.h"
38 #include "mount/chunk_locator.h"
39 
40 class ChunkReader {
41 public:
42 	ChunkReader(ChunkConnector& connector, double bandwidth_overuse);
43 
44 	/**
45 	 * Uses a locator to locate the chunk and chooses chunkservers to read from.
46 	 * Doesn't do anything if the chunk given by (inode, index) is already known to the reader
47 	 * (ie. the last call to this method had the same inode and index) unless forcePrepare is true.
48 	 */
49 	void prepareReadingChunk(uint32_t inode, uint32_t index, bool forcePrepare);
50 
51 	/**
52 	 * Reads data from the previously located chunk and appends it to the buffer
53 	 */
54 	uint32_t readData(std::vector<uint8_t>& buffer, uint32_t offset, uint32_t size,
55 			uint32_t connectTimeout_ms, uint32_t wave_timeout_ms,
56 			const Timeout& communicationTimeout, bool prefetchXorStripes);
57 
isChunkLocated()58 	bool isChunkLocated() const {
59 		return (bool)location_;
60 	}
inode()61 	uint32_t inode() const {
62 		return inode_;
63 	}
index()64 	uint32_t index() const {
65 		return index_;
66 	}
chunkId()67 	uint64_t chunkId() const {
68 		return location_->chunkId;
69 	}
version()70 	uint32_t version() const {
71 		return location_->version;
72 	}
73 
74 	/// Counter for the .lizardfds_tweaks file.
75 	static std::atomic<uint64_t> preparations;
76 
77 private:
78 	ChunkConnector& connector_;
79 	ReadChunkLocator locator_;
80 	uint32_t inode_;
81 	uint32_t index_;
82 	std::shared_ptr<const ChunkLocationInfo> location_;
83 	ChunkReadPlanner planner_;
84 	ReadPlan::PartsContainer available_parts_;
85 	ReadPlanExecutor::ChunkTypeLocations chunk_type_locations_;
86 	std::vector<ChunkTypeWithAddress> crcErrors_;
87 	bool chunkAlreadyRead;
88 };
89