druid icon indicating copy to clipboard operation
druid copied to clipboard

API to drop and reload data by interval

Open surekhasaharan opened this issue 7 years ago • 17 comments

Motivation

The process to delete data (mark segments unused) is not simple currently, look at the steps in tutorial http://druid.io/docs/latest/tutorials/tutorial-delete-data.html. It would be useful to provide coordinator APIs which can be used to delete(mark unused) or reload(mark used) segments for a datasource by interval. The API to reload data should mark any non-overshadowed segments as used.

Proposed changes

Propose to add two new API's to DataSourcesResource.java

POST /druid/coordinator/v1/datasources/{dataSourceName}/markUnused POST /druid/coordinator/v1/datasources/{dataSourceName}/markUsed

The payload would contain the interval or segment ids which needs to be marked used/unused. Either interval or segment ids should be provided, if both are provided in the payload , the API would throw an error. Interval specifies the start and end times as IS0 8601 strings. interval = [start, end] where start and end are both inclusive. Only the segments which are contained in the interval will be affected, i.e. segments which partially fall in the start or end will not be affected by these API's.

For example, interval payload json can be:

{
     "interval" : "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000"
}

sample payload with segment ids :

{
	"segmentIds": 
	[
		"wikipedia_2019-01-03T19:00:00.000Z_2019-01-03T20:00:00.000Z_2019-01-03T19:00:05.964Z",
		"wikipedia_2019-01-03T22:00:00.000Z_2019-01-03T23:00:00.000Z_2019-01-03T19:00:05.964Z_2"
	]
}

Following http response code would be returned by both APIs:

200 : if data is successfully dropped or reloaded 204 : if no content is found for given interval or segment ids not found in db 400 : if the payload is incorrect, either none or both (interval and segments) are provided 500 : if there was an exception trying carry out the action

Handling overshadowed segments while reloading data

The API to reload data would only mark used=true for segments which are not overshadowed by other segments to avoid the needless reload and drop of segments on historicals. This can be done in SQLMetadataSegmentManager by filtering segments which are overshadowed before updating in the metadata store.

Rationale

Considered DELETE /druid/coordinator/v1/datasources/{dataSourceName}/intervals/{interval} and POST /druid/coordinator/v1/datasources/{dataSourceName}/intervals/{interval}, but the former is already being used to kill segments. So decided to use PUT with the datasource and action, with the payload.

Operational impact

New api, so no operational impact.

Future work

DELETE /druid/coordinator/v1/datasources/{dataSourceName}/intervals/{interval} kills the segments, but DELETE /druid/coordinator/v1/datasources/{dataSourceName}/segments/{segmentId} disables segments. It would be useful to make the behavior of these two APIs coherent or change the URL to make it more intuitive.

surekhasaharan avatar Apr 10 '19 22:04 surekhasaharan

Please be aware of the impact of re-using segments on the coordinator's timeline logic. When you manually flag all segments that umbrella a time interval as "used" then the coordinator has to consider them ALL when doing the "what should be loaded?" logic. And I don't remember if it was fixed where those segments had to be loaded before it was able to make that distinction. This can cause a lot of churn on the coordinator during such an operation for datasources with a lot of segments.

drcrallen avatar Apr 11 '19 15:04 drcrallen

thanks @drcrallen for heads up. The proposal mentions "The API to reload data should mark any non-overshadowed segments as used." So I think that would avoid the needless churn. I also added some more details to the "Proposed Changes" section and added a sub-section "Handling overshadowed segments while reloading data" to clarify reload API behavior.

surekhasaharan avatar Apr 12 '19 06:04 surekhasaharan

Updated proposal to change PUT to POST as PUT is supposed to be idempotent and these API's may not be idempotent if invoked multiple times. Specified the exclusivity behavior of the interval start/end values.

surekhasaharan avatar Apr 19 '19 23:04 surekhasaharan

In case of overshadowed segments found in the interval or segment-ids payload, the reload API would return response code 200 and response body would contain the number of non-overshadowed segments that were successfully reloaded.

Returning a count of affected segments is good for the default case, but I think a non-default option on the API to report per-segment errors/failures in the response would be useful, in case the user wants to know info at such granularity. I think this can be done in a follow-on patch to the PRs that implement this proposal, if you agree that it's useful.

jon-wei avatar Apr 22 '19 20:04 jon-wei

For new APIs, it's better to use PUT for endpoints like .../markUsed and .../markUnused. It's semantically more correct than POST.

leventov avatar Apr 29 '19 11:04 leventov

Updated proposal to change PUT to POST as PUT is supposed to be idempotent and these API's may not be idempotent if invoked multiple times.

@surekhasaharan how the API may not be idempotent?

