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

Shared block cache in RocksJava #3623

Closed
wants to merge 3 commits into from

Conversation

sagar0
Copy link
Contributor

@sagar0 sagar0 commented Mar 16, 2018

Changes to support sharing block cache using the Java API.

Previously DB instances could share the block cache only when the same Options instance is passed to all the DB instances. But now, with this change, it is possible to explicitly create a cache and pass it to multiple options instances, to share the block cache.

Implementing this for Rocksandra, but this feature has been requested by many java api users over the years.

Test Plan:

  • Added a unit test to make sure shared block cache is working.
  • Added another simple test to make sure that setBlockCache() is working.

Sample code to show how it works:

import java.lang.IllegalArgumentException;

import org.rocksdb.*;
import org.rocksdb.util.SizeUnit;

public class SharedCacheSample
{
  static {
    RocksDB.loadLibrary();
  }

  public static void main( String[] args ) throws Exception {
    if (args.length < 2) {
      System.out.println("usage: SharedCacheSample db_path num_shards");
      System.exit(-1);
    }

    String baseDBPath = args[0];
    int numShards = 0;
    try {
      numShards = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
      System.out.println("Enter a good value for num_shards");
      throw e;
    }

    try {
      Statistics stats = new Statistics();
      Cache cache = new LRUCache(64 * SizeUnit.MB);
      for (int shard = 0; shard < numShards; shard++) {
        BlockBasedTableConfig table_options = new BlockBasedTableConfig();
        table_options.setBlockCache(cache);
        Options options = new Options();
        options.setCreateIfMissing(true)
               .setStatistics(stats)
               .setTableFormatConfig(table_options);

        String db_path = baseDBPath + "/" + shard;
        RocksDB db = null;
        db = RocksDB.open(options, db_path);
        db.put("hello".getBytes(), "world".getBytes());

        FlushOptions flushOptions = new FlushOptions();
        db.flush(flushOptions);

        byte[] value = db.get("hello".getBytes());
        assert ("world".equals(new String(value)));

        System.out.println(stats.getTickerCount(TickerType.BLOCK_CACHE_ADD));
      }
    } catch (IllegalArgumentException e) {
      assert (false);
    } catch (RocksDBException e) {
      System.out.format("[ERROR] caught an unexpected RocksDB exception -- %s\n", e);
      throw e;
      // assert (false);
    } catch (Exception e) {
      assert (false);
    }
  }
}

Build and Run:

javac -g -verbose -cp ~/rocksdb/java/target/rocksdbjni-5.12.0-linux64.jar SharedCacheSample.java
java -cp ~/rocksdb/java/target/rocksdbjni-5.12.0-linux64.jar:. SharedCacheSample /tmp/rocksjavabc 8
1
2
3
4
5
6
7
8  

In the above output, you can see the counter for the block cache is increasing even though they are different DB instances.

Note that all the block caches have the same memory address below:

svemuri@devbig187 ~ $ ls -l /tmp/rocksjavabc
total 32
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 0
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 1
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 2
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 3
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 4
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 5
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 6
drwxr-xr-x. 2 svemuri users 4096 Mar 16 09:54 7

svemuri@devbig187 ~ $ cat /tmp/rocksjavabc/*/LOG | egrep "\sblock_cache:"
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8
  block_cache: 0x7f214c50a2a8

svemuri@devbig187 ~ $ cat /tmp/rocksjavabc/*/LOG | egrep -A5 "\sblock_cache:"
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0
--
  block_cache: 0x7f214c50a2a8
  block_cache_name: LRUCache
  block_cache_options:
    capacity : 67108864
    num_shard_bits : 6
    strict_capacity_limit : 0

Also changed a couple of variable names to conform to c++ style.
@facebook-github-bot
Copy link
Contributor

@sagar0 has updated the pull request. View: changes

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

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

@benclay
Copy link
Contributor

benclay commented Mar 17, 2018

Nice. We've been using the same/copied Options trick, this will be cleaner.

@sagar0 sagar0 requested a review from adamretter March 19, 2018 21:30
@facebook-github-bot
Copy link
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

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

@wpc
Copy link
Contributor

wpc commented Mar 22, 2018

Read throught , PR looks solid good. Merge it? Can't wait test it in our cluster :-)

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@sagar0 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

sagar0 added a commit that referenced this pull request Mar 23, 2018
Summary:
Changes to support sharing block cache using the Java API.

Previously DB instances could share the block cache only when the same Options instance is passed to all the DB instances. But now, with this change, it is possible to explicitly create a cache and pass it to multiple options instances, to share the block cache.

Implementing this for [Rocksandra](https://github.com/instagram/cassandra/tree/rocks_3.0), but this feature has been requested by many java api users over the years.
Closes #3623

Differential Revision: D7305794

Pulled By: sagar0

fbshipit-source-id: 03e4e8ed7aeee6f88bada4a8365d4279ede2ad71
facebook-github-bot pushed a commit that referenced this pull request Mar 23, 2018
Summary:
We have not been updating our HISTORY.md change log with the RocksJava changes. Going forward, lets add Java changes also to HISTORY.md.
There is an old java/HISTORY-JAVA.md, but it hasn't been updated in years. It is much easier to remember to update the change log in a single file, HISTORY.md.

I added information about shared block cache here, which was introduced in #3623.
Closes #3647

Differential Revision: D7384448

Pulled By: sagar0

fbshipit-source-id: 9b6e569f44e6df5cb7ba06413d9975df0b517d20
@sagar0 sagar0 deleted the java-shared-blockcache branch March 23, 2018 21:49
sagar0 added a commit that referenced this pull request Mar 23, 2018
Summary:
Changes to support sharing block cache using the Java API.

Previously DB instances could share the block cache only when the same Options instance is passed to all the DB instances. But now, with this change, it is possible to explicitly create a cache and pass it to multiple options instances, to share the block cache.

Implementing this for [Rocksandra](https://github.com/instagram/cassandra/tree/rocks_3.0), but this feature has been requested by many java api users over the years.
Closes #3623

Differential Revision: D7305794

Pulled By: sagar0

fbshipit-source-id: 03e4e8ed7aeee6f88bada4a8365d4279ede2ad71
sagar0 added a commit that referenced this pull request Mar 23, 2018
Summary:
We have not been updating our HISTORY.md change log with the RocksJava changes. Going forward, lets add Java changes also to HISTORY.md.
There is an old java/HISTORY-JAVA.md, but it hasn't been updated in years. It is much easier to remember to update the change log in a single file, HISTORY.md.

I added information about shared block cache here, which was introduced in #3623.
Closes #3647

Differential Revision: D7384448

Pulled By: sagar0

fbshipit-source-id: 9b6e569f44e6df5cb7ba06413d9975df0b517d20
DikangGu pushed a commit to DikangGu/rocksdb that referenced this pull request Mar 30, 2018
Summary:
Changes to support sharing block cache using the Java API.

Previously DB instances could share the block cache only when the same Options instance is passed to all the DB instances. But now, with this change, it is possible to explicitly create a cache and pass it to multiple options instances, to share the block cache.

Implementing this for [Rocksandra](https://github.com/instagram/cassandra/tree/rocks_3.0), but this feature has been requested by many java api users over the years.
Closes facebook#3623

Differential Revision: D7305794

Pulled By: sagar0

fbshipit-source-id: 03e4e8ed7aeee6f88bada4a8365d4279ede2ad71
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.

None yet

4 participants