tsreaper commented on a change in pull request #56:
URL: https://github.com/apache/flink-table-store/pull/56#discussion_r833143315
##########
File path:
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/FileStoreOptions.java
##########
@@ -94,7 +94,7 @@
public static final ConfigOption<Duration> SNAPSHOT_TIME_RETAINED =
ConfigOptions.key("snapshot.time-retained")
.durationType()
- .defaultValue(Duration.ofDays(1))
+ .defaultValue(Duration.ofHours(1))
Review comment:
Why do we change this?
##########
File path:
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/operation/FileStoreExpireImpl.java
##########
@@ -101,18 +120,28 @@ public void expire() {
}
// no snapshot can be retained, expire all but last one
- expireUntil(latestSnapshotId);
+ expireUntil(earliest, latestSnapshotId);
}
- private void expireUntil(long endExclusiveId) {
- if (endExclusiveId <= Snapshot.FIRST_SNAPSHOT_ID) {
+ private void expireUntil(long earliestId, long endExclusiveId) {
+ if (endExclusiveId <= earliestId) {
+ // write hint file if not exists
+ Path hint = new Path(pathFactory.snapshotDirectory(),
SnapshotFinder.EARLIEST);
+ try {
+ if (!hint.getFileSystem().exists(hint)) {
Review comment:
Do we need to check this? We're creating and renaming new files in
`commitEarliestHint` so it doesn't matter if the old file exists.
##########
File path:
flink-table-store-core/src/test/java/org/apache/flink/table/store/file/TestFileStore.java
##########
@@ -314,6 +315,12 @@ public void assertCleaned() {
} catch (IOException e) {
throw new RuntimeException(e);
}
+
+ // remove best effort latest and earliest hint files
+ Path snapshotDir = pathFactory().snapshotDirectory();
+ actualFiles.remove(new Path(snapshotDir, SnapshotFinder.LATEST));
+ actualFiles.remove(new Path(snapshotDir, SnapshotFinder.EARLIEST));
Review comment:
Check their values are valid instead of removing them.
##########
File path:
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/operation/FileStoreExpireImpl.java
##########
@@ -91,7 +110,7 @@ public void expire() {
<= millisRetained) {
// within time threshold, can assume that all snapshots
after it are also within
// the threshold
- expireUntil(id);
+ expireUntil(earliest, id);
Review comment:
Move the two calls for `writeEarliestHint` to here. `id` will be the
earliest snapshot remaining no matter how.
##########
File path:
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/utils/SnapshotFinder.java
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.flink.table.store.file.utils;
+
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.UUID;
+import java.util.function.BinaryOperator;
+
+/** Find latest and earliest snapshot. */
+public class SnapshotFinder {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(SnapshotFinder.class);
+
+ public static final String SNAPSHOT_PREFIX = "snapshot-";
+
+ public static final String EARLIEST = "EARLIEST";
+
+ public static final String LATEST = "LATEST";
+
+ public static Long findLatest(Path snapshotDir) throws IOException {
+ return find(snapshotDir, LATEST, Math::max);
+ }
+
+ public static Long findEarliest(Path snapshotDir) throws IOException {
+ return find(snapshotDir, EARLIEST, Math::min);
+ }
+
+ private static Long find(Path snapshotDir, String hintFile,
BinaryOperator<Long> reducer)
+ throws IOException {
+ FileSystem fs = snapshotDir.getFileSystem();
+ if (!fs.exists(snapshotDir)) {
+ LOG.debug("The snapshot director '{}' is not exist.", snapshotDir);
+ return null;
+ }
+
+ Path hint = new Path(snapshotDir, hintFile);
+ try {
+ return Long.parseLong(FileUtils.readFileUtf8(hint));
+ } catch (Exception ignore) {
+ FileStatus[] statuses = fs.listStatus(snapshotDir);
+ if (statuses == null) {
+ throw new RuntimeException(
+ "The return value is null of the listStatus for the
snapshot directory.");
+ }
+
+ Long result = null;
+ for (FileStatus status : statuses) {
+ String fileName = status.getPath().getName();
+ if (fileName.startsWith(SNAPSHOT_PREFIX)) {
+ try {
+ long id =
Long.parseLong(fileName.substring(SNAPSHOT_PREFIX.length()));
+ result = result == null ? id : reducer.apply(result,
id);
+ } catch (NumberFormatException e) {
+ throw new RuntimeException(
+ "Invalid snapshot file name found " +
fileName, e);
+ }
+ }
+ }
+ return result;
+ }
+ }
+
+ public static void commitLatestHint(Path snapshotDir, long snapshotId) {
+ commitHint(snapshotDir, snapshotId, LATEST);
+ }
+
+ public static void commitEarliestHint(Path snapshotDir, long snapshotId) {
+ commitHint(snapshotDir, snapshotId, EARLIEST);
+ }
+
+ private static void commitHint(Path snapshotDir, long snapshotId, String
fileName) {
+ try {
+ FileSystem fs = snapshotDir.getFileSystem();
+ Path hintFile = new Path(snapshotDir, fileName);
+ Path tempFile = new Path(snapshotDir, UUID.randomUUID() + "-" +
fileName + ".temp");
+ FileUtils.writeFileUtf8(tempFile, String.valueOf(snapshotId));
+ fs.delete(hintFile, false);
+ fs.rename(tempFile, hintFile);
+ } catch (IOException e) {
+ LOG.warn(String.format("Commit hint file '%s' failed.", fileName),
e);
+ }
Review comment:
Throw directly, so that commit operation can fail property. If you run
commit IT cases repeatedly they'll fail.
--
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]