@Documented @Retention(value=RUNTIME) @Target(value={FIELD,METHOD}) public @interface CacheAffinityKeyMapped
@CacheAffinityKeyMapped
annotation.
One of the major use cases for this annotation is the routing of grid computations
to the nodes where the data for this computation is cached, the concept
otherwise known as Collocation Of Computations And Data
.
CacheAffinityKeyMapper
, which will be used
if no explicit affinity mapper is specified in cache configuration, will first look
for any field or method annotated with @CacheAffinityKeyMapped
annotation.
If such field or method is not found, then the cache key itself will be used for
key-to-node affinity (this means that all objects with the same cache key will always
be routed to the same node). If such field or method is found, then the value of this
field or method will be used for key-to-node affinity. This allows to specify alternate
affinity key, other than the cache key itself, whenever needed.
For example, if a Person
object is always accessed together with a Company
object
for which this person is an employee, then for better performance and scalability it makes sense to
collocate Person
objects together with their Company
object when storing them in
cache. To achieve that, cache key used to cache Person
objects should have a field or method
annotated with @CacheAffinityKeyMapped
annotation, which will provide the value of
the company key for which that person works, like so:
public class PersonKey { // Person ID used to identify a person. private String personId; // Company ID which will be used for affinity. @CacheAffinityKeyMapped private String companyId; ... } ... // Instantiate person keys. Object personKey1 = new PersonKey("myPersonId1", "myCompanyId"); Object personKey2 = new PersonKey("myPersonId2", "myCompanyId"); // Both, the company and the person objects will be cached on the same node. cache.put("myCompanyId", new Company(..)); cache.put(personKey1, new Person(..)); cache.put(personKey2, new Person(..));
CacheAffinityKey
class. Here is how a
PersonKey
defined above would look using CacheAffinityKey
:
Object personKey1 = new CacheAffinityKey("myPersonId1", "myCompanyId"); Object personKey2 = new CacheAffinityKey("myPersonId2", "myCompanyId"); // Both, the company and the person objects will be cached on the same node. cache.put(myCompanyId, new Company(..)); cache.put(personKey1, new Person(..)); cache.put(personKey2, new Person(..));
Collocation Of Computations And Data
. In this case,
@CacheAffinityKeyMapped
annotation allows to specify a routing affinity key for a
ComputeJob
or any other grid computation, such as Runnable
, Callable
, or
IgniteClosure
. It should be attached to a method or field that provides affinity key
for the computation. Only one annotation per class is allowed. Whenever such annotation is detected,
then LoadBalancingSpi
will be bypassed, and computation will be routed to the grid node
where the specified affinity key is cached. You can also use optional @CacheName
annotation whenever non-default cache name needs to be specified.
Here is how this annotation can be used to route a job to a node where Person object is cached with ID "1234":
G.grid().run(new Runnable() { // This annotation is optional. If omitted, then default // no-name cache will be used. @CacheName private String cacheName = "myCache"; // This annotation specifies that computation should be routed // precisely to the node where key '1234' is cached. @CacheAffinityKeyMapped private String personKey = "1234"; @Override public void run() { // Some computation logic here. ... } };The same can be achieved by annotating method instead of field as follows:
G.grid().run(new Runnable() { @Override public void run() { // Some computation logic here. ... } // This annotation is optional. If omitted, then default // no-name cache will be used. @CacheName public String cacheName() { return "myCache"; } // This annotation specifies that computation should be routed // precisely to the node where key '1234' is cached. @CacheAffinityKeyMapped public String personKey() { return "1234"; } };
For more information about cache affinity also see CacheAffinityKeyMapper
and
CacheAffinityFunction
documentation.
Affinity for a key can be found from any node, regardless of whether it has cache started
or not. If cache is not started, affinity function will be fetched from the remote node
which does have the cache running.
org.apache.ignite.cache.CacheName
,
CacheAffinityFunction
,
CacheAffinityKeyMapper
,
CacheAffinityKey
Follow @ApacheIgnite
Apache Ignite Fabric : ver. 1.0.0-RC1 Release Date : February 16 2015