leventov avatar Apr 29 '19 11:04 leventov

@leventov the drop API may not be idempotent between calls, if more segments get added to the same interval. And the reload API can have different outcomes based on the number of segments overshadowed in the interval at a time, so for same interval, reload API can mark different subset of segments as used=true. See the discussion here

surekhasaharan avatar Apr 29 '19 15:04 surekhasaharan

@leventov the drop API may not be idempotent between calls, if more segments get added to the same interval. And the reload API can have different outcomes based on the number of segments overshadowed in the interval at a time, so for same interval, reload API can mark different subset of segments as used=true. See the discussion here

Hmm this seems borderline to me. You can call the reload-by-interval API over and over and it is idempotent in the sense that it always takes the system to the 'all latest segments are marked used' state. But the specific segments may change from run to run and so the effects of the call may change from run to run.

To me it's idempotent in the loose sense of 'nothing bad will happen if you call it repeatedly' but not in the strict sense of 'every call will have the same effect'.

gianm avatar Apr 29 '19 16:04 gianm

@surekhasaharan I think in so strict sense that you described, very little APIs should ever be PUT. Imagine on some website, PUT /users/fooUser/description, body: user's description. Then PUT is not idempotent, because a user may be removed or renamed between calls, and the new call will return an error.

I think, semantically, this operation is PUT.

leventov avatar Apr 29 '19 17:04 leventov

Then PUT is not idempotent, because a user may be removed or renamed between calls, and the new call will return an error.

If the API is designed to return an error in that case, then it probably should be a POST instead.

Accepting the request and creating the user would be more PUT-like behavior as I understand (see first example here https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT#Example)

Looking at RFC 7231: https://tools.ietf.org/html/rfc7231#section-4.3.4

The PUT method requests that the state of the target resource be
   created or replaced with the state defined by the representation
   enclosed in the request message payload.

...

If the target resource does not have a current representation and the
   PUT successfully creates one, then the origin server MUST inform the
   user agent by sending a 201 (Created) response.  If the target
   resource does have a current representation and that representation
   is successfully modified in accordance with the state of the enclosed
   representation, then the origin server MUST send either a 200 (OK) or
   a 204 (No Content) response to indicate successful completion of the
   request.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

From those characterizations of the methods, I think these drop/reload APIs are more POST.

I think in so strict sense that you described, very little APIs should ever be PUT.

Taking that premise, couldn't one also argue that very few APIs are really idempotent and thus there should be very few PUTs?

jon-wei avatar Apr 29 '19 18:04 jon-wei

To add a few more thoughts on PUT vs POST, I think if something is ambiguous I would rather go with POST for the following reasons:

  • Searching our codebase, I don't see any PUT endpoints currently. Having a few PUTs in a sea of POSTS I think would make it easy for users to make mistakes, and I don't really see much of a functional benefit to using PUT instead POST.
  • POST seems "safer": I think user beliefs on what is idempotent or not may vary (and so they may have different opinions on whether this API is idempotent or not), but the user that expects idempotence and doesn't get it would probably be more displeased than the user that expects non-idempotence and gets idempotence, so misclassifying (if this is a misclassification) as POST has less damage.

jon-wei avatar Apr 30 '19 05:04 jon-wei

@jon-wei I agree with the last two points that you brought.

leventov avatar Apr 30 '19 16:04 leventov

@surekhasaharan is there a use case when it's necessary to mark as used/unused only non-overshadowed segments when an endpoint is provided with a list of segment ids, "segmentIds": [...]? I find it considerably complicating the implementation, and being "too smart" in general: maybe there are reasons why we want to update the state of overshadowed segments. DruidCoordinatorMarkAsUnusedOvershadowedSegments should eventually clean it up.

leventov avatar May 01 '19 11:05 leventov

The markUnused API doesn't check for overshadowed in either case i.e. interval or segmentIds. The markUsed API does and the reason is to avoid needless load on historicals as discussed in the initial comments. We kept the behavior consistent for both interval and segmentIds case, otherwise it becomes confusing for the user. I can't think of a reason why someone would want to mark a overshadowed segment used intentionally , when it's going to be dropped again by the coordinator anyways.

surekhasaharan avatar May 01 '19 15:05 surekhasaharan

I would say it's confusing when an endpoint accepts a list of things to do something with them, and instead of just doing that, it filters the list further.

leventov avatar May 02 '19 07:05 leventov

Yeah, i agree it can be confusing, but this is something that can be addressed by clear documentation of the API, instead of different behavior of the same API with varying payload.

surekhasaharan avatar May 02 '19 17:05 surekhasaharan

With the web console additions I believe this is completed

vogievetsky avatar Jun 10 '19 05:06 vogievetsky