|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.druid.server.coordinator; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import org.apache.druid.client.DataSourcesSnapshot; |
| 24 | +import org.apache.druid.client.ImmutableDruidDataSource; |
| 25 | +import org.apache.druid.client.ImmutableDruidServer; |
| 26 | +import org.apache.druid.java.util.common.DateTimes; |
| 27 | +import org.apache.druid.server.coordination.DruidServerMetadata; |
| 28 | +import org.apache.druid.server.coordination.ServerType; |
| 29 | +import org.apache.druid.server.coordinator.balancer.RandomBalancerStrategy; |
| 30 | +import org.apache.druid.server.coordinator.duty.MarkOvershadowedSegmentsAsUnused; |
| 31 | +import org.apache.druid.server.coordinator.loading.SegmentLoadQueueManager; |
| 32 | +import org.apache.druid.server.coordinator.loading.TestLoadQueuePeon; |
| 33 | +import org.apache.druid.timeline.DataSegment; |
| 34 | +import org.joda.time.DateTime; |
| 35 | +import org.joda.time.Interval; |
| 36 | +import org.openjdk.jmh.annotations.Benchmark; |
| 37 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 38 | +import org.openjdk.jmh.annotations.Fork; |
| 39 | +import org.openjdk.jmh.annotations.Level; |
| 40 | +import org.openjdk.jmh.annotations.Measurement; |
| 41 | +import org.openjdk.jmh.annotations.Mode; |
| 42 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 43 | +import org.openjdk.jmh.annotations.Param; |
| 44 | +import org.openjdk.jmh.annotations.Scope; |
| 45 | +import org.openjdk.jmh.annotations.Setup; |
| 46 | +import org.openjdk.jmh.annotations.State; |
| 47 | +import org.openjdk.jmh.annotations.Warmup; |
| 48 | + |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.List; |
| 51 | +import java.util.concurrent.TimeUnit; |
| 52 | + |
| 53 | +/** |
| 54 | + * Benchmarks the {@code MarkOvershadowedSegmentsAsUnused} coordinator duty's {@code run} method against a cluster where |
| 55 | + * most datasources have no overshadowed segments (as in production). The duty builds segment timelines from the served |
| 56 | + * segments only for the datasources that actually have overshadowed segments, so the cost is dominated by how many |
| 57 | + * datasources are relevant relative to the total served. |
| 58 | + */ |
| 59 | +@State(Scope.Benchmark) |
| 60 | +@BenchmarkMode(Mode.AverageTime) |
| 61 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 62 | +@Warmup(iterations = 3, time = 2) |
| 63 | +@Measurement(iterations = 5, time = 2) |
| 64 | +@Fork(1) |
| 65 | +public class MarkOvershadowedSegmentsAsUnusedBenchmark |
| 66 | +{ |
| 67 | + private static final DateTime START = DateTimes.of("2024-01-01"); |
| 68 | + |
| 69 | + /** Total datasources served by the cluster. */ |
| 70 | + @Param({"1000"}) |
| 71 | + private int numDatasources; |
| 72 | + |
| 73 | + /** Datasources that have overshadowed segments (an older version shadowed by a newer one). */ |
| 74 | + @Param({"10", "100"}) |
| 75 | + private int relevantDatasources; |
| 76 | + |
| 77 | + @Param({"4"}) |
| 78 | + private int intervalsPerDatasource; |
| 79 | + |
| 80 | + private MarkOvershadowedSegmentsAsUnused duty; |
| 81 | + private DruidCoordinatorRuntimeParams params; |
| 82 | + |
| 83 | + @Setup(Level.Trial) |
| 84 | + public void setup() |
| 85 | + { |
| 86 | + final List<DataSegment> usedSegments = new ArrayList<>(); |
| 87 | + final ImmutableMap.Builder<String, ImmutableDruidDataSource> dataSources = ImmutableMap.builder(); |
| 88 | + |
| 89 | + for (int d = 0; d < numDatasources; d++) { |
| 90 | + final String datasource = "ds_" + d; |
| 91 | + final boolean hasOvershadowed = d < relevantDatasources; |
| 92 | + final List<DataSegment> dsSegments = new ArrayList<>(); |
| 93 | + for (int i = 0; i < intervalsPerDatasource; i++) { |
| 94 | + final Interval interval = new Interval(START.plusDays(i), START.plusDays(i + 1)); |
| 95 | + dsSegments.add(segment(datasource, interval, "v1")); |
| 96 | + if (hasOvershadowed) { |
| 97 | + // A newer version overshadows the v1 segment for the same interval. |
| 98 | + dsSegments.add(segment(datasource, interval, "v2")); |
| 99 | + } |
| 100 | + } |
| 101 | + usedSegments.addAll(dsSegments); |
| 102 | + dataSources.put(datasource, new ImmutableDruidDataSource(datasource, ImmutableMap.of(), dsSegments)); |
| 103 | + } |
| 104 | + |
| 105 | + final ImmutableDruidServer server = new ImmutableDruidServer( |
| 106 | + new DruidServerMetadata("server", "host", null, 1L << 40, null, ServerType.HISTORICAL, "_default_tier", 0), |
| 107 | + 0L, |
| 108 | + dataSources.build(), |
| 109 | + usedSegments.size() |
| 110 | + ); |
| 111 | + final DruidCluster cluster = DruidCluster |
| 112 | + .builder() |
| 113 | + .add(new ServerHolder(server, new TestLoadQueuePeon())) |
| 114 | + .build(); |
| 115 | + |
| 116 | + params = DruidCoordinatorRuntimeParams |
| 117 | + .builder() |
| 118 | + .withDataSourcesSnapshot(DataSourcesSnapshot.fromUsedSegments(usedSegments)) |
| 119 | + .withDruidCluster(cluster) |
| 120 | + .withDynamicConfigs(CoordinatorDynamicConfig.builder().withMarkSegmentAsUnusedDelayMillis(0).build()) |
| 121 | + .withBalancerStrategy(new RandomBalancerStrategy()) |
| 122 | + .withSegmentAssignerUsing(new SegmentLoadQueueManager(null, null)) |
| 123 | + .build(); |
| 124 | + |
| 125 | + // A no-op delete handler so the benchmark measures the timeline build + overshadow check, not the metadata write. |
| 126 | + duty = new MarkOvershadowedSegmentsAsUnused((datasource, segmentIds) -> segmentIds.size()); |
| 127 | + } |
| 128 | + |
| 129 | + @Benchmark |
| 130 | + public DruidCoordinatorRuntimeParams run() |
| 131 | + { |
| 132 | + return duty.run(params); |
| 133 | + } |
| 134 | + |
| 135 | + private static DataSegment segment(String datasource, Interval interval, String version) |
| 136 | + { |
| 137 | + return DataSegment.builder() |
| 138 | + .dataSource(datasource) |
| 139 | + .interval(interval) |
| 140 | + .version(version) |
| 141 | + .size(1) |
| 142 | + .build(); |
| 143 | + } |
| 144 | +} |
0 commit comments