-
Notifications
You must be signed in to change notification settings - Fork 906
[#12403] fix(core): defer catalog wrapper cleanup with an operation lease #12404
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
Open
yuqi1129
wants to merge
7
commits into
apache:main
Choose a base branch
from
yuqi1129:issue-12403-catalog-wrapper-lease
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95db76b
[#12403] fix(core): lease cached catalog wrappers during operations
yuqi1129 5223b66
[#12403] refactor(core): keep the catalog lease API package-private
yuqi1129 b8eda28
[#12403] fix(core): keep NoSuchCatalogException unwrapped in getCapab…
yuqi1129 5683765
[#12403] fix(core): address review comments on the catalog lease
yuqi1129 06b0aaa
[#12403] fix(core): address catalog lease lifecycle gaps
yuqi1129 4d70335
Merge remote-tracking branch 'apache/main' into issue-12403-catalog-w…
yuqi1129 50f663c
[#12403] test(server-common): update catalog lease mock
yuqi1129 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
core/src/main/java/org/apache/gravitino/catalog/CatalogLease.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.gravitino.catalog; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.apache.gravitino.catalog.CatalogManager.CatalogWrapper; | ||
| import org.apache.gravitino.connector.BaseCatalog; | ||
|
|
||
| /** | ||
| * A lease on a {@link CatalogWrapper} held for the duration of one catalog operation. | ||
| * | ||
| * <p>While the lease is held, the wrapper's catalog instance and its {@link | ||
| * org.apache.gravitino.utils.IsolatedClassLoader} stay alive even if the catalog cache evicts the | ||
| * wrapper concurrently (expiry, explicit invalidation, or remote change-log invalidation). The | ||
| * resources are released once the wrapper is retired and its last lease is closed, so an operation | ||
| * can never observe a half-closed catalog. | ||
| * | ||
| * <p>Leases are obtained from {@link CatalogManager#acquireCatalogLease(org.apache.gravitino | ||
| * .NameIdentifier)} and must be closed exactly once, ideally with try-with-resources: | ||
| * | ||
| * <pre>{@code | ||
| * try (CatalogLease lease = catalogManager.acquireCatalogLease(ident)) { | ||
| * return lease.wrapper().doWithTableOps(ops -> ops.loadTable(tableIdent)); | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public final class CatalogLease implements AutoCloseable { | ||
|
|
||
| private final CatalogWrapper wrapper; | ||
| private final AtomicBoolean released = new AtomicBoolean(false); | ||
|
|
||
| CatalogLease(CatalogWrapper wrapper) { | ||
| this.wrapper = wrapper; | ||
| } | ||
|
|
||
| /** | ||
| * Takes a lease on a wrapper the caller already holds. Prefer {@link | ||
| * CatalogManager#acquireCatalogLease(org.apache.gravitino.NameIdentifier)}, which reloads a fresh | ||
| * wrapper when the cached one has been retired. | ||
| * | ||
| * @param wrapper the wrapper to lease. | ||
| * @return a lease on the given wrapper. | ||
| * @throws IllegalStateException if the wrapper has already been retired. | ||
| */ | ||
| public static CatalogLease of(CatalogWrapper wrapper) { | ||
| Preconditions.checkState( | ||
| wrapper.tryAcquire(), "Catalog wrapper has already been retired, cannot lease it"); | ||
| return new CatalogLease(wrapper); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the leased catalog wrapper. | ||
| * | ||
| * @return the leased catalog wrapper, guaranteed to stay usable until this lease is closed. | ||
| */ | ||
| public CatalogWrapper wrapper() { | ||
| return wrapper; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the catalog of the leased wrapper. | ||
| * | ||
| * @return the leased catalog, guaranteed to stay usable until this lease is closed. | ||
| */ | ||
| public BaseCatalog catalog() { | ||
| return wrapper.catalog(); | ||
| } | ||
|
|
||
| /** Releases the lease. Closing an already closed lease is a no-op. */ | ||
| @Override | ||
| public void close() { | ||
| if (released.compareAndSet(false, true)) { | ||
| wrapper.release(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.