-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add api for Retrieving unused segments #15415
Changes from 19 commits
1d135e7
c2d52f2
190a379
49cbe5a
49c1c1a
bf15374
90362c3
af81f10
3950732
002f00e
5ada621
c82ab4a
68a98dd
6675228
82259c6
c92aaf1
817d8be
0ea6b5a
ff0e5c5
a23b044
d6685a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,30 @@ Optional<Iterable<DataSegment>> iterateAllUsedNonOvershadowedSegmentsForDatasour | |
boolean requiresLatest | ||
); | ||
|
||
/** | ||
* Returns an iterable to go over un-used segments for a given datasource over an optional interval. | ||
* The order in which segments are iterated is from earliest start-time, with ties being broken with earliest end-time | ||
* first. Note: the iteration may not be as trivially cheap as, | ||
* for example, iteration over an ArrayList. Try (to some reasonable extent) to organize the code so that it | ||
* iterates the returned iterable only once rather than several times. | ||
* | ||
* @param datasource the name of the datasource. | ||
* @param interval an optional interval to search over. If none is specified, {@link org.apache.druid.java.util.common.Intervals#ETERNITY} | ||
* @param limit an optional maximum number of results to return. If none is specified, the results are not limited. | ||
* @param lastSegmentId an optional last segment id from which to search for results. All segments returned are > | ||
* this segment lexigraphically if sortOrder is null or {@link SortOrder#ASC}, or < this segment | ||
* lexigraphically if sortOrder is {@link SortOrder#DESC}. If none is specified, no such filter is used. | ||
* @param sortOrder an optional order with which to return the matching segments by id, start time, end time. | ||
* If none is specified, the order of the results is not guarenteed. | ||
*/ | ||
Iterable<DataSegment> iterateAllUnusedSegmentsForDatasource( | ||
String datasource, | ||
@Nullable Interval interval, | ||
@Nullable Integer limit, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be a good idea to go ahead and add a sort order now too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Good suggestion. Added |
||
@Nullable String lastSegmentId, | ||
@Nullable SortOrder sortOrder | ||
); | ||
|
||
/** | ||
* Retrieves all data source names for which there are segment in the database, regardless of whether those segments | ||
* are used or not. If there are no segments in the database, returns an empty set. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.metadata; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import org.apache.druid.error.InvalidInput; | ||
import org.apache.druid.java.util.common.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Specifies the sort order when doing metadata store queries. | ||
*/ | ||
public enum SortOrder | ||
{ | ||
ASC("ASC"), | ||
|
||
DESC("DESC"); | ||
|
||
private String value; | ||
|
||
SortOrder(String value) | ||
{ | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
@JsonValue | ||
public String toString() | ||
{ | ||
return String.valueOf(value); | ||
} | ||
|
||
@JsonCreator | ||
public static SortOrder fromValue(String value) | ||
{ | ||
for (SortOrder b : SortOrder.values()) { | ||
if (String.valueOf(b.value).equalsIgnoreCase(String.valueOf(value))) { | ||
return b; | ||
} | ||
} | ||
throw InvalidInput.exception(StringUtils.format( | ||
"Unexpected value[%s] for SortOrder. Possible values are: %s", | ||
value, Arrays.stream(SortOrder.values()).map(SortOrder::toString).collect(Collectors.toList()) | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,6 +25,7 @@ | |||||
import com.google.common.base.Preconditions; | ||||||
import com.google.common.base.Supplier; | ||||||
import com.google.common.base.Throwables; | ||||||
import com.google.common.collect.ImmutableList; | ||||||
import com.google.common.collect.ImmutableMap; | ||||||
import com.google.common.collect.Iterables; | ||||||
import com.google.common.collect.Iterators; | ||||||
|
@@ -686,7 +687,7 @@ private int doMarkAsUsedNonOvershadowedSegments(String dataSourceName, @Nullable | |||||
} | ||||||
|
||||||
try (final CloseableIterator<DataSegment> iterator = | ||||||
queryTool.retrieveUnusedSegments(dataSourceName, intervals, null)) { | ||||||
queryTool.retrieveUnusedSegments(dataSourceName, intervals, null, null, null)) { | ||||||
while (iterator.hasNext()) { | ||||||
final DataSegment dataSegment = iterator.next(); | ||||||
timeline.addSegments(Iterators.singletonIterator(dataSegment)); | ||||||
|
@@ -955,6 +956,50 @@ public Optional<Iterable<DataSegment>> iterateAllUsedNonOvershadowedSegmentsForD | |||||
.transform(timeline -> timeline.findNonOvershadowedObjectsInInterval(interval, Partitions.ONLY_COMPLETE)); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Retrieves segments for a given datasource that are marked unused and that are *fully contained by* an optionally | ||||||
* specified interval. If the interval specified is null, this method will retrieve all unused segments. | ||||||
* | ||||||
* This call does not return any information about realtime segments. | ||||||
* | ||||||
* @param datasource The name of the datasource | ||||||
* @param interval The intervals to search over | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||
* @param limit The limit of segments to return | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||
* @param lastSegmentId an optional last segment id from which to search for results. All segments returned are > | ||||||
* this segment lexigraphically if sortOrder is null or {@link SortOrder#ASC}, or < this | ||||||
* segment lexigraphically if sortOrder is {@link SortOrder#DESC}. If none is specified, no | ||||||
* such filter is used. | ||||||
* @param sortOrder an optional order with which to return the matching segments by id, start time, end time. If | ||||||
* none is specified, the order of the results is not guarenteed. | ||||||
|
||||||
* Returns an iterable. | ||||||
*/ | ||||||
@Override | ||||||
public Iterable<DataSegment> iterateAllUnusedSegmentsForDatasource( | ||||||
final String datasource, | ||||||
@Nullable final Interval interval, | ||||||
@Nullable final Integer limit, | ||||||
@Nullable final String lastSegmentId, | ||||||
@Nullable final SortOrder sortOrder | ||||||
) | ||||||
{ | ||||||
return connector.inReadOnlyTransaction( | ||||||
(handle, status) -> { | ||||||
final SqlSegmentsMetadataQuery queryTool = | ||||||
SqlSegmentsMetadataQuery.forHandle(handle, connector, dbTables.get(), jsonMapper); | ||||||
|
||||||
final List<Interval> intervals = | ||||||
interval == null | ||||||
? Intervals.ONLY_ETERNITY | ||||||
: Collections.singletonList(interval); | ||||||
try (final CloseableIterator<DataSegment> iterator = | ||||||
queryTool.retrieveUnusedSegments(datasource, intervals, limit, lastSegmentId, sortOrder)) { | ||||||
return ImmutableList.copyOf(iterator); | ||||||
} | ||||||
} | ||||||
); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Set<String> retrieveAllDataSourceNames() | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,12 @@ | |
import com.sun.jersey.spi.container.ResourceFilters; | ||
import org.apache.druid.client.DataSourcesSnapshot; | ||
import org.apache.druid.client.ImmutableDruidDataSource; | ||
import org.apache.druid.error.InvalidInput; | ||
import org.apache.druid.indexing.overlord.IndexerMetadataStorageCoordinator; | ||
import org.apache.druid.indexing.overlord.Segments; | ||
import org.apache.druid.java.util.common.Intervals; | ||
import org.apache.druid.metadata.SegmentsMetadataManager; | ||
import org.apache.druid.metadata.SortOrder; | ||
import org.apache.druid.segment.metadata.AvailableSegmentMetadata; | ||
import org.apache.druid.segment.metadata.CoordinatorSegmentMetadataCache; | ||
import org.apache.druid.segment.metadata.DataSourceInformation; | ||
|
@@ -334,6 +337,49 @@ public Response getUsedSegmentsInDataSourceForIntervals( | |
return builder.entity(Collections2.transform(segments, DataSegment::getId)).build(); | ||
} | ||
|
||
@GET | ||
@Path("/datasources/{dataSourceName}/unusedSegments") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response getUnusedSegmentsInDataSource( | ||
@Context final HttpServletRequest req, | ||
@PathParam("dataSourceName") final String dataSource, | ||
@QueryParam("interval") @Nullable String interval, | ||
@QueryParam("limit") @Nullable Integer limit, | ||
@QueryParam("lastSegmentId") @Nullable final String lastSegmentId, | ||
@QueryParam("sortOrder") @Nullable final String sortOrder | ||
) | ||
{ | ||
if (dataSource == null || dataSource.isEmpty()) { | ||
throw InvalidInput.exception("dataSourceName must be non-empty"); | ||
} | ||
if (limit != null && limit < 0) { | ||
throw InvalidInput.exception("Invalid limit[%s] specified. Limit must be > 0", limit); | ||
} | ||
|
||
SortOrder theSortOrder = sortOrder == null ? null : SortOrder.fromValue(sortOrder); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also add a validation check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
final Interval theInterval = interval != null ? Intervals.of(interval.replace('_', '/')) : null; | ||
Iterable<DataSegment> unusedSegments = segmentsMetadataManager.iterateAllUnusedSegmentsForDatasource( | ||
dataSource, | ||
theInterval, | ||
limit, | ||
lastSegmentId, | ||
theSortOrder | ||
); | ||
|
||
final Function<DataSegment, Iterable<ResourceAction>> raGenerator = segment -> Collections.singletonList( | ||
AuthorizationUtils.DATASOURCE_READ_RA_GENERATOR.apply(segment.getDataSource())); | ||
|
||
final Iterable<DataSegment> authorizedSegments = | ||
AuthorizationUtils.filterAuthorizedResources(req, unusedSegments, raGenerator, authorizerMapper); | ||
|
||
|
||
// sort by earliest start interval first, then end interval. DataSegment are sorted in this same order due to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this comment here since the sorting actually happens inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
// how the segment id is generated. | ||
final List<DataSegment> retVal = new ArrayList<>(); | ||
authorizedSegments.iterator().forEachRemaining(retVal::add); | ||
return Response.status(Response.Status.OK).entity(retVal).build(); | ||
} | ||
|
||
@GET | ||
@Path("/datasources/{dataSourceName}/segments/{segmentId}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
|
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.
To make the API behavior clear in the absence of all the optional parameters, maybe specify what the defaults for the no interval, no limit, etc. Also, it'd be good to include an example for these params, so a user knows what to specify for params like
sortOrder
(wish we had an open API spec in Druid :)).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.
fixed