cmccabe commented on code in PR #12776:
URL: https://github.com/apache/kafka/pull/12776#discussion_r1011323975


##########
metadata/src/main/java/org/apache/kafka/image/publisher/SnapshotGenerator.java:
##########
@@ -0,0 +1,262 @@
+/*
+ * 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.image.publisher;
+
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.image.loader.LogDeltaManifest;
+import org.apache.kafka.image.loader.PreVersionChangeManifest;
+import org.apache.kafka.image.loader.SnapshotManifest;
+import org.apache.kafka.queue.EventQueue;
+import org.apache.kafka.queue.KafkaEventQueue;
+import org.apache.kafka.server.fault.FaultHandler;
+import org.slf4j.Logger;
+
+import java.util.concurrent.TimeUnit;
+
+
+/**
+ * A metadata publisher that generates snapshots when appropriate.
+ */
+public class SnapshotGenerator implements MetadataPublisher {
+    public static class Builder {
+        private int nodeId = 0;
+        private Time time = Time.SYSTEM;
+        private Emitter emitter = null;
+        private FaultHandler faultHandler = (m, e) -> null;
+        private long minBytesSinceLastSnapshot = 100 * 1024L * 1024L;
+        private long minTimeSinceLastSnapshotNs = TimeUnit.DAYS.toNanos(1);
+
+        public Builder setNodeId(int nodeId) {
+            this.nodeId = nodeId;
+            return this;
+        }
+
+        public Builder setTime(Time time) {
+            this.time = time;
+            return this;
+        }
+
+        public Builder setEmitter(Emitter emitter) {
+            this.emitter = emitter;
+            return this;
+        }
+
+        public Builder setFaultHandler(FaultHandler faultHandler) {
+            this.faultHandler = faultHandler;
+            return this;
+        }
+
+        public Builder setMinBytesSinceLastSnapshot(long 
minBytesSinceLastSnapshot) {
+            this.minBytesSinceLastSnapshot = minBytesSinceLastSnapshot;
+            return this;
+        }
+
+        public Builder setMinTimeSinceLastSnapshotNs(long 
minTimeSinceLastSnapshotNs) {
+            this.minTimeSinceLastSnapshotNs = minTimeSinceLastSnapshotNs;
+            return this;
+        }
+
+        public SnapshotGenerator build() {
+            if (emitter == null) throw new RuntimeException("You must set a 
snapshot emitter.");

Review Comment:
   Typically I don't add mandatory params to builder constructors because part 
of the"value add" builders provide is a visually nicer way of identifying 
(essentially) constructor parameters. Basically, since we don't have named 
parameters in Java, we can sort of fake it this way. A mandatory parameter may 
eventually also become optional, over time.
   
   We wouldn't want to have builders for every object since there is some 
overhead, both in runtime and visual terms. But for big fat objects (which you 
typically don't have a lot of anyway), it really does make life a lot nicer.
   
   [ Side note: I really regret not creating builders on day 1 for BrokerServer 
and ControllerServer since changing the constructor parameters is now an 
exercise in suffering, even with Scala's named parameters. But I never seem to 
find a good time to do that refactor. ]
   
   Anyway, in this case, I'll add it, since a SnapshotGenerator without an 
emitter would be silly.



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

Reply via email to