Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method MetaAPI::rename_table() #3765

Closed
drmingdrmer opened this issue Jan 4, 2022 · 13 comments · Fixed by #4288
Closed

add method MetaAPI::rename_table() #3765

drmingdrmer opened this issue Jan 4, 2022 · 13 comments · Fixed by #4288
Labels
A-meta Area: databend meta serive C-feature Category: feature community-take good first issue Category: good first issue prio: normal Medium priority

Comments

@drmingdrmer
Copy link
Member

drmingdrmer commented Jan 4, 2022

async fn rename_table(&self, req : RenameTableReq) -> Result<RenameTableReply>

struct RenameTableReq {
    db_id: u64,
    table_name: String,
    new_table_name: String
}

This method will remove old table record and add a new one in a sled-tree-transaction.

The impl would be similar to CreateTable:
https://github.com/datafuselabs/databend/blob/77fe82ff05ebd8c8c689101bb64a9f53cd50133f/common/meta/raft-store/src/state_machine/sm.rs#L482-L495

BTW, There may be consistency concern: databend-query currently just loads table by (database-name, table-name).


Originally posted by @BohuTANG in #3557 (comment)

@drmingdrmer drmingdrmer added A-meta Area: databend meta serive C-feature Category: feature good first issue Category: good first issue prio: normal Medium priority labels Jan 4, 2022
@kevinw66
Copy link
Contributor

kevinw66 commented Jan 4, 2022

Why not use alter_table for more common usage ?
According to the document https://dev.mysql.com/doc/refman/8.0/en/rename-table.html
We can find rename table_1 to table_2 and alter table_1 rename table_2 has the same behavior.

@drmingdrmer
Copy link
Member Author

This is an API to operate the underlying kv store(the meta data store).
It's hard to capsulate many variants into one single method(as sql does).

Here this method just rename a table, and does nothing more else.
Intuitively ,alter_table should include changing table schema etc.

@junnplus
Copy link
Contributor

junnplus commented Jan 5, 2022

/assignme

I take this issue to learn meta api.

@drmingdrmer
Copy link
Member Author

/assignme

I take this issue to learn meta API.

@ariesdevil has an ongoing working branch that may have conflicts with this issue.
You may like to have a discussion about the work with him. 🤔

@ariesdevil
Copy link
Collaborator

@junnplus Here you go~

@junnplus
Copy link
Contributor

I might need help. : (

I try to add rename method to meta api:

  1. rename needs to be done on a transaction: remove old table record and add a new one.
    That is, we need to return a ErrorCode::UnknownTable / TableAlreadyExists instead of AppliedState in the sled transaction. But before https://github.com/datafuselabs/databend/pull/3837/files I had a hard time getting the errors in the transaction, Thank @ariesdevil
  2. But then ErrorCode::UnknownTable will be wrapped into ErrorCode::TransactionAbort, it doesn't seem to be a problem, I can get the original error with cause
  3. The most troublesome is in the grpc handler: I got a error like Err(UnknownError("Code: 2102, displayText = Transaction abort, cause: Code: 1025, displayText = Unknown table: 'tb2'.\nCode: 1025, displayText = Unknown table: 'tb2'..")).
    Yes, it is a MetaError, maybe I can get the original error, but that's very inelegant.

I'm not sure if I'm missing something I don't know. : (

@drmingdrmer @ariesdevil

@drmingdrmer
Copy link
Member Author

3. The most troublesome is in the grpc handler: I got a error like Err(UnknownError("Code: 2102, displayText = Transaction abort, cause: Code: 1025, displayText = Unknown table: 'tb2'.\nCode: 1025, displayText = Unknown table: 'tb2'..")).
Yes, it is a MetaError, maybe I can get the original error, but that's very inelegant.

In general, ErrorCode is not a good choice in such a case.

And a failure to rename a table should not emit a MetaError. MetaError is meant for server error, not an application error.

AppliedState is meant to express application-data changes in the state machine.
AppliedState contains the two states before and after applying a log entry.
Although, for now, it can not express state change about renaming table. Maybe we need to add another enum entry, such as TableBla(Change<(TableName, TableId)>):

  • Success: before: (name_1, id_1), after: (name_2, id_1).
  • Unknown table: before:None, after: None.
  • Target name already exists: before: (name_2, id_2), after: (name_2, id_2).

It might be working, but not quite sure if it is a good idea

@junnplus
Copy link
Contributor

@drmingdrmer It seems that using AppliedState as the return cannot rollback the transaction. If the drop table succeeds and the create table fails, the transaction cannot be rolled back.

@ariesdevil
Copy link
Collaborator

@drmingdrmer It seems that using AppliedState as the return cannot rollback the transaction. If the drop table succeeds and the create table fails, the transaction cannot be rolled back.

AppliedState just part of Result<AppliedState, ErrorCode>,so there is an error that transaction can handle and rollback

@drmingdrmer
Copy link
Member Author

What if we remove ErrorCode, and make UnknownTable and TableExists sub errors of MetaError.
IMHO, that every crate has its own hierarchical error types would be better than using non-structured errors

@ariesdevil
Copy link
Collaborator

What if we remove ErrorCode, and make UnknownTable and TableExists sub errors of MetaError. IMHO, that every crate has its own hierarchical error types would be better than using non-structured errors

ref: #4000

@drmingdrmer
Copy link
Member Author

AppliedState will be able to express an application error such as UnknownDatabase after the following pr is merged:

It looks good enough to support the expected modification in this issue? :D

@junnplus
Copy link
Contributor

junnplus commented Mar 2, 2022

Sorry for the late reply. :(

#4099 very useful for me, big thanks! I continue to work on this issue at #4288 .

@mergify mergify bot closed this as completed in #4288 Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-meta Area: databend meta serive C-feature Category: feature community-take good first issue Category: good first issue prio: normal Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants