Skip to content

Commit

Permalink
pongasoft#63: added metadata and tags to initParameters
Browse files Browse the repository at this point in the history
fixed couple of issues (in bounce/redeploy)
  • Loading branch information
ypujante committed Jun 12, 2011
1 parent 4f6fdd9 commit 921baa2
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class AgentsServiceImpl implements AgentsService, AgentURIProvider
return info.getURI()
}

@Override
URI findAgentURI(String fabric, String agent)
{
return trackerService.getAgentInfo(fabric, agent)?.URI
}

def getAllInfosWithAccuracy(Fabric fabric)
{
return trackerService.getAllInfosWithAccuracy(fabric)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,44 @@ class DeploymentServiceImpl implements DeploymentService
private Collection<Plan<ActionDescriptor>> computeDeploymentPlans(params,
def metadata,
Closure closure)
{
computeDeploymentPlans(params, metadata, null, null, closure)
}

/**
* Compute deployment plans by doing the following:
* <ol>
* <li>compute delta between expected model (params.system) and current model (computed)
* <li>compute the deployment plan(s) (closure callback) (use params.type if a given type only
* is required
* <li>set various metadata on the plan(s) as well as the name
* </ol>
* @return a collection of plans (<code>null</code> if no expected model) which may be empty
*/
private Collection<Plan<ActionDescriptor>> computeDeploymentPlans(params,
def metadata,
def expectedModelFilter,
def currentModelFiter,
Closure closure)
{
SystemModel expectedModel = params.system

if(!expectedModel)
return null

if(expectedModelFilter)
expectedModel = expectedModel.filterBy(expectedModelFilter)

Fabric fabric = fabricService.findFabric(expectedModel.fabric)

if(!fabric)
throw new IllegalArgumentException("unknown fabric ${expectedModel.fabric}")

SystemModel currentModel = agentsService.getCurrentSystemModel(fabric)

if(currentModelFiter)
currentModel = currentModel.filterBy(currentModelFiter)

computeDeploymentPlans(params, expectedModel, currentModel, metadata, closure)
}

Expand Down Expand Up @@ -198,24 +223,21 @@ class DeploymentServiceImpl implements DeploymentService
}
}

// we filter by entries where the current state is 'running' or 'stopped'
private def bounceCurrentModelFilter = { SystemEntry entry ->
entry.entryState == 'running' || entry.entryState == 'stopped'
}

/**
* Compute a bounce plan to bounce (= stop/start) containers.
* @param metadata any metadata to add to the plan(s)
*/
Collection<Plan<ActionDescriptor>> computeBouncePlans(params, def metadata)
{
SystemModel expectedModel = params.system

if(!expectedModel)
return null

// we filter by entries where the 'expectedState' should be 'running'!
expectedModel = expectedModel.filterBy { SystemEntry entry ->
entry.entryState == 'running'
}
params.system = expectedModel

computeDeploymentPlans(params, metadata) { Type type, SystemModelDelta delta ->
computeDeploymentPlans(params,
metadata,
null,
bounceCurrentModelFilter) { Type type, SystemModelDelta delta ->
planner.computeTransitionPlan(type, delta, ['stopped', 'running'])
}
}
Expand All @@ -231,13 +253,22 @@ class DeploymentServiceImpl implements DeploymentService
}
}

// the purpose of this filter is to exclude entries that should not be deployed in the first
// place (should not be part of 'redeploy' plan!
private def redeployCurrentModelFilter = { SystemEntry entry ->
true
}

/**
* Compute a redeploy plan (= undeploy/deploy).
* @param metadata any metadata to add to the plan(s)
*/
Collection<Plan<ActionDescriptor>> computeRedeployPlans(params, def metadata)
{
computeDeploymentPlans(params, metadata) { Type type, SystemModelDelta delta ->
computeDeploymentPlans(params,
metadata,
null,
redeployCurrentModelFilter) { Type type, SystemModelDelta delta ->
planner.computeTransitionPlan(type, delta, [null, '<expected>'])
}
}
Expand All @@ -251,18 +282,15 @@ class DeploymentServiceImpl implements DeploymentService
{
SystemModel currentModel = agentsService.getCurrentSystemModel(params.fabric)
def agents = (params.agents ?: []) as Set
currentModel = currentModel.filterBy { SystemEntry entry ->
def filteredCurrentModel = currentModel.filterBy { SystemEntry entry ->
agents.contains(entry.agent)
}

// we keep only the agents that are part of the current model!
agents = new HashSet()
currentModel.each { SystemEntry entry ->
filteredCurrentModel.each { SystemEntry entry ->
agents << entry.agent
}
currentModel = currentModel.filterBy { SystemEntry entry ->
entry.mountPoint == DeploymentService.AGENT_SELF_UPGRADE_MOUNT_POINT
}

SystemModel expectedModel = new SystemModel(fabric: currentModel.fabric)
agents.each { String agent ->
Expand All @@ -277,30 +305,32 @@ class DeploymentServiceImpl implements DeploymentService
expectedModel.addEntry(entry)
}

expectedModel = expectedModel.filterBy { SystemEntry entry ->
entry.mountPoint == DeploymentService.AGENT_SELF_UPGRADE_MOUNT_POINT
}

computeDeploymentPlans(params, expectedModel, currentModel, metadata) { Type type, SystemModelDelta delta ->
planner.computeTransitionPlan(type, delta, ['<expected>', null])
}
}

private def agentsCleanupUpgradeExpectedModelFilter = { SystemEntry entry ->
entry.mountPoint == DeploymentService.AGENT_SELF_UPGRADE_MOUNT_POINT
}

/**
* Computes the deployment plan for cleaning any upgrade that failed
* @param metadata any metadata to add to the plan(s)
*/
@Override
Collection<Plan<ActionDescriptor>> computeAgentsCleanupUpgradePlan(params, def metadata)
{
SystemModel expectedModel = params.system

if(!expectedModel)
return null

// we filter by entries with only self upgrade mountpoint
expectedModel = expectedModel.filterBy { SystemEntry entry ->
entry.mountPoint == DeploymentService.AGENT_SELF_UPGRADE_MOUNT_POINT
computeDeploymentPlans(params,
metadata,
agentsCleanupUpgradeExpectedModelFilter,
null) { Type type, SystemModelDelta delta ->
planner.computeDeploymentPlan(type, delta)
}
params.system = expectedModel

computeDeployPlans(params, metadata)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public interface AgentURIProvider
* @throws NoSuchAgentException if agent cannot be found
*/
URI getAgentURI(String fabric, String agent) throws NoSuchAgentException;

/**
* @return the agent uri given the agent name or <code>null</code> if not found (no exception!)
*/
URI findAgentURI(String fabric, String agent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.linkedin.glu.orchestration.engine.delta.SystemEntryValueWithDelta;
import org.linkedin.glu.orchestration.engine.delta.SystemModelDelta;
import org.linkedin.glu.provisioner.core.model.SystemEntry;
import org.linkedin.glu.provisioner.core.model.SystemFilter;
import org.linkedin.glu.provisioner.core.model.SystemModel;
import org.linkedin.groovy.util.state.StateMachine;
import org.linkedin.util.annotations.Initializer;
Expand Down Expand Up @@ -90,8 +91,8 @@ public SystemModelDelta computeDelta(SystemModel filteredExpectedModel,
filteredCurrentModel);

// 1. we get all the keys that will be part of the delta
Set<String> filteredKeys = SystemModel.filterKeys(filteredExpectedModel,
filteredCurrentModel);
Set<String> filteredKeys = computeFilteredKeys(filteredExpectedModel,
filteredCurrentModel);

// 2. we compute the dependencies (on the unfiltered model) which may bring back some keys
// that were filtered out in step 1.
Expand Down Expand Up @@ -137,6 +138,48 @@ public SystemModelDelta computeDelta(SystemModel filteredExpectedModel,
return systemModelDelta;
}

/**
* Compute the set of keys that will be part of the delta: the filters set on the model will
* be use as filter
*/
protected Set<String> computeFilteredKeys(SystemModel filteredExpectedModel,
SystemModel filteredCurrentModel)
{
Set<String> keys = new HashSet<String>();

SystemFilter currentModelFilters = filteredCurrentModel.getFilters();
SystemFilter expectedModelFilters = filteredExpectedModel.getFilters();

// 1. we make sure to exclude all entries from the current model where that were filtered
// out from the expected model
filteredCurrentModel = filteredCurrentModel.filterBy(expectedModelFilters);
Set<String> currentKeys = filteredCurrentModel.getKeys(new HashSet<String>());

// 2. we make sure to exclude all entries from the expected model where that were filtered
// out from the current model
filteredExpectedModel = filteredExpectedModel.filterBy(currentModelFilters);
Set<String> expectedKeys = filteredExpectedModel.getKeys(new HashSet<String>());

// if a filter was provided for the current model then we take only the keys from the
// current model (excluding the one not in the expected model!)
if(currentModelFilters != null)
{
for(String key : currentKeys)
{
if(expectedKeys.contains(key))
keys.add(key);
}
}
else
{
// otherwise we take keys from both
keys.addAll(currentKeys);
keys.addAll(expectedKeys);
}

return keys;
}

protected void adjustDeltaFromDependencies(InternalSystemModelDelta systemModelDelta)
{
// we start from the parents
Expand All @@ -150,13 +193,14 @@ protected void adjustDeltaFromDependencies(InternalSystemModelDelta systemModelD
systemModelDelta.findExpectedChildrenEntryDelta(key);

String parentExpectedState = parentEntryDelta.getExpectedEntryState();
int parentExpectedDepth = computeDepth(stateMachine, parentExpectedState);

for(InternalSystemEntryDelta childEntryDelta : expectedChildrenEntryDelta)
{
int childDepth =
stateMachine.getDepth(childEntryDelta.getExpectedEntryState());
if(childDepth > stateMachine.getDepth(parentExpectedState))
parentExpectedState = childEntryDelta.getExpectedEntryState();
String childExpectedEntryState = childEntryDelta.getExpectedEntryState();
int childDepth = computeDepth(stateMachine, childExpectedEntryState);
if(childDepth > parentExpectedDepth)
parentExpectedState = childExpectedEntryState;
}

adjustDeltaFromDependencies(parentEntryDelta, parentExpectedState);
Expand All @@ -177,6 +221,14 @@ protected void adjustDeltaFromDependencies(InternalSystemModelDelta systemModelD
}
}

private int computeDepth(StateMachine stateMachine, String state)
{
if(state == null)
return -1;
else
return stateMachine.getDepth(state);
}

protected void adjustDeltaFromDependencies(InternalSystemEntryDelta systemEntryDelta,
String expectedState)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* @author yan@pongasoft.com
Expand Down Expand Up @@ -118,6 +120,7 @@ protected Collection<Transition> collectLinearTransitions(String entryKey,
}

@Override
@SuppressWarnings("unchecked")
public void addSteps(ICompositeStepBuilder<ActionDescriptor> builder)
{
InternalSystemEntryDelta entryDelta = getSystemEntryDelta();
Expand All @@ -134,7 +137,17 @@ public void addSteps(ICompositeStepBuilder<ActionDescriptor> builder)
if(!expectedEntry.isDefaultParent())
ad.setParent(expectedEntry.getParent());
ad.setScript(expectedEntry.getScript());
ad.setInitParameters((Map) expectedEntry.getInitParameters());
Map initParameters = (Map) expectedEntry.getInitParameters();
if(initParameters == null)
initParameters = new TreeMap();
else
initParameters = new TreeMap(initParameters);
if(!expectedEntry.getMetadata().isEmpty())
initParameters.put("metadata", expectedEntry.getMetadata());
if(expectedEntry.hasTags())
initParameters.put("tags", expectedEntry.getTags());
if(!initParameters.isEmpty())
ad.setInitParameters(initParameters);
actionDescriptor = ad;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.linkedin.glu.orchestration.engine.action.descriptor.ActionDescriptor;
import org.linkedin.glu.orchestration.engine.action.descriptor.ActionDescriptorAdjuster;
import org.linkedin.glu.orchestration.engine.action.descriptor.AgentURIProvider;
import org.linkedin.glu.orchestration.engine.agents.NoSuchAgentException;
import org.linkedin.glu.orchestration.engine.delta.SystemEntryDelta;
import org.linkedin.glu.orchestration.engine.delta.impl.InternalSystemEntryDelta;
import org.linkedin.glu.orchestration.engine.delta.impl.InternalSystemModelDelta;
Expand Down Expand Up @@ -253,12 +252,8 @@ protected Transition createTransition(InternalSystemEntryDelta entryDelta,
}
if(_agentURIProvider != null)
{
try
{
_agentURIProvider.getAgentURI(getSystemModelDelta().getFabric(),
entryDelta.getAgent());
}
catch(NoSuchAgentException e)
if(_agentURIProvider.findAgentURI(getSystemModelDelta().getFabric(),
entryDelta.getAgent()) == null)
{
return new MissingAgentTransition(this,
key,
Expand Down
Loading

0 comments on commit 921baa2

Please sign in to comment.