zddr commented on code in PR #27666: URL: https://github.com/apache/doris/pull/27666#discussion_r1445828252
########## fe/fe-core/src/main/java/org/apache/doris/datasource/MetaIdMappingsLog.java: ########## @@ -0,0 +1,239 @@ +// 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.datasource; + +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.persist.gson.GsonUtils; + +import com.google.common.collect.Lists; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.Getter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +@Data +public class MetaIdMappingsLog implements Writable { + + @SerializedName(value = "catalogId") + private long catalogId = -1L; + + @SerializedName(value = "fromHmsEvent") + private boolean fromHmsEvent = false; Review Comment: only for test? ########## fe/fe-core/src/main/java/org/apache/doris/datasource/MetaIdMappingsLog.java: ########## @@ -0,0 +1,239 @@ +// 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.datasource; + +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.persist.gson.GsonUtils; + +import com.google.common.collect.Lists; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.Getter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +@Data +public class MetaIdMappingsLog implements Writable { + + @SerializedName(value = "catalogId") + private long catalogId = -1L; + + @SerializedName(value = "fromHmsEvent") + private boolean fromHmsEvent = false; + + // The synced event id of master + @SerializedName(value = "lastSyncedEventId") + private long lastSyncedEventId = -1L; + + @SerializedName(value = "metaIdMappings") + private List<MetaIdMapping> metaIdMappings = Lists.newLinkedList(); + + public MetaIdMappingsLog() { + } + + @Override + public int hashCode() { + return Objects.hash(catalogId, lastSyncedEventId, + metaIdMappings == null ? 0 : Arrays.hashCode(metaIdMappings.toArray())); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MetaIdMappingsLog)) { + return false; + } + return Objects.equals(this.catalogId, ((MetaIdMappingsLog) obj).catalogId) + && Objects.equals(this.fromHmsEvent, ((MetaIdMappingsLog) obj).fromHmsEvent) + && Objects.equals(this.lastSyncedEventId, ((MetaIdMappingsLog) obj).lastSyncedEventId) + && Objects.equals(this.metaIdMappings, ((MetaIdMappingsLog) obj).metaIdMappings); + } + + @Override + public void write(DataOutput out) throws IOException { + Text.writeString(out, GsonUtils.GSON.toJson(this)); + } + + public static MetaIdMappingsLog read(DataInput in) throws IOException { + String json = Text.readString(in); + return GsonUtils.GSON.fromJson(json, MetaIdMappingsLog.class); + } + + public void addFromCreateDatabaseEvent(String databaseName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 1, databaseName, null, null, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromCreateTableEvent(String dbName, String tableName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 2, dbName, tableName, null, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromAddPartitionEvent(String dbName, String tblName, String partitionName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 3, dbName, tblName, partitionName, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromDropDatabaseEvent(String dbName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 1, dbName, null, null, -1L); + this.metaIdMappings.add(mapping); + } + + public void addFromDropTableEvent(String dbName, String tblName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 2, dbName, tblName, null, -1L); + this.metaIdMappings.add(mapping); + } + + public void addFromDropPartitionEvent(String dbName, String tblName, String partitionName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 3, dbName, tblName, partitionName, -1L); + this.metaIdMappings.add(mapping); + } + + public static OperationType getOperationType(short opType) { + switch (opType) { + case 1: + return OperationType.ADD; + case 2: + return OperationType.DELETE; + default: + return OperationType.IGNORE; + } + } + + public static MetaObjectType getMetaObjectType(short metaObjType) { + switch (metaObjType) { + case 1: + return MetaObjectType.DATABASE; + case 2: + return MetaObjectType.TABLE; + case 3: + return MetaObjectType.PARTITION; + default: + return MetaObjectType.IGNORE; + } + } + + @Getter + public static class MetaIdMapping implements Writable { + + @SerializedName(value = "opType") + private short opType; + @SerializedName(value = "metaObjType") + private short metaObjType; + // name of Database + @SerializedName(value = "dbName") + private String dbName; + // name of Table + @SerializedName(value = "tblName") + private String tblName; + // name of Partition + @SerializedName(value = "partitionName") + private String partitionName; + // id of Database/Table/Partition + @SerializedName(value = "id") + private long id; + + public MetaIdMapping() {} + + private MetaIdMapping(short opType, + short metaObjType, + String dbName, + String tblName, + String partitionName, + long id) { + this.opType = opType; + this.metaObjType = metaObjType; + this.dbName = dbName; + this.tblName = tblName; + this.partitionName = partitionName; + this.id = id; + } + + @Override + public void write(DataOutput out) throws IOException { Review Comment: write and read not need? ########## fe/fe-core/src/main/java/org/apache/doris/datasource/hive/event/MetastoreEventFactory.java: ########## @@ -77,23 +79,67 @@ List<MetastoreEvent> getMetastoreEvents(List<NotificationEvent> events, HMSExter for (NotificationEvent event : events) { metastoreEvents.addAll(transferNotificationEventToMetastoreEvents(event, catalogName)); } - return createBatchEvents(catalogName, metastoreEvents); + List<MetastoreEvent> mergedEvents = mergeEvents(catalogName, metastoreEvents); + if (Env.getCurrentEnv().isMaster()) { + logMetaIdMappings(hmsExternalCatalog.getId(), events.get(events.size() - 1).getEventId(), mergedEvents); + } + return mergedEvents; + } + + private void logMetaIdMappings(long catalogId, long lastSyncedEventId, List<MetastoreEvent> mergedEvents) { + MetaIdMappingsLog log = new MetaIdMappingsLog(); + log.setCatalogId(catalogId); + log.setFromHmsEvent(true); + log.setLastSyncedEventId(lastSyncedEventId); + for (MetastoreEvent event : mergedEvents) { + switch (event.getEventType()) { + case CREATE_DATABASE: Review Comment: Add an abstract method to MetastoreEvent or create an interface, such as transferToMetaIdMapping, and only log. addMapping (transferToMetaIdMapping()) is needed in this context; How is it going? ########## fe/fe-core/src/main/java/org/apache/doris/datasource/MetaIdMappingsLog.java: ########## @@ -0,0 +1,239 @@ +// 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.datasource; + +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.persist.gson.GsonUtils; + +import com.google.common.collect.Lists; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.Getter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +@Data +public class MetaIdMappingsLog implements Writable { + + @SerializedName(value = "catalogId") + private long catalogId = -1L; + + @SerializedName(value = "fromHmsEvent") + private boolean fromHmsEvent = false; + + // The synced event id of master + @SerializedName(value = "lastSyncedEventId") + private long lastSyncedEventId = -1L; + + @SerializedName(value = "metaIdMappings") + private List<MetaIdMapping> metaIdMappings = Lists.newLinkedList(); + + public MetaIdMappingsLog() { + } + + @Override + public int hashCode() { + return Objects.hash(catalogId, lastSyncedEventId, + metaIdMappings == null ? 0 : Arrays.hashCode(metaIdMappings.toArray())); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MetaIdMappingsLog)) { + return false; + } + return Objects.equals(this.catalogId, ((MetaIdMappingsLog) obj).catalogId) + && Objects.equals(this.fromHmsEvent, ((MetaIdMappingsLog) obj).fromHmsEvent) + && Objects.equals(this.lastSyncedEventId, ((MetaIdMappingsLog) obj).lastSyncedEventId) + && Objects.equals(this.metaIdMappings, ((MetaIdMappingsLog) obj).metaIdMappings); + } + + @Override + public void write(DataOutput out) throws IOException { + Text.writeString(out, GsonUtils.GSON.toJson(this)); + } + + public static MetaIdMappingsLog read(DataInput in) throws IOException { + String json = Text.readString(in); + return GsonUtils.GSON.fromJson(json, MetaIdMappingsLog.class); + } + + public void addFromCreateDatabaseEvent(String databaseName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 1, databaseName, null, null, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromCreateTableEvent(String dbName, String tableName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 2, dbName, tableName, null, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromAddPartitionEvent(String dbName, String tblName, String partitionName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 3, dbName, tblName, partitionName, + ExternalMetaIdMgr.nextMetaId()); + this.metaIdMappings.add(mapping); + } + + public void addFromDropDatabaseEvent(String dbName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 1, dbName, null, null, -1L); + this.metaIdMappings.add(mapping); + } + + public void addFromDropTableEvent(String dbName, String tblName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 2, dbName, tblName, null, -1L); + this.metaIdMappings.add(mapping); + } + + public void addFromDropPartitionEvent(String dbName, String tblName, String partitionName) { + MetaIdMapping mapping = new MetaIdMapping((short) 2, (short) 3, dbName, tblName, partitionName, -1L); + this.metaIdMappings.add(mapping); + } + + public static OperationType getOperationType(short opType) { + switch (opType) { + case 1: + return OperationType.ADD; + case 2: + return OperationType.DELETE; + default: + return OperationType.IGNORE; + } + } + + public static MetaObjectType getMetaObjectType(short metaObjType) { + switch (metaObjType) { + case 1: + return MetaObjectType.DATABASE; + case 2: + return MetaObjectType.TABLE; + case 3: + return MetaObjectType.PARTITION; + default: + return MetaObjectType.IGNORE; + } + } + + @Getter + public static class MetaIdMapping implements Writable { + + @SerializedName(value = "opType") + private short opType; + @SerializedName(value = "metaObjType") + private short metaObjType; + // name of Database + @SerializedName(value = "dbName") + private String dbName; + // name of Table + @SerializedName(value = "tblName") + private String tblName; + // name of Partition + @SerializedName(value = "partitionName") + private String partitionName; + // id of Database/Table/Partition + @SerializedName(value = "id") + private long id; + + public MetaIdMapping() {} + + private MetaIdMapping(short opType, Review Comment: It is best to provide more parameter construction methods, such as without the partitionName parameter, and then process it as null in the construction method ########## fe/fe-core/src/main/java/org/apache/doris/datasource/MetaIdMappingsLog.java: ########## @@ -0,0 +1,239 @@ +// 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.datasource; + +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.persist.gson.GsonUtils; + +import com.google.common.collect.Lists; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.Getter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +@Data +public class MetaIdMappingsLog implements Writable { + + @SerializedName(value = "catalogId") Review Comment: Use abbreviations to reduce image size,eg:cid ########## fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalMetaIdMgr.java: ########## @@ -0,0 +1,261 @@ +// 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.datasource; + +import org.apache.doris.catalog.Env; +import org.apache.doris.datasource.hive.event.MetastoreEventsProcessor; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Map; +import javax.annotation.Nullable; +import javax.validation.constraints.NotNull; + +/** + * <pre> + * ExternalMetaIdMgr is responsible for managing external meta ids. + * Now it just manages the external meta ids of hms events, + * but it will be extended to manage other external meta ids in the future. + * </pre> + * TODO: remove InitCatalogLog and InitDatabaseLog, manage external meta ids at ExternalMetaIdMgr + */ +public class ExternalMetaIdMgr { + + private static final Logger LOG = LogManager.getLogger(ExternalMetaIdMgr.class); + + private final Map<Long, CtlMetaIdMgr> idToCtlMgr = Maps.newConcurrentMap(); Review Comment: not persist? ########## fe/fe-core/src/main/java/org/apache/doris/datasource/MetaIdMappingsLog.java: ########## @@ -0,0 +1,239 @@ +// 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.datasource; + +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.persist.gson.GsonUtils; + +import com.google.common.collect.Lists; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.Getter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +@Data +public class MetaIdMappingsLog implements Writable { + + @SerializedName(value = "catalogId") + private long catalogId = -1L; + + @SerializedName(value = "fromHmsEvent") + private boolean fromHmsEvent = false; + + // The synced event id of master + @SerializedName(value = "lastSyncedEventId") + private long lastSyncedEventId = -1L; + + @SerializedName(value = "metaIdMappings") + private List<MetaIdMapping> metaIdMappings = Lists.newLinkedList(); + + public MetaIdMappingsLog() { + } + + @Override + public int hashCode() { + return Objects.hash(catalogId, lastSyncedEventId, + metaIdMappings == null ? 0 : Arrays.hashCode(metaIdMappings.toArray())); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MetaIdMappingsLog)) { + return false; + } + return Objects.equals(this.catalogId, ((MetaIdMappingsLog) obj).catalogId) + && Objects.equals(this.fromHmsEvent, ((MetaIdMappingsLog) obj).fromHmsEvent) + && Objects.equals(this.lastSyncedEventId, ((MetaIdMappingsLog) obj).lastSyncedEventId) + && Objects.equals(this.metaIdMappings, ((MetaIdMappingsLog) obj).metaIdMappings); + } + + @Override + public void write(DataOutput out) throws IOException { + Text.writeString(out, GsonUtils.GSON.toJson(this)); + } + + public static MetaIdMappingsLog read(DataInput in) throws IOException { + String json = Text.readString(in); + return GsonUtils.GSON.fromJson(json, MetaIdMappingsLog.class); + } + + public void addFromCreateDatabaseEvent(String databaseName) { + MetaIdMapping mapping = new MetaIdMapping((short) 1, (short) 1, databaseName, null, null, Review Comment: constant or enum? -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org