Skip to content

Commit

Permalink
[#356] sto_82v1: user choosing import strategy for 3rd lvl DPU instan…
Browse files Browse the repository at this point in the history
…ces that use the configuration of its template

In system the pipeline is imported to, DPU template with matching name was found, but they have different configuration. Two different import strategies are available:
1. to use the DPU template of the system it is imported to and lose the original configuration (the pipeline may not work the way as in the original system)
2. to replace the DPU instance configuration so it will work exactly as in the system it is imported from
  • Loading branch information
mvi-eea-sk committed Sep 2, 2015
1 parent c624109 commit 11b6f94
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,26 @@ public class ImportService {
private AppConfig appConfig;

@PreAuthorize("hasRole('pipeline.import') and hasRole('pipeline.create')")
public Pipeline importPipeline(File zipFile, boolean importUserDataFile, boolean importScheduleFile)
public Pipeline importPipeline(File zipFile, boolean importUserDataFile, boolean importScheduleFile)
throws ImportException, IOException {
return importPipeline(zipFile, importUserDataFile, importScheduleFile, new HashMap<String, ImportStrategy>(0));
}

@PreAuthorize("hasRole('pipeline.import') and hasRole('pipeline.create')")
public Pipeline importPipeline(File zipFile, boolean importUserDataFile, boolean importScheduleFile,
Map<String, ImportStrategy> choosenStrategies) throws ImportException, IOException {
final File tempDir;
try {
tempDir = resourceManager.getNewImportTempDir();
} catch (MissingResourceException ex) {
throw new ImportException(Messages.getString("ImportService.pipeline.temp.dir.fail"), ex);
}
return importPipeline(zipFile, tempDir, importUserDataFile, importScheduleFile);
return importPipeline(zipFile, tempDir, importUserDataFile, importScheduleFile, choosenStrategies);
}

@PreAuthorize("hasRole('pipeline.import') and hasRole('pipeline.create')")
public Pipeline importPipeline(File zipFile, File tempDirectory, boolean importUserDataFile, boolean importScheduleFile)
throws ImportException, IOException {
public Pipeline importPipeline(File zipFile, File tempDirectory, boolean importUserDataFile, boolean importScheduleFile,
Map<String, ImportStrategy> choosenStrategies) throws ImportException, IOException {
// delete tempDirectory
ResourceManager.cleanupQuietly(tempDirectory);

Expand All @@ -123,31 +129,25 @@ public Pipeline importPipeline(File zipFile, File tempDirectory, boolean importU
pipe.setActor(actor);
pipe.setShareType(ShareType.PRIVATE);

Map<DPUTemplateRecord, DPUTemplateRecord> importedTemplates = new HashMap<>();
for (Node node : pipe.getGraph().getNodes()) {
final DPUInstanceRecord dpu = node.getDpuInstance();
final DPUTemplateRecord template = dpu.getTemplate();
final DPUTemplateRecord templateToUse;
if (importedTemplates.containsKey(template)) {
// already imported
templateToUse = importedTemplates.get(template);
} else {
// prepare data for import
final File jarFile = new File(tempDirectory,
ArchiveStructure.DPU_JAR.getValue() + File.separator
+ template.getJarPath());
final File userDataFile = new File(tempDirectory,
ArchiveStructure.DPU_DATA_USER.getValue() + File.separator
+ template.getJarDirectory());
final File globalDataFile = new File(tempDirectory,
ArchiveStructure.DPU_DATA_GLOBAL.getValue() + File.separator
+ template.getJarDirectory());
// import
templateToUse = findDPUTemplate(dpu, template, user, jarFile,
userDataFile, globalDataFile, importUserDataFile);
// add to cache
importedTemplates.put(template, templateToUse);
}

// prepare data for import
final File jarFile = new File(tempDirectory,
ArchiveStructure.DPU_JAR.getValue() + File.separator
+ template.getJarPath());
final File userDataFile = new File(tempDirectory,
ArchiveStructure.DPU_DATA_USER.getValue() + File.separator
+ template.getJarDirectory());
final File globalDataFile = new File(tempDirectory,
ArchiveStructure.DPU_DATA_GLOBAL.getValue() + File.separator
+ template.getJarDirectory());

// import
templateToUse = findDPUTemplate(dpu, template, user, jarFile,
userDataFile, globalDataFile, importUserDataFile, choosenStrategies);
// set DPU instance
dpu.setTemplate(templateToUse);
}
Expand Down Expand Up @@ -221,13 +221,14 @@ public List<DpuItem> loadUsedDpus(File baseDir) throws ImportException {
* @param jarFile
* @param userDataDir
* @param globalDataDir
* @param choosenStrategies
* @return Template that is stored in database and is equivalent to the
* given one.
* @throws ImportException
*/
private DPUTemplateRecord findDPUTemplate(DPUInstanceRecord dpu, DPUTemplateRecord template,
User user, File jarFile, File userDataDir, File globalDataDir, boolean importUserDataFile)
throws ImportException {
User user, File jarFile, File userDataDir, File globalDataDir, boolean importUserDataFile,
Map<String, ImportStrategy> choosenStrategies) throws ImportException {

String dpuDir;
try {
Expand Down Expand Up @@ -268,9 +269,18 @@ private DPUTemplateRecord findDPUTemplate(DPUInstanceRecord dpu, DPUTemplateReco
checkPersmissions(matchingNameDpu, user);
result = matchingNameDpu;
} else {
throw new ImportException(Messages.getString("ImportService.pipeline.dpu.import.fail.different.config",
template.getName(),
jarFile.getName()));
switch (choosenStrategies.get(dpu.getName())) {
case REPLACE_INSTANCE_CONFIG:
dpu.setUseTemplateConfig(false);
dpu.setRawConf(template.getRawConf());
case CHANGE_TO_EXISTING:
result = matchingNameDpu;
break;
default:
throw new ImportException(Messages.getString("ImportService.pipeline.dpu.import.fail.different.config",
template.getName(),
jarFile.getName()));
}
}
} else {
checkPersmissions(parentDpu, user);
Expand Down Expand Up @@ -369,6 +379,7 @@ public ImportedFileInformation getImportedInformation(File zipFile)
List<DpuItem> usedDpus = loadUsedDpus(tempDirectory);
Map<String, DpuItem> missingDpus = new TreeMap<>();
Map<String, DpuItem> oldDpus = new TreeMap<>();
Set<String> toDecideDpus = new HashSet<>();

if (pipeline != null) {
PipelineGraph graph = pipeline.getGraph();
Expand Down Expand Up @@ -397,30 +408,24 @@ public ImportedFileInformation getImportedInformation(File zipFile)
throw new ImportException(e.getMessage(), e);
}

DpuItem dpuItem = new DpuItem(dpu.getName(), template.getJarName(), version);
DPUTemplateRecord parentDpuTemplate = dpuFacade.getByDirectory(jarDir);

final DpuItem dpuItem = new DpuItem(template.getName(), template.getJarName(), version);
final DPUTemplateRecord parentDpuTemplate = dpuFacade.getByDirectory(jarDir);

try {
// checking version
if (parentDpuTemplate != null
&& moduleManipulator.compareVersions(parentDpuTemplate.getJarName(), template.getJarName()) < 0) {

if (parentDpuTemplate == null) {
missingDpus.put(jarDir, dpuItem);
} else if (moduleManipulator.compareVersions(parentDpuTemplate.getJarName(), template.getJarName()) < 0) {
missingDpus.put(jarDir, dpuItem);
oldDpus.put(jarDir, dpuItem);
} else {
if (dpu.isUseTemplateConfig()) {
DPUTemplateRecord dpuTemplateRecord = dpuFacade.getByDirectoryAndName(jarDir, template.getName());

if (dpuTemplateRecord == null
|| dpuTemplateRecord.getRawConf() == null
|| !dpuTemplateRecord.getRawConf().equals(template.getRawConf())) {

if (dpuTemplateRecord == null) {
missingDpus.put(template.getName(), dpuItem);
} else if (!haveTheSameConfig(dpuTemplateRecord, template)) {
toDecideDpus.add(dpu.getName());
}
} else if (parentDpuTemplate == null) {
dpuItem.setDpuName(jarDir);
missingDpus.put(jarDir, dpuItem);
}
}
} catch (DPUJarNameFormatException e) {
Expand Down Expand Up @@ -449,7 +454,7 @@ public ImportedFileInformation getImportedInformation(File zipFile)
}

ImportedFileInformation result = new ImportedFileInformation(usedDpus,
missingDpus, isUserData, isScheduleFile, oldDpus);
missingDpus, isUserData, isScheduleFile, oldDpus, toDecideDpus);

LOG.debug("<<< Leaving getImportedInformation: {}", result);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This file is part of UnifiedViews.
*
* UnifiedViews is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedViews is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with UnifiedViews. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cuni.mff.xrg.odcs.commons.app.pipeline.transfer;

/**
* When importing pipeline, for DPU instance the following conditions apply:
* <ul>
* <li>its DPU template is a child (3rd lvl) DPU template</li>
* <li>DPUInstanceRecord.useTemplateConfig == true</li>
* <li>for its DPU template, DPU template with matching name was found in current system</li>
* <li>but the found DPU template has different configuration.</li>
* </ul>
* Describes available import strategies for this child DPU template.
*
* @author mvi
*
*/
public enum ImportStrategy {
/**
* Ignore configuration of imported pipeline, use configuration of DPU template already in system.
* This strategy can cause that the imported pipeline may not run the same way as in the system it's
* imported from.
*/
CHANGE_TO_EXISTING,
/**
* Create new child DPU template with different name.
* This is the least "aggressive" strategy, because it doesn't affect existing pipelines and the imported
* pipeline will (should) run the same way as in the system it's imported from.
*/
CREATE_NEW_CHILD,
/**
* Overwrite configuration in existing child DPU with the configuration of imported pipeline.
* This may cause that existing pipeline would be affected.
*/
OVERWRITE,
/**
* Instance DPUInstanceRecord.useTemplateConfig will be changed to false and the template configuration will
* be copied to the instance.
*/
REPLACE_INSTANCE_CONFIG;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,46 @@

import java.util.List;
import java.util.Map;
import java.util.Set;


public class ImportedFileInformation {

/**
* All DPU's used in the imported pipelines
*/
private List<DpuItem> usedDpus;
/**
* DPU's that are not present in the current system
*/
private Map<String, DpuItem> missingDpus;
/**
* DPU's in current system that are of older version as the DPU,
* that system its imported from
*/
private Map<String, DpuItem> oldDpus;
/**
* 3rd lvl (child) DPU's that use config of their DPU templates,
* are in conflict due to different template configuration between
* the system it is imported to and from.
* The {@link ImportStrategy} needs to be decided for these DPU
*/
private Set<String> toDecideDpus;

boolean userDataFile = false;
boolean scheduleFile = false;

public ImportedFileInformation(List<DpuItem> usedDpus,
Map<String, DpuItem> missingDpus, boolean userDataFile,
boolean scheduleFile, Map<String, DpuItem> oldDpus) {
boolean scheduleFile, Map<String, DpuItem> oldDpus,
Set<String> toDecideDpus) {

this.usedDpus = usedDpus;
this.missingDpus = missingDpus;
this.userDataFile = userDataFile;
this.scheduleFile = scheduleFile;
this.oldDpus = oldDpus;
this.toDecideDpus = toDecideDpus;
}

public List<DpuItem> getUsedDpus() {
Expand Down Expand Up @@ -79,13 +99,22 @@ public Map<String, DpuItem> getOldDpus() {
public void setOldDpus(Map<String, DpuItem> oldDpus) {
this.oldDpus = oldDpus;
}

public Set<String> getToDecideDpus() {
return toDecideDpus;
}

public void setToDecideDpus(Set<String> toDecideDpus) {
this.toDecideDpus = toDecideDpus;
}

@Override
public String toString() {
return "ImportedFileInformation [usedDpus=" + usedDpus
+ ", missingDpus=" + missingDpus + ", userDataFile="
+ userDataFile + ", scheduleFile=" + scheduleFile
+ ", oldDpus=" + oldDpus + "]";
+ ", oldDpus=" + oldDpus
+ ", toDecideDpus=" + toDecideDpus + "]";
}


Expand Down
Loading

0 comments on commit 11b6f94

Please sign in to comment.