bluestore使用的cache

bluestore 自己管理裸设备为了提高性能自己也建立了cache

BlueStore::Cache *BlueStore::Cache::create(CephContext* cct, string type,
					   PerfCounters *logger)
{
  Cache *c = nullptr;

  if (type == "lru")
    c = new LRUCache(cct);
  else if (type == "2q")
    c = new TwoQCache(cct);
  else
    assert(0 == "unrecognized cache type");

  c->logger = logger;
  return c;
}
从bluestore中可以看到 目前cache 分为两类,一类是lru,一类是2q。默认使用的是2q的实现。
bluestore中调用set_cache_shards 来初始化cache,在bluestore的构造函数中会调用 set_cache_shards(1);
void BlueStore::set_cache_shards(unsigned num)
{
  dout(10) << __func__ << " " << num << dendl;
  size_t old = cache_shards.size();
  assert(num >= old);
  cache_shards.resize(num);
  for (unsigned i = old; i < num; ++i) {
    cache_shards[i] = Cache::create(cct, cct->_conf->bluestore_cache_type,
				    logger);
  }
}
从这段code 可以知道系统可以存在多个cache,具体是采用lru还是2q的话,是由ceph的cct->_conf->bluestore_cache_type 这个选项来决定的
int BlueStore::_create_collection(
  TransContext *txc,
  const coll_t &cid,
  unsigned bits,
  CollectionRef *c)
{
  dout(15) << __func__ << " " << cid << " bits " << bits << dendl;
  int r;
  bufferlist bl;

  {
    RWLock::WLocker l(coll_lock);
    if (*c) {
      r = -EEXIST;
      goto out;
    }
	#bluestore在创建collection的时候制定了一个cache。
    c->reset(
      new Collection(
	this,
	cache_shards[cid.hash_to_shard(cache_shards.size())],
	cid));
    (*c)->cnode.bits = bits;
    coll_map[cid] = *c;
  }
}
BlueStore::Collection::Collection(BlueStore *ns, Cache *c, coll_t cid)
  : store(ns),
    cache(c),
    cid(cid),
    lock("BlueStore::Collection::lock", true, false),
    exists(true),
    onode_map(c)
{
}
从Collection的构造函数可以看到这里将cache赋值给onode_map
BlueStore::OnodeRef BlueStore::Collection::get_onode(
  const ghobject_t& oid,
  bool create)
{
  OnodeRef o = onode_map.lookup(oid);
  if (o)
    return o;

  mempool::bluestore_cache_other::string key;
  get_object_key(store->cct, oid, &key);
  return onode_map.add(oid, o);
}
这样在get_onode 中在读取get_onode的时候首先在onode_map中查找,如果有的话,就直接返回了,没有在cache中找到的话
,则找到后加入到cache中
与此同时在bluestore中有新建一个线程来定义回收cache占用的memory
void *BlueStore::MempoolThread::entry()
{
  Mutex::Locker l(lock);
  while (!stop) {
    for (auto i : store->cache_shards) {
	#调用2q的trim函数来回收内存
      i->trim(shard_target,
	      store->cache_meta_ratio,
	      store->cache_data_ratio,
	      bytes_per_onode);
    }

    store->_update_cache_logger();

    utime_t wait;
    wait += store->cct->_conf->bluestore_cache_trim_interval;
	#定期wakeup 唤醒来执行trim
    cond.WaitInterval(lock, wait);
  }
  stop = false;
  return NULL;
}
void BlueStore::TwoQCache::_trim(uint64_t onode_max, uint64_t buffer_max)
{
  dout(20) << __func__ << " onodes " << onode_lru.size() << " / " << onode_max
	   << " buffers " << buffer_bytes << " / " << buffer_max
	   << dendl;

   // onodes
   #如果cache占用的size小于最大的size,就不用回收了
  int num = onode_lru.size() - onode_max;
  if (num <= 0)
    return; // don't even try
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JetCache 是一个基于 Java 的开源缓存库,它提供了对方法级缓存的支持。使用 JetCache 可以方便地将方法的结果缓存起来,提高应用程序的性能和响应速度。 下面是使用 JetCache 的基本步骤: 1. 添加 JetCache 依赖:将 JetCache 的依赖项添加到项目的构建文件中(如 Maven 或 Gradle)。 2. 配置缓存:在应用程序的配置文件中,配置需要使用的缓存类型和相关属性。例如,可以配置内存缓存、Redis 缓存等。 3. 注解方法:在需要进行缓存的方法上添加 JetCache 的注解,如 `@Cached`、`@CacheRemove` 等。这些注解可以指定缓存的 key、过期时间、条件等。 4. 使用缓存:在调用被注解的方法时,JetCache 会根据注解的配置自动处理缓存。如果缓存中存在所需数据,则直接返回缓存数据;否则,执行方法并将结果放入缓存。 下面是一个简单的示例: ```java import io.github.jiashunx.cache.Cache; import io.github.jiashunx.cache.annotation.Cached; public class MyService { private Cache<String, String> cache; // 构造函数或依赖注入注入 Cache 实例 @Cached(name = "myCache", key = "#param", expire = 600) public String getData(String param) { // 从数据库或其他数据源中获取数据 // ... return data; } } ``` 在上述示例中,`MyService` 使用了 JetCache 的 `@Cached` 注解对 `getData` 方法进行了缓存配置。缓存的名称为 "myCache",缓存的 key 使用方法参数 `param`,缓存的过期时间为 600 秒。当调用 `getData` 方法时,JetCache 会自动处理缓存逻辑,如果缓存中存在对应的数据,则直接返回缓存数据;否则,执行方法并将结果放入缓存。 这只是 JetCache 的基本用法,JetCache 还提供了其他更复杂的缓存策略和配置选项,可以根据具体需求进行配置和使用。 希望这个回答对您有帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值