use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.
the class TestDatabaseMetadataService method testGetLatestSnapshotDoesNotExist.
@Test
public void testGetLatestSnapshotDoesNotExist() {
final FlowSnapshotEntity latest = metadataService.getLatestSnapshot("DOES-NOT-EXIST");
assertNull(latest);
}
use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.
the class RegistryService method createFlowSnapshot.
// ---------------------- VersionedFlowSnapshot methods ---------------------------------------------
public VersionedFlowSnapshot createFlowSnapshot(final VersionedFlowSnapshot flowSnapshot) {
if (flowSnapshot == null) {
throw new IllegalArgumentException("Versioned flow snapshot cannot be null");
}
// validation will ensure that the metadata and contents are not null
if (flowSnapshot.getSnapshotMetadata() != null) {
flowSnapshot.getSnapshotMetadata().setTimestamp(System.currentTimeMillis());
}
// these fields aren't used for creation
flowSnapshot.setFlow(null);
flowSnapshot.setBucket(null);
validate(flowSnapshot, "Cannot create versioned flow snapshot");
writeLock.lock();
try {
final VersionedFlowSnapshotMetadata snapshotMetadata = flowSnapshot.getSnapshotMetadata();
// ensure the bucket exists
final BucketEntity existingBucket = metadataService.getBucketById(snapshotMetadata.getBucketIdentifier());
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", snapshotMetadata.getBucketIdentifier());
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// ensure the flow exists
final FlowEntity existingFlow = metadataService.getFlowById(snapshotMetadata.getFlowIdentifier());
if (existingFlow == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", snapshotMetadata.getFlowIdentifier());
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata
final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>();
final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
if (existingFlowSnapshots != null) {
existingFlowSnapshots.stream().forEach(s -> sortedSnapshots.add(DataModelMapper.map(existingBucket, s)));
}
// if we already have snapshots we need to verify the new one has the correct version
if (sortedSnapshots != null && sortedSnapshots.size() > 0) {
final VersionedFlowSnapshotMetadata lastSnapshot = sortedSnapshots.last();
if (snapshotMetadata.getVersion() <= lastSnapshot.getVersion()) {
throw new IllegalStateException("A Versioned flow snapshot with the same version already exists: " + snapshotMetadata.getVersion());
}
if (snapshotMetadata.getVersion() > (lastSnapshot.getVersion() + 1)) {
throw new IllegalStateException("Version must be a one-up number, last version was " + lastSnapshot.getVersion() + " and version for this snapshot was " + snapshotMetadata.getVersion());
}
} else if (snapshotMetadata.getVersion() != 1) {
throw new IllegalStateException("Version of first snapshot must be 1");
}
// serialize the snapshot
final ByteArrayOutputStream out = new ByteArrayOutputStream();
processGroupSerializer.serialize(flowSnapshot.getFlowContents(), out);
// save the serialized snapshot to the persistence provider
final Bucket bucket = DataModelMapper.map(existingBucket);
final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, existingFlow);
final FlowSnapshotContext context = new StandardFlowSnapshotContext.Builder(bucket, versionedFlow, snapshotMetadata).build();
flowPersistenceProvider.saveFlowContent(context, out.toByteArray());
// create snapshot in the metadata provider
metadataService.createFlowSnapshot(DataModelMapper.map(snapshotMetadata));
// update the modified date on the flow
metadataService.updateFlow(existingFlow);
// get the updated flow, we need to use "with counts" here so we can return this is a part of the response
final FlowEntity updatedFlow = metadataService.getFlowByIdWithSnapshotCounts(snapshotMetadata.getFlowIdentifier());
if (updatedFlow == null) {
throw new ResourceNotFoundException("Versioned flow does not exist for identifier " + snapshotMetadata.getFlowIdentifier());
}
final VersionedFlow updatedVersionedFlow = DataModelMapper.map(existingBucket, updatedFlow);
flowSnapshot.setBucket(bucket);
flowSnapshot.setFlow(updatedVersionedFlow);
return flowSnapshot;
} finally {
writeLock.unlock();
}
}
use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.
the class RegistryService method getFlowSnapshot.
public VersionedFlowSnapshot getFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
if (StringUtils.isBlank(bucketIdentifier)) {
throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
}
if (StringUtils.isBlank(flowIdentifier)) {
throw new IllegalArgumentException("Flow identifier cannot be null or blank");
}
if (version == null) {
throw new IllegalArgumentException("Version cannot be null or blank");
}
readLock.lock();
try {
final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// we need to populate the version count here so we have to do this retrieval instead of snapshotEntity.getFlow()
final FlowEntity flowEntityWithCount = metadataService.getFlowByIdWithSnapshotCounts(flowIdentifier);
if (flowEntityWithCount == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(flowEntityWithCount.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// ensure the snapshot exists
final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
if (snapshotEntity == null) {
LOGGER.warn("The specified flow snapshot id [{}] does not exist for version [{}].", flowIdentifier, version);
throw new ResourceNotFoundException("The specified versioned flow snapshot does not exist for this flow.");
}
// get the serialized bytes of the snapshot
final byte[] serializedSnapshot = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, version);
if (serializedSnapshot == null || serializedSnapshot.length == 0) {
throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + version);
}
// deserialize the contents
final InputStream input = new ByteArrayInputStream(serializedSnapshot);
final VersionedProcessGroup flowContents = processGroupSerializer.deserialize(input);
// map entities to data model
final Bucket bucket = DataModelMapper.map(existingBucket);
final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, flowEntityWithCount);
final VersionedFlowSnapshotMetadata snapshotMetadata = DataModelMapper.map(existingBucket, snapshotEntity);
// create the snapshot to return
final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
snapshot.setFlowContents(flowContents);
snapshot.setSnapshotMetadata(snapshotMetadata);
snapshot.setFlow(versionedFlow);
snapshot.setBucket(bucket);
return snapshot;
} finally {
readLock.unlock();
}
}
use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.
the class RegistryService method getLatestFlowSnapshotMetadata.
public VersionedFlowSnapshotMetadata getLatestFlowSnapshotMetadata(final String bucketIdentifier, final String flowIdentifier) {
if (StringUtils.isBlank(bucketIdentifier)) {
throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
}
if (StringUtils.isBlank(flowIdentifier)) {
throw new IllegalArgumentException("Flow identifier cannot be null or blank");
}
readLock.lock();
try {
// ensure the bucket exists
final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// ensure the flow exists
final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
if (existingFlow == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// get latest snapshot for the flow
final FlowSnapshotEntity latestSnapshot = metadataService.getLatestSnapshot(existingFlow.getId());
if (latestSnapshot == null) {
throw new ResourceNotFoundException("The specified flow ID has no versions");
}
return DataModelMapper.map(existingBucket, latestSnapshot);
} finally {
readLock.unlock();
}
}
use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.
the class RegistryService method deleteFlowSnapshot.
public VersionedFlowSnapshotMetadata deleteFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
if (StringUtils.isBlank(bucketIdentifier)) {
throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
}
if (StringUtils.isBlank(flowIdentifier)) {
throw new IllegalArgumentException("Flow identifier cannot be null or blank");
}
if (version == null) {
throw new IllegalArgumentException("Version cannot be null or blank");
}
writeLock.lock();
try {
// ensure the bucket exists
final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// ensure the flow exists
final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
if (existingFlow == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// ensure the snapshot exists
final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
if (snapshotEntity == null) {
throw new ResourceNotFoundException("Versioned flow snapshot does not exist for flow " + flowIdentifier + " and version " + version);
}
// delete the content of the snapshot
flowPersistenceProvider.deleteFlowContent(bucketIdentifier, flowIdentifier, version);
// delete the snapshot itself
metadataService.deleteFlowSnapshot(snapshotEntity);
return DataModelMapper.map(existingBucket, snapshotEntity);
} finally {
writeLock.unlock();
}
}
Aggregations