| 143 | |
| 144 | |
| 145 | void ReplicatedMergeTreeBlockOutputStream::write(const Block & block) |
| 146 | { |
| 147 | last_block_is_duplicate = false; |
| 148 | |
| 149 | auto zookeeper = storage.getZooKeeper(); |
| 150 | assertSessionIsNotExpired(zookeeper); |
| 151 | |
| 152 | /** If write is with quorum, then we check that the required number of replicas is now live, |
| 153 | * and also that for all previous parts for which quorum is required, this quorum is reached. |
| 154 | * And also check that during the insertion, the replica was not reinitialized or disabled (by the value of `is_active` node). |
| 155 | * TODO Too complex logic, you can do better. |
| 156 | */ |
| 157 | if (quorum) |
| 158 | checkQuorumPrecondition(zookeeper); |
| 159 | |
| 160 | auto part_blocks = storage.writer.splitBlockIntoParts(block, max_parts_per_block, metadata_snapshot, context); |
| 161 | |
| 162 | for (auto & current_block : part_blocks) |
| 163 | { |
| 164 | Stopwatch watch; |
| 165 | |
| 166 | /// Write part to the filesystem under temporary name. Calculate a checksum. |
| 167 | |
| 168 | MergeTreeData::MutableDataPartPtr part = storage.writer.writeTempPart(current_block, metadata_snapshot, context); |
| 169 | |
| 170 | /// If optimize_on_insert setting is true, current_block could become empty after merge |
| 171 | /// and we didn't create part. |
| 172 | if (!part) |
| 173 | continue; |
| 174 | |
| 175 | String block_id; |
| 176 | |
| 177 | if (deduplicate) |
| 178 | { |
| 179 | /// We add the hash from the data and partition identifier to deduplication ID. |
| 180 | /// That is, do not insert the same data to the same partition twice. |
| 181 | block_id = part->getZeroLevelPartBlockID(); |
| 182 | |
| 183 | LOG_DEBUG(log, "Wrote block with ID '{}', {} rows", block_id, current_block.block.rows()); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | LOG_DEBUG(log, "Wrote block with {} rows", current_block.block.rows()); |
| 188 | } |
| 189 | |
| 190 | try |
| 191 | { |
| 192 | commitPart(zookeeper, part, block_id); |
| 193 | |
| 194 | /// Set a special error code if the block is duplicate |
| 195 | int error = (deduplicate && last_block_is_duplicate) ? ErrorCodes::INSERT_WAS_DEDUPLICATED : 0; |
| 196 | PartLog::addNewPart(storage.getContext(), part, watch.elapsed(), ExecutionStatus(error)); |
| 197 | } |
| 198 | catch (...) |
| 199 | { |
| 200 | PartLog::addNewPart(storage.getContext(), part, watch.elapsed(), ExecutionStatus::fromCurrentException(__PRETTY_FUNCTION__)); |
| 201 | throw; |
| 202 | } |
nothing calls this directly
no test coverage detected