jgehrcke / timegaps

Accept or reject items based on age categorization (command line program, runs on Python 2/3, Unix, and Windows).
Other
49 stars 8 forks source link

Keep last n items #5

Open OE2WNL opened 6 years ago

OE2WNL commented 6 years ago

Usecase - productive ZFS server makes snapshots via cronjob every 0 and 30 minutes after the hour and only once a day these snapshots are transfered offsite to another server with zfs send command.

After transfering snapshots to offsite system I would like to clear some snapshots on productive system. But I must assure to keep (in any case) the last snapshot for the next incremental zfs send - so a keeplast1 would be needed.

Another usecase from this would be that on the productive Server I would like to assure to keep the latest 10 snapshots in any case (independed of creation time). Everything after the 10 recent snapshots can be thinned out with hours,days,weeks etc.

So categorie keeplast would be helpfull.

Probably a helpful source would be the program Restic Backup I use Restic for some other backups and that's where I'm used to have a keep-last parameter.

Again ;-) interessted in your thoughts. Greetings Wolfgang

bhelm commented 6 years ago

I initially thought this is what "recent" is about. I thought wrong. After 2,2TB and a successful snapshot transfer, it chose to delete the last and only source snapshot :(

After some testing, i think "recent" should be named "hours".

+1 for keeplast implementation. I would call it "latest" and rename recent to hours.

jgehrcke commented 5 years ago

Thanks for the feedback Wolfgang and Bernd!

I agree that the paradigm of "keep the last N" is intuitively useful. In fact, that is what I thought initially. But things seem to be a little more complicated than that.

The following example hopefully shows that simply keeping the last N can result in data loss, too, when invoking timegaps very frequently.

An item younger than 1 hour is in the "recent" category.

Let's imagine timegaps was to keep the 5 most recent (as you wrote, the 5 last or newest items) in that category.

Let's imagine items are created at a high rate, e.g. every two minutes. Let's consider the case where timegaps is invoked at a high rate, too (many times per hour, regularly or irregularly).

In this example scenario items would be deleted before they get to be 1 hour old. Propagation of the older items would not happen; and timegaps would break one of its promises.

It is precisely this effect which made me switch to the "keep the oldest items" paradigm. I seem to have thought about that a few years ago and wrote this into the TODO file:

For recent items, we should also keep the older ones so that it is safe to invoke timegaps at any time and at any frequency. In the extreme case this will reject the newest backup and only keep the almost-one-hour-old one, but this is predictable and can be reliably worked around by the user. If we rejected the oldest recent items, however, sudden frequent invocation of item creation and timegaps would reject older items. Instead, sudden frequent invocation of item creation and timegaps should reject the unnecessary amount of newer items.

Got that from here: https://github.com/jgehrcke/timegaps/blob/fffaf133654447ec4b7e05b4c7035074f20b2fc8/TODO#L29 (thanks to past Jan-Philip for writing some things down)

Now, you brought up very valid concerns. What can we do?

What if we were to keep the oldest N and the newest M in the "recent" category (again, this is where all items are in that are younger than 1 hour)? That would ensure backpropagation of the oldest items. In an extreme case it could still rip a hole of <1 hour into the item distribution, though. In terms of thinning data out this should be desired. However, if that is a problem in any workflow the only viable solution is to add another time category with a more fine-grained resolution than 1 hour.

This is just me thinking out loud!

jgehrcke commented 5 years ago

Related: https://github.com/jgehrcke/timegaps/issues/3#issuecomment-44063851

Another relevant thought: not thinning out in the "recent" category should be an option. Is it currently?