-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] BROOKLYN-535: restarter policy when entity stopping #829
Open
aledsage
wants to merge
2
commits into
apache:master
Choose a base branch
from
aledsage:BROOKLYN-534-restarter-whenStopping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,11 @@ | |
import org.apache.brooklyn.api.sensor.SensorEventListener; | ||
import org.apache.brooklyn.config.ConfigKey; | ||
import org.apache.brooklyn.core.config.ConfigKeys; | ||
import org.apache.brooklyn.core.entity.Attributes; | ||
import org.apache.brooklyn.core.entity.Entities; | ||
import org.apache.brooklyn.core.entity.EntityInternal; | ||
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; | ||
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle.Transition; | ||
import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; | ||
import org.apache.brooklyn.core.entity.trait.Startable; | ||
import org.apache.brooklyn.core.policy.AbstractPolicy; | ||
|
@@ -133,6 +135,10 @@ protected synchronized void onDetectedFailure(SensorEvent<Object> event) { | |
LOG.warn("ServiceRestarter suspended, so not acting on failure detected at "+entity+" ("+event.getValue()+")"); | ||
return; | ||
} | ||
if (isEntityStopping()) { | ||
highlightViolation("Failure detected but entity stopping"); | ||
LOG.info("Entity stopping, so ServiceRestarter not acting on failure detected at "+entity+" ("+event.getValue()+")"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are missing a |
||
} | ||
|
||
LOG.warn("ServiceRestarter acting on failure detected at "+entity+" ("+event.getValue()+")"); | ||
long current = System.currentTimeMillis(); | ||
|
@@ -154,6 +160,13 @@ protected synchronized void onDetectedFailure(SensorEvent<Object> event) { | |
} | ||
} | ||
|
||
protected boolean isEntityStopping() { | ||
if (entity == null) return false; | ||
Transition expectedState = entity.sensors().get(Attributes.SERVICE_STATE_EXPECTED); | ||
Lifecycle state = (expectedState != null) ? expectedState.getState() : null; | ||
return (state == Lifecycle.STOPPING) || (state == Lifecycle.STOPPED) || (state == Lifecycle.DESTROYED); | ||
} | ||
|
||
protected void onRestartFailed(String msg) { | ||
LOG.warn("ServiceRestarter failed for "+entity+": "+msg); | ||
if (getConfig(SET_ON_FIRE_ON_FAILURE)) { | ||
|
136 changes: 136 additions & 0 deletions
136
...pache/brooklyn/entity/software/base/test/restarting/ServiceRestarterPolicyErrorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.brooklyn.entity.software.base.test.restarting; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.brooklyn.api.entity.EntitySpec; | ||
import org.apache.brooklyn.api.location.Location; | ||
import org.apache.brooklyn.api.location.LocationSpec; | ||
import org.apache.brooklyn.api.location.MachineLocation; | ||
import org.apache.brooklyn.api.policy.PolicySpec; | ||
import org.apache.brooklyn.api.sensor.EnricherSpec; | ||
import org.apache.brooklyn.config.ConfigKey; | ||
import org.apache.brooklyn.core.config.ConfigKeys; | ||
import org.apache.brooklyn.core.entity.Attributes; | ||
import org.apache.brooklyn.core.entity.BrooklynConfigKeys; | ||
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; | ||
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle.Transition; | ||
import org.apache.brooklyn.core.location.LocationPredicates; | ||
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; | ||
import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; | ||
import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; | ||
import org.apache.brooklyn.location.ssh.SshMachineLocation; | ||
import org.apache.brooklyn.policy.ha.ServiceFailureDetector; | ||
import org.apache.brooklyn.policy.ha.ServiceRestarter; | ||
import org.apache.brooklyn.test.Asserts; | ||
import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; | ||
import org.apache.brooklyn.util.exceptions.Exceptions; | ||
import org.apache.brooklyn.util.time.Duration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.reflect.TypeToken; | ||
|
||
public class ServiceRestarterPolicyErrorsTest extends BrooklynAppUnitTestSupport { | ||
|
||
@SuppressWarnings("unused") | ||
private static final Logger log = LoggerFactory.getLogger(ServiceRestarterPolicyErrorsTest.class); | ||
|
||
// See https://issues.apache.org/jira/browse/BROOKLYN-534 | ||
@Test | ||
public void testRestarterWhenStopFails() throws Exception { | ||
List<LocationSpec<? extends MachineLocation>> machineSpecs = ImmutableList.of( | ||
LocationSpec.create(SshMachineLocation.class).displayName("machine1").configure("address", "1.1.1.1"), | ||
LocationSpec.create(SshMachineLocation.class).displayName("machine2").configure("address", "1.1.1.2")); | ||
FailingMachineProvisioningLocation failingLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(FailingMachineProvisioningLocation.class) | ||
.configure(SshMachineLocation.SSH_TOOL_CLASS, RecordingSshTool.class.getName()) | ||
.configure(FailingMachineProvisioningLocation.MACHINE_SPECS, machineSpecs) | ||
.configure(FailingMachineProvisioningLocation.FAIL_ON_RELEASE, true)); | ||
|
||
EmptySoftwareProcess entity = app.createAndManageChild(EntitySpec.create(EmptySoftwareProcess.class) | ||
.configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true) | ||
.configure(EmptySoftwareProcess.USE_SSH_MONITORING, false) | ||
.policy(PolicySpec.create(ServiceRestarter.class)) | ||
.enricher(EnricherSpec.create(ServiceFailureDetector.class) | ||
.configure(ServiceFailureDetector.ENTITY_FAILED_STABILIZATION_DELAY, Duration.ZERO))); | ||
app.start(ImmutableList.<Location>of(failingLoc)); | ||
|
||
try { | ||
entity.stop(); | ||
Asserts.shouldHaveFailedPreviously(); | ||
} catch (Exception e) { | ||
Asserts.expectedFailureContains(e, "Simulating failure of release"); | ||
} | ||
|
||
// As reported in https://issues.apache.org/jira/browse/BROOKLYN-534, previously the stop-failure | ||
// would cause the ServiceFailureDetector to emit a failure event, which would cause the ServiceRestarter | ||
// to call start() again, which would make the entity obtain a new machine! | ||
Asserts.succeedsContinually(new Runnable() { | ||
public void run() { | ||
Transition serviceStateExpected = entity.sensors().get(Attributes.SERVICE_STATE_EXPECTED); | ||
Iterable<MachineLocation> entityMachines = Iterables.filter(entity.getLocations(), MachineLocation.class); | ||
Optional<MachineLocation> availableMachine2 = Iterables.tryFind(failingLoc.getAvailable(), LocationPredicates.displayNameEqualTo("machine2")); | ||
String errMsg = "entityStateExpected="+serviceStateExpected+"; entityMachines="+entityMachines+"; availableMachine2="+availableMachine2; | ||
assertEquals(serviceStateExpected.getState(), Lifecycle.STOPPED, errMsg); | ||
assertTrue(Iterables.isEmpty(entityMachines), errMsg); | ||
assertTrue(availableMachine2.isPresent(), errMsg); | ||
}}); | ||
} | ||
|
||
public static class FailingMachineProvisioningLocation extends FixedListMachineProvisioningLocation<MachineLocation> { | ||
public static final ConfigKey<Boolean> FAIL_ON_RELEASE = ConfigKeys.newBooleanConfigKey("failOnRelease", "Whether to throw exception on call to release", false); | ||
|
||
@SuppressWarnings("serial") | ||
ConfigKey<Class<? extends Exception>> EXCEPTION_CLAZZ = ConfigKeys.newConfigKey( | ||
new TypeToken<Class<? extends Exception>>() {}, | ||
"exceptionClazz", | ||
"Type of exception to throw", | ||
IllegalStateException.class); | ||
|
||
@Override | ||
public void release(MachineLocation machine) { | ||
if (Boolean.TRUE.equals(config().get(FAIL_ON_RELEASE))) { | ||
throw newException("Simulating failure of release("+machine+")"); | ||
} else { | ||
super.release(machine); | ||
} | ||
} | ||
|
||
private RuntimeException newException(String msg) { | ||
try { | ||
Exception result = getConfig(EXCEPTION_CLAZZ).getConstructor(String.class).newInstance(msg); | ||
if (!(result instanceof RuntimeException)) { | ||
return new RuntimeException("wrapping", result); | ||
} else { | ||
return (RuntimeException)result; | ||
} | ||
} catch (Exception e) { | ||
throw Exceptions.propagate(e); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we highlight the violation here? I guess the question is more: is it really a violation when we are in a stopping state?