|
| 1 | +// Copyright 2025 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <Common/TiFlashMetrics.h> |
| 16 | +#include <Storages/KVStore/Read/RegionException.h> |
| 17 | +#include <kvproto/coprocessor.pb.h> |
| 18 | +#include <kvproto/errorpb.pb.h> |
| 19 | + |
| 20 | +namespace DB |
| 21 | +{ |
| 22 | +void setResponseByRegionException(coprocessor::Response * response, const RegionException & e, RegionID region_id) |
| 23 | +{ |
| 24 | + assert(response != nullptr); |
| 25 | + |
| 26 | + errorpb::Error * region_err; |
| 27 | + switch (e.status) |
| 28 | + { |
| 29 | + case RegionException::RegionReadStatus::OK: |
| 30 | + case RegionException::RegionReadStatus::MEET_LOCK: |
| 31 | + // Should not happen RegionException with RegionReadStatus::OK status |
| 32 | + // And RegionReadStatus::MEET_LOCK should be handled in LockException |
| 33 | + region_err = response->mutable_region_error(); |
| 34 | + region_err->set_message( |
| 35 | + fmt::format("Unexpected RegionException with status {}", magic_enum::enum_name(e.status))); |
| 36 | + region_err->mutable_region_not_found()->set_region_id(region_id); |
| 37 | + break; |
| 38 | + case RegionException::RegionReadStatus::OTHER: |
| 39 | + case RegionException::RegionReadStatus::BUCKET_EPOCH_NOT_MATCH: |
| 40 | + case RegionException::RegionReadStatus::FLASHBACK: |
| 41 | + case RegionException::RegionReadStatus::KEY_NOT_IN_REGION: |
| 42 | + case RegionException::RegionReadStatus::TIKV_SERVER_ISSUE: |
| 43 | + case RegionException::RegionReadStatus::READ_INDEX_TIMEOUT: |
| 44 | + case RegionException::RegionReadStatus::NOT_LEADER: |
| 45 | + case RegionException::RegionReadStatus::NOT_FOUND_TIKV: |
| 46 | + case RegionException::RegionReadStatus::NOT_FOUND: |
| 47 | + case RegionException::RegionReadStatus::STALE_COMMAND: |
| 48 | + case RegionException::RegionReadStatus::STORE_NOT_MATCH: |
| 49 | + GET_METRIC(tiflash_coprocessor_request_error, reason_region_not_found).Increment(); |
| 50 | + region_err = response->mutable_region_error(); |
| 51 | + region_err->mutable_region_not_found()->set_region_id(region_id); |
| 52 | + break; |
| 53 | + case RegionException::RegionReadStatus::EPOCH_NOT_MATCH: |
| 54 | + GET_METRIC(tiflash_coprocessor_request_error, reason_epoch_not_match).Increment(); |
| 55 | + region_err = response->mutable_region_error(); |
| 56 | + region_err->mutable_epoch_not_match(); |
| 57 | + // TODO: set current_regions when mutable epoch not match is hit |
| 58 | + break; |
| 59 | + // Notice: We need to handle all enum cases here to ensure correctly retrying. |
| 60 | + // Do NOT add a default case here. |
| 61 | + // default: |
| 62 | + // break; |
| 63 | + } |
| 64 | +} |
| 65 | +} // namespace DB |
0 commit comments