Call the location customizers on obtain error Adds preReleaseOnObtainError, postReleaseOnObtainError invoked when there's an exception during obtain.
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/96967fbd Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/96967fbd Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/96967fbd Branch: refs/heads/master Commit: 96967fbdb06fe1cc8725248cf5aa1ff15f5866e6 Parents: ec8caac Author: Svetoslav Neykov <[email protected]> Authored: Thu May 11 19:07:53 2017 +0300 Committer: Svetoslav Neykov <[email protected]> Committed: Tue Jun 13 16:17:38 2017 +0300 ---------------------------------------------------------------------- .../jclouds/BasicJcloudsLocationCustomizer.java | 14 +++- .../location/jclouds/JcloudsLocation.java | 15 ++++ .../jclouds/JcloudsLocationCustomizer.java | 18 ++++ .../jclouds/LocationCustomizerDelegate.java | 37 +++++++++ .../JcloudsSshMachineLocationStubbedTest.java | 87 ++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/96967fbd/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java index c410e5c..adeae73 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java @@ -22,6 +22,8 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; + import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntityInitializer; import org.apache.brooklyn.api.entity.EntityLocal; @@ -29,10 +31,8 @@ import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.entity.BrooklynConfigKeys; import org.apache.brooklyn.core.location.LocationConfigKeys; -import org.apache.brooklyn.core.mgmt.BrooklynTaskTags; import org.apache.brooklyn.core.objs.BasicConfigurableObject; import org.apache.brooklyn.util.core.config.ConfigBag; -import org.apache.brooklyn.util.core.task.Tasks; import org.jclouds.compute.ComputeService; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.Template; @@ -124,6 +124,16 @@ public class BasicJcloudsLocationCustomizer extends BasicConfigurableObject impl // no-op } + @Override + public void preReleaseOnObtainError(JcloudsLocation jcloudsLocation, @Nullable JcloudsMachineLocation machineLocation, Exception cause) { + // no-op + } + + @Override + public void postReleaseOnObtainError(JcloudsLocation jcloudsLocation, @Nullable JcloudsMachineLocation machineLocation, Exception cause) { + // no-op + } + /** * @return the calling entity */ http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/96967fbd/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java index 8b2f7ea..28b56e9 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java @@ -1055,6 +1055,13 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im + ": "+e.getMessage()); LOG.debug(Throwables.getStackTraceAsString(e)); + try { + customizersDelegate.preReleaseOnObtainError(this, machineLocation, e); + } catch (Exception customizerException) { + LOG.info("Got exception on calling customizer preReleaseOnObtainError, ignoring. Location is {}, machine location is {}, node is {}", + new Object[] {this, machineLocation, node, customizerException}); + } + if (destroyNode) { Stopwatch destroyingStopwatch = Stopwatch.createStarted(); if (machineLocation != null) { @@ -1064,6 +1071,14 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im } LOG.info("Destroyed " + (machineLocation != null ? "machine " + machineLocation : "node " + node) + " in " + Duration.of(destroyingStopwatch).toStringRounded()); + + try { + customizersDelegate.postReleaseOnObtainError(this, machineLocation, e); + } catch (Exception customizerException) { + LOG.debug("Got exception on calling customizer postReleaseOnObtainError, ignoring. Location is {}, machine Location is {}, node is {}", + new Object[] {this, machineLocation, node, customizerException}); + } + } throw Exceptions.propagate(e); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/96967fbd/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocationCustomizer.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocationCustomizer.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocationCustomizer.java index b61fce6..04a716e 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocationCustomizer.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocationCustomizer.java @@ -18,6 +18,9 @@ */ package org.apache.brooklyn.location.jclouds; +import javax.annotation.Nullable; + +import org.apache.brooklyn.core.location.cloud.CloudLocationConfig; import org.apache.brooklyn.util.core.config.ConfigBag; import org.jclouds.compute.ComputeService; import org.jclouds.compute.domain.NodeMetadata; @@ -87,4 +90,19 @@ public interface JcloudsLocationCustomizer { * Override to handle machine-related cleanup after Jclouds is called to release (destroy) the machine. */ void postRelease(JcloudsMachineLocation machine); + + /** + * Override to handle cleanup after failure to obtain a machine. Could be called as a result of an {@link InterruptedException} + * in which case return as quickly as possible. + */ + void preReleaseOnObtainError(JcloudsLocation location, @Nullable JcloudsMachineLocation machineLocation, Exception cause); + + /** + * <p> + * Override to handle cleanup after failure to obtain a machine. Called after releasing the locations' underlying node. + * Could be called as a result of an {@link InterruptedException} in which case return as quickly as possible. + * <p> + * Will be skipped if {@link CloudLocationConfig#DESTROY_ON_FAILURE} is set to {@code false}. + */ + void postReleaseOnObtainError(JcloudsLocation location, @Nullable JcloudsMachineLocation machineLocation, Exception cause); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/96967fbd/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java index b639472..b268e53 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java @@ -154,6 +154,43 @@ public class LocationCustomizerDelegate implements JcloudsLocationCustomizer { } } + @Override + public void preReleaseOnObtainError(JcloudsLocation location, JcloudsMachineLocation machineLocation, + Exception cause) { + Exception tothrow = null; + for (JcloudsLocationCustomizer customizer : customizers) { + try { + customizer.preReleaseOnObtainError(location, machineLocation, cause); + } catch (Exception e) { + LOG.error("Problem invoking customizer preReleaseOnObtainError for "+customizer+" for machine "+machineLocation+ + ", locaiton=" + location + "; ignoring and continuing, " + + (tothrow==null ? "will throw subsequently" : "swallowing due to previous error")+": "+e, e); + if (tothrow==null) tothrow = e; + } + } + if (tothrow != null) { + throw Exceptions.propagate(tothrow); + } + } + + @Override + public void postReleaseOnObtainError(JcloudsLocation location, JcloudsMachineLocation machineLocation, Exception cause) { + Exception tothrow = null; + for (JcloudsLocationCustomizer customizer : customizers) { + try { + customizer.postReleaseOnObtainError(location, machineLocation, cause); + } catch (Exception e) { + LOG.error("Problem invoking customizer postReleaseOnObtainError for "+customizer+" for machine "+machineLocation+ + ", locaiton=" + location + "; ignoring and continuing, " + + (tothrow==null ? "will throw subsequently" : "swallowing due to previous error")+": "+e, e); + if (tothrow==null) tothrow = e; + } + } + if (tothrow != null) { + throw Exceptions.propagate(tothrow); + } + } + @SuppressWarnings("deprecation") public static Collection<JcloudsLocationCustomizer> getCustomizers(ManagementContext mgmt, ConfigBag setup) { JcloudsLocationCustomizer customizer = setup.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZER); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/96967fbd/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocationStubbedTest.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocationStubbedTest.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocationStubbedTest.java index 5c7647a..c5b0ef0 100644 --- a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocationStubbedTest.java +++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocationStubbedTest.java @@ -21,19 +21,26 @@ package org.apache.brooklyn.location.jclouds; import static org.apache.brooklyn.location.jclouds.JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS; import static org.apache.brooklyn.util.core.internal.ssh.SshTool.ADDITIONAL_CONNECTION_METADATA; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.annotation.Nullable; import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.AbstractNodeCreator; import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.NodeCreator; import org.apache.brooklyn.location.ssh.SshMachineLocation; import org.apache.brooklyn.location.winrm.WinRmMachineLocation; +import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.util.core.config.ConfigBag; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; import org.apache.brooklyn.util.core.internal.ssh.SshTool; import org.apache.brooklyn.util.core.internal.winrm.WinRmTool; +import org.jclouds.compute.ComputeService; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; @@ -55,6 +62,13 @@ public class JcloudsSshMachineLocationStubbedTest extends AbstractJcloudsStubbed @SuppressWarnings("unused") private static final Logger log = LoggerFactory.getLogger(JcloudsImageChoiceStubbedLiveTest.class); + @SuppressWarnings("serial") + private static class FailObtainOnPurposeException extends RuntimeException { + public FailObtainOnPurposeException(String message) { + super(message); + } + } + private List<String> privateAddresses; private List<String> publicAddresses; @@ -139,4 +153,77 @@ public class JcloudsSshMachineLocationStubbedTest extends AbstractJcloudsStubbed Map<?, ?> lastConstructorProps = RecordingSshTool.getLastConstructorProps(); assertEquals(lastConstructorProps.get(ADDITIONAL_CONNECTION_METADATA.getName()), testMetadata); } + + @Test + public void testNodeObtainErrorCustomizer() throws Exception { + final AtomicBoolean calledPreRelease = new AtomicBoolean(); + final AtomicBoolean calledPostRelease = new AtomicBoolean(); + try { + obtainMachine(ImmutableMap.of( + JcloudsLocationConfig.MACHINE_CREATE_ATTEMPTS, 1, + JCLOUDS_LOCATION_CUSTOMIZERS, ImmutableList.of(new BasicJcloudsLocationCustomizer(){ + + @Override + public void customize(JcloudsLocation location, ComputeService computeService, + JcloudsMachineLocation machine) { + // failing with a node already created + throw new FailObtainOnPurposeException("testing obtain failure customizer callbacks"); + } + @Override + public void preReleaseOnObtainError(JcloudsLocation jcloudsLocation, + @Nullable JcloudsMachineLocation machineLocation, + Exception cause) { + calledPreRelease.set(true); + } + @Override + public void postReleaseOnObtainError(JcloudsLocation jcloudsLocation, + @Nullable JcloudsMachineLocation machineLocation, + Exception cause) { + calledPostRelease.set(true); + } + }))); + Asserts.shouldHaveFailedPreviously("Expected exception of type " + FailObtainOnPurposeException.class.getSimpleName()); + } catch (FailObtainOnPurposeException e) { + // expected + } + assertTrue(calledPreRelease.get(), "preReleaseOnObtainError not called on failed onObtain"); + assertTrue(calledPostRelease.get(), "postReleaseOnObtainError not called on failed onObtain"); + } + + @Test + public void testNodeObtainErrorCustomizerNoDestroy() throws Exception { + final AtomicBoolean calledPreRelease = new AtomicBoolean(); + final AtomicBoolean calledPostRelease = new AtomicBoolean(); + try { + obtainMachine(ImmutableMap.of( + JcloudsLocationConfig.MACHINE_CREATE_ATTEMPTS, 1, + JcloudsLocationConfig.DESTROY_ON_FAILURE, false, + JCLOUDS_LOCATION_CUSTOMIZERS, ImmutableList.of(new BasicJcloudsLocationCustomizer(){ + + @Override + public void customize(JcloudsLocation location, ComputeService computeService, + JcloudsMachineLocation machine) { + // failing with a node already created + throw new FailObtainOnPurposeException("testing obtain failure customizer callbacks"); + } + @Override + public void preReleaseOnObtainError(JcloudsLocation jcloudsLocation, + @Nullable JcloudsMachineLocation machineLocation, + Exception cause) { + calledPreRelease.set(true); + } + @Override + public void postReleaseOnObtainError(JcloudsLocation jcloudsLocation, + @Nullable JcloudsMachineLocation machineLocation, + Exception cause) { + calledPostRelease.set(true); + } + }))); + Asserts.shouldHaveFailedPreviously("Expected exception of type " + FailObtainOnPurposeException.class.getSimpleName()); + } catch (FailObtainOnPurposeException e) { + // expected + } + assertTrue(calledPreRelease.get(), "preReleaseOnObtainError not called on failed onObtain"); + assertFalse(calledPostRelease.get(), "postReleaseOnObtainError not to be called on failed onObtain when destroyOnFailure=false"); + } }
