use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class OptimizedExportStrategy method performOptimizedTransform.
private void performOptimizedTransform(ExportParameters parameters, OutputStream outputStream) throws IOException {
// Initial check
final OptimizedPreparationInput optimizedPreparationInput = new OptimizedPreparationInput(parameters).invoke();
if (optimizedPreparationInput == null) {
throw new IllegalStateException("Unable to use this strategy (call accept() before calling this).");
}
final String preparationId = parameters.getPreparationId();
final String dataSetId = optimizedPreparationInput.getDataSetId();
final TransformationCacheKey transformationCacheKey = optimizedPreparationInput.getTransformationCacheKey();
final DataSetMetadata metadata = optimizedPreparationInput.getMetadata();
final String previousVersion = optimizedPreparationInput.getPreviousVersion();
final String version = optimizedPreparationInput.getVersion();
final ExportFormat format = getFormat(parameters.getExportType());
// Get content from previous step
try (JsonParser parser = mapper.getFactory().createParser(new InputStreamReader(contentCache.get(transformationCacheKey), UTF_8))) {
// Create dataset
final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
dataSet.setMetadata(metadata);
// get the actions to apply (no preparation ==> dataset export ==> no actions)
final String actions = getActions(preparationId, previousVersion, version);
final PreparationMessage preparation = getPreparation(preparationId);
preparation.setSteps(getMatchingSteps(preparation.getSteps(), previousVersion, version));
LOGGER.debug("Running optimized strategy for preparation {} @ step #{}", preparationId, version);
// create tee to broadcast to cache + service output
final TransformationCacheKey key = //
cacheKeyGenerator.generateContentKey(//
dataSetId, //
preparationId, //
version, //
parameters.getExportType(), //
parameters.getFrom(), //
parameters.getArguments(), //
parameters.getFilter());
LOGGER.debug("Cache key: " + key.getKey());
LOGGER.debug("Cache key details: " + key.toString());
try (final TeeOutputStream tee = new TeeOutputStream(outputStream, contentCache.put(key, ContentCache.TimeToLive.DEFAULT))) {
final Configuration configuration = //
Configuration.builder().args(//
parameters.getArguments()).outFilter(//
rm -> filterService.build(parameters.getFilter(), rm)).sourceType(parameters.getFrom()).format(//
format.getName()).actions(//
actions).preparation(//
preparation).stepId(//
version).volume(//
Configuration.Volume.SMALL).output(//
tee).limit(//
limit).build();
factory.get(configuration).buildExecutable(dataSet, configuration).execute();
tee.flush();
} catch (Throwable e) {
// NOSONAR
contentCache.evict(key);
throw e;
}
} catch (TDPException e) {
throw e;
} catch (Exception e) {
throw new TDPException(TransformationErrorCodes.UNABLE_TO_TRANSFORM_DATASET, e);
}
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FileUpgradeTaskRepository method applied.
/**
* @see UpgradeTaskRepository#applied(String, UpgradeTaskId)
*/
@Override
public void applied(String targetId, UpgradeTaskId id) {
AppliedUpgradeTask task = new AppliedUpgradeTask(id);
File file = new File(getRootFolder(targetId), id.getUniqueKey() + ".json");
try {
mapper.writerFor(AppliedUpgradeTask.class).writeValue(file, task);
} catch (IOException e) {
throw new TDPException(UNEXPECTED_EXCEPTION, e);
}
LOG.debug("{} for {} saved here {}", task, targetId, file.getPath());
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FileSystemDataSetMetadataRepository method save.
@Override
public void save(DataSetMetadata metadata) {
String id = metadata.getId();
ReentrantReadWriteLock lock = locks.getLock(id);
final File file = getFile(id);
lock.writeLock().lock();
try (GZIPOutputStream output = new GZIPOutputStream(new FileOutputStream(file))) {
mapper.writer().writeValue(output, metadata);
} catch (IOException e) {
LOG.error("Error saving {}", metadata, e);
throw new TDPException(DataSetErrorCodes.UNABLE_TO_STORE_DATASET_METADATA, e, ExceptionContext.build().put("id", metadata.getId()));
} finally {
lock.writeLock().unlock();
}
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FolderService method list.
/**
* Get folders. If parentId is supplied, it will be used as filter.
*
* @param parentId the parent folder id parameter
* @return direct sub folders for the given id.
*/
// @formatter:off
@RequestMapping(value = "/folders", method = GET)
@ApiOperation(value = "List children folders of the parameter if null list root children.", notes = "List all child folders of the one as parameter")
@Timed
public Stream<Folder> list(@RequestParam(required = false) @ApiParam(value = "Parent id filter.") String parentId, @RequestParam(defaultValue = "lastModificationDate") @ApiParam(value = "Sort key (by name or date).") Sort sort, @RequestParam(defaultValue = "desc") @ApiParam(value = "Order for sort key (desc or asc).") Order order) {
// @formatter:on
Stream<Folder> children;
if (parentId != null) {
if (!folderRepository.exists(parentId)) {
throw new TDPException(FOLDER_NOT_FOUND, build().put("id", parentId));
}
children = folderRepository.children(parentId);
} else {
// This will list all folders
children = folderRepository.searchFolders("", false);
}
final AtomicInteger folderCount = new AtomicInteger();
// update the number of preparations in each children
children = children.peek(f -> {
final long count = folderRepository.count(f.getId(), PREPARATION);
f.setNbPreparations(count);
folderCount.addAndGet(1);
});
LOGGER.info("Found {} children for parentId: {}", folderCount.get(), parentId);
// sort the folders
return children.sorted(getFolderComparator(sort, order));
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class StreamModuleTest method shouldHaveCorrectJSONWithTDPFailure.
@Test
public void shouldHaveCorrectJSONWithTDPFailure() throws Exception {
// Given
Iterator<String> failedIterator = new Iterator<String>() {
int i = 0;
@Override
public boolean hasNext() {
return i < 2;
}
@Override
public String next() {
if (i <= 0) {
i++;
return "string1";
} else {
throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION);
}
}
};
final Stream<String> stringStream = stream(spliterator(failedIterator, 2, 0), false);
// When
final StringWriter writer = new StringWriter();
try {
mapper.writeValue(writer, stringStream);
} catch (IOException e) {
// Ignored
}
// Then
assertThat(writer.toString(), sameJSONAs("[\"string1\"]"));
}
Aggregations