Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

XD-1036: Composed module deletion #390

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,59 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.xd.dirt.module;

import java.util.Set;

import org.springframework.xd.dirt.core.XDRuntimeException;
import org.springframework.xd.module.ModuleType;


/**
* Thrown when performing an action cannot be carried over because some dependency would be broken.
*
* @author Eric Bottard
*/
@SuppressWarnings("serial")
public class /* Module? */DependencyException extends XDRuntimeException {

private final Set<String> dependents;

private final String name;

private final ModuleType type;

public DependencyException(String message, String name, ModuleType type, Set<String> dependents) {
super(String.format(message, name, type, dependents));
this.name = name;
this.type = type;
this.dependents = dependents;
}

public Set<String> getDependents() {
return dependents;
}

public String getName() {
return name;
}

public ModuleType getType() {
return type;
}


}
@@ -0,0 +1,48 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.xd.dirt.module;

import java.util.Set;

import org.springframework.xd.module.ModuleType;


/**
* Used to track usage of modules from streams and composed modules.
*
* @author Eric Bottard
*/
public interface ModuleDependencyRepository {

/**
* Store one atomic dependency from a module (composed or not) to some target (stream, or composed module).
*/
void store(String moduleName, ModuleType type, /* BaseDefinition? */String target);

/**
* Return the set of things that depend on the given module.
*
* @return a set of Strings of the form {@code type:name}, never {@code null}
*/
Set</* BaseDefinition? */String> findDependents(String name, ModuleType type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the other methods are "store" and "delete" (no mention of "dependents" since it's understood), shouldn't this one be simply "find"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing "findDependents" to "find" on merge


/**
* Remove an atomic dependency from a module (composed or not) to some target (stream, or composed module).
*/
void delete(String module, ModuleType type, /* BaseDefinition? */String target);

}
@@ -0,0 +1,72 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.xd.dirt.module;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.xd.module.ModuleType;


/**
* Used to compute and track dependencies between modules and other elements of the system.
*
* @author Eric Bottard
*/
public class ModuleDependencyTracker {

private final ModuleDependencyRepository dependencyRepository;

@Autowired
public ModuleDependencyTracker(ModuleDependencyRepository dependencyRepository) {
this.dependencyRepository = dependencyRepository;
}

public void record(ModuleDeploymentRequest source, String target) {
dependencyRepository.store(source.getModule(), source.getType(), target);
if (source instanceof CompositeModuleDeploymentRequest) {
CompositeModuleDeploymentRequest composed = (CompositeModuleDeploymentRequest) source;
for (ModuleDeploymentRequest child : composed.getChildren()) {
record(child, target);
}
}
}


/**
* Return the set of things that depend on the given module.
*
* @return a set of Strings of the form {@code type:name}, never {@code null}
*/
public Set<String> findDependents(String name, ModuleType type) {
return dependencyRepository.findDependents(name, type);
}

/**
* @param request
* @param dependencyKey
*/
public void remove(ModuleDeploymentRequest source, String target) {
dependencyRepository.delete(source.getModule(), source.getType(), target);
if (source instanceof CompositeModuleDeploymentRequest) {
CompositeModuleDeploymentRequest composed = (CompositeModuleDeploymentRequest) source;
for (ModuleDeploymentRequest child : composed.getChildren()) {
remove(child, target);
}
}
}
}
@@ -0,0 +1,67 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.xd.dirt.module.memory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.xd.dirt.module.ModuleDependencyRepository;
import org.springframework.xd.module.ModuleType;


/**
* In memory implementation of {@link ModuleDependencyRepository}.
*
* @author Eric Bottard
*/
public class InMemoryModuleDependencyRepository implements ModuleDependencyRepository {

private Map<String, Set<String>> dependencies = new HashMap<String, Set<String>>() {

@Override
public Set<String> get(Object key) {
Set<String> result = super.get(key);
if (result == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, this allows for a race condition; should probably use a ConcurrentHashMap

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing on merge

result = new HashSet<String>();
put((String) key, result);
}
return result;
}
};

@Override
public void store(String moduleName, ModuleType type, String target) {
dependencies.get(keyFor(moduleName, type)).add(target);
}

@Override
public void delete(String module, ModuleType type, String target) {
dependencies.get(keyFor(module, type)).remove(target);
}

@Override
public Set<String> findDependents(String name, ModuleType type) {
return dependencies.get(keyFor(name, type));
}

private String keyFor(String moduleName, ModuleType type) {
return type.name() + ":" + moduleName;
}

}
@@ -0,0 +1,62 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.xd.dirt.module.redis;

import java.util.Set;

import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.xd.dirt.module.ModuleDependencyRepository;
import org.springframework.xd.module.ModuleType;


/**
* Redis implementation of {@link ModuleDependencyRepository}. Uses sets whose name is of the form
* {@code dependencies.module.<type>:<name>}.
*
* @author Eric Bottard
*/
public class RedisModuleDependencyRepository implements ModuleDependencyRepository {

private RedisOperations<String, String> redisOperations;

public RedisModuleDependencyRepository(RedisOperations<String, String> redisOperations) {
this.redisOperations = redisOperations;
}


@Override
public void store(String moduleName, ModuleType type, String target) {
setOpsFor(moduleName, type).add(target);
}

@Override
public void delete(String module, ModuleType type, String target) {
setOpsFor(module, type).remove(target);
}

private BoundSetOperations<String, String> setOpsFor(String moduleName, ModuleType type) {
return redisOperations.boundSetOps(String.format("dependencies.module.%s:%s", type.name(), moduleName));
}


@Override
public Set<String> findDependents(String name, ModuleType type) {
return setOpsFor(name, type).members();
}

}
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -41,8 +42,10 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.xd.dirt.module.DependencyException;
import org.springframework.xd.dirt.module.ModuleAlreadyExistsException;
import org.springframework.xd.dirt.module.ModuleDefinitionRepository;
import org.springframework.xd.dirt.module.ModuleDependencyTracker;
import org.springframework.xd.dirt.module.ModuleDeploymentRequest;
import org.springframework.xd.dirt.module.NoSuchModuleException;
import org.springframework.xd.dirt.stream.XDStreamParser;
Expand Down Expand Up @@ -71,11 +74,15 @@ public class ModulesController {

private ModuleDefinitionResourceAssembler moduleDefinitionResourceAssembler = new ModuleDefinitionResourceAssembler();

private ModuleDependencyTracker dependencyTracker;

@Autowired
public ModulesController(ModuleDefinitionRepository moduleDefinitionRepository) {
public ModulesController(ModuleDefinitionRepository moduleDefinitionRepository,
ModuleDependencyTracker dependencyTracker) {
Assert.notNull(moduleDefinitionRepository, "moduleDefinitionRepository must not be null");
this.repository = moduleDefinitionRepository;
this.parser = new XDStreamParser(moduleDefinitionRepository);
this.dependencyTracker = dependencyTracker;
}

/**
Expand Down Expand Up @@ -120,6 +127,9 @@ public ModuleDefinitionResource save(@RequestParam("name") String name,
if (repository.findByNameAndType(name, type) != null) {
throw new ModuleAlreadyExistsException(name, type);
}
for (ModuleDeploymentRequest child : modules) {
dependencyTracker.record(child, dependencyKey(name, type));
}

ModuleDefinition moduleDefinition = new ModuleDefinition(name, type);
moduleDefinition.setDefinition(definition);
Expand All @@ -128,6 +138,35 @@ public ModuleDefinitionResource save(@RequestParam("name") String name,
return resource;
}

private String dependencyKey(String name, ModuleType type) {
return String.format("module:%s:%s", type.name(), name);
}

/**
* Delete a (composite) module.
*/
@RequestMapping(value = "/{type}/{name}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable("type") ModuleType type, @PathVariable("name") String name) {
ModuleDefinition definition = repository.findByNameAndType(name, type);
if (definition == null) {
throw new NoSuchModuleException(name, type);
}
if (definition.getDefinition() == null) {
throw new IllegalStateException(String.format("Cannot delete non-composed module %s:%s", type, name));
}
Set<String> dependedUpon = dependencyTracker.findDependents(name, type);
if (!dependedUpon.isEmpty()) {
throw new DependencyException("Cannot delete module %2$s:%1$s because it is used by %3$s", name, type,
dependedUpon);
}
repository.delete(type.name() + ":" + name);
List<ModuleDeploymentRequest> requests = parser.parse(name, definition.getDefinition());
for (ModuleDeploymentRequest request : requests) {
dependencyTracker.remove(request, dependencyKey(name, type));
}
}

private ModuleType determineType(List<ModuleDeploymentRequest> modules) {
Collections.sort(modules);
Assert.isTrue(modules != null && modules.size() > 0, "at least one module required");
Expand Down