项目作者: dstieglitz

项目描述 :
Grails plugin for Apache Ignite
高级语言: Groovy
项目地址: git://github.com/dstieglitz/grails-ignite.git
创建时间: 2015-06-06T21:28:07Z
项目社区:https://github.com/dstieglitz/grails-ignite

开源协议:Apache License 2.0

下载


Grails Ignite Plugin

A plugin that provides basic Grails integration with the Apache Ignite compute grid framework.

Branch structure

  • master compatible with Grails 2.x
  • grails3 compatible with Grails 3.x
  • grails4 compatible with Grails 4.0.x

Supported Features

Under Development

  • Clustered Hibernate L2 Caching

Grid Bean

The plugin provides a configured instance of the Ingite grid as a bean called “grid”, which you can access via injection in controllers and services:

  1. def grid

Configuration

In order to support hibernate l2 caching, which requires the Ignite grid to be started prior to the sessionFactory and therefore the vast majority of Grails artifacts, Ignite must be configured from external configuration files.

The external files must be referenced in the ignite configuration block in Config.groovy:

  1. ignite {
  2. enabled=true
  3. config.locations = [
  4. "file:ignite/conf/*.groovy"
  5. ]
  6. gridName="myGrid"
  7. /*
  8. * This setting must be enabled on the machine that builds the WAR file for the target environment,
  9. * since it will determine if the correct filters are incorporated into the web.xml file
  10. */
  11. webSessionClusteringEnabled=true
  12. /*
  13. * Configure timeouts for long-running jobs. If not specified, defaults to 60000
  14. */
  15. defaultJobTimeout = 60000
  16. /**
  17. * Set of Ant paths to exclude from web session clustering logic
  18. */
  19. webSessionClusteringPathExcludes = ['*/api/**','*/otherExclude/']
  20. /**
  21. * Enable distributed hibernate l2 caching
  22. * You must also set the region factory correctly
  23. */
  24. l2CacheEnabled=true
  25. /**
  26. * DEFAULTS; you can also configure individual caches as spring beans
  27. */
  28. l2Cache {
  29. entity {
  30. evictionPolicy = 'lru'
  31. cacheMode = CacheMode.PARTITIONED
  32. memoryMode = CacheMemoryMode.OFFHEAP_TIERED
  33. atomicityMode = CacheAtomicityMode.TRANSACTIONAL
  34. writeSynchronizationMode = CacheWriteSynchronizationMode.FULL_ASYNC
  35. offHeapMaxMemory = (1024L * 1024L * 1024L) * 0.25;
  36. maxElements = 1000
  37. swapEnabled = false
  38. statisticsEnabled = true
  39. managementEnabled = true
  40. }
  41. association {
  42. evictionPolicy = 'lru'
  43. cacheMode = CacheMode.PARTITIONED
  44. memoryMode = CacheMemoryMode.OFFHEAP_TIERED
  45. atomicityMode = CacheAtomicityMode.TRANSACTIONAL
  46. writeSynchronizationMode = CacheWriteSynchronizationMode.FULL_ASYNC
  47. offHeapMaxMemory = (1024L * 1024L * 1024L) * 0.25;
  48. maxElements = 1000
  49. swapEnabled = false
  50. statisticsEnabled = true
  51. managementEnabled = true
  52. }
  53. }
  54. peerClassLoadingEnabled=false
  55. discoverySpi {
  56. networkTimeout = 5000
  57. addresses = ["${myIP}:47500..47509"]
  58. }
  59. }

The files can be located anywhere but in the example above we have put them under ignite/conf in the project root. In a production deployment they would likely be in a completely different directory.

The configuration files follow the standard Ignite spring configuration conventions, however they must (for the time being) be expressed as Grails Spring DSL files for use with a BeanBuilder.

See the ignite/conf directory for sample configuration files. For basic configuration you can copy the directory to your project.

Startup

