-
Notifications
You must be signed in to change notification settings - Fork 21
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
More efficient database unmarshaling #775
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,7 @@ class HttpGoogleDataprocDAO(appName: String, | |
override def getClusterStatus(googleProject: GoogleProject, clusterName: ClusterName): Future[ClusterStatus] = { | ||
val transformed = for { | ||
cluster <- OptionT(getCluster(googleProject, clusterName)) | ||
status <- OptionT.pure[Future, ClusterStatus]( | ||
status <- OptionT.pure[Future]( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cats API change - |
||
Try(ClusterStatus.withNameInsensitive(cluster.getStatus.getState)).toOption.getOrElse(ClusterStatus.Unknown)) | ||
} yield status | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import java.time.Instant | |
import java.sql.Timestamp | ||
import java.util.UUID | ||
|
||
import cats.data.Chain | ||
import cats.implicits._ | ||
import org.broadinstitute.dsde.workbench.leonardo.model.Cluster.LabelMap | ||
import org.broadinstitute.dsde.workbench.leonardo.model._ | ||
|
@@ -193,6 +194,19 @@ trait ClusterComponent extends LeoComponent { | |
} | ||
} | ||
|
||
def getActiveClusterForDnsCache(project: GoogleProject, name: ClusterName): DBIO[Option[Cluster]] = { | ||
clusterQuery | ||
.filter { _.googleProject === project.value } | ||
.filter { _.clusterName === name.value } | ||
.filter { _.destroyedDate === Timestamp.from(dummyDate) } | ||
.result | ||
.map { recs => | ||
recs.headOption.map { clusterRec => | ||
unmarshalCluster(clusterRec, Seq.empty, List.empty, Map.empty, List.empty, List.empty, List.empty) | ||
} | ||
} | ||
} | ||
|
||
def getClusterById(id: Long): DBIO[Option[Cluster]] = { | ||
fullClusterQuery.filter { _._1.id === id }.result map { recs => | ||
unmarshalFullCluster(recs).headOption | ||
|
@@ -404,32 +418,36 @@ trait ClusterComponent extends LeoComponent { | |
|
||
private def unmarshalMinimalCluster(clusterLabels: Seq[(ClusterRecord, Option[LabelRecord])]): Seq[Cluster] = { | ||
// Call foldMap to aggregate a Seq[(ClusterRecord, LabelRecord)] returned by the query to a Map[ClusterRecord, Map[labelKey, labelValue]]. | ||
val clusterLabelMap: Map[ClusterRecord, Map[String, List[String]]] = clusterLabels.toList.foldMap { case (clusterRecord, labelRecordOpt) => | ||
val labelMap = labelRecordOpt.map(labelRecordOpt => labelRecordOpt.key -> List(labelRecordOpt.value)).toMap | ||
// Note we use Chain instead of List inside the foldMap because the Chain monoid is much more efficient than the List monoid. | ||
// See: https://typelevel.org/cats/datatypes/chain.html | ||
val clusterLabelMap: Map[ClusterRecord, Map[String, Chain[String]]] = clusterLabels.toList.foldMap { case (clusterRecord, labelRecordOpt) => | ||
val labelMap = labelRecordOpt.map(labelRecord => labelRecord.key -> Chain(labelRecord.value)).toMap | ||
Map(clusterRecord -> labelMap) | ||
} | ||
|
||
// Unmarshal each (ClusterRecord, Map[labelKey, labelValue]) to a Cluster object | ||
clusterLabelMap.map { case (clusterRec, labelMap) => | ||
unmarshalCluster(clusterRec, Seq.empty, List.empty, labelMap.mapValues(_.toSet.head), List.empty, List.empty, List.empty) | ||
unmarshalCluster(clusterRec, Seq.empty, List.empty, labelMap.mapValues(_.toList.toSet.head), List.empty, List.empty, List.empty) | ||
}.toSeq | ||
} | ||
|
||
private def unmarshalFullCluster(clusterRecords: Seq[(ClusterRecord, Option[InstanceRecord], Option[ClusterErrorRecord], Option[LabelRecord], Option[ExtensionRecord], Option[ClusterImageRecord], Option[ScopeRecord])]): Seq[Cluster] = { | ||
// Call foldMap to aggregate a flat sequence of (cluster, instance, label) triples returned by the query | ||
// to a grouped (cluster -> (instances, labels)) structure. | ||
val clusterRecordMap: Map[ClusterRecord, (List[InstanceRecord], List[ClusterErrorRecord], Map[String, List[String]], List[ExtensionRecord], List[ClusterImageRecord], List[ScopeRecord])] = clusterRecords.toList.foldMap { case (clusterRecord, instanceRecordOpt, errorRecordOpt, labelRecordOpt, extensionOpt, clusterImageOpt, scopeOpt) => | ||
// Note we use Chain instead of List inside the foldMap because the Chain monoid is much more efficient than the List monoid. | ||
// See: https://typelevel.org/cats/datatypes/chain.html | ||
val clusterRecordMap: Map[ClusterRecord, (Chain[InstanceRecord], Chain[ClusterErrorRecord], Map[String, Chain[String]], Chain[ExtensionRecord], Chain[ClusterImageRecord], Chain[ScopeRecord])] = clusterRecords.toList.foldMap { case (clusterRecord, instanceRecordOpt, errorRecordOpt, labelRecordOpt, extensionOpt, clusterImageOpt, scopeOpt) => | ||
val instanceList = instanceRecordOpt.toList | ||
val labelMap = labelRecordOpt.map(labelRecordOpt => labelRecordOpt.key -> List(labelRecordOpt.value)).toMap | ||
val labelMap = labelRecordOpt.map(labelRecordOpt => labelRecordOpt.key -> Chain(labelRecordOpt.value)).toMap | ||
val errorList = errorRecordOpt.toList | ||
val extList = extensionOpt.toList | ||
val clusterImageList = clusterImageOpt.toList | ||
val scopeList = scopeOpt.toList | ||
Map(clusterRecord -> (instanceList, errorList, labelMap, extList, clusterImageList, scopeList)) | ||
Map(clusterRecord -> (Chain.fromSeq(instanceList), Chain.fromSeq(errorList), labelMap, Chain.fromSeq(extList), Chain.fromSeq(clusterImageList), Chain.fromSeq(scopeList))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's where I'm using https://typelevel.org/cats/datatypes/chain.html |
||
} | ||
|
||
clusterRecordMap.map { case (clusterRecord, (instanceRecords, errorRecords, labels, extensions, clusterImages, scopes)) => | ||
unmarshalCluster(clusterRecord, instanceRecords.toSet.toSeq, errorRecords.groupBy(_.timestamp).map(_._2.head).toList, labels.mapValues(_.toSet.head), extensions, clusterImages.toSet.toList, scopes) | ||
unmarshalCluster(clusterRecord, instanceRecords.toList, errorRecords.toList.groupBy(_.timestamp).map(_._2.head).toList, labels.mapValues(_.toList.toSet.head), extensions.toList, clusterImages.toList, scopes.toList) | ||
}.toSeq | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,8 +86,8 @@ class ZombieClusterMonitor(config: ZombieClusterConfig, gdDAO: GoogleDataprocDAO | |
|
||
private def isProjectActiveInGoogle(googleProject: GoogleProject): Future[Boolean] = { | ||
// Check the project and its billing info | ||
(googleProjectDAO.isProjectActive(googleProject.value) |@| googleProjectDAO.isBillingActive(googleProject.value)) | ||
.map(_ && _) | ||
(googleProjectDAO.isProjectActive(googleProject.value), googleProjectDAO.isBillingActive(googleProject.value)) | ||
.mapN(_ && _) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This style is deprecated in cats:
They now recommend:
|
||
.recover { case e => | ||
logger.warn(s"Unable to check status of project ${googleProject.value} for zombie cluster detection", e) | ||
true | ||
|
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.
Upgraded workbenchUtil to not pull in an older version of cats.