morningman commented on a change in pull request #6207:
URL: https://github.com/apache/incubator-doris/pull/6207#discussion_r669433023



##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/FeMetaFormat.java
##########
@@ -0,0 +1,44 @@
+// 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.doris.common;
+
+public enum FeMetaFormat {
+    COR1("COR1", "v1"),
+    ETL1("ETL1", "v1");

Review comment:
       ETL can be removed, and how about rename COR1 to DORIS_IMAGE? make it 
more readable

##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/MetaFooter.java
##########
@@ -0,0 +1,86 @@
+// 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.doris.common;
+
+import com.google.common.collect.Lists;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.Arrays;
+import java.util.List;
+
+public class MetaFooter {
+    private static final Logger LOG = LogManager.getLogger(MetaFooter.class);
+
+    private static final long FOOTER_LENGTH_SIZE = 8L;
+    public static final MetaFooter EMPTY_FOOTER = new MetaFooter(null, 0L);
+
+    // length of footer
+    private long length;
+    // meta indices
+    public List<MetaIndex> metaIndices;
+
+    public static MetaFooter read(File imageFile) throws IOException {
+        try(RandomAccessFile raf = new RandomAccessFile(imageFile, "r")) {
+            long fileLength = raf.length();
+            long footerLengthIndex = fileLength - FOOTER_LENGTH_SIZE - 
MetaMagicNumber.MAGIC_STR.length();
+            raf.seek(footerLengthIndex);
+            long footerLength = raf.readLong();
+            MetaMagicNumber magicNumber = MetaMagicNumber.read(raf);
+            if (!Arrays.equals(MetaMagicNumber.MAGIC, magicNumber.getBytes())) 
{
+                LOG.warn("Image file {} format mismatch. Expected magic number 
is {}, actual is {}",
+                        imageFile.getPath(), 
Arrays.toString(MetaMagicNumber.MAGIC), 
Arrays.toString(magicNumber.getBytes()));
+                return EMPTY_FOOTER;

Review comment:
       I think we should throw an exception here, because this is an invalid 
file, not a "empty" one.
   And why put MagicNumber in both header and footer? I think one place is 
enough.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/MetaFooter.java
##########
@@ -0,0 +1,86 @@
+// 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.doris.common;
+
+import com.google.common.collect.Lists;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.Arrays;
+import java.util.List;
+
+public class MetaFooter {

Review comment:
       You can add comment of the format of this footer

##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/MetaHeader.java
##########
@@ -0,0 +1,103 @@
+// 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.doris.common;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.Arrays;
+
+public class MetaHeader {

Review comment:
       Add format in comment

##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/MetaHeader.java
##########
@@ -0,0 +1,103 @@
+// 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.doris.common;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.Arrays;
+
+public class MetaHeader {
+    private static final Logger LOG = LogManager.getLogger(MetaHeader.class);
+
+    public static final MetaHeader EMPTY_HEADER = new MetaHeader(null, 0);
+    private static final long HEADER_LENGTH_SIZE = 4L;
+
+    // length of Header
+    private long length;
+    // format of image
+    private FeMetaFormat metaFormat;
+    // json header
+    public MetaJsonHeader metaJsonHeader;
+
+    public static MetaHeader read(File imageFile) throws IOException {
+        try(RandomAccessFile raf = new RandomAccessFile(imageFile, "r")) {
+            raf.seek(0);
+            MetaMagicNumber magicNumber = MetaMagicNumber.read(raf);
+            if (!Arrays.equals(MetaMagicNumber.MAGIC, magicNumber.getBytes())) 
{
+                LOG.warn("Image file {} format mismatch. Expected magic number 
is {}, actual is {}",
+                        imageFile.getPath(), 
Arrays.toString(MetaMagicNumber.MAGIC), 
Arrays.toString(magicNumber.getBytes()));
+                return EMPTY_HEADER;

Review comment:
       throw exception

##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/MetaWriter.java
##########
@@ -0,0 +1,131 @@
+// 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.doris.common;
+
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.common.io.CountingDataOutputStream;
+import com.google.common.collect.Lists;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Image Format:
+ * |- Image --------------------------------------|
+ * | - Magic String (4 bytes)                     |
+ * | - Header Length (4 bytes)                    |
+ * | |- Header -----------------------------|     |
+ * | | |- Json Header ---------------|      |     |
+ * | | | - version                   |      |     |
+ * | | | - other key/value(undecided)|      |     |
+ * | | |-----------------------------|      |     |
+ * | |--------------------------------------|     |
+ * |                                              |
+ * | |- Image Body -------------------------|     |
+ * | | Object a                             |     |
+ * | | Object b                             |     |
+ * | | ...                                  |     |
+ * | |--------------------------------------|     |
+ * |                                              |
+ * | |- Footer -----------------------------|     |
+ * | | |- object index --------------|      |     |
+ * | | | - index a                   |      |     |
+ * | | | - index b                   |      |     |
+ * | | | ...                         |      |     |
+ * | | |-----------------------------|      |     |
+ * | | - other value(undecided)             |     |
+ * | |--------------------------------------|     |
+ * | - Footer Length (8 bytes)                    |
+ * | - Magic String (4 bytes)                     |
+ * |----------------------------------------------|
+ */
+
+public class MetaWriter {
+    private static final Logger LOG = LogManager.getLogger(MetaWriter.class);
+
+    public static void write(File imageFile, Catalog catalog) throws 
IOException {
+        // save image does not need any lock. because only checkpoint thread 
will call this method.
+        LOG.info("start save image to {}. is ckpt: {}", 
imageFile.getAbsolutePath(), Catalog.isCheckpointThread());
+
+        long checksum = 0;
+        long saveImageStartTime = System.currentTimeMillis();
+        long startPosition = MetaHeader.write(imageFile);
+        List<MetaIndex> metaIndices = Lists.newArrayList();
+        try (CountingDataOutputStream dos = new CountingDataOutputStream(new 
BufferedOutputStream(
+                new FileOutputStream(imageFile, true)), startPosition)) {
+            long replayedJournalId = catalog.getReplayedJournalId();
+            metaIndices.add(new MetaIndex("header", dos.getCount()));
+            checksum = catalog.saveHeader(dos, replayedJournalId, checksum);
+            metaIndices.add(new MetaIndex("masterInfo", dos.getCount()));
+            checksum = catalog.saveMasterInfo(dos, checksum);
+            metaIndices.add(new MetaIndex("frontends", dos.getCount()));
+            checksum = catalog.saveFrontends(dos, checksum);
+            metaIndices.add(new MetaIndex("backends", dos.getCount()));
+            checksum = Catalog.getCurrentSystemInfo().saveBackends(dos, 
checksum);
+            metaIndices.add(new MetaIndex("db", dos.getCount()));
+            checksum = catalog.saveDb(dos, checksum);
+            metaIndices.add(new MetaIndex("loadJob", dos.getCount()));
+            checksum = catalog.saveLoadJob(dos, checksum);
+            metaIndices.add(new MetaIndex("alterJob", dos.getCount()));
+            checksum = catalog.saveAlterJob(dos, checksum);
+            metaIndices.add(new MetaIndex("recycleBin", dos.getCount()));
+            checksum = catalog.saveRecycleBin(dos, checksum);
+            metaIndices.add(new MetaIndex("globalVariable", dos.getCount()));
+            checksum = catalog.saveGlobalVariable(dos, checksum);
+            metaIndices.add(new MetaIndex("cluster", dos.getCount()));
+            checksum = catalog.saveCluster(dos, checksum);
+            metaIndices.add(new MetaIndex("broker", dos.getCount()));
+            checksum = catalog.saveBrokers(dos, checksum);
+            metaIndices.add(new MetaIndex("resources", dos.getCount()));
+            checksum = catalog.saveResources(dos, checksum);
+            metaIndices.add(new MetaIndex("exportJob", dos.getCount()));
+            checksum = catalog.saveExportJob(dos, checksum);
+            metaIndices.add(new MetaIndex("backupHandler", dos.getCount()));
+            checksum = catalog.saveBackupHandler(dos, checksum);
+            metaIndices.add(new MetaIndex("paloAuth", dos.getCount()));
+            checksum = catalog.savePaloAuth(dos, checksum);
+            metaIndices.add(new MetaIndex("transactionState", dos.getCount()));
+            checksum = catalog.saveTransactionState(dos, checksum);
+            metaIndices.add(new MetaIndex("colocateTableIndex", 
dos.getCount()));
+            checksum = catalog.saveColocateTableIndex(dos, checksum);
+            metaIndices.add(new MetaIndex("routineLoadJobs", dos.getCount()));
+            checksum = catalog.saveRoutineLoadJobs(dos, checksum);
+            metaIndices.add(new MetaIndex("loadJobV2", dos.getCount()));
+            checksum = catalog.saveLoadJobsV2(dos, checksum);
+            metaIndices.add(new MetaIndex("smallFiles", dos.getCount()));
+            checksum = catalog.saveSmallFiles(dos, checksum);
+            metaIndices.add(new MetaIndex("plugins", dos.getCount()));
+            checksum = catalog.savePlugins(dos, checksum);
+            metaIndices.add(new MetaIndex("deleteHandler", dos.getCount()));
+            checksum = catalog.saveDeleteHandler(dos, checksum);
+            dos.writeLong(checksum);

Review comment:
       checksum should be wrote in footer.




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

Reply via email to