cmccabe commented on a change in pull request #9901:
URL: https://github.com/apache/kafka/pull/9901#discussion_r566468092



##########
File path: 
metadata/src/main/java/org/apache/kafka/timeline/timeline/SnapshotRegistry.java
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.kafka.timeline;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.stream.Collectors;
+
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+/**
+ * A registry containing snapshots of timeline data structures.
+ * We generally expect a small number of snapshots-- perhaps 1 or 2 at a time.
+ * Therefore, we use ArrayLists here rather than a data structure with higher 
overhead.
+ */
+public class SnapshotRegistry {
+    private final Logger log;
+
+    /**
+     * The current epoch.  All snapshot epochs are lower than this number.
+     */
+    private long curEpoch;
+
+    /**
+     * An ArrayList of snapshots, kept in sorted order.
+     */
+    private final ArrayList<Snapshot> snapshots;
+
+    public SnapshotRegistry(long startEpoch) {
+        this(new LogContext(), startEpoch);
+    }
+
+    public SnapshotRegistry(LogContext logContext, long startEpoch) {
+        this.log = logContext.logger(SnapshotRegistry.class);
+        this.curEpoch = startEpoch;
+        this.snapshots = new ArrayList<>(5);
+    }
+
+    /**
+     * Returns an iterator that moves through snapshots from the lowest to the 
highest epoch.
+     */
+    public Iterator<Snapshot> snapshots() {
+        return snapshots.iterator();
+    }
+
+    /**
+     * Gets the snapshot for a specific epoch.
+     */
+    public Snapshot get(long epoch) {
+        for (Snapshot snapshot : snapshots) {
+            if (snapshot.epoch() == epoch) {
+                return snapshot;
+            }
+        }
+        throw new RuntimeException("No snapshot for epoch " + epoch);
+    }
+
+    /**
+     * Creates a new snapshot at the given epoch.
+     *
+     * @param epoch             The epoch to create the snapshot at.  The 
current epoch
+     *                          will be advanced to one past this epoch.
+     */
+    public Snapshot createSnapshot(long epoch) {
+        if (epoch < curEpoch) {
+            throw new RuntimeException("Can't create a new snapshot at epoch " 
+ epoch +
+                    " because the current epoch is " + curEpoch);
+        }
+        Snapshot snapshot = new Snapshot(epoch);
+        snapshots.add(snapshot);
+        curEpoch = epoch + 1;
+        log.debug("Creating snapshot {}", epoch);
+        return snapshot;
+    }
+
+    /**
+     * Deletes the snapshot with the given epoch.
+     *
+     * @param epoch             The epoch of the snapshot to delete.
+     */
+    public void deleteSnapshot(long epoch) {
+        Iterator<Snapshot> iter = snapshots.iterator();
+        while (iter.hasNext()) {
+            Snapshot snapshot = iter.next();
+            if (snapshot.epoch() == epoch) {
+                log.debug("Deleting snapshot {}", epoch);
+                iter.remove();
+                return;
+            }
+        }
+        throw new RuntimeException(String.format(
+            "No snapshot at epoch %d found. Snapshot epochs are %s.", epoch,
+                snapshots.stream().map(snapshot -> 
String.valueOf(snapshot.epoch())).
+                    collect(Collectors.joining(", "))));
+    }
+
+    /**
+     * Reverts the state of all data structures to the state at the given 
epoch.
+     *
+     * @param epoch             The epoch of the snapshot to revert to.
+     */
+    public void revertToSnapshot(long epoch) {

Review comment:
       It does do that.  I'll add a line to the JavaDoc.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to