-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[opt](memory) Replace TabletMeta object map with SoA CompactTabletMetaStore #62086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dataroaring
wants to merge
3
commits into
apache:master
Choose a base branch
from
dataroaring:compact-tablet-meta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4eaaab4
[opt](memory) Replace TabletMeta object map with SoA CompactTabletMet…
dataroaring 4117345
[opt](memory) Address review feedback: remove unused array, reduce al…
dataroaring b50ad34
[opt](memory) Avoid TabletMeta allocation in buildPartitionInfoBySkew…
dataroaring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
226 changes: 226 additions & 0 deletions
226
fe/fe-core/src/main/java/org/apache/doris/catalog/CompactTabletMetaStore.java
This file contains hidden or 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,226 @@ | ||
| // 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.catalog; | ||
|
|
||
| import org.apache.doris.thrift.TStorageMedium; | ||
|
|
||
| import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Memory-compact storage for TabletMeta using Structure-of-Arrays layout. | ||
| * | ||
| * Instead of storing one TabletMeta object per tablet (each with a 16-byte Java object header), | ||
| * this class stores each field in a parallel primitive array indexed by an internal slot. | ||
| * A Long2IntOpenHashMap maps tabletId to the slot index. | ||
| * | ||
| * Deleted slots are reused via a free list embedded in the dbIds array. | ||
| * | ||
| * Thread safety: callers must hold appropriate locks (provided by TabletInvertedIndex). | ||
| */ | ||
| public class CompactTabletMetaStore { | ||
|
|
||
| private static final int INITIAL_CAPACITY = 1024; | ||
| private static final int ABSENT = -1; | ||
|
|
||
| // tabletId -> slot index | ||
| private Long2IntOpenHashMap tabletIdToSlot; | ||
|
|
||
| // parallel arrays indexed by slot | ||
| private long[] dbIds; | ||
| private long[] tableIds; | ||
| private long[] partitionIds; | ||
| private long[] indexIds; | ||
| private int[] oldSchemaHashes; | ||
| private byte[] storageMediumOrdinals; | ||
|
|
||
| // free list head; ABSENT means empty | ||
| private int freeHead = ABSENT; | ||
| // next never-used slot index | ||
| private int highWaterMark = 0; | ||
| // number of live entries | ||
| private int size = 0; | ||
| // allocated length of arrays | ||
| private int capacity; | ||
|
|
||
| private static final TStorageMedium[] MEDIUM_VALUES = TStorageMedium.values(); | ||
|
|
||
| public CompactTabletMetaStore() { | ||
| this(INITIAL_CAPACITY); | ||
| } | ||
|
|
||
| public CompactTabletMetaStore(int initialCapacity) { | ||
| this.capacity = Math.max(initialCapacity, 4); | ||
| this.tabletIdToSlot = new Long2IntOpenHashMap(this.capacity); | ||
| this.tabletIdToSlot.defaultReturnValue(ABSENT); | ||
| this.dbIds = new long[this.capacity]; | ||
| this.tableIds = new long[this.capacity]; | ||
| this.partitionIds = new long[this.capacity]; | ||
| this.indexIds = new long[this.capacity]; | ||
| this.oldSchemaHashes = new int[this.capacity]; | ||
| this.storageMediumOrdinals = new byte[this.capacity]; | ||
| } | ||
|
|
||
| public boolean add(long tabletId, TabletMeta meta) { | ||
| if (tabletIdToSlot.containsKey(tabletId)) { | ||
| return false; | ||
| } | ||
| int slot = allocateSlot(); | ||
| tabletIdToSlot.put(tabletId, slot); | ||
| dbIds[slot] = meta.getDbId(); | ||
| tableIds[slot] = meta.getTableId(); | ||
| partitionIds[slot] = meta.getPartitionId(); | ||
| indexIds[slot] = meta.getIndexId(); | ||
| oldSchemaHashes[slot] = meta.getOldSchemaHash(); | ||
| storageMediumOrdinals[slot] = (byte) meta.getStorageMedium().getValue(); | ||
| size++; | ||
| return true; | ||
| } | ||
|
|
||
| public void remove(long tabletId) { | ||
| int slot = tabletIdToSlot.remove(tabletId); | ||
| if (slot == ABSENT) { | ||
| return; | ||
| } | ||
| freeSlot(slot); | ||
| size--; | ||
| } | ||
|
|
||
| public boolean containsKey(long tabletId) { | ||
| return tabletIdToSlot.containsKey(tabletId); | ||
| } | ||
|
|
||
| public long getDbId(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| return slot == ABSENT ? TabletInvertedIndex.NOT_EXIST_VALUE : dbIds[slot]; | ||
| } | ||
|
|
||
| public long getTableId(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| return slot == ABSENT ? TabletInvertedIndex.NOT_EXIST_VALUE : tableIds[slot]; | ||
| } | ||
|
|
||
| public long getPartitionId(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| return slot == ABSENT ? TabletInvertedIndex.NOT_EXIST_VALUE : partitionIds[slot]; | ||
| } | ||
|
|
||
| public long getIndexId(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| return slot == ABSENT ? TabletInvertedIndex.NOT_EXIST_VALUE : indexIds[slot]; | ||
| } | ||
|
|
||
| public int getOldSchemaHash(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| return slot == ABSENT ? TabletInvertedIndex.NOT_EXIST_VALUE : oldSchemaHashes[slot]; | ||
| } | ||
|
|
||
| public TStorageMedium getStorageMedium(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| if (slot == ABSENT) { | ||
| return null; | ||
| } | ||
| return MEDIUM_VALUES[storageMediumOrdinals[slot]]; | ||
| } | ||
|
|
||
| public void setStorageMedium(long tabletId, TStorageMedium medium) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| if (slot != ABSENT) { | ||
| storageMediumOrdinals[slot] = (byte) medium.getValue(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Construct a TabletMeta on demand for backward compatibility. | ||
| * Returns null if the tabletId is not present. | ||
| */ | ||
| public TabletMeta getTabletMeta(long tabletId) { | ||
| int slot = tabletIdToSlot.get(tabletId); | ||
| if (slot == ABSENT) { | ||
| return null; | ||
| } | ||
| return new TabletMeta( | ||
| dbIds[slot], | ||
| tableIds[slot], | ||
| partitionIds[slot], | ||
| indexIds[slot], | ||
| oldSchemaHashes[slot], | ||
| MEDIUM_VALUES[storageMediumOrdinals[slot]]); | ||
| } | ||
|
|
||
| /** | ||
| * Build a full Map for backward compatibility (test-only usage). | ||
| */ | ||
| public Map<Long, TabletMeta> toMap() { | ||
| Map<Long, TabletMeta> map = new HashMap<>(size * 4 / 3 + 1); | ||
| for (Long2IntOpenHashMap.Entry entry : tabletIdToSlot.long2IntEntrySet()) { | ||
| long tabletId = entry.getLongKey(); | ||
| int slot = entry.getIntValue(); | ||
| map.put(tabletId, new TabletMeta( | ||
| dbIds[slot], | ||
| tableIds[slot], | ||
| partitionIds[slot], | ||
| indexIds[slot], | ||
| oldSchemaHashes[slot], | ||
| MEDIUM_VALUES[storageMediumOrdinals[slot]])); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| public void clear() { | ||
| tabletIdToSlot.clear(); | ||
| freeHead = ABSENT; | ||
| highWaterMark = 0; | ||
| size = 0; | ||
| } | ||
|
|
||
| private int allocateSlot() { | ||
| if (freeHead != ABSENT) { | ||
| int slot = freeHead; | ||
| freeHead = (int) dbIds[freeHead]; | ||
| return slot; | ||
| } | ||
| if (highWaterMark == capacity) { | ||
| grow(); | ||
| } | ||
| return highWaterMark++; | ||
| } | ||
|
|
||
| private void freeSlot(int slot) { | ||
| dbIds[slot] = freeHead; | ||
| freeHead = slot; | ||
| } | ||
|
|
||
| private void grow() { | ||
| int newCapacity = capacity * 2; | ||
| dbIds = Arrays.copyOf(dbIds, newCapacity); | ||
| tableIds = Arrays.copyOf(tableIds, newCapacity); | ||
| partitionIds = Arrays.copyOf(partitionIds, newCapacity); | ||
| indexIds = Arrays.copyOf(indexIds, newCapacity); | ||
| oldSchemaHashes = Arrays.copyOf(oldSchemaHashes, newCapacity); | ||
| storageMediumOrdinals = Arrays.copyOf(storageMediumOrdinals, newCapacity); | ||
| capacity = newCapacity; | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newSchemaHashesis allocated and grown alongside the other arrays, but it is never read or exposed, andTabletMetacurrently has no getter fornewSchemaHash. This adds per-tablet memory overhead and undermines the compaction goal.Either remove
newSchemaHashesentirely for now, or fully wire it through (capture the value on add and expose a getter / include it when constructingTabletMeta).