forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
777 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
...core/src/main/java/org/apache/doris/common/cache/NereidsSortedPartitionsCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// 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.cache; | ||
|
||
import org.apache.doris.catalog.DatabaseIf; | ||
import org.apache.doris.catalog.Env; | ||
import org.apache.doris.catalog.OlapTable; | ||
import org.apache.doris.catalog.PartitionInfo; | ||
import org.apache.doris.catalog.PartitionItem; | ||
import org.apache.doris.common.Config; | ||
import org.apache.doris.common.ConfigBase.DefaultConfHandler; | ||
import org.apache.doris.datasource.CatalogIf; | ||
import org.apache.doris.nereids.rules.expression.rules.MultiColumnBound; | ||
import org.apache.doris.nereids.rules.expression.rules.PartitionItemToRange; | ||
import org.apache.doris.nereids.rules.expression.rules.SortedPartitionRanges; | ||
import org.apache.doris.nereids.rules.expression.rules.SortedPartitionRanges.PartitionItemAndRange; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.TreeRangeSet; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import org.apache.hadoop.util.Lists; | ||
|
||
import java.lang.reflect.Field; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
|
||
/** NereidsSortedPartitionsCacheManager */ | ||
public class NereidsSortedPartitionsCacheManager { | ||
private volatile Cache<TableIdentifier, PartitionCacheContext> partitionCaches; | ||
|
||
public NereidsSortedPartitionsCacheManager() { | ||
partitionCaches = buildCaches( | ||
Config.cache_partition_meta_table_manage_num, | ||
Config.expire_cache_partition_meta_table_in_fe_second | ||
); | ||
} | ||
|
||
public Optional<SortedPartitionRanges<?>> get(OlapTable table) { | ||
DatabaseIf<?> database = table.getDatabase(); | ||
if (database == null) { | ||
return Optional.empty(); | ||
} | ||
CatalogIf<?> catalog = database.getCatalog(); | ||
if (catalog == null) { | ||
return Optional.empty(); | ||
} | ||
TableIdentifier key = new TableIdentifier( | ||
catalog.getName(), database.getFullName(), table.getName()); | ||
PartitionCacheContext partitionCacheContext = partitionCaches.getIfPresent(key); | ||
if (partitionCacheContext == null) { | ||
return Optional.of(loadCache(key, table)); | ||
} | ||
if (table.getId() != partitionCacheContext.tableId | ||
|| table.getVisibleVersion() != partitionCacheContext.tableVersion) { | ||
partitionCaches.invalidate(key); | ||
return Optional.empty(); | ||
} | ||
return Optional.of(partitionCacheContext.sortedPartitionRanges); | ||
} | ||
|
||
private SortedPartitionRanges<?> loadCache(TableIdentifier key, OlapTable olapTable) { | ||
PartitionInfo partitionInfo = olapTable.getPartitionInfo(); | ||
Map<Long, PartitionItem> allPartitions = partitionInfo.getIdToItem(false); | ||
List<Entry<Long, PartitionItem>> sortedList = Lists.newArrayList(allPartitions.entrySet()); | ||
List<PartitionItemAndRange<?>> sortedRanges = Lists.newArrayListWithCapacity(allPartitions.size()); | ||
for (Entry<Long, PartitionItem> entry : sortedList) { | ||
TreeRangeSet<MultiColumnBound> rangeSet = PartitionItemToRange.toRangeSets(entry.getValue()); | ||
sortedRanges.add(new PartitionItemAndRange<>(entry.getKey(), entry.getValue(), rangeSet)); | ||
} | ||
|
||
sortedRanges.sort((o1, o2) -> { | ||
Range<MultiColumnBound> span1 = o1.ranges.span(); | ||
Range<MultiColumnBound> span2 = o2.ranges.span(); | ||
int result = span1.lowerEndpoint().compareTo(span2.lowerEndpoint()); | ||
if (result != 0) { | ||
return result; | ||
} | ||
result = span1.upperEndpoint().compareTo(span2.upperEndpoint()); | ||
return result; | ||
}); | ||
SortedPartitionRanges sortedPartitionRanges = new SortedPartitionRanges(sortedRanges); | ||
PartitionCacheContext context = new PartitionCacheContext( | ||
olapTable.getId(), olapTable.getVisibleVersion(), sortedPartitionRanges); | ||
partitionCaches.put(key, context); | ||
return sortedPartitionRanges; | ||
} | ||
|
||
private static Cache<TableIdentifier, PartitionCacheContext> buildCaches( | ||
int sortedPartitionTableManageNum, int expireSortedPartitionTableInFeSecond) { | ||
Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder() | ||
// auto evict cache when jvm memory too low | ||
.softValues(); | ||
if (sortedPartitionTableManageNum > 0) { | ||
cacheBuilder = cacheBuilder.maximumSize(sortedPartitionTableManageNum); | ||
} | ||
if (expireSortedPartitionTableInFeSecond > 0) { | ||
cacheBuilder = cacheBuilder.expireAfterAccess(Duration.ofSeconds(expireSortedPartitionTableInFeSecond)); | ||
} | ||
|
||
return cacheBuilder.build(); | ||
} | ||
|
||
public static synchronized void updateConfig() { | ||
Env currentEnv = Env.getCurrentEnv(); | ||
if (currentEnv == null) { | ||
return; | ||
} | ||
NereidsSortedPartitionsCacheManager cacheManager = currentEnv.getSortedPartitionsCacheManager(); | ||
if (cacheManager == null) { | ||
return; | ||
} | ||
|
||
Cache<TableIdentifier, PartitionCacheContext> caches = buildCaches( | ||
Config.cache_partition_meta_table_manage_num, | ||
Config.expire_cache_partition_meta_table_in_fe_second | ||
); | ||
caches.putAll(cacheManager.partitionCaches.asMap()); | ||
cacheManager.partitionCaches = caches; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
private static class TableIdentifier { | ||
public final String catalog; | ||
public final String db; | ||
public final String table; | ||
} | ||
|
||
private static class PartitionCacheContext { | ||
private final long tableId; | ||
private final long tableVersion; | ||
private final SortedPartitionRanges sortedPartitionRanges; | ||
|
||
public PartitionCacheContext( | ||
long tableId, long tableVersion, SortedPartitionRanges sortedPartitionRanges) { | ||
this.tableId = tableId; | ||
this.tableVersion = tableVersion; | ||
this.sortedPartitionRanges = sortedPartitionRanges; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PartitionCacheContext(tableId=" | ||
+ tableId + ", tableVersion=" + tableVersion | ||
+ ", partitionNum=" + sortedPartitionRanges.sortedPartitions.size() + ")"; | ||
} | ||
} | ||
|
||
// NOTE: used in Config.cache_partition_meta_table_manage_num.callbackClassString and | ||
// Config.expire_cache_partition_meta_table_in_fe_second.callbackClassString, | ||
// don't remove it! | ||
public static class UpdateConfig extends DefaultConfHandler { | ||
@Override | ||
public void handle(Field field, String confVal) throws Exception { | ||
super.handle(field, confVal); | ||
NereidsSortedPartitionsCacheManager.updateConfig(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/MultiColumnBound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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.nereids.rules.expression.rules; | ||
|
||
import java.util.List; | ||
|
||
/** MultiColumnBound */ | ||
public class MultiColumnBound implements Comparable<MultiColumnBound> { | ||
private final List<ColumnBound> columnBounds; | ||
|
||
public MultiColumnBound(List<ColumnBound> columnBounds) { | ||
this.columnBounds = columnBounds; | ||
} | ||
|
||
@Override | ||
public int compareTo(MultiColumnBound o) { | ||
for (int i = 0; i < columnBounds.size(); i++) { | ||
ColumnBound columnBound = columnBounds.get(i); | ||
ColumnBound otherColumnBound = o.columnBounds.get(i); | ||
int result = columnBound.compareTo(otherColumnBound); | ||
if (result != 0) { | ||
return result; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < columnBounds.size(); i++) { | ||
if (i > 0) { | ||
sb.append(","); | ||
} | ||
sb.append(columnBounds.get(i)); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.