如何在创建缓存时启动升温?

时间:2016-11-01 23:03:31

标签: ignite

我想使用Ignite群集来加热现有数据库中的PARTITIONED缓存。现有数据库没有分区且扫描成本高,因此我希望在群集创建缓存时执行单次扫描。作业完成后,结果将是一个缓存,其中包含来自现有数据库的所有数据,这些数据在分区中均匀分布并均匀分布。

如何实现Ignite创建缓存时运行的作业?

2 个答案:

答案 0 :(得分:2)

Ignite通过CacheStore [1]实现与底层商店集成。有关特定用例的详细信息,请参阅[2]。

[1] https://apacheignite.readme.io/docs/persistent-store

[2] https://apacheignite.readme.io/docs/data-loading

答案 1 :(得分:1)

您可以创建在群集启动时运行一次的Service,然后取消自身。它可以使用缓存来存储状态,因此如果它第二次部署在集群中,它将无法运行。

以下抽象服务在群集启动后第一次部署时每个群集运行一次executeOnce

abstract class ExecuteOnceService extends Service {

  val ExecuteOnceCacheName = "_execute_once_service"

  val config = new CacheConfiguration[String, java.lang.Boolean](ExecuteOnceCacheName)
    .setCacheMode(CacheMode.PARTITIONED)
    .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)

  @IgniteInstanceResource
  var ignite: Ignite = _

  override def execute(ctx: ServiceContext): Unit = {
    val cache = ignite.getOrCreateCache(config)
    val executed = cache.getAndPutIfAbsent(ctx.name(), java.lang.Boolean.TRUE)
    if (executed != java.lang.Boolean.TRUE) executeOnce(ctx)
    ignite.services().cancel(ctx.name())
  }

  def executeOnce(ctx: ServiceContext): Unit

}