예제 #1
0
  /**
   * Extract the BinaryStore out of Modeshape (infinspan, jdbc, file, transient, etc)
   *
   * @return
   */
  @Override
  public Map<String, String> apply(final Repository input) {
    checkNotNull(input, "null cannot have a BinaryStore!");

    final Map<String, String> result = new LinkedHashMap<>();
    final BinaryStore store = getBinaryStore.apply(input);

    if (!(store instanceof InfinispanBinaryStore)) {
      return result;
    }

    final InfinispanBinaryStore ispnStore = (InfinispanBinaryStore) store;

    final List<Cache<?, ?>> caches = ispnStore.getCaches();
    final DefaultCacheManager cm = (DefaultCacheManager) caches.get(0).getCacheManager();

    if (cm == null) {
      LOGGER.debug("Could not get cluster configuration information");
      return result;
    }

    final int nodeView;

    if (cm.getTransport() != null) {
      nodeView = cm.getTransport().getViewId() + 1;
    } else {
      nodeView = UNKNOWN_NODE_VIEW;
    }

    result.put(CLUSTER_NAME, cm.getClusterName());
    result.put(
        CACHE_MODE, cm.getCache().getCacheConfiguration().clustering().cacheMode().toString());
    result.put(NODE_ADDRESS, cm.getNodeAddress());
    result.put(PHYSICAL_ADDRESS, cm.getPhysicalAddresses());
    result.put(NODE_VIEW, nodeView == UNKNOWN_NODE_VIEW ? "Unknown" : String.valueOf(nodeView));
    result.put(CLUSTER_SIZE, String.valueOf(cm.getClusterSize()));
    result.put(CLUSTER_MEMBERS, cm.getClusterMembers());
    return result;
  }
  @SuppressWarnings("unchecked")
  @Test
  public void shouldRetrieveLowLevelCacheStoresForCompositeStore() throws Exception {

    mockStatic(ServiceHelpers.class);

    final Cache<?, ?> ispnCache1 = mock(Cache.class);
    final Cache<?, ?> ispnCache2 = mock(Cache.class);
    final CacheStore ispnCacheStore1 = mock(CacheStore.class);
    final CacheStore ispnCacheStore2 = mock(CacheStore.class);
    final BinaryStore plainBinaryStore = mock(BinaryStore.class);
    final BinaryStore plainBinaryStore2 = mock(BinaryStore.class);

    final GetCacheStore mockCacheStoreFunc = mock(GetCacheStore.class);
    when(mockCacheStoreFunc.apply(ispnCache1)).thenReturn(ispnCacheStore1);
    when(mockCacheStoreFunc.apply(ispnCache2)).thenReturn(ispnCacheStore2);

    final CompositeBinaryStore mockStore = mock(CompositeBinaryStore.class);

    final HashMap<String, BinaryStore> map = new HashMap<>();
    final List<Cache<?, ?>> caches = new ArrayList<>();
    caches.add(ispnCache1);
    caches.add(ispnCache2);

    map.put("default", plainBinaryStore);
    map.put("a", plainBinaryStore2);
    final InfinispanBinaryStore infinispanBinaryStore = mock(InfinispanBinaryStore.class);
    when(infinispanBinaryStore.getCaches()).thenReturn(caches);
    map.put("b", infinispanBinaryStore);
    when(mockStore.getNamedStoreIterator()).thenReturn(map.entrySet().iterator());

    final DistributedExecutorService mockCluster = mock(DistributedExecutorService.class);
    when(getClusterExecutor(infinispanBinaryStore)).thenReturn(mockCluster);

    final BinaryKey key = new BinaryKey("key-123");

    final LowLevelCacheEntry cacheEntry1 = new CacheStoreEntry(ispnCacheStore1, "cache1", key);
    final Set<LowLevelCacheEntry> cacheResponse1 = of(cacheEntry1);
    final LowLevelCacheEntry cacheEntry2 = new CacheStoreEntry(ispnCacheStore2, "cache2", key);
    final Set<LowLevelCacheEntry> cacheResponse2 = of(cacheEntry2);
    final Future<Collection<LowLevelCacheEntry>> future1 = mock(Future.class);
    final Future<Collection<LowLevelCacheEntry>> future2 = mock(Future.class);
    when(future1.get(any(Long.class), eq(MILLISECONDS))).thenReturn(cacheResponse1);
    when(future2.get(any(Long.class), eq(MILLISECONDS))).thenReturn(cacheResponse2);

    final List<Future<?>> mockClusterResults = new ArrayList<>(2);
    mockClusterResults.add(future1);
    mockClusterResults.add(future2);

    when(mockCluster.submitEverywhere(any(CacheLocalTransform.class)))
        .thenReturn(mockClusterResults);

    final LowLevelStorageService testObj = new LowLevelStorageService();

    when(plainBinaryStore.hasBinary(key)).thenReturn(true);
    when(plainBinaryStore2.hasBinary(key)).thenReturn(false);
    when(infinispanBinaryStore.hasBinary(key)).thenReturn(true);
    final Set<LowLevelCacheEntry> entries =
        testObj.getLowLevelCacheEntriesFromStore(mockStore, key);

    assertEquals(3, entries.size());

    assertTrue(entries.contains(new LocalBinaryStoreEntry(plainBinaryStore, key)));
    assertFalse(entries.contains(new LocalBinaryStoreEntry(plainBinaryStore2, key)));
    assertTrue(entries.contains(new CacheStoreEntry(ispnCacheStore1, "cache1", key)));
    assertTrue(entries.contains(new CacheStoreEntry(ispnCacheStore2, "cache2", key)));
  }