You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "kfaraz (via GitHub)" <gi...@apache.org> on 2025/12/03 03:22:31 UTC
[PR] Add new compaction policy to prioritize fragmented intervals (druid)
kfaraz opened a new pull request, #18802:
URL: https://github.com/apache/druid/pull/18802
### Changes
- Update `CompactionStatus.compute()` to track the number of compacted and uncompacted segments
- Add new compaction policy `mostFragmentedFirst` which prioritizes intervals with most small uncompacted segments
<hr>
This PR has:
- [ ] been self-reviewed.
- [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
- [ ] added documentation for new or modified features or behaviors.
- [ ] a release note entry in the PR description.
- [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
- [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
- [ ] added integration tests.
- [ ] been tested in a test Druid cluster.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583878078
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
+ */
+ private double computePriority(CompactionCandidate candidate)
+ {
+ final CompactionStatistics compacted = candidate.getCompactedStats();
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null || compacted == null) {
+ return 0;
+ }
+
+ final long avgUncompactedSize = Math.max(1, uncompacted.getTotalBytes() / uncompacted.getNumSegments());
+
+ // Priority increases as size decreases and number increases
+ final double normalizingFactor = 1000f;
Review Comment:
I had started out keeping the priority as a `long` and used a multiplication factor in an effort to get whole number values. But I later realized that it would be difficult to have a universal factor that works for all cases, so best to use a `double` instead.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583878078
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
+ */
+ private double computePriority(CompactionCandidate candidate)
+ {
+ final CompactionStatistics compacted = candidate.getCompactedStats();
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null || compacted == null) {
+ return 0;
+ }
+
+ final long avgUncompactedSize = Math.max(1, uncompacted.getTotalBytes() / uncompacted.getNumSegments());
+
+ // Priority increases as size decreases and number increases
+ final double normalizingFactor = 1000f;
Review Comment:
Yes, I think this doesn't serve any purpose any more.
I had started out keeping the priority as a `long` and used a multiplication factor in an effort to get whole number values. But I later realized that it would be difficult to have a universal factor that works for all cases, so best to use a `double` instead.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583881029
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
Review Comment:
Yeah, I agree that average number of rows per segment would be a better parameter for this function.
I have used the average bytes here as a proxy until we have the row count for each segment available in the metadata.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583910471
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
> Intuitively what does the priority "mean"? It's helpful to add that to the javadocs.
Would it also make sense to rename the method as `computeDegreeOfFragmentation()`, since essentially that is what we are trying to achieve here?
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on PR #18802:
URL: https://github.com/apache/druid/pull/18802#issuecomment-3615304083
Thanks for the review, @gianm !
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583835135
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
Review Comment:
Use `HumanReadableBytes`?
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
Review Comment:
The names of `minUncompactedBytes` and `maxUncompactedSize` are confusing, because they look very similar but one refers to total and one refers to average. How about `minUncompactedBytes` and `maxAverageUncompactedBytesPerSegment`?
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
+ */
+ private double computePriority(CompactionCandidate candidate)
+ {
+ final CompactionStatistics compacted = candidate.getCompactedStats();
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null || compacted == null) {
+ return 0;
+ }
+
+ final long avgUncompactedSize = Math.max(1, uncompacted.getTotalBytes() / uncompacted.getNumSegments());
+
+ // Priority increases as size decreases and number increases
+ final double normalizingFactor = 1000f;
Review Comment:
What's the purpose of this factor? seems like it wouldn't affect the results.
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
Intuitively what does the priority "mean"? It's helpful to add that to the javadocs.
Reading through, it seems like the priority boils down to `pow(uncompacted.getNumSegments, 2) / uncompacted.getTotalBytes`. Why that particular formula?
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
Review Comment:
Also, I wish this could be specified in terms of row counts rather than byte counts. The target segment size `rowsPerSegment` is specified as a number of rows, so it's most natural to specify the max average uncompacted size in terms of rows as well. Like, I might say that the target is 3M rows per segment but don't bother compacting if the segments average 2.5M already.
I recognize this requires additional metadata to be available that may not currently be available, so it doesn't have to be done in this PR. I'm just having a thought.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583871267
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
Sure, will update the javadocs.
I wanted it to be directly proportional to the number of uncompacted segments and inversely proportional to the size of uncompacted segments.
So
```
priority
= uncompactedCount / avgUncompactedSize
= uncompactedCount * (uncompactedCount / uncompactedBytes)
```
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583887923
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
The other alternative would be to have something like
```
priority = factor1 * uncompactedCount + factor2 * avgUncompactedSize
```
but that seemed like it would be difficult to tune as no set of factors 1 and 2 would apply universally to all cases.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm merged PR #18802:
URL: https://github.com/apache/druid/pull/18802
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2588759543
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
Updated the javadocs. Also updated the fragmentation index formula. Details in the PR description.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Add new compaction policy to prioritize fragmented intervals (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm commented on code in PR #18802:
URL: https://github.com/apache/druid/pull/18802#discussion_r2583862234
##########
server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.druid.server.compaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.common.config.Configs;
+
+import javax.annotation.Nullable;
+
+/**
+ * {@link CompactionCandidateSearchPolicy} which prioritizes compaction of the
+ * intervals with the largest number of small uncompacted segments.
+ * <p>
+ * This policy favors cluster stability (by prioritizing reduction of segment
+ * count) over performance of queries on newer intervals. For the latter, use
+ * {@link NewestSegmentFirstPolicy}.
+ */
+public class MostFragmentedIntervalFirstPolicy implements CompactionCandidateSearchPolicy
+{
+ private static final long SIZE_2_GB = 2_000_000_000;
+ private static final long SIZE_10_MB = 10_000_000;
+
+ private final int minUncompactedCount;
+ private final long minUncompactedBytes;
+ private final long maxUncompactedSize;
+
+ @JsonCreator
+ public MostFragmentedIntervalFirstPolicy(
+ @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount,
+ @JsonProperty("minUncompactedBytes") @Nullable Long minUncompactedBytes,
+ @JsonProperty("maxUncompactedSize") @Nullable Long maxUncompactedSize
+ )
+ {
+ this.minUncompactedCount = Configs.valueOrDefault(minUncompactedCount, 100);
+ this.minUncompactedBytes = Configs.valueOrDefault(minUncompactedBytes, SIZE_10_MB);
+ this.maxUncompactedSize = Configs.valueOrDefault(maxUncompactedSize, SIZE_2_GB);
+ }
+
+ /**
+ * Minimum number of uncompacted segments that must be present in an interval
+ * to make it eligible for compaction.
+ */
+ @JsonProperty
+ public int getMinUncompactedCount()
+ {
+ return minUncompactedCount;
+ }
+
+ /**
+ * Minimum total bytes of uncompacted segments that must be present in an
+ * interval to make it eligible for compaction. Default value is {@link #SIZE_10_MB}.
+ */
+ @JsonProperty
+ public long getMinUncompactedBytes()
+ {
+ return minUncompactedBytes;
+ }
+
+ /**
+ * Maximum average size of uncompacted segments in an interval eligible for
+ * compaction. Default value is {@link #SIZE_2_GB}.
+ */
+ @JsonProperty
+ public long getMaxUncompactedSize()
+ {
+ return maxUncompactedSize;
+ }
+
+ @Override
+ public int compareCandidates(CompactionCandidate candidateA, CompactionCandidate candidateB)
+ {
+ return computePriority(candidateA) - computePriority(candidateB) > 0
+ ? 1 : -1;
+ }
+
+ @Override
+ public boolean isEligibleForCompaction(
+ CompactionCandidate candidate,
+ CompactionTaskStatus latestTaskStatus
+ )
+ {
+ final CompactionStatistics uncompacted = candidate.getUncompactedStats();
+ if (uncompacted == null) {
+ return true;
+ } else if (uncompacted.getNumSegments() < 1) {
+ return false;
+ } else {
+ return uncompacted.getNumSegments() >= minUncompactedCount
+ && uncompacted.getTotalBytes() >= minUncompactedBytes
+ && (uncompacted.getTotalBytes() / uncompacted.getNumSegments()) <= maxUncompactedSize;
+ }
+ }
+
+ /**
+ * Computes the priority of the given compaction candidate by checking the
+ * total number and average size of uncompacted segments.
Review Comment:
Unit tests for this function would be helpful, to illustrate its behavior in various situations.
--
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-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org