Annotation Type AffinityKeyMapped
-
@Documented @Retention(RUNTIME) @Target(FIELD) public @interface AffinityKeyMapped
Optional annotation to specify custom key-to-node affinity. Affinity key is a key which will be used to determine a node on which given cache key will be stored. This annotation allows to mark a field in the cache key object that will be used as an affinity key (instead of the entire cache key object that is used for affinity by default). Note that a class can have only one field annotated with@AffinityKeyMappedannotation.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.Mapping Cache Keys
The default implementation ofAffinityKeyMapper, which will be used if no explicit affinity mapper is specified in cache configuration, will first look for any field annotated with@AffinityKeyMappedannotation. If such field 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 is found, then the value of this field 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
Personobject is always accessed together with aCompanyobject for which this person is an employee, then for better performance and scalability it makes sense to collocatePersonobjects together with theirCompanyobject when storing them in cache. To achieve that, cache key used to cachePersonobjects should have a field or method annotated with@AffinityKeyMappedannotation, 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. @AffinityKeyMapped 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(..));AffinityKey
For convenience, you can also optionally useAffinityKeyclass. Here is how aPersonKeydefined above would look usingAffinityKey:Object personKey1 = new AffinityKey("myPersonId1", "myCompanyId"); Object personKey2 = new AffinityKey("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(..));Collocating Computations And Data
It is also possible to route computations to the nodes where the data is cached. This concept is otherwise known asCollocation Of Computations And Data. In this case,@AffinityKeyMappedannotation allows to specify a routing affinity key for aComputeJobor any other grid computation, such asRunnable,Callable, orIgniteClosure. It should be attached to a field that provides affinity key for the computation. Only one annotation per class is allowed. Whenever such annotation is detected, thenLoadBalancingSpiwill be bypassed, and computation will be routed to the grid node where the specified affinity key is cached.For more information about cache affinity also see
AffinityKeyMapperandAffinityFunctiondocumentation. 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.- See Also:
AffinityFunction,AffinityKeyMapper,AffinityKey