morningman commented on code in PR #17881:
URL: https://github.com/apache/doris/pull/17881#discussion_r1204289786


##########
fe/fe-core/src/main/java/org/apache/doris/persist/PartitionPersistInfo.java:
##########
@@ -27,24 +27,36 @@
 import org.apache.doris.common.FeMetaVersion;
 import org.apache.doris.common.io.Writable;
 import org.apache.doris.common.util.RangeUtils;
+import org.apache.doris.persist.gson.GsonUtils;
 
 import com.google.common.collect.Range;
+import com.google.gson.annotations.SerializedName;
 
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 
 public class PartitionPersistInfo implements Writable {
+    @SerializedName(value = "dbId")

Review Comment:
   If using `@SerializedName`, better modify the read/write method too



##########
fe/fe-core/src/main/java/org/apache/doris/binlog/BinlogManager.java:
##########
@@ -0,0 +1,266 @@
+// 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.binlog;
+
+import org.apache.doris.common.Pair;
+import org.apache.doris.thrift.TBinlog;
+import org.apache.doris.thrift.TBinlogType;
+import org.apache.doris.thrift.TStatus;
+import org.apache.doris.thrift.TStatusCode;
+
+import com.google.common.collect.Maps;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.transport.TMemoryBuffer;
+import org.apache.thrift.transport.TMemoryInputTransport;
+import org.apache.thrift.transport.TTransportException;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class BinlogManager {
+    private static final Logger LOG = 
LogManager.getLogger(BinlogManager.class);
+    private static final int BUFFER_SIZE = 16 * 1024;
+
+    private ReentrantReadWriteLock lock;
+    private Map<Long, DBBinlog> dbBinlogMap;
+    // Pair(commitSeq, timestamp), used for gc
+    // need UpsertRecord to add timestamps for gc
+    private List<Pair<Long, Long>> timestamps;
+
+    public BinlogManager() {
+        lock = new ReentrantReadWriteLock();
+        dbBinlogMap = Maps.newHashMap();
+        timestamps = new ArrayList<Pair<Long, Long>>();
+    }
+
+    private void addBinlog(TBinlog binlog) {
+        LOG.info("add binlog : {}", binlog);
+
+        long dbId = binlog.getDbId();
+        DBBinlog dbBinlog;
+        lock.writeLock().lock();
+        try {
+            dbBinlog = dbBinlogMap.get(dbId);
+            if (dbBinlog == null) {
+                dbBinlog = new DBBinlog(dbId);
+                dbBinlogMap.put(dbId, dbBinlog);
+            }
+            if (binlog.getTimestamp() > 0) {
+                timestamps.add(Pair.of(binlog.getCommitSeq(), 
binlog.getTimestamp()));
+            }
+        } finally {
+            lock.writeLock().unlock();
+        }
+
+        dbBinlog.addBinlog(binlog);
+    }
+
+    private void addBinlog(long dbId, List<Long> tableIds, long commitSeq, 
long timestamp, TBinlogType type, String data) {
+        TBinlog binlog = new TBinlog(commitSeq, timestamp, type, dbId, data);
+        if (tableIds != null && !tableIds.isEmpty()) {
+            binlog.setTableIds(tableIds);
+        }
+        addBinlog(binlog);
+    }
+
+    public void addUpsertRecord(UpsertRecord upsertRecord) {
+        LOG.info("add upsert record. upsertRecord: {}", upsertRecord);
+
+        long dbId = upsertRecord.getDbId();
+        List<Long> tableIds = upsertRecord.getAllReleatedTableIds();
+        long commitSeq = upsertRecord.getCommitSeq();
+        long timestamp = upsertRecord.getTimestamp();
+        TBinlogType type = TBinlogType.UPSERT;
+        String data = upsertRecord.toJson();
+
+        addBinlog(dbId, tableIds, commitSeq, timestamp, type, data);
+    }
+
+    public void addAddPartitionRecord(AddPartitionRecord addPartitionRecord) {
+        LOG.info("add partition record. partitionRecord: {}", 
addPartitionRecord);
+
+        long dbId = addPartitionRecord.getDbId();
+        List<Long> tableIds = new ArrayList<Long>();
+        tableIds.add(addPartitionRecord.getTableId());
+        long commitSeq = addPartitionRecord.getCommitSeq();
+        long timestamp = -1;
+        TBinlogType type = TBinlogType.ADD_PARTITION;
+        String data = addPartitionRecord.toJson();
+
+        addBinlog(dbId, tableIds, commitSeq, timestamp, type, data);
+    }
+
+    // get binlog by dbId, return first binlog.version > version
+    public Pair<TStatus, TBinlog> getBinlog(long dbId, long tableId, long 
commitSeq) {
+        LOG.info("get binlog. dbId: {}, tableId: {}, commitSeq: {}", dbId, 
tableId, commitSeq);
+        TStatus status = new TStatus(TStatusCode.OK);
+        lock.readLock().lock();
+        try {
+            DBBinlog dbBinlog = dbBinlogMap.get(dbId);
+            if (dbBinlog == null) {
+                status.setStatusCode(TStatusCode.BINLOG_NOT_FOUND_DB);
+                LOG.warn("dbBinlog not found. dbId: {}", dbId);
+                return Pair.of(status, null);
+            }
+
+            return dbBinlog.getBinlog(tableId, commitSeq);
+        } finally {
+            lock.readLock().unlock();
+        }
+    }
+
+    // gc binlog, remove all binlog timestamp < minTimestamp
+    // TODO(Drogon): get minCommitSeq from timestamps
+    public void gc(long minTimestamp) {
+        lock.writeLock().lock();
+        long minCommitSeq = -1;
+        try {
+            // user iterator to remove element in timestamps
+            for (Iterator<Pair<Long, Long>> iterator = timestamps.iterator(); 
iterator.hasNext();) {
+                Pair<Long, Long> pair = iterator.next();
+                long commitSeq = pair.first;
+                long timestamp = pair.second;
+
+                if (timestamp >= minTimestamp) {
+                    break;
+                }
+
+                iterator.remove();
+            }
+        } finally {
+            lock.writeLock().unlock();
+        }
+
+        if (minCommitSeq == -1) {
+            return;
+        }
+
+        lock.writeLock().lock();
+    }
+
+    private static void writeTBinlogToStream(DataOutputStream dos, TBinlog 
binlog) throws TException, IOException {
+        TMemoryBuffer buffer = new TMemoryBuffer(BUFFER_SIZE);
+        TBinaryProtocol protocol = new TBinaryProtocol(buffer);
+        binlog.write(protocol);
+        byte[] data = buffer.getArray();
+        dos.writeInt(data.length);
+        dos.write(data);
+    }
+
+
+    // not thread safety, do this without lock
+    public long write(DataOutputStream dos, long checksum) throws IOException {
+        List<TBinlog> binlogs = new ArrayList<TBinlog>();
+        // Step 1: get all binlogs
+        for (DBBinlog dbBinlog : dbBinlogMap.values()) {
+            dbBinlog.getAllBinlogs(binlogs);
+        }
+        // sort binlogs by commitSeq
+        Collections.sort(binlogs, new Comparator<TBinlog>() {
+            @Override
+            public int compare(TBinlog o1, TBinlog o2) {
+                return Long.compare(o1.getCommitSeq(), o2.getCommitSeq());
+            }
+        });
+
+        // Step 2: write binlogs length
+        dos.writeInt(binlogs.size());
+        LOG.info("write binlogs length: {}", binlogs.size());
+
+        // Step 3: write all binlogs to dos
+        // binlog is a thrift type TBinlog
+        for (TBinlog binlog : binlogs) {
+            try {
+                writeTBinlogToStream(dos, binlog);

Review Comment:
   Why using thrift to serde binlog?



##########
gensrc/thrift/FrontendService.thrift:
##########
@@ -523,6 +523,33 @@ struct TLoadTxnBeginResult {
     4: optional i64 db_id
 }
 
+struct Coordinator {
+    1: required string host
+    2: required i32 port
+}
+
+struct TBeginTxnRequest {
+    1: optional string cluster

Review Comment:
   All field should be optional. Do not use requried



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