Skip to content

Conversation

@the-mikedavis
Copy link
Collaborator

These changes are split off of #10915 - I'd like to approach these changes incrementally and work through each module so that the diff is readable. This PR covers rabbit_db_binding: create/2 and delete/2 may return {error, timeout} when Khepri is in a minority and this PR updates the direct and indirect callers so that the error is handled gracefully.

@the-mikedavis the-mikedavis force-pushed the md/khepri-minority-errors/rabbit_db_binding branch 2 times, most recently from 5dcb809 to d5fdd45 Compare July 12, 2024 15:02
@the-mikedavis the-mikedavis marked this pull request as ready for review July 12, 2024 15:27
@the-mikedavis the-mikedavis requested a review from dcorbacho July 12, 2024 15:27
ok ->
ok;
{error, _} = Err ->
throw(Err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't object to this change but throwing on the definition import path will halt the entire process. Perhaps we already do that in other places :( It's a good question whether we should continue or halt, "some definitions" can be better than none.

But for cases where the definitions are imported from an external tool on boot (and that happens to early), failing makes more sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was originally thinking we should refactor rabbit_definitions to import all definitions in one big transaction. Then the side effects like starting vhost or queue processes would happen after the transaction succeeded. It would be a really really big refactor though and definitions import is idempotent so if an import fails partially all you should need to do is re-run it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We do end up catching these throws here

_ = try
WorkPoolFun(M)
catch {error, E} -> gatherer:in(Gatherer, {error, E});
_:E:Stacktrace ->
rabbit_log:debug("Definition import: a work pool operation has thrown an exception ~st, stacktrace: ~p",
[E, Stacktrace]),
gatherer:in(Gatherer, {error, E})
end,
gatherer:finish(Gatherer)

and convert it into an {error, _} but it will halt the import after the first concurrent_for_all/4 throws. It won't be a crash+stacktrace in the logs at least 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

If the metadata store is timing out, most likely it won't be the only failing operation. I wonder if this is one of the cases where we should retry the operation. Like if it fails during boot, retry a few times. Or just abort the boot process?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll add a bullet for looking into startup specifically to my todo list. It looks like currently we crash when trying to register projections if you start in a minority during rabbit_khepri:setup/0 which comes before definitions import. (Start a 3-node cluster, enable khepri_db, stop the cluster, start one node.) I think we should have retries there so you have some time to form a majority before we error out and fail the boot. There are also hard matches on ok for when we insert default data

insert_default_data() ->
DefaultUser = get_default_data_param(default_user),
DefaultPass = get_default_data_param(default_pass),
{ok, DefaultTags} = application:get_env(default_user_tags),
DefaultVHost = get_default_data_param(default_vhost),
{ok, [DefaultConfigurePerm, DefaultWritePerm, DefaultReadPerm]} =
application:get_env(default_permissions),
DefaultUserBin = rabbit_data_coercion:to_binary(DefaultUser),
DefaultPassBin = rabbit_data_coercion:to_binary(DefaultPass),
DefaultVHostBin = rabbit_data_coercion:to_binary(DefaultVHost),
DefaultConfigurePermBin = rabbit_data_coercion:to_binary(DefaultConfigurePerm),
DefaultWritePermBin = rabbit_data_coercion:to_binary(DefaultWritePerm),
DefaultReadPermBin = rabbit_data_coercion:to_binary(DefaultReadPerm),
ok = rabbit_vhost:add(DefaultVHostBin, <<"Default virtual host">>, [], ?INTERNAL_USER),
ok = rabbit_auth_backend_internal:add_user(
DefaultUserBin,
DefaultPassBin,
?INTERNAL_USER
),
ok = rabbit_auth_backend_internal:set_tags(DefaultUserBin, DefaultTags,
?INTERNAL_USER),
ok = rabbit_auth_backend_internal:set_permissions(DefaultUserBin,
DefaultVHostBin,
DefaultConfigurePermBin,
DefaultWritePermBin,
DefaultReadPermBin,
?INTERNAL_USER),
ok.

so I think keeping definitions imports and default data behaving the same is probably ok for now. These operations happen right after we start up Khepri and send commands like register_projections so if we get to this part of the boot we have already been in a majority and probably still are, unless some of the nodes disappear right after boot.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we added retries at some point to wait for other nodes, as we do for Mnesia. I'll have a look

Copy link
Contributor

Choose a reason for hiding this comment

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

We just wait for the leader, so I opened a new PR waiting for the projections: #11741

This is essentially a cosmetic change. Read-only transactions are done
with queries in Khepri rather than commands, like read-write
transactions. Local queries cannot timeout like commands so marking the
transaction as 'ro' means that we don't need to handle a potential
'{error, timeout}' return.
This ensures that the call graph of `rabbit_db_binding:create/2` and
`rabbit_db_binding:delete/2` handle the `{error, timeout}` error
possible when Khepri is in a minority.
@the-mikedavis the-mikedavis force-pushed the md/khepri-minority-errors/rabbit_db_binding branch from d5fdd45 to f1be7ba Compare July 22, 2024 18:18
@the-mikedavis
Copy link
Collaborator Author

Force-push was a rebase

@the-mikedavis the-mikedavis merged commit 3a0808c into main Jul 22, 2024
@the-mikedavis the-mikedavis deleted the md/khepri-minority-errors/rabbit_db_binding branch July 22, 2024 19:54
@michaelklishin michaelklishin added this to the 4.0.0 milestone Jul 22, 2024
the-mikedavis added a commit that referenced this pull request Jul 22, 2024
Handle timeouts possible in Khepri minority in `rabbit_db_binding` (backport #11685)
the-mikedavis added a commit that referenced this pull request Jul 23, 2024
Handle timeouts possible in Khepri minority in `rabbit_db_binding` (backport #11685) (backport #11784)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants