-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
376 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
116 changes: 116 additions & 0 deletions
116
commons/src/main/java/io/aiven/kafka/tieredstorage/commons/SegmentManifestProvider.java
This file contains 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,116 @@ | ||
/* | ||
* Copyright 2023 Aiven Oy | ||
* | ||
* Licensed 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 io.aiven.kafka.tieredstorage.commons; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.kafka.server.log.remote.storage.RemoteLogSegmentMetadata; | ||
|
||
import io.aiven.kafka.tieredstorage.commons.manifest.SegmentManifest; | ||
import io.aiven.kafka.tieredstorage.commons.storage.FileFetcher; | ||
import io.aiven.kafka.tieredstorage.commons.storage.StorageBackEndException; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.benmanes.caffeine.cache.AsyncCacheLoader; | ||
import com.github.benmanes.caffeine.cache.AsyncLoadingCache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
|
||
class SegmentManifestProvider { | ||
private static final long GET_TIMEOUT_SEC = 10; | ||
|
||
private final ObjectKey objectKey; | ||
private final AsyncLoadingCache<String, SegmentManifest> cache; | ||
|
||
/** | ||
* @param maxCacheSize the max cache size (in items) or empty if the cache is unbounded. | ||
* @param cacheRetention the retention time of items in the cache or empty if infinite retention. | ||
*/ | ||
SegmentManifestProvider(final ObjectKey objectKey, | ||
final Optional<Long> maxCacheSize, | ||
final Optional<Duration> cacheRetention, | ||
final FileFetcher fileFetcher, | ||
final ObjectMapper mapper, | ||
final Executor executor) { | ||
this.objectKey = objectKey; | ||
final var cacheBuilder = Caffeine.newBuilder() | ||
.executor(executor); | ||
maxCacheSize.ifPresent(cacheBuilder::maximumSize); | ||
cacheRetention.ifPresent(cacheBuilder::expireAfterWrite); | ||
this.cache = cacheBuilder.buildAsync(new Loader(fileFetcher, mapper)); | ||
} | ||
|
||
private static class Loader implements AsyncCacheLoader<String, SegmentManifest> { | ||
private final FileFetcher fileFetcher; | ||
private final ObjectMapper mapper; | ||
|
||
private Loader(final FileFetcher fileFetcher, final ObjectMapper mapper) { | ||
this.fileFetcher = fileFetcher; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<? extends SegmentManifest> asyncLoad( | ||
final String key, final Executor executor) throws Exception { | ||
final Supplier<SegmentManifest> fetcher = () -> { | ||
try (final InputStream is = fileFetcher.fetch(key)) { | ||
return mapper.readValue(is, SegmentManifest.class); | ||
} catch (final StorageBackEndException | IOException e) { | ||
// Wrap these exceptions to unwrap them later. | ||
throw new CompletionException(e); | ||
} | ||
}; | ||
return CompletableFuture.supplyAsync(fetcher, executor); | ||
} | ||
} | ||
|
||
SegmentManifest get(final RemoteLogSegmentMetadata remoteLogSegmentMetadata) | ||
throws StorageBackEndException, IOException { | ||
final String key = objectKey.key(remoteLogSegmentMetadata, ObjectKey.Suffix.MANIFEST); | ||
try { | ||
return cache.get(key).get(GET_TIMEOUT_SEC, TimeUnit.SECONDS); | ||
} catch (final ExecutionException e) { | ||
// Unwrap previously wrapped exceptions if possible. | ||
|
||
final Throwable cause = e.getCause(); | ||
|
||
// We don't really expect this case, but handle it nevertheless. | ||
if (cause == null) { | ||
throw new RuntimeException(e); | ||
} | ||
if (e.getCause() instanceof StorageBackEndException) { | ||
throw (StorageBackEndException) e.getCause(); | ||
} | ||
if (e.getCause() instanceof IOException) { | ||
throw (IOException) e.getCause(); | ||
} | ||
|
||
throw new RuntimeException(e); | ||
} catch (final InterruptedException | TimeoutException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.