send more heavy reads to mongodb secondaries

pull/2850/head
Thibault Duplessis 2017-03-25 12:17:03 +01:00
parent 608eee8be5
commit d5f3dad590
7 changed files with 18 additions and 15 deletions

View File

@ -49,7 +49,7 @@ final class CoachApi(
def listedWithUserList: Fu[List[Coach.WithUser]] =
all.map(_.filter(_.isListed)) flatMap { coaches =>
UserRepo.byIds(coaches.map(_.id.value)) map { users =>
UserRepo.byIdsSecondary(coaches.map(_.id.value)) map { users =>
coaches.flatMap { coach =>
users find coach.is filter Granter(_.Coach) map { Coach.WithUser(coach, _) }
}

View File

@ -13,11 +13,11 @@ trait CollExt { self: dsl with QueryBuilderExt =>
def uno[D: BSONDocumentReader](selector: Bdoc): Fu[Option[D]] =
coll.find(selector).uno[D]
def list[D: BSONDocumentReader](selector: Bdoc): Fu[List[D]] =
coll.find(selector).list[D]()
def list[D: BSONDocumentReader](selector: Bdoc, readPreference: ReadPreference = ReadPreference.primary): Fu[List[D]] =
coll.find(selector).list[D](readPreference = readPreference)
def list[D: BSONDocumentReader](selector: Bdoc, max: Int): Fu[List[D]] =
coll.find(selector).list[D](max)
def list[D: BSONDocumentReader](selector: Bdoc, limit: Int): Fu[List[D]] =
coll.find(selector).list[D](limit = limit)
def byId[D: BSONDocumentReader, I: BSONValueWriter](id: I): Fu[Option[D]] =
uno[D]($id(id))
@ -26,11 +26,11 @@ trait CollExt { self: dsl with QueryBuilderExt =>
def byId[D: BSONDocumentReader](id: Int): Fu[Option[D]] = uno[D]($id(id))
def byIds[D: BSONDocumentReader, I: BSONValueWriter](ids: Iterable[I]): Fu[List[D]] =
def byIds[D: BSONDocumentReader, I: BSONValueWriter](ids: Iterable[I], readPreference: ReadPreference): Fu[List[D]] =
list[D]($inIds(ids))
def byIds[D: BSONDocumentReader](ids: Iterable[String]): Fu[List[D]] =
byIds[D, String](ids)
def byIds[D: BSONDocumentReader](ids: Iterable[String], readPreference: ReadPreference = ReadPreference.primary): Fu[List[D]] =
byIds[D, String](ids, readPreference)
def countSel(
selector: Bdoc,
@ -54,8 +54,8 @@ trait CollExt { self: dsl with QueryBuilderExt =>
// def byOrderedIds[A <: Identified[String]: TubeInColl](ids: Iterable[String]): Fu[List[A]] =
// byOrderedIds[String, A](ids)
def optionsByOrderedIds[D: BSONDocumentReader, I: BSONValueWriter](ids: Iterable[I])(docId: D => I): Fu[List[Option[D]]] =
byIds[D, I](ids) map { docs =>
def optionsByOrderedIds[D: BSONDocumentReader, I: BSONValueWriter](ids: Iterable[I], readPreference: ReadPreference = ReadPreference.primary)(docId: D => I): Fu[List[Option[D]]] =
byIds[D, I](ids, readPreference) map { docs =>
val docsMap = docs.map(u => docId(u) -> u).toMap
ids.map(docsMap.get).toList
}

View File

@ -21,7 +21,7 @@ trait QueryBuilderExt { self: dsl =>
def list[A: BSONDocumentReader](limit: Int): Fu[List[A]] = list[A](limit.some)
def list[A: BSONDocumentReader](): Fu[List[A]] = list[A](none)
def list[A: BSONDocumentReader](readPreference: ReadPreference = ReadPreference.primary): Fu[List[A]] = list[A](none)
// like one, but with stopOnError defaulting to false
def uno[A: BSONDocumentReader]: Fu[Option[A]] = uno[A](ReadPreference.primary)

View File

@ -34,7 +34,8 @@ object Team {
def contains(teamId: ID) = value contains teamId
def toList = if (value.isEmpty) Nil else value.split(IdsStr.separator).toList
def toArray: Array[String] = value.split(IdsStr.separator)
def toList = if (value.isEmpty) Nil else toArray.toList
}
object IdsStr {

View File

@ -57,7 +57,7 @@ final class TeamApi(
}
def mine(me: User): Fu[List[Team]] =
cached teamIds me.id dmap (_.toList) flatMap coll.team.byIds[Team]
cached teamIds me.id flatMap { ids => coll.team.byIds[Team](ids.toArray) }
def hasTeams(me: User): Fu[Boolean] = cached.teamIds(me.id).map(_.value.nonEmpty)

View File

@ -41,7 +41,7 @@ private[timeline] final class Push(
case Users(ids) => fuccess(ids)
case Followers(id) => getFollowerIds(id)
case Friends(id) => getFriendIds(id)
case StaffFriends(id) => getFriendIds(id) flatMap UserRepo.byIds map {
case StaffFriends(id) => getFriendIds(id) flatMap UserRepo.byIdsSecondary map {
_ filter Granter(_.StaffForum) map (_.id)
}
case ExceptUser(_) => fuccess(Nil)

View File

@ -31,6 +31,8 @@ object UserRepo {
def byIds(ids: Iterable[ID]): Fu[List[User]] = coll.byIds[User](ids)
def byIdsSecondary(ids: Iterable[ID]): Fu[List[User]] = coll.byIds[User](ids, ReadPreference.secondaryPreferred)
def byEmail(email: String): Fu[Option[User]] = coll.uno[User]($doc(F.email -> email))
def idByEmail(email: String): Fu[Option[String]] =
@ -48,7 +50,7 @@ object UserRepo {
coll.byOrderedIds[User, User.ID](ids)(_.id)
def enabledByIds(ids: Iterable[ID]): Fu[List[User]] =
coll.list[User](enabledSelect ++ $inIds(ids))
coll.list[User](enabledSelect ++ $inIds(ids), ReadPreference.secondaryPreferred)
def enabledById(id: ID): Fu[Option[User]] =
coll.uno[User](enabledSelect ++ $id(id))