facebook / facebook/rocksdb

Key are not deleted after TTL expire

Open
#12,116 14 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
32.1k
Forks
6.9k
Avg merge
32m
Merged PRs (30d)
1

Description

### Expected behavior
when using TTL , expected behavior is that the key/value entry will be deleted when TTL is expire

### Actual behavior
Executed a simple program that put key/value entry into rocks db , set TTL to be 10 seconds , stop thread for 20 seconds
key/value entry is not deleted

### Question
use case I looking for is to set TTL , and that when TTL expire key/value entry will be deleted automaticly , base only the time entry "sit" in database , is this supported on Rocks-db ?
can you please provide a configuration sample that will do that ?

### Steps to reproduce the behavior

`package com.rocksdb.sample;

import org.rocksdb.CompactionOptionsFIFO;
import org.rocksdb.Env;
import org.rocksdb.Options;
import org.rocksdb.RocksDB;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.rocksdb.util.SizeUnit.MB;

public class RocksDbTTLSample {

private static RocksDB rockdDb = null;

public static void main(String[] args) {

try {
RocksDB.loadLibrary();
Options options = new Options();
options.setCreateIfMissing(true);
options.setEnv(Env.getDefault());

//fifo related
CompactionOptionsFIFO compactionOptionsFIFO = new CompactionOptionsFIFO();
compactionOptionsFIFO.setAllowCompaction(true);
compactionOptionsFIFO.setMaxTableFilesSize(500 * MB);
options.setCompactionOptionsFIFO(compactionOptionsFIFO);

options.setTtl(10L);
System.out.println("set TTL to 10 seconds");
options.setPeriodicCompactionSeconds(10);
options.setMaxBackgroundJobs(4);
options.setMaxOpenFiles(-1);

System.out.println("Rocks-db - done configuration");
rockdDb = RocksDB.open(options, "/tmp/myRocksDb");
System.out.println("Rocks-db is started");

//put key value entry
Charset charset = StandardCharsets.US_ASCII;
String key = "myKey";
String value = "myValue";
rockdDb.put(key.getBytes(charset), value.getBytes(charset));
System.out.println("saved key/value in rocks db for key - " + key + " with value " + value);
System.out.println("waiting for 20 second for apply the TTL and key eviction");
Thread.sleep(20000);

//get value
byte[] valueOfMyKey = rockdDb.get(key.getBytes(charset));

if (valueOfMyKey.length > 0) {
System.out.println("found value for key - " + key);
System.out.println("TTL not work as expected as key/value entry is not evicted from db");
}
} catch (Exception e) {
System.out.println("error rocks db");
e.printStackTrace();
throw new RuntimeException(e);
} finally {
rockdDb.close();
}
}
}`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.