|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "storage/mow/key_probe.h" |
| 19 | + |
| 20 | +#include "common/cast_set.h" |
| 21 | +#include "common/config.h" |
| 22 | +#include "common/logging.h" |
| 23 | +#include "service/point_query_executor.h" |
| 24 | +#include "storage/key/row_key_encoder.h" |
| 25 | +#include "storage/partial_update_info.h" |
| 26 | +#include "storage/tablet/base_tablet.h" |
| 27 | +#include "storage/tablet/tablet_meta.h" |
| 28 | +#include "storage/tablet/tablet_schema.h" |
| 29 | + |
| 30 | +namespace doris::segment_v2 { |
| 31 | + |
| 32 | +using namespace ErrorCode; |
| 33 | + |
| 34 | +MowKeyProbe::MowKeyProbe(BaseTablet* tablet, TabletSchema* lookup_schema, bool has_sequence_col, |
| 35 | + std::shared_ptr<MowContext> mow_context, const RowsetId& writing_rowset_id, |
| 36 | + uint32_t writing_segment_id, Policy policy) |
| 37 | + : _tablet(tablet), |
| 38 | + _lookup_schema(lookup_schema), |
| 39 | + _has_sequence_col(has_sequence_col), |
| 40 | + _mow_context(std::move(mow_context)), |
| 41 | + _writing_rowset_id(writing_rowset_id), |
| 42 | + _writing_segment_id(writing_segment_id), |
| 43 | + _policy(policy) {} |
| 44 | + |
| 45 | +Result<ProbeOutcome> MowKeyProbe::probe( |
| 46 | + const std::string& key, size_t segment_pos, bool key_has_seq_suffix, bool have_delete_sign, |
| 47 | + const std::vector<RowsetSharedPtr>& specified_rowsets, |
| 48 | + std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
| 49 | + PartialUpdateStats& stats) const { |
| 50 | + RowLocation loc; |
| 51 | + // save rowset shared ptr so this rowset wouldn't delete |
| 52 | + RowsetSharedPtr rowset; |
| 53 | + auto st = _tablet->lookup_row_key(key, _lookup_schema, key_has_seq_suffix, specified_rowsets, |
| 54 | + &loc, _mow_context->max_version, segment_caches, &rowset, |
| 55 | + /*with_rowid=*/false); |
| 56 | + if (st.is<KEY_NOT_FOUND>()) { |
| 57 | + ++stats.num_rows_new_added; |
| 58 | + return ProbeOutcome {KeyProbeResult::NOT_FOUND, {}, nullptr, /*use_default_or_null=*/true}; |
| 59 | + } |
| 60 | + if (!st.ok() && !st.is<KEY_ALREADY_EXISTS>()) { |
| 61 | + LOG(WARNING) << "failed to lookup row key, error: " << st; |
| 62 | + return ResultError(std::move(st)); |
| 63 | + } |
| 64 | + |
| 65 | + // Stored row's seq is larger, so the incoming row loses. |
| 66 | + bool seq_loses = st.is<KEY_ALREADY_EXISTS>(); |
| 67 | + // A delete-signed row needs no old values -- but only without a seq col, |
| 68 | + // since the seq must still be read for merge-on-read compaction. |
| 69 | + bool delete_sign_skip = have_delete_sign && !_has_sequence_col && _policy.skip_delete_sign; |
| 70 | + // Flexible PU insert-after-delete: the old row was already deleted in this |
| 71 | + // load, so the insert counts as a brand-new row. |
| 72 | + bool in_load_deleted = |
| 73 | + _policy.skip_in_load_deleted && |
| 74 | + _mow_context->delete_bitmap->contains( |
| 75 | + {loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, loc.row_id); |
| 76 | + // Skip reading the old row (fill defaults) in any of these cases. |
| 77 | + bool use_default = (seq_loses && _policy.skip_seq_loses) || delete_sign_skip || in_load_deleted; |
| 78 | + ProbeOutcome outcome {seq_loses ? KeyProbeResult::FOUND_NEWER : KeyProbeResult::FOUND, loc, |
| 79 | + std::move(rowset), use_default}; |
| 80 | + |
| 81 | + // Apply the delete-bitmap marks right away -- see class comment (segcompaction). |
| 82 | + if (seq_loses) { |
| 83 | + if (_policy.delete_bitmap_mode == DeleteBitmapMode::OLD_AND_NEW_ROW) { |
| 84 | + // although we need to mark delete current row, we still need to read missing |
| 85 | + // columns for this row, we need to ensure that each column is aligned |
| 86 | + _mow_context->delete_bitmap->add( |
| 87 | + {_writing_rowset_id, _writing_segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, |
| 88 | + cast_set<uint32_t>(segment_pos)); |
| 89 | + ++stats.num_rows_deleted; |
| 90 | + } |
| 91 | + } else if (_policy.delete_bitmap_mode != DeleteBitmapMode::READ_ONLY) { |
| 92 | + _mow_context->delete_bitmap->add( |
| 93 | + {loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, loc.row_id); |
| 94 | + ++stats.num_rows_updated; |
| 95 | + } |
| 96 | + return outcome; |
| 97 | +} |
| 98 | + |
| 99 | +Result<PrevSeqProbe> MowKeyProbe::probe_previous_seq_value( |
| 100 | + const std::string& key, const std::vector<RowsetSharedPtr>& specified_rowsets, |
| 101 | + std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches) const { |
| 102 | + RowLocation loc; |
| 103 | + RowsetSharedPtr rowset; |
| 104 | + PrevSeqProbe result; |
| 105 | + auto st = |
| 106 | + _tablet->lookup_row_key(key, _lookup_schema, /*with_seq_col=*/false, specified_rowsets, |
| 107 | + &loc, _mow_context->max_version, segment_caches, &rowset, |
| 108 | + /*with_rowid=*/true, &result.encoded_seq_value); |
| 109 | + if (st.is<KEY_NOT_FOUND>()) { |
| 110 | + result.outcome = ProbeOutcome {KeyProbeResult::NOT_FOUND, |
| 111 | + {}, |
| 112 | + nullptr, |
| 113 | + /*use_default_or_null=*/true}; |
| 114 | + return result; |
| 115 | + } |
| 116 | + if (!st.ok()) { |
| 117 | + return ResultError(std::move(st)); |
| 118 | + } |
| 119 | + result.outcome.result = KeyProbeResult::FOUND; |
| 120 | + result.outcome.loc = loc; |
| 121 | + result.outcome.rowset = std::move(rowset); |
| 122 | + result.outcome.use_default_or_null = false; |
| 123 | + return result; |
| 124 | +} |
| 125 | + |
| 126 | +void MowKeyProbe::maybe_invalidate_row_cache(int64_t tablet_id, const TabletSchema& schema, |
| 127 | + DataWriteType write_type, const std::string& key) { |
| 128 | + // Just invalid row cache for simplicity, since the rowset is not visible at present. |
| 129 | + // If we update/insert cache, if load failed rowset will not be visible but cached data |
| 130 | + // will be visible, and lead to inconsistency. |
| 131 | + if (!config::disable_storage_row_cache && schema.has_row_store_for_all_columns() && |
| 132 | + write_type == DataWriteType::TYPE_DIRECT) { |
| 133 | + // invalidate cache |
| 134 | + RowCache::instance()->erase({tablet_id, key}); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +std::string encode_mow_key_invalidate_cache( |
| 139 | + const RowKeyEncoder& key_encoder, const std::vector<IOlapColumnDataAccessor*>& key_columns, |
| 140 | + const IOlapColumnDataAccessor* seq_column, size_t pos, bool row_has_seq, int64_t tablet_id, |
| 141 | + const TabletSchema& schema, DataWriteType write_type) { |
| 142 | + std::string key = key_encoder.full_encode(key_columns, pos); |
| 143 | + // the row cache uses the key without the seq as its key, so invalidate before the suffix |
| 144 | + MowKeyProbe::maybe_invalidate_row_cache(tablet_id, schema, write_type, key); |
| 145 | + if (row_has_seq) { |
| 146 | + key_encoder.append_seq_suffix(&key, seq_column, pos); |
| 147 | + } |
| 148 | + return key; |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace doris::segment_v2 |
0 commit comments