-
Notifications
You must be signed in to change notification settings - Fork 29
Validate DataSources more strictly when reading from disk #8894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec69f71
Validate DataSources more strictly when reading from disk
fm3 29bc4f9
check valid dataLayerNames, also validate datasource in reserveManual…
fm3 17b5d97
changelog
fm3 f79da6c
lint
fm3 692976c
Merge branch 'reserve-manual' into datasource-validation
fm3 ad54a6f
Merge branch 'reserve-manual' into datasource-validation
fm3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### Changed | ||
- DataSources are now more strictly validated when reading them from disk or when reserving manual uploads. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...-datastore/app/com/scalableminds/webknossos/datastore/services/DataSourceValidation.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.scalableminds.webknossos.datastore.services | ||
|
||
import com.scalableminds.util.tools.{Box, Full, ParamFailure} | ||
import com.scalableminds.webknossos.datastore.models.datasource.{ElementClass, UsableDataSource} | ||
import play.api.libs.json.Json | ||
|
||
trait DataSourceValidation { | ||
|
||
protected def assertValidateDataSource(dataSource: UsableDataSource): Box[Unit] = { | ||
val errors = validateDataSourceGetErrors(dataSource) | ||
if (errors.isEmpty) { | ||
Full(()) | ||
} else { | ||
ParamFailure("DataSource is invalid", Json.toJson(errors.map(e => Json.obj("error" -> e)))) | ||
} | ||
} | ||
|
||
protected def validateDataSourceGetErrors(dataSource: UsableDataSource): Seq[String] = { | ||
def check(expression: Boolean, msg: String): Option[String] = if (!expression) Some(msg) else None | ||
|
||
// Check that when mags are sorted by max dimension, all dimensions are sorted. | ||
// This means each dimension increases monotonically. | ||
val magsSorted = dataSource.dataLayers.map(_.resolutions.sortBy(_.maxDim)) | ||
val magsXIsSorted = magsSorted.map(_.map(_.x)) == magsSorted.map(_.map(_.x).sorted) | ||
val magsYIsSorted = magsSorted.map(_.map(_.y)) == magsSorted.map(_.map(_.y).sorted) | ||
val magsZIsSorted = magsSorted.map(_.map(_.z)) == magsSorted.map(_.map(_.z).sorted) | ||
|
||
val errors = List( | ||
check(dataSource.scale.factor.isStrictlyPositive, "Voxel size (scale) is negative in at least one dimension."), | ||
check(magsXIsSorted && magsYIsSorted && magsZIsSorted, "Mags do not monotonically increase in all dimensions."), | ||
check(magsSorted.forall(magsOfLayer => magsOfLayer.length == magsOfLayer.distinct.length), | ||
"There are duplicate mags in a layer."), | ||
check(dataSource.dataLayers.nonEmpty, "No layers."), | ||
check(dataSource.dataLayers.forall(!_.boundingBox.isEmpty), "Empty bounding box in a layer."), | ||
check( | ||
dataSource.segmentationLayers.forall { layer => | ||
ElementClass.segmentationElementClasses.contains(layer.elementClass) | ||
}, | ||
s"Invalid element class for a segmentation layer." | ||
), | ||
check( | ||
dataSource.segmentationLayers.forall { layer => | ||
ElementClass.largestSegmentIdIsInRange(layer.largestSegmentId, layer.elementClass) | ||
}, | ||
"Largest segment id exceeds range (must be nonnegative, within element class range, and < 2^53)." | ||
), | ||
check( | ||
dataSource.dataLayers.map(_.name).distinct.length == dataSource.dataLayers.length, | ||
"Layer names must be unique. At least two layers have the same name." | ||
), | ||
check( | ||
dataSource.dataLayers.map(_.name).forall(!_.contains("/")), | ||
"Layer names must not contain forward slash." | ||
), | ||
check( | ||
dataSource.dataLayers.map(_.name).forall(!_.startsWith(".")), | ||
"Layer names must not start with dot." | ||
) | ||
).flatten | ||
|
||
errors | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you add this change? Is there a possible scenario where the datasource cannot be retrieved from the cache but exists on disk / in dir?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the cache only handles usable datasources. If the existing datasource is unusable due to an error, the cache will just return a Failure. However, in this context, we want to re-read the one from disk in this case, re-validate it, and sent it to webknossos if it is now usable. Only after that, it will also be available through the cache. So this part is actually the bugfix for the reload button in the dashboard (for non-virtual datasets). Does that make sense?