Skip to content
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 OpenAndTrimHistory API to support trimming data with specified timestamp #9410

Closed
wants to merge 18 commits into from

Conversation

sunlike-Lipo
Copy link
Contributor

@sunlike-Lipo sunlike-Lipo commented Jan 20, 2022

As disscussed in (#9223), Here added a new API named DB::OpenAndTrimHistory, this API will open DB and trim data to the timestamp specofied by trim_ts (The data with newer timestamp than specified trim bound will be removed). This API should only be used at a timestamp-enabled db instance recovery.

And this PR implemented a new iterator named HistoryTrimmingIterator to support trimming history with a new API named DB::OpenAndTrimHistory. HistoryTrimmingIterator wrapped around the underlying InternalITerator such that keys whose timestamps newer than trim_ts should not be returned to the compaction iterator while trim_ts is not null.

@siying
Copy link
Contributor

siying commented Jan 31, 2022

Since the previous discussion was with @riversand963 , assigning to @riversand963 to triage.

@riversand963
Copy link
Contributor

Thanks @sunlike-Lipo for the PR and sorry for late reply.
According to #9223 (comment), is this history trimming only needed when we open a database (not sure if manually demotion requires re-open)? If so, I think it's better not to extend this history trimming to CompactRange which can be called at any time, since it's hard to reason about correctness when the db is accepting new writes.
An example is OpenAndCompact(). Similar to this, we can have DB::OpenAndTrimHistory(..., ts).

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sunlike-Lipo for the PR. I left a few comments.

util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
db/dbformat.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
@sunlike-Lipo sunlike-Lipo force-pushed the main branch 2 times, most recently from 42de914 to 3b0a0c4 Compare February 7, 2022 17:47
@sunlike-Lipo
Copy link
Contributor Author

sunlike-Lipo commented Feb 7, 2022

Thanks @sunlike-Lipo for the PR and sorry for late reply. According to #9223 (comment), is this history trimming only needed when we open a database (not sure if manually demotion requires re-open)? If so, I think it's better not to extend this history trimming to CompactRange which can be called at any time, since it's hard to reason about correctness when the db is accepting new writes. An example is OpenAndCompact(). Similar to this, we can have DB::OpenAndTrimHistory(..., ts).

@riversand963
Thank you for your review and very sorry for the late reply...
I added DB::OpenAndTrimHistory() API in the new commit. Please let me know if this API meets your requirements.

@sunlike-Lipo
Copy link
Contributor Author

sunlike-Lipo commented Feb 8, 2022

@riversand963
It seems the review comments have been marked outdated after I resolved merge-conflicts and squash the new commits.
Very sorry for that if it has caused you any inconvenience. I'll be more cautious in dealing with similar situations.

@sunlike-Lipo sunlike-Lipo changed the title Add HistoryTrimmingIterator to support trimming history with CompactRange Add HistoryTrimmingIterator to support trimming history with OpenAndTrimHistory Feb 9, 2022
@sunlike-Lipo sunlike-Lipo changed the title Add HistoryTrimmingIterator to support trimming history with OpenAndTrimHistory Add OpenAndTrimHistory API to support trimming data with specified timestamp Feb 9, 2022
@riversand963
Copy link
Contributor

Thanks @sunlike-Lipo for the PR. I will try to review this week.

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sunlike-Lipo for the improvement.
How do you handle data in the WAL if avoid_flush_during_recovery is true? If possible, these data will remain in memtable.

HISTORY.md Outdated Show resolved Hide resolved
db/compaction/compaction.h Show resolved Hide resolved
include/rocksdb/db.h Outdated Show resolved Hide resolved
util/history_trimming_iterator.h Outdated Show resolved Hide resolved
@riversand963
Copy link
Contributor

How do you handle data in the WAL if avoid_flush_during_recovery is true? If possible, these data will remain in memtable.

Maybe a few options to consider:

  1. Declare that we do not currently support calling the new OpenAndTrim API if avoid_flush_during_recovery is true. Return NotSupported() or InvalidArgument() if user does so.
  2. Implement the trimming in flush code path.
  3. Perform trimming before inserting into memtable during recovery.

I think 1 is good for now, but add TODO for the other two.

@sunlike-Lipo
Copy link
Contributor Author

Thanks @riversand963 review. I will provide a new commits this weekend

 * Skip column families that do not enable user-defined timestamp,
 * Add validateOptions operation in OpenAndTrimHistory,
 * Modified some comments in the api to clarify that the trim operation
   of this api is only valid for the column family with timestamp
   enabled,
 * Added cleanup operation in case of failure, clean up db and column family
@sunlike-Lipo
Copy link
Contributor Author

sunlike-Lipo commented Feb 21, 2022

How do you handle data in the WAL if avoid_flush_during_recovery is true? If possible, these data will remain in memtable.

Maybe a few options to consider:

  1. Declare that we do not currently support calling the new OpenAndTrim API if avoid_flush_during_recovery is true. Return NotSupported() or InvalidArgument() if user does so.
  2. Implement the trimming in flush code path.
  3. Perform trimming before inserting into memtable during recovery.

I think 1 is good for now, but add TODO for the other two.

@riversand963 1 has been fixed, 2, 3 also added TODO.
btw, I think we should also add BottommostLevelCompaction::kForceOptimized, otherwise, the bottommost-level files may be skipped when CompactRangeInternal.
I also think we can skip files by its min_timestamp and max_timestamp before trimming history, to reduce the total io-cost. Only files with file.max_timestamp > trim_ts are necessary to trim. I will post another PR for optimization after this PR is merged.

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sunlike-Lipo for the PR. Left some comments and we are getting close.

include/rocksdb/db.h Outdated Show resolved Hide resolved
include/rocksdb/db.h Outdated Show resolved Hide resolved
db/db_impl/db_impl_open.cc Outdated Show resolved Hide resolved
db/db_impl/db_impl_open.cc Outdated Show resolved Hide resolved
db/db_impl/db_impl_open.cc Outdated Show resolved Hide resolved
db/history_trimming_iterator.h Outdated Show resolved Hide resolved
db/db_with_timestamp_basic_test.cc Outdated Show resolved Hide resolved
db/db_with_timestamp_basic_test.cc Outdated Show resolved Hide resolved
db/db_with_timestamp_basic_test.cc Show resolved Hide resolved
db/db_impl/db_impl_open.cc Show resolved Hide resolved
@facebook-github-bot
Copy link
Contributor

@riversand963 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Fix review comments
@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

@@ -25,7 +27,7 @@ class HistoryTrimmingIterator : public InternalIterator {
return true;
}
Slice current_ts = ExtractTimestampFromKey(key(), cmp_->timestamp_size());
return cmp_->CompareTimestamp(current_ts, Slice(filter_ts_)) < 0;
return cmp_->CompareTimestamp(current_ts, Slice(filter_ts_)) <= 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the API's definition, data with ts <= filter_ts_ should be reserved, I added a unittest for the = case in the last commit.

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sunlike-Lipo for the PR! LGTM. Can you resolve the conflicts and rebase?

include/rocksdb/db.h Outdated Show resolved Hide resolved
include/rocksdb/db.h Outdated Show resolved Hide resolved
@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

@sunlike-Lipo
Copy link
Contributor Author

sunlike-Lipo commented Feb 26, 2022

Thanks @sunlike-Lipo for the PR! LGTM. Can you resolve the conflicts and rebase?

@riversand963
I've fixed the conflicts in HISTORY.md.
Thank you for your review and guidance at last. I've learned a lot from that.

@facebook-github-bot
Copy link
Contributor

@riversand963 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@riversand963 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@sunlike-Lipo has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@riversand963 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants