adutra commented on code in PR #18754: URL: https://github.com/apache/iceberg/pull/18754#discussion_r4469436978
########## core/src/main/java/org/apache/iceberg/rest/auth/FileTokenSource.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.iceberg.rest.auth; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.LongSupplier; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@link BearerTokenSource} that reads the token from a local file, and re-reads it once the + * token is older than the configured reload interval. + * + * <p>No background thread is used: reloads happen lazily, on the calling thread, when the token is + * requested. Only one thread reloads at a time; concurrent callers get the current token without + * waiting. + * + * <p>If a reload fails, the last valid token is kept, and the next reload is attempted after a full + * reload interval. As a consequence, deleting the token file does not revoke the token. + */ +final class FileTokenSource implements BearerTokenSource { + + private static final Logger LOG = LoggerFactory.getLogger(FileTokenSource.class); + + /** + * Maximum token file size. Large enough for JWTs signed with the largest post-quantum signature + * schemes. + */ + static final int MAX_FILE_SIZE = 256 * 1024; + + private final Path path; + private final Duration reloadInterval; + private final long reloadIntervalNanos; + private final LongSupplier nanoTime; + private final AtomicReference<State> state; + + /** + * An immutable snapshot of the source's mutable state, swapped atomically. + * + * @param token the last valid token + * @param readAtNanos when the token was last read, or when the last reload attempt failed + * @param reloading whether a thread has claimed the current reload + * @param failing whether the last reload attempt failed, used to log on transitions only + */ + private record State(String token, long readAtNanos, boolean reloading, boolean failing) {} Review Comment: In this case I'd prefer to keep a `record`, if that's OK. JEP 395 defines records as "carriers for immutable data" or "nominal tuples". This use case imho fits perfectly under these denominations. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
