Skip to content

Commit e4ccfb6

Browse files
committed
chore: fix get_auto_increment_next_value & next_val return error
1 parent 3e6d7a0 commit e4ccfb6

File tree

7 files changed

+47
-68
lines changed

7 files changed

+47
-68
lines changed

src/meta/api/src/auto_increment_api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
use databend_common_meta_app::schema::GetAutoIncrementNextValueReply;
1616
use databend_common_meta_app::schema::GetAutoIncrementNextValueReq;
1717

18-
use crate::kv_app_error::KVAppError;
18+
use crate::errors::AutoIncrementError;
19+
use crate::meta_txn_error::MetaTxnError;
1920

2021
#[async_trait::async_trait]
2122
pub trait AutoIncrementApi: Send + Sync {
2223
async fn get_auto_increment_next_value(
2324
&self,
2425
req: GetAutoIncrementNextValueReq,
25-
) -> Result<GetAutoIncrementNextValueReply, KVAppError>;
26+
) -> Result<Result<GetAutoIncrementNextValueReply, AutoIncrementError>, MetaTxnError>;
2627
}

src/meta/api/src/auto_increment_impl.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ use log::debug;
2121

2222
use crate::auto_increment_api::AutoIncrementApi;
2323
use crate::auto_increment_nextval_impl::NextVal;
24-
use crate::kv_app_error::KVAppError;
24+
use crate::errors::AutoIncrementError;
25+
use crate::meta_txn_error::MetaTxnError;
2526

2627
#[async_trait::async_trait]
2728
#[tonic::async_trait]
2829
impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> AutoIncrementApi for KV {
2930
async fn get_auto_increment_next_value(
3031
&self,
3132
req: GetAutoIncrementNextValueReq,
32-
) -> Result<GetAutoIncrementNextValueReply, KVAppError> {
33+
) -> Result<Result<GetAutoIncrementNextValueReply, AutoIncrementError>, MetaTxnError> {
3334
debug!(req :? =(&req); "AutoIncrementApi: {}", func_name!());
3435

3536
let next_val = NextVal {
@@ -38,12 +39,17 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> AutoIncrementApi for KV {
3839
expr: req.expr.clone(),
3940
};
4041

41-
let resp = next_val.next_val(&req.tenant, req.count).await?;
42+
let (start, end) = match next_val.next_val(&req.tenant, req.count).await? {
43+
Ok(resp) => (resp.before, resp.after),
44+
Err(err) => {
45+
return Ok(Err(err));
46+
}
47+
};
4248

43-
Ok(GetAutoIncrementNextValueReply {
44-
start: resp.before,
49+
Ok(Ok(GetAutoIncrementNextValueReply {
50+
start,
4551
step: req.expr.step,
46-
end: resp.after,
47-
})
52+
end,
53+
}))
4854
}
4955
}

src/meta/api/src/auto_increment_nextval_impl.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
// limitations under the License.
1414

1515
use databend_common_expression::AutoIncrementExpr;
16-
use databend_common_meta_app::app_error::AppError;
17-
use databend_common_meta_app::app_error::AutoIncrementError;
18-
use databend_common_meta_app::app_error::OutOfAutoIncrementRange;
1916
use databend_common_meta_app::principal::AutoIncrementKey;
2017
use databend_common_meta_app::schema::AutoIncrementStorageIdent;
2118
use databend_common_meta_app::tenant::ToTenant;
@@ -28,7 +25,8 @@ use databend_common_meta_types::TxnOp;
2825
use databend_common_meta_types::TxnRequest;
2926
use log::debug;
3027

31-
use crate::kv_app_error::KVAppError;
28+
use crate::errors::AutoIncrementError;
29+
use crate::meta_txn_error::MetaTxnError;
3230
use crate::send_txn;
3331

3432
/// The implementation of `next_val` for sequence number.
@@ -48,7 +46,7 @@ where KV: kvapi::KVApi<Error = MetaError> + ?Sized
4846
self,
4947
tenant: impl ToTenant,
5048
count: u64,
51-
) -> Result<FetchAddU64Response, KVAppError> {
49+
) -> Result<Result<FetchAddU64Response, AutoIncrementError>, MetaTxnError> {
5250
debug!("{}", func_name!());
5351

5452
// Key for the sequence number value.
@@ -75,17 +73,15 @@ where KV: kvapi::KVApi<Error = MetaError> + ?Sized
7573
let got_delta = resp.delta();
7674

7775
if got_delta < delta {
78-
return Err(KVAppError::AppError(AppError::AutoIncrementError(
79-
AutoIncrementError::OutOfAutoIncrementRange(OutOfAutoIncrementRange::new(
80-
self.key,
81-
format!(
82-
"{}: count: {count}; expected delta: {delta}, but got: {resp}",
83-
self.expr.step,
84-
),
85-
)),
86-
)));
76+
return Ok(Err(AutoIncrementError::OutOfAutoIncrementRange {
77+
key: self.key,
78+
context: format!(
79+
"{}: count: {count}; expected delta: {delta}, but got: {resp}",
80+
self.expr.step,
81+
),
82+
}));
8783
}
8884

89-
Ok(resp.clone())
85+
Ok(Ok(resp.clone()))
9086
}
9187
}

