junrao commented on code in PR #14465:
URL: https://github.com/apache/kafka/pull/14465#discussion_r1361271632


##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;
+    int currentVersion = 0;
+    private final Logger logger;
+
+    private enum Fields {
+        VERSION,
+        BROKER_EPOCH;
+
+        @Override
+        public String toString() {
+            return name().toLowerCase(Locale.ROOT);
+        }
+    }
+
+    public CleanShutdownFileHandler(String dirPath) {
+        logger = new LogContext().logger(CleanShutdownFileHandler.class);
+        try {
+            this.cleanShutdownFile = new File(dirPath, 
CleanShutdownFileHandler.CLEAN_SHUTDOWN_FILE_NAME);
+        } catch (Exception e) {
+            logger.warn("Fail to initiate the clean shutdown file handler: " + 
e);
+        }
+    }
+
+    public void write(long brokerEpoch) throws Exception {
+        if (cleanShutdownFile == null) return;
+        write(brokerEpoch, currentVersion);
+    }
+
+    // visible to test.
+    void write(long brokerEpoch, int version) throws Exception {
+        FileOutputStream os = new FileOutputStream(cleanShutdownFile);
+        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, 
StandardCharsets.UTF_8));
+        try {
+            Map<String, String> payload = new HashMap<>();
+            payload.put(Fields.VERSION.toString(), Integer.toString(version));
+            payload.put(Fields.BROKER_EPOCH.toString(), 
Long.toString(brokerEpoch));
+            bw.write(new ObjectMapper().writeValueAsString(payload));
+            bw.flush();
+            os.getFD().sync();
+        } finally {
+            bw.close();
+            os.close();
+        }
+    }
+
+    long read() throws Exception {
+        if (cleanShutdownFile == null) return -1L;
+        BufferedReader br = 
Files.newBufferedReader(cleanShutdownFile.toPath(), StandardCharsets.UTF_8);

Review Comment:
   Could we reuse `Utils.readFileAsString`?



##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;
+    int currentVersion = 0;
+    private final Logger logger;
+
+    private enum Fields {
+        VERSION,
+        BROKER_EPOCH;
+
+        @Override
+        public String toString() {
+            return name().toLowerCase(Locale.ROOT);
+        }
+    }
+
+    public CleanShutdownFileHandler(String dirPath) {
+        logger = new LogContext().logger(CleanShutdownFileHandler.class);
+        try {
+            this.cleanShutdownFile = new File(dirPath, 
CleanShutdownFileHandler.CLEAN_SHUTDOWN_FILE_NAME);
+        } catch (Exception e) {

Review Comment:
   Is this necessary since what's in the `try` clause doesn't throw a declared 
exception?



##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;

Review Comment:
   Could this be final?



##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;
+    int currentVersion = 0;
+    private final Logger logger;
+
+    private enum Fields {
+        VERSION,
+        BROKER_EPOCH;
+
+        @Override
+        public String toString() {
+            return name().toLowerCase(Locale.ROOT);
+        }
+    }
+
+    public CleanShutdownFileHandler(String dirPath) {
+        logger = new LogContext().logger(CleanShutdownFileHandler.class);
+        try {
+            this.cleanShutdownFile = new File(dirPath, 
CleanShutdownFileHandler.CLEAN_SHUTDOWN_FILE_NAME);
+        } catch (Exception e) {
+            logger.warn("Fail to initiate the clean shutdown file handler: " + 
e);
+        }
+    }
+
+    public void write(long brokerEpoch) throws Exception {
+        if (cleanShutdownFile == null) return;
+        write(brokerEpoch, currentVersion);
+    }
+
+    // visible to test.
+    void write(long brokerEpoch, int version) throws Exception {
+        FileOutputStream os = new FileOutputStream(cleanShutdownFile);
+        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, 
StandardCharsets.UTF_8));
+        try {
+            Map<String, String> payload = new HashMap<>();
+            payload.put(Fields.VERSION.toString(), Integer.toString(version));
+            payload.put(Fields.BROKER_EPOCH.toString(), 
Long.toString(brokerEpoch));
+            bw.write(new ObjectMapper().writeValueAsString(payload));
+            bw.flush();
+            os.getFD().sync();
+        } finally {
+            bw.close();
+            os.close();
+        }
+    }
+
+    long read() throws Exception {
+        if (cleanShutdownFile == null) return -1L;
+        BufferedReader br = 
Files.newBufferedReader(cleanShutdownFile.toPath(), StandardCharsets.UTF_8);
+        long brokerEpoch = -1L;
+        try {
+            String text = br.lines().collect(Collectors.joining());
+            Map<String, String> content = new ObjectMapper().readValue(text, 
HashMap.class);
+            int version = 
Integer.parseInt(content.getOrDefault(Fields.VERSION.toString(), "-1"));
+            if (version > currentVersion || version == -1) {

Review Comment:
   This still means that we can't downgrade if we add a new field since the old 
version of the software will see a higher version. In such a case, we probably 
only want to fail if some of the fields in the old version are missing.
   
   Also, when we upgrade the broker to this new version for the first time, the 
broker will see the old cleanShutdownFile with no version. This logic will fail 
the in-place upgrade.



##########
core/src/main/scala/kafka/server/BrokerLifecycleManager.scala:
##########
@@ -187,6 +187,11 @@ class BrokerLifecycleManager(
    */
   private var _channelManager: NodeToControllerChannelManager = _
 
+  /**
+   * The broker epoch from the previous run, or -1 if the epoch is not able to 
be found.
+   */
+  private var previousBrokerEpoch: Long = -1L

Review Comment:
   Does this need to be volatile since it's written and read by different 
threads?



##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;
+    int currentVersion = 0;

Review Comment:
   Could this be final?



##########
core/src/main/java/kafka/log/CleanShutdownFileHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 kafka.log;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Clean shutdown file that indicates the broker was cleanly shutdown in 0.8 
and higher.
+ * This is used to avoid unnecessary recovery after a clean shutdown. In 
theory this could be
+ * avoided by passing in the recovery point, however finding the correct 
position to do this
+ * requires accessing the offset index which may not be safe in an unclean 
shutdown.
+ * For more information see the discussion in PR#2104
+ *
+ * Also, the clean shutdown file can also store the broker epoch, this can be 
used in the broker registration to
+ * demonstrate the last reboot is a clean shutdown. (KIP-966)
+ */
+
+public class CleanShutdownFileHandler {
+    public static final String CLEAN_SHUTDOWN_FILE_NAME = 
".kafka_cleanshutdown";
+    File cleanShutdownFile;
+    int currentVersion = 0;
+    private final Logger logger;
+
+    private enum Fields {
+        VERSION,
+        BROKER_EPOCH;
+
+        @Override
+        public String toString() {
+            return name().toLowerCase(Locale.ROOT);
+        }
+    }
+
+    public CleanShutdownFileHandler(String dirPath) {
+        logger = new LogContext().logger(CleanShutdownFileHandler.class);
+        try {
+            this.cleanShutdownFile = new File(dirPath, 
CleanShutdownFileHandler.CLEAN_SHUTDOWN_FILE_NAME);
+        } catch (Exception e) {
+            logger.warn("Fail to initiate the clean shutdown file handler: " + 
e);
+        }
+    }
+
+    public void write(long brokerEpoch) throws Exception {

Review Comment:
   Why is `write` public while `read`/`delete`/`exists` are protected?



##########
metadata/src/main/java/org/apache/kafka/controller/ClusterControlManager.java:
##########
@@ -327,6 +328,9 @@ public ControllerResult<BrokerRegistrationReply> 
registerBroker(
         }
         int brokerId = request.brokerId();
         BrokerRegistration existing = brokerRegistrations.get(brokerId);
+        if (version < 2 || request.previousBrokerEpoch() != existing.epoch()) {
+            log.debug("Received an unclean shutdown request");

Review Comment:
   Could we add a TODO to indicate that the implementation is not complete?



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