第45章 競合マネージャーの使用方法

45.1. キャッシュの競合の検索および解決

競合マネージャー (Conflict Manager) は通常 パーティション処理 (Partition Handling) とともに使用されます。スプリットブレインは、クラスターのノードがお互いに通信できない複数のグループ (パーティション) に分割されると発生します。場合によっては、ノードに異なるデータを書き込むことが可能になります。このような場合、JBoss Data Grid のパーティション処理を競合マネージャーとともに使用すると、ノード全体で同じ CacheEntries の違いを自動的に解決することができます。競合マネージャーは、手動による競合の検索および解決にも使用できます。

以下のコードは、EmbeddedCacheManagerConflictManager を取得する方法、指定キーのバージョンをすべて取得する方法、および指定キャッシュ全体で競合をチェックする方法を示しています。

EmbeddedCacheManager manager = new DefaultCacheManager("example-config.xml");
Cache<Integer, String> cache = manager.getCache("testCache");
ConflictManager<Integer, String> crm = ConflictManagerFactory.get(cache.getAdvancedCache());

// Get All Versions of Key
Map<Address, InternalCacheValue<String>> versions = crm.getAllVersions(1);

// Process conflicts stream and perform some operation on the cache
Stream<Map<Address, InternalCacheEntry<Integer, String>>> stream = crm.getConflicts();
stream.forEach(map -> {
   CacheEntry<Object, Object> entry = map.values().iterator().next();
   Object conflictKey = entry.getKey();
   cache.remove(conflictKey);
});

// Detect and then resolve conflicts using the configured EntryMergePolicy
crm.resolveConflicts();

// Detect and then resolve conflicts using the passed EntryMergePolicy instance
crm.resolveConflicts((preferredEntry, otherEntries) -> preferredEntry);
注記

ConflictManager::getConflicts ストリームはエントリーごとに処理されますが、基盤のスプリッテレーター (spliterator) はセグメントごとにキャッシュエントリーをレイジーにロードします。