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

fix(query): onwership/role info serialize to pb #14393

Merged
merged 4 commits into from Jan 22, 2024

Conversation

TCeason
Copy link
Collaborator

@TCeason TCeason commented Jan 21, 2024

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

  1. onwership/role info serialize to pb
  2. get role/ownership : first try to deserialize data as pb, if failed, deserialize as json. And then, seriazlie it as pb.
  • Fixes #[Link the issue here]

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@TCeason TCeason marked this pull request as draft January 21, 2024 04:01
@github-actions github-actions bot added the pr-bugfix this PR patches a bug in codebase label Jan 21, 2024
@TCeason
Copy link
Collaborator Author

TCeason commented Jan 21, 2024

Part 2 of #14380

@TCeason TCeason force-pushed the role_comp_ut-2 branch 9 times, most recently from c16c9d6 to 12a576f Compare January 21, 2024 14:43
@TCeason TCeason marked this pull request as ready for review January 21, 2024 15:06
@TCeason
Copy link
Collaborator Author

TCeason commented Jan 21, 2024

Now in this pr, role/ownership will serialize to pb.

In https://github.com/datafuselabs/databend/releases/tag/v1.2.307-nightly support deserialize role/ownership from pb and json.

before v1.2.307 only support deserialize from json.

So if rollback to before v1.2.307, show roles, and then, show grants for role 'role_name', record the grants sql. Drop all user roles in current version.

And then rollback to before v1.2.307. Regenerate the role.

Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice shot!

Reviewed 1 of 1 files at r1, 6 of 6 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @BohuTANG and @flaneur2020)

@TCeason
Copy link
Collaborator Author

TCeason commented Jan 22, 2024

After release, maybe we also need add a not in there? https://github.com/datafuselabs/databend-docs/blob/acc8e8e2a16fa96ca2668db58a286e76bd51caca/docs/guides/10-deploy/12-upgrade/10-compatibility.md#compatibility-between-databend-meta

like compatibility-between-databend-query?

Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 8 files at r3.
Reviewable status: 10 of 14 files reviewed, 2 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)


src/query/management/src/serde/pb_serde.rs line 105 at r3 (raw file):

    }
    Ok(SeqV::new(seq_value.seq, data))
}

Minimize the indent and return early:

Suggestion:

pub async fn check_and_upgrade_to_pb<'a, T, ErrFn, CtxFn, D>(
    key: String,
    seq_value: &'a SeqV,
    kv_api: Arc<dyn kvapi::KVApi<Error = MetaError> + Send + Sync>,
    err_code_fn: ErrFn,
    context_fn: CtxFn,
) -> std::result::Result<SeqV<T>, ErrorCode>
where
    T: FromToProto + serde::de::DeserializeOwned + 'a + 'static,
    T::PB: databend_common_protos::prost::Message + Default,
    ErrFn: FnOnce(String) -> ErrorCode + std::marker::Copy,
    D: Display,
    CtxFn: FnOnce() -> D + std::marker::Copy,
{
    let deserialize_result =
        deserialize_struct(&seq_value.data, ErrorCode::IllegalUserInfoFormat, || "");

    let err = match deserialize_result {
        Ok(data) => return Ok(SeqV::new(seq_value.seq, data)),
        Err(err) => err,
    };

    log::debug!("deserialize as pb err: {}, rollback to use serde json", err);

    // If there's an error, try to deserialize as JSON.
    let data = serde_json::from_slice::<T>(&seq_value.data)?;

    // If we reached here, it means JSON deserialization was successful but we need to serialize to protobuf format.
    let value = serialize_struct(&data, ErrorCode::IllegalUserInfoFormat, || "")?;
    kv_api
        .upsert_kv(UpsertKVReq::new(
            &key,
            MatchSeq::Exact(seq_value.seq),
            Operation::Update(value),
            None,
        ))
        .await
        .map_err_to_code(err_code_fn, context_fn)?;

    Ok(SeqV::new(seq_value.seq, data))
}

@TCeason TCeason force-pushed the role_comp_ut-2 branch 2 times, most recently from aac172a to e8347bd Compare January 22, 2024 06:09
Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 8 files at r3, 1 of 1 files at r4, 2 of 3 files at r5, all commit messages.
Reviewable status: 13 of 14 files reviewed, 3 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)


src/query/management/tests/it/role.rs line 36 at r5 (raw file):

// and mock!
mock! {

Do not use mock... Just implement a KvApi for test. I mock! introduces non structured code that can not be indexed or refactored...

@TCeason TCeason force-pushed the role_comp_ut-2 branch 2 times, most recently from 04d2b34 to 4bf6b0f Compare January 22, 2024 06:47
Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 13 of 14 files reviewed, 3 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)

@TCeason
Copy link
Collaborator Author

TCeason commented Jan 22, 2024

All conversation fixed. Please review this pr again. @BohuTANG

Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r7, all commit messages.
Reviewable status: 12 of 14 files reviewed, 6 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)


src/query/management/src/serde/pb_serde.rs line 109 at r9 (raw file):

    }

    Ok(SeqV::new(seq_value.seq, data))

After upsert_kv(), the seq is changed. you should use the new seq to build the return value.

On success, upsert_kv returns a Change<T>, you can find out the new seq in it.
https://github.com/drmingdrmer/databend/blob/991ea29af45e3fea3dd908be7864b6453ad568b0/src/meta/types/src/change.rs#L31

@TCeason
Copy link
Collaborator Author

TCeason commented Jan 22, 2024

Reviewed 1 of 1 files at r7, all commit messages.
Reviewable status: 12 of 14 files reviewed, 6 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)

src/query/management/src/serde/pb_serde.rs line 109 at r9 (raw file):

    }

    Ok(SeqV::new(seq_value.seq, data))

After upsert_kv(), the seq is changed. you should use the new seq to build the return value.

On success, upsert_kv returns a Change<T>, you can find out the new seq in it. https://github.com/drmingdrmer/databend/blob/991ea29af45e3fea3dd908be7864b6453ad568b0/src/meta/types/src/change.rs#L31

Push in this commit: 20511a8

Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else is great to me:D

Reviewed all commit messages.
Reviewable status: 12 of 14 files reviewed, 7 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)


src/query/management/src/serde/pb_serde.rs line 104 at r10 (raw file):

        ))
        .await
        .map_err_to_code(err_code_fn, context_fn)

If you were not going to return this error but just logging it, you do not need to map it.

@TCeason
Copy link
Collaborator Author

TCeason commented Jan 22, 2024

Everything else is great to me:D

Reviewed all commit messages.
Reviewable status: 12 of 14 files reviewed, 7 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)


src/query/management/src/serde/pb_serde.rs line 104 at r10 (raw file):

        ))
        .await
        .map_err_to_code(err_code_fn, context_fn)

If you were not going to return this error but just logging it, you do not need to map it.

Got but I think maybe better to return a readable err

@TCeason
Copy link
Collaborator Author

TCeason commented Jan 22, 2024

Can this PR be merged?

Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think so.

Reviewed 1 of 2 files at r9, 1 of 1 files at r10.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @BohuTANG, @flaneur2020, and @TCeason)

@drmingdrmer drmingdrmer added this pull request to the merge queue Jan 22, 2024
Merged via the queue into datafuselabs:main with commit 79e75b6 Jan 22, 2024
71 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-bugfix this PR patches a bug in codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants