private void tryDestroy() { try { boolean destroyed = reference.destroy(); if (destroyed) { LOGGER.debug("Destroyed lock entity " + reference.getName()); } } catch (EntityNotProvidedException e) { throw new AssertionError(e); } catch (EntityNotFoundException e) { // Nothing to do } catch (PermanentEntityException e) { LOGGER.error("Failed to destroy lock entity - server says it is permanent", e); throw new AssertionError(e); } }
@Override public void destroy() throws EntityNotFoundException, EntityBusyException { EntityRef<E, C, U> ref = getEntityRef(); try { if (!ref.destroy()) { throw new EntityBusyException("Destroy operation failed; " + entityIdentifier + " cluster tier in use by other clients"); } } catch (EntityNotProvidedException e) { LOGGER.error("Unable to destroy entity {} for id {}", entityType.getName(), entityIdentifier, e); throw new AssertionError(e); } catch (PermanentEntityException e) { LOGGER.error("Unable to destroy entity - server says it is permanent", e); throw new AssertionError(e); } }
public void destroyClusteredStoreEntity(String clusterTierManagerIdentifier, String storeIdentifier) throws EntityNotFoundException, CachePersistenceException { EntityRef<InternalClusterTierClientEntity, ClusterTierEntityConfiguration, Void> entityRef; try { entityRef = connection.getEntityRef(InternalClusterTierClientEntity.class, ENTITY_VERSION, entityName(clusterTierManagerIdentifier, storeIdentifier)); if (!entityRef.destroy()) { throw new CachePersistenceException("Cannot destroy cluster tier '" + storeIdentifier + "': in use by other client(s)"); } } catch (EntityNotProvidedException | PermanentEntityException e) { throw new AssertionError(e); } }
public void destroy(final String identifier) throws EntityBusyException { Hold existingMaintenance = maintenanceHolds.get(identifier); try(Hold localMaintenance = (existingMaintenance == null ? createAccessLockFor(identifier).tryWriteLock() : null)) { if (localMaintenance == null && existingMaintenance == null) { throw new EntityBusyException("Unable to obtain maintenance lease for " + identifier); } EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, ClusterTierUserData> ref = getEntityRef(identifier); destroyAllClusterTiers(ref, identifier); try { if (!ref.destroy()) { throw new EntityBusyException("Destroy operation failed; " + identifier + " cluster tier in use by other clients"); } } catch (EntityNotProvidedException e) { LOGGER.error("Unable to delete cluster tier manager for id {}", identifier, e); throw new AssertionError(e); } catch (EntityNotFoundException e) { // Ignore - entity does not exist } catch (PermanentEntityException e) { LOGGER.error("Unable to destroy entity - server says it is permanent", e); throw new AssertionError(e); } } }
@Test @SuppressWarnings("unchecked") public void testDestroyWhenNotExisting() throws Exception { when(entityRef.fetchEntity(null)).thenThrow(EntityNotFoundException.class); doThrow(EntityNotFoundException.class).when(entityRef).destroy(); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(entityRef); addMockUnlockedLock(connection, "VoltronReadWriteLock-ClusterTierManagerClientEntityFactory-AccessLock-test"); ClusterTierManagerClientEntityFactory factory = new ClusterTierManagerClientEntityFactory(connection); factory.destroy("test"); verify(entityRef).destroy(); }
@Test public void testDestroy() throws Exception { ClusterTierManagerClientEntity mockEntity = mock(ClusterTierManagerClientEntity.class); when(entityRef.fetchEntity(null)).thenReturn(mockEntity); doReturn(Boolean.TRUE).when(entityRef).destroy(); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(entityRef); addMockUnlockedLock(connection, "VoltronReadWriteLock-ClusterTierManagerClientEntityFactory-AccessLock-test"); ClusterTierManagerClientEntityFactory factory = new ClusterTierManagerClientEntityFactory(connection); factory.destroy("test"); verify(entityRef).destroy(); }
@Test public void testTryWriteUnlockDestroysEntity() throws Exception { when(client.tryLock(WRITE)).thenReturn(true); when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.tryWriteLock().unlock(); verify(entityRef).destroy(); }
@Test public void testDestroyAllFullyMocked() throws Exception { mockLockForWriteLockSuccess(); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(managerEntityRef); ClusterTierManagerClientEntity managerEntity = mock(ClusterTierManagerClientEntity.class); when(managerEntityRef.fetchEntity(null)).thenReturn(managerEntity); Set<String> stores = new HashSet<>(); stores.add("store1"); stores.add("store2"); when(managerEntity.prepareForDestroy()).thenReturn(stores); when(getEntityRef(InternalClusterTierClientEntity.class)).thenReturn(tierEntityRef); when(tierEntityRef.destroy()).thenReturn(true); when(managerEntityRef.destroy()).thenReturn(true); DefaultClusteringService service = new DefaultClusteringService(new ClusteringServiceConfiguration(URI .create("mock://localhost/whatever"))); service.startForMaintenance(null, MaintainableService.MaintenanceScope.CACHE_MANAGER); service.destroyAll(); verify(managerEntity).prepareForDestroy(); verify(tierEntityRef, times(2)).destroy(); verify(managerEntityRef).destroy(); }
@Test public void testTryReadUnlockDestroysEntity() throws Exception { when(client.tryLock(READ)).thenReturn(true); when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.tryReadLock().unlock(); verify(entityRef).destroy(); }
@Test public void testDestroyOnPartialDestroyState() throws Exception { mockLockForWriteLockSuccess(); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(managerEntityRef); ClusterTierManagerClientEntity managerEntity = mock(ClusterTierManagerClientEntity.class); when(managerEntityRef.fetchEntity(null)).thenReturn(managerEntity); doThrow(new DestroyInProgressException("destroying")).when(managerEntity).validate(any()); Set<String> stores = new HashSet<>(); stores.add("store1"); stores.add("store2"); when(managerEntity.prepareForDestroy()).thenReturn(stores); when(getEntityRef(InternalClusterTierClientEntity.class)).thenReturn(tierEntityRef); when(tierEntityRef.destroy()).thenReturn(true); when(managerEntityRef.destroy()).thenReturn(true); DefaultClusteringService service = new DefaultClusteringService(new ClusteringServiceConfiguration(URI .create("mock://localhost/whatever"))); service.startForMaintenance(null, MaintainableService.MaintenanceScope.CACHE_MANAGER); service.destroyAll(); verify(managerEntity).prepareForDestroy(); verify(tierEntityRef, times(2)).destroy(); verify(managerEntityRef).destroy(); }
@Test public void testWriteUnlockDestroysEntity() throws Exception { when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.writeLock().unlock(); verify(entityRef).destroy(); }
@Test public void testReadUnlockDestroysEntity() throws Exception { when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.readLock().unlock(); verify(entityRef).destroy(); }
@Test public void testTryReadLockFailingDestroysEntity() throws Exception { when(client.tryLock(READ)).thenReturn(false); when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); assertThat(lock.tryReadLock(), nullValue()); verify(entityRef).destroy(); } }
@Before public void cleanup() throws Exception { try (Connection connection = ConnectionFactory.connect(TEST_URI, new Properties())) { EntityRef<VoltronReadWriteLockClient, Void, Void> ref = getEntityReference(connection); try { assertThat(ref.destroy(), is(true)); } catch (EntityNotFoundException e) { //expected } } }
@Test public void testTryWriteLockFailingDestroysEntity() throws Exception { when(client.tryLock(WRITE)).thenReturn(false); when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); assertThat(lock.tryWriteLock(), nullValue()); verify(entityRef).destroy(); }
@Test public void testAbsentEntityDestroyFails() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); try { ref.destroy(); fail("Expected EntityNotFoundException"); } catch (EntityNotFoundException e) { //expected } } }
@Test public void testDestroyedEntityAllowsRecreation() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.destroy(); ref.create(blankConfiguration); assertThat(ref.fetchEntity(null), not(nullValue())); } }
@Test public void testPresentEntityDestroyNotBlockedByReleasedReferenceSucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.fetchEntity(null).close(); ref.destroy(); } }
@Test @Ignore @SuppressWarnings("try") public void testPresentEntityDestroyBlockedByHeldReferenceSucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); try (ClusterTierManagerClientEntity entity = ref.fetchEntity(null)) { ref.destroy(); } } }
@Test public void testPresentEntityDestroySucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.destroy(); try { ref.fetchEntity(null); fail("Expected EntityNotFoundException"); } catch (EntityNotFoundException e) { //expected } } }