darin
darin

Reputation: 86

How to initiate warming when a cache is created?

I'd like to to use an Ignite cluster to warm a PARTITIONED cache from an existing database. The existing database is not partitioned and expensive to scan, so I'd like to perform a single scan when the cache is created by the cluster. Once the job completes, the result would be a cache containing all data from the existing database partitioned and evenly distributed across the cluster.

How do you implement a job that runs when a cache is created by Ignite?

Upvotes: 2

Views: 192

Answers (2)

darin
darin

Reputation: 86

You can create a Service that runs once on cluster start and then cancels itself. It can use a cache to store state, so it will not run if it's deployed in the cluster a second time.

The following abstract Service runs executeOnce once per cluster the first time it's deployed after cluster start:

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

}

Upvotes: 1

Valentin Kulichenko
Valentin Kulichenko

Reputation: 8390

Ignite integrates with underlying stores via CacheStore [1] implementations. Refer to [2] for details about your particular use case.

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

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

Upvotes: 2

Related Questions