Questo contenuto non è disponibile nella lingua selezionata.

30.5. Key Affinity Service


The key affinity service allows a value to be placed in a certain node in a distributed Red Hat JBoss Data Grid cluster. The service returns a key that is hashed to a particular node based on a supplied cluster address identifying it.
The keys returned by the key affinity service cannot hold any meaning, such as a username. These are only random identifiers that are used throughout the application for this record. The provided key generators do not guarantee that the keys returned by this service are unique. For custom key format, you can pass your own implementation of KeyGenerator.
The following is an example of how to obtain and use a reference to this service.

Example 30.2. Key Affinity Service

EmbeddedCacheManager cacheManager = getCacheManager();
Cache cache = cacheManager.getCache();
KeyAffinityService keyAffinityService = 
        KeyAffinityServiceFactory.newLocalKeyAffinityService(
            cache,
            new RndKeyGenerator(),
            Executors.newSingleThreadExecutor(),
            100);
Object localKey = keyAffinityService.getKeyForAddress(cacheManager.getAddress());
cache.put(localKey, "yourValue");
Copy to Clipboard Toggle word wrap
The following procedure is an explanation of the provided example.

Procedure 30.3. Using the Key Affinity Service

  1. Obtain a reference to a cache manager and cache.
  2. This starts the service, then uses the supplied Executor to generate and queue keys.
  3. Obtain a key from the service which will be mapped to the local node (cacheManager.getAddress() returns the local address).
  4. The entry with a key obtained from the KeyAffinityService is always stored on the node with the provided address. In this case, it is the local node.

30.5.1. Lifecycle

KeyAffinityService extends Lifecycle, which allows the key affinity service to be stopped, started, and restarted.

Example 30.3. Key Affinity Service Lifecycle Parameter

public interface Lifecycle {  
     void start();   
     void stop();
}
Copy to Clipboard Toggle word wrap
The service is instantiated through the KeyAffinityServiceFactory. All factory methods have an Executor, that is used for asynchronous key generation, so that this does not occur in the caller's thread. The user controls the shutting down of this Executor.
The KeyAffinityService must be explicitly stopped when it is no longer required. This stops the background key generation, and releases other held resources. The KeyAffinityServce will only stop itself when the cache manager with which it is registered is shut down.

30.5.2. Topology Changes

KeyAffinityService key ownership may change when a topology change occurs. The key affinity service monitors topology changes and updates so that it doesn't return stale keys, or keys that would map to a different node than the one specified. However, this does not guarantee that a node affinity hasn't changed when a key is used. For example:
  1. Thread (T1) reads a key (K1) that maps to a node (A).
  2. A topology change occurs, resulting in K1 mapping to node B.
  3. T1 uses K1 to add something to the cache. At this point, K1 maps to B, a different node to the one requested at the time of read.
The above scenario is a not ideal, however it is a supported behavior for the application, as the keys that are already in use may be moved over during cluster change. The KeyAffinityService provides an access proximity optimization for stable clusters, which does not apply during the instability of topology changes.
Torna in cima
Red Hat logoGithubredditYoutubeTwitter

Formazione

Prova, acquista e vendi

Community

Informazioni sulla documentazione di Red Hat

Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi. Esplora i nostri ultimi aggiornamenti.

Rendiamo l’open source più inclusivo

Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita il Blog di Red Hat.

Informazioni su Red Hat

Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.

Theme

© 2025 Red Hat