src/meta/api/src/errors.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use databend_common_exception::ErrorCode;
16+
use databend_common_meta_app::principal::AutoIncrementKey;
1617

1718
/// Table logic error, unrelated to the backend service providing Table management, or dependent component.
1819
#[derive(Clone, Debug, thiserror::Error)]
@@ -37,3 +38,21 @@ impl From<TableError> for ErrorCode {
3738
}
3839
}
3940
}
41+
42+
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
43+
pub enum AutoIncrementError {
44+
#[error("OutOfAutoIncrementRange: `{key}` while `{context}`")]
45+
OutOfAutoIncrementRange {
46+
key: AutoIncrementKey,
47+
context: String,
48+
},
49+
}
50+
51+
impl From<AutoIncrementError> for ErrorCode {
52+
fn from(value: AutoIncrementError) -> Self {
53+
let s = value.to_string();
54+
match value {
55+
AutoIncrementError::OutOfAutoIncrementRange { .. } => ErrorCode::AutoIncrementError(s),
56+
}
57+
}
58+
}

src/meta/app/src/app_error.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use databend_common_meta_types::MatchSeq;
1919

2020
use crate::data_mask::data_mask_name_ident;
2121
use crate::principal::procedure_name_ident;
22-
use crate::principal::AutoIncrementKey;
2322
use crate::principal::ProcedureIdentity;
2423
use crate::row_access_policy::row_access_policy_name_ident;
2524
use crate::schema::catalog_name_ident;
@@ -968,22 +967,6 @@ impl UnsupportedSequenceStorageVersion {
968967
}
969968
}
970969

971-
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
972-
#[error("OutOfAutoIncrementRange: `{key}` while `{context}`")]
973-
pub struct OutOfAutoIncrementRange {
974-
key: AutoIncrementKey,
975-
context: String,
976-
}
977-
978-
impl OutOfAutoIncrementRange {
979-
pub fn new(key: AutoIncrementKey, context: impl ToString) -> Self {
980-
Self {
981-
key,
982-
context: context.to_string(),
983-
}
984-
}
985-
}
986-
987970
/// Application error.
988971
///
989972
/// The application does not get expected result but there is nothing wrong with meta-service.
@@ -1165,9 +1148,6 @@ pub enum AppError {
11651148
#[error(transparent)]
11661149
SequenceError(#[from] SequenceError),
11671150

1168-
#[error(transparent)]
1169-
AutoIncrementError(#[from] AutoIncrementError),
1170-
11711151
#[error(transparent)]
11721152
UpdateStreamMetasFailed(#[from] UpdateStreamMetasFailed),
11731153

@@ -1236,12 +1216,6 @@ pub enum SequenceError {
12361216
UnsupportedSequenceStorageVersion(#[from] UnsupportedSequenceStorageVersion),
12371217
}
12381218

1239-
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
1240-
pub enum AutoIncrementError {
1241-
#[error(transparent)]
1242-
OutOfAutoIncrementRange(#[from] OutOfAutoIncrementRange),
1243-
}
1244-
12451219
impl AppErrorMessage for TenantIsEmpty {
12461220
fn message(&self) -> String {
12471221
self.to_string()
@@ -1594,22 +1568,6 @@ impl AppErrorMessage for SequenceError {
15941568
}
15951569
}
15961570

1597-
impl AppErrorMessage for OutOfAutoIncrementRange {
1598-
fn message(&self) -> String {
1599-
format!("AutoIncrement '{}' out of range", self.key)
1600-
}
1601-
}
1602-
1603-
impl AppErrorMessage for AutoIncrementError {
1604-
fn message(&self) -> String {
1605-
match self {
1606-
AutoIncrementError::OutOfAutoIncrementRange(e) => {
1607-
format!("OutOfAutoIncrementRange: '{}'", e.message())
1608-
}
1609-
}
1610-
}
1611-
}
1612-
16131571
impl AppErrorMessage for VirtualColumnIdOutBound {}
16141572

16151573
impl AppErrorMessage for VirtualColumnTooMany {}
@@ -1737,7 +1695,6 @@ impl From<AppError> for ErrorCode {
17371695
AppError::CleanDbIdTableNamesFailed(err) => {
17381696
ErrorCode::GeneralDbGcFailure(err.message())
17391697
}
1740-
AppError::AutoIncrementError(err) => ErrorCode::AutoIncrementError(err.message()),
17411698
}
17421699
}
17431700
}

src/query/service/src/catalogs/default/mutable_catalog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ impl Catalog for MutableCatalog {
937937
&self,
938938
req: GetAutoIncrementNextValueReq,
939939
) -> Result<GetAutoIncrementNextValueReply> {
940-
let res = self.ctx.meta.get_auto_increment_next_value(req).await?;
940+
let res = self.ctx.meta.get_auto_increment_next_value(req).await??;
941941
Ok(res)
942942
}
943943
}

tests/suites/0_stateless/02_ddl/02_0001_autoincrement.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)