Kafka indexing service duplicate entry exception in druid_pendingSegments
After upgrading to Druid 0.16.0-incubating, I am receiving a MySQLIntegrityConstraintViolationException complaining about:
"Duplicate entry XXX for key 'PRIMARY' [statement:"INSERT INTO druid_pendingSegments (id, dataSource.."
This results in the Kafka indexing tasks not being able to complete and the eventual failure of the coordinator/overlord nodes. This scenario only seems to happen after I drop some segments from Druid and then push in new data for the time period which was dropped. The only way I have found to fix this has been to force stop all of my Kafka indexing supervisors & tasks and manually delete all of the entires in the druid_pendingSegments table. After I do that, I no longer receive the sql exception and corresponding duplicate entry error message. Any thoughts on this would be greatly appreciated!
How to Reproduce:
- Suspend a Kafka indexing supervisor for a given data source and wait for the indexing task(s) to complete.
- Drop segments for a certain time period from the given data source and wait for the segments to be unloaded from the historical nodes.
- Resume the Kafka indexing supervisor for a given data source.
- Push new data through Kafka for the same time period which was previously dropped on the given data source.
- Check the indexing logs for the Kafka indexing tasks to see them complaining about duplicate primary key errors.
Other Notables:
- The druid_pendingSegments table doesn't seem to get cleaned up once a Kafka indexing supervisor is suspended. Entries are still left in this table for the given data source despite all the segments having been published to deepstorage / historical nodes. I do have
druid.coordinator.kill.pendingSegments.on=trueenabled. Maybe this is normal?
Hi @teeram thank you for the report. I'm trying to replicate this error in my local but there has been no luck so far. Would you please answer to a couple of questions?
- Do you always see this error if you follow the steps in the issue?
- By any chance, did you update
druid_pendingSegmentsordruid_segmentstable between the step 2 and 4 except dropping segments in some time chunks?
@jihoonson, thank you so much for looking into this.
- I would say this error occurs roughly 33% of the time I repeat the process described above.
- I'm not explicitly updating the
druid_pendingSegmentsordruid_segmentstables between step 2 and 4. I generally don't touch anything in metadata storage and always try to work within the standard Druid api's. I don't trust myself to not break something :) But I do occasionally peek in there when I'm doing some debugging.
So I wrote a small utility that will suspend a supervisor and drop various segments for a given time period from that data source before resuming supervisor. It does wait for tasks to finish and segments to be fully unloaded before continuing on to the next steps. I updated this utility to go and delete any entries for a given data source in the druid_pendingSegments table before resuming the supervisor. This seems to prevent the duplicate key error from occurring.
Any chance there were some changes between 0.14.1 and 0.16.0 to the way the druid_pendingSegments table is manipulated? I upgraded straight from 0.14.1 to 0.16.0 and skipped some of the 0.15.X versions. Maybe that was a bad move on my part? I don't believe I had this issue in 0.14.1 so I'm guessing something changed between that release and 0.16.0. Thanks again for all your help!
The biggest change around the segment ID allocation was #7547 between 0.14.1 and 0.16.0, but it shouldn't change anything about the new segment ID verification. I'm still not sure how this error could happen. If possible, could you share what was the duplicate segment ID and what other segments were in the same time chunk? Including both published (used = true) and unpublished (used = false) segments in druid_segments table and all segments in the same time chunk in druid_pendingSegments table.
Hey @jihoonson, sorry for the delays in response. Hmm, I haven't turned on the minor compaction features mentioned in that issue, so I don't suspect that to be the culprit either.
Unfortunately, I can't share those exact segment id's that are in conflict. But I am looking through those tables and can describe some of the oddities I am seeing. One thing I have noticed in the druid_pendingSegments table is that the id in conflict has a created_date that is typically from the previous day. For instance, I am getting an id conflict today (2019-10-15) for an entry in druid_pendingSegments with a created_date of 2019-10-14. That seems a little odd to me that the resumed Kafka indexing task would not attempt to place an entry in druid_pendingSegments with an updated value for the created_date field. There are other entries in the druid_pendingSegments table with a created_date field from today (2019-10-15).
I did a quick query in the druid_segments table to see what segments were available for the time chunk in conflict. I found that all the segments in this table had been unpublished (used = false). This was the behavior I was expecting as I was dropping old segments before new ones were created.
Thanks again for all your assistance!
Hmm, the entries in druid_pendingSegments table are never updated but should be reused if possible. The way the segment ID allocation works is as below:
- a task asks a new segment ID to the overlord with
datasource,interval, andsequenceName. Segment ID is unique perdatasource,interval, andsequenceNameand segment ID allocation is idempotent. This means, if a task asks with the samedatasource,interval, andsequenceName, the overlord will return the same segment ID instead of creating a new one. The basesequenceNameis generated as here, and each task will attach a sequential number whenever intermediate publish is triggered. - The overlord first looks up
druid_pendingSegmentstable to see if there's a reusable segment ID. - If not, it looks up both
druid_segmentsanddruid_pendingSegmentstables to find the next available segment ID. This is done by searching for the max partition id of all segments in the interval including bothpublishedandunpublishedones. The new segment will have the partition id ofcurrent max partition id + 1. - Once it finds the next segment ID, it will insert it into the metadata store. Otherwise, the segment ID allocation fails.
2) - 4) should be done atomically. Based on what you see in the metadata store, do you think there could be a bug in any step?
Based on that info, I suspect there might be a bug in the 3rd step. It seems like I would not receive a duplicate entry exception if the segment ID had the partition id increased by 1. The entry in conflict seems to always be using the current max partition id instead of it being incremented by 1.
I also meet the problem, it may be a bug.
Faced the same issue too. I am using 0.16.1
Sorry I forgot about this issue. @teeram, @yangliang9004, @1ambda would you please give more contexts about your environment? For example,
- Are you using the auto compaction?
- Are you using segment locking?
- Are you using other ingestions (batch, kinesis, etc) in addition to Kafka ingestion? If so, are they ingesting to the same datasource?
Hi @jihoonson, yes the auto-compaction feature is enabled. No, I'm not using any explicit segment locking. No other ingestion processes are running except for Kafka. However, there are multiple Kafka ingestion Supervisors running concurrently when this occurs. This didn't seem to be an issue until upgrading to the 0.16.0 version of Druid from 0.14.1.
@teeram thanks.. I'm trying to scope out what places I need to look into. One more question: was there any manual intervention with metadata store? Anything can be a manual intervention if it has done without going through the Overlord. For example, executing UPDATE queries on the metadata store directly, calling the Coordinator APIs to enable/disable segments (these APIs don't go through the Overlord), etc.
Hii @jihoonson
- We use auto compaction too.
druid.indexer.tasklock.forceTimeChunkLockis true (default value)- We use Kafka indexing service only.
Thanks for the reply. Please let me know if other config or information needed.
One more question: was there any manual intervention with metadata store?
Cleaned up metadata store before because of the same issue (remove rows in the druid_pendingsegments table in MySQL)
Coordinator APIs to enable/disable segments (these APIs don't go through the Overlord),
Called enable / disable segments APIs manually using cURL or something. But not sure it was for the same datasource which is having the issue.
Q. dropping a datasource in druid UI (console) is the same API w/ what you mentioned here?
@1ambda thank you for more information.
Q. dropping datasource in druid UI (console) is the same API w/ what you mentioned here?
I think it uses a coordinator API, but it would probably be ok because it disables all segments in a datasource.
Called enable / disable segments APIs manually using cURL or something. But not sure it was for the same datasource or not which is having the issue.
Thanks, I'll take some manual intervention into consideration when I debug.
Hey @jihoonson, the way I have gotten around this problem is to manually connect to the metadata store and delete any rows associated with a datasource in the druid_pendingSegments table. I suspend the Kafka indexing service before this occurs by using the coordinator API. Once the rows have been deleted, I fire the Kafka indexing service back up again using the coordinator API.
@teeram thanks. That sounds like a valid approach to workaround this issue manually.
At least in 0.17.0 you can delete entries from that table by using overlord API:
curl -X DELETE -H 'Accept: application/json, text/plain, */*' http://[yourhost]:[yourport]/druid/indexer/v1/pendingSegments/[datasource]?interval=1000/3000
Faced the same issue too.
- druid version: 0.18.1
- using auto compaction
- using timeChunk lock
Facing the same issue also with version 0.18.1.
Facing the same issue also with version 0.16.0-incubating.
I have encountered this problem several times. The scenario at that time was that I needed to delete the data for a certain period of time and re-import the data into kafka for druid calculation; this problem would occur if the operation interval was short.
When this problem occurred, I could not connect to the coordinator / overload node. So my solution is to connect to the metadata storage and manually delete the related records in druid_pendingSegments