In order to facilitate the correct startup order of the Ignite grid (to support hibernate L2 caching it must start before the SessionFactory, which in turn is started by Grails before the application), Ignite uses a special startup hook called IgniteStartupHelper. The IgniteStartupHelper class will load it’s own application context specifically for the Ignite grid. This application context can be managed using the Spring DSL by specifying beans in *Resources.groovy files in the ignite/conf directory of your application, or by specifying external config directories for these files:

  1. ignite {
  2. enabled=true
  3. config.locations = [
  4. "file:ignite/conf/*.groovy"
  5. ]
  6. gridName="myGrid"

Discovery

Ignite can be configured to discover nodes via static, multicast or s3 discovery mechanisms. You need to include the correct ignite spring resources in your project and set the proper Grails configuration.

See IgniteResources.groovy

The example below shows how to configure discovery via the Grails configuration:

  1. discoverySpi {
  2. s3Discovery = true
  3. awsAccessKey = <ACCESS KEY WITH S3 PERMISSIONS>
  4. awsSecretKey = <SECRET KEY FOR ACCESS KEY>
  5. s3DiscoveryBucketName = "ignite-localdev"
  6. /* OR */
  7. multicastDiscovery = false
  8. networkTimeout = 5000
  9. addresses = ["${myIP}:47500..47509".toString()]
  10. /* OR */
  11. // use static IP discovery
  12. addresses = ['1.2.3.4','5.6.7.8']
  13. }

Logging

The project contains an implementation of IgniteLogger for use with Grails. This class allows you to use the Grails log4j DSL to configure logging for the embedded Ignite node. The logger can be configured from the Ignite spring bean:

  1. gridLogger(org.grails.ignite.IgniteGrailsLogger)
  2. igniteCfg(IgniteConfiguration) {
  3. gridLogger = ref('gridLogger')
  4. }

Distributed Hibernate L2 Caching

Requires Hibernate 4

A basic functional version of distributed Hibernate L2 caching can be utilized by setting the region factory class as follows:

  1. hibernate {
  2. cache.region.factory_class = 'org.grails.ignite.HibernateRegionFactory'
  3. org.apache.ignite.hibernate.grid_name = '<MY GRID NAME>'
  4. org.apache.ignite.hibernate.default_access_type = 'READ_ONLY' // see Ignite docs
  5. }

By default, the plugin will create caches with reasonable defaults (whatever defaults existing when using Ignite.getOrCreateCache()) on demand when Hibernate configures the regions. You can override these defaults by creating the appropriate CacheConfiguration beans in IgniteCacheResources.groovy

Example Spring Cache Configuration

In grails-app/ignite/conf/resources/IgniteCacheResources.groovy:

  1. 'com.mypackage.MyDomainClass' { bean ->
  2. name = 'com.package.MyDomainClass'
  3. cacheMode = CacheMode.PARTITIONED
  4. atomicityMode = CacheAtomicityMode.TRANSACTIONAL
  5. writeSynchronizationMode = CacheWriteSynchronizationMode.FULL_SYNC
  6. evictionPolicy = { org.apache.ignite.cache.eviction.lru.LruEvictionPolicy policy ->
  7. maxSize = 1000000
  8. }
  9. }

See Also:

http://apacheignite.gridgain.org/v1.1/docs/evictions

Scheduled, Distributed Tasks

This plugin provides an Ignite service called DistributedSchedulerService that provides a partial implementation of the ScheduledThreadPoolExectutor interface but allows you to run the submitted jobs on the Ignite grid.

The methods scheduleAtFixedRate and scheduleWithFixedDelay are currently implemented. The service keeps track of submitted job schedules using a grid-aware Set that is configured for REPLICATED caching, so that if any grid node goes down.

A Grails service of the same name (“DistributedSchedulerService“) is also provided to facilitiate easy injection into other Grails applications.

Example

  1. distributedSchedulerService.scheduleAtFixedRate(new HelloWorldGroovyTask(), 0, 1000, TimeUnit.MILLISECONDS);

This example shows how to schedule the supplied task to execute once per second on the entire grid, regardless of the grid topology. The execution will be evenly load-balanced across all grid nodes. If any grid nodes go down the rebalancing will result in the same execution rate (once per second in this example).

The example above can be run out-of-the-box (the HelloWorldGroovyTask is included in the plugin). You can then try neat things like spinning up another instance on a different port, and watching the grid fail-over and recover by killing one instance and bringing it back up.

Notes

Requires h2 version 1.3.137 (or higher)? Make sure you do this:

  1. inherits("global") {
  2. // specify dependency exclusions here; for example, uncomment this to disable ehcache:
  3. // excludes 'ehcache'
  4. excludes 'h2'
  5. }