morningman commented on a change in pull request #2477: [PROPOSAL]Support Dynamic Partition(adapt GSON serialize) URL: https://github.com/apache/incubator-doris/pull/2477#discussion_r361675378
########## File path: fe/src/main/java/org/apache/doris/clone/DynamicPartitionScheduler.java ########## @@ -0,0 +1,259 @@ +// 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.clone; + +import com.google.common.collect.Maps; +import com.google.common.collect.Range; +import com.google.common.collect.Sets; +import org.apache.doris.analysis.AddPartitionClause; +import org.apache.doris.analysis.DistributionDesc; +import org.apache.doris.analysis.HashDistributionDesc; +import org.apache.doris.analysis.PartitionKeyDesc; +import org.apache.doris.analysis.PartitionValue; +import org.apache.doris.analysis.SingleRangePartitionDesc; +import org.apache.doris.catalog.Catalog; +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.Database; +import org.apache.doris.catalog.DynamicPartitionProperty; +import org.apache.doris.catalog.HashDistributionInfo; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.PartitionInfo; +import org.apache.doris.catalog.PartitionKey; +import org.apache.doris.catalog.RangePartitionInfo; +import org.apache.doris.catalog.Table; +import org.apache.doris.catalog.TableProperty; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.Pair; +import org.apache.doris.common.util.DynamicPartitionUtil; +import org.apache.doris.common.util.MasterDaemon; +import org.apache.doris.common.util.TimeUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * This class is used to periodically add or drop partition on an olapTable which specify dynamic partition properties + * Config.dynamic_partition_enable determine whether this feature is enable, Config.dynamic_partition_check_interval_seconds + * determine how often the task is performed + */ +public class DynamicPartitionScheduler extends MasterDaemon { + private static final Logger LOG = LogManager.getLogger(DynamicPartitionScheduler.class); + public static final String LAST_SCHEDULER_TIME = "lastSchedulerTime"; + public static final String LAST_UPDATE_TIME = "lastUpdateTime"; + public static final String DYNAMIC_PARTITION_STATE = "dynamicPartitionState"; + public static final String MSG = "msg"; + + private final String DEFAULT_RUNTIME_VALUE = "N/A"; + + private Map<String, Map<String, String>> runtimeInfos = Maps.newConcurrentMap(); + private Set<Pair<Long, Long>> dynamicPartitionTableInfo = Sets.newConcurrentHashSet(); + private boolean initialize; + + public enum State { + NORMAL, + ERROR + } + + + public DynamicPartitionScheduler(String name, long intervalMs) { + super(name, intervalMs); + this.initialize = false; + } + + public void registerDynamicPartitionTable(Long dbId, Long tableId) { + dynamicPartitionTableInfo.add(new Pair<>(dbId, tableId)); + } + + public void removeDynamicPartitionTable(Long dbId, Long tableId) { + dynamicPartitionTableInfo.remove(new Pair<>(dbId, tableId)); + } + + public String getRuntimeInfo(String tableName, String key) { + Map<String, String> tableRuntimeInfo = runtimeInfos.getOrDefault(tableName, createDefaultRuntimeInfo()); + return tableRuntimeInfo.getOrDefault(key, DEFAULT_RUNTIME_VALUE); + } + + public void removeRuntimeInfo(String tableName) { + runtimeInfos.remove(tableName); + } + + public void createOrUpdateRuntimeInfo(String tableName, String key, String value) { + Map<String, String> runtimeInfo = runtimeInfos.get(tableName); + if (runtimeInfo == null) { + runtimeInfo = createDefaultRuntimeInfo(); + runtimeInfo.put(key, value); + runtimeInfos.put(tableName, runtimeInfo); + } else { + runtimeInfo.put(key, value); + } + } + + private Map<String, String> createDefaultRuntimeInfo() { + Map<String, String> defaultRuntimeInfo = Maps.newConcurrentMap(); + defaultRuntimeInfo.put(LAST_UPDATE_TIME, DEFAULT_RUNTIME_VALUE); + defaultRuntimeInfo.put(LAST_SCHEDULER_TIME, DEFAULT_RUNTIME_VALUE); + defaultRuntimeInfo.put(DYNAMIC_PARTITION_STATE, State.NORMAL.toString()); + defaultRuntimeInfo.put(MSG, DEFAULT_RUNTIME_VALUE); + return defaultRuntimeInfo; + } + + private void dynamicAddPartition() { + Iterator<Pair<Long, Long>> iterator = dynamicPartitionTableInfo.iterator(); + while (iterator.hasNext()) { + Pair<Long, Long> tableInfo = iterator.next(); + Long dbId = tableInfo.first; + Long tableId = tableInfo.second; + Database db = Catalog.getInstance().getDb(dbId); + if (db == null) { + iterator.remove(); + continue; + } + String tableName; + ArrayList<AddPartitionClause> addPartitionClauses = new ArrayList<>(); + db.readLock(); + try { + // Only OlapTable has DynamicPartitionProperty + OlapTable olapTable = (OlapTable) db.getTable(tableId); Review comment: When olapTable's state is not NORMAL, should also skip this round. For example, when table is under SCHEMA CHANGE, add partition will not be allowed. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org