Skip to content

Commit

Permalink
make scalafmt not crash on membership
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Jul 22, 2024
1 parent 8dd1072 commit d1ed843
Showing 1 changed file with 51 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import rdts.base.LocalUid.replicaId
import rdts.base.{Bottom, Lattice, LocalUid, Uid}
import rdts.time.Time

case class Membership[A, C[_], D[_]]
(counter: Time,
membersConsensus: C[Set[Uid]],
innerConsensus: D[A],
log: List[A],
membershipChanging: Boolean = false)
(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]]
case class Membership[A, C[_], D[_]](
counter: Time,
membersConsensus: C[Set[Uid]],
innerConsensus: D[A],
log: List[A],
membershipChanging: Boolean = false
)(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]]
) {

override def toString: String =
Expand All @@ -30,32 +30,35 @@ case class Membership[A, C[_], D[_]]
if isMember then
copy(
membershipChanging = true,
membersConsensus = membersConsensus.merge(membersConsensus.write(currentMembers + id)))
membersConsensus = membersConsensus.merge(membersConsensus.write(currentMembers + id))
)
else this

def removeMember(id: Uid)(using LocalUid): Membership[A, C, D] =
if currentMembers.size > 1 && isMember then // cannot remove last member
copy(
membershipChanging = true,
membersConsensus = membersConsensus.merge(membersConsensus.write(currentMembers - id)))
membersConsensus = membersConsensus.merge(membersConsensus.write(currentMembers - id))
)
else this

def read: List[A] = log

def write(value: A)(using LocalUid): Membership[A, C, D] =
if !membershipChanging && isMember then
copy(
innerConsensus = innerConsensus.merge(innerConsensus.write(value)))
innerConsensus = innerConsensus.merge(innerConsensus.write(value))
)
else this

def isMember(using LocalUid) = currentMembers.contains(replicaId)

def upkeep()(using LocalUid): Membership[A, C, D] =
if !isMember then return this // do nothing if we are not a member anymore
val memberUpkeep = membersConsensus.upkeep()
val innerUpkeep = innerConsensus.upkeep()
val newMembers = membersConsensus.merge(membersConsensus.upkeep())
val newInner = innerConsensus.merge(innerConsensus.upkeep())
val innerUpkeep = innerConsensus.upkeep()
val newMembers = membersConsensus.merge(membersConsensus.upkeep())
val newInner = innerConsensus.merge(innerConsensus.upkeep())
(newMembers.read, newInner.read) match
// member consensus reached -> members have changed
case (Some(members), _) =>
Expand Down Expand Up @@ -85,30 +88,28 @@ case class Membership[A, C[_], D[_]]

object Membership {

def init[A, C[_], D[_]]
(initialMembers: Set[Uid])
(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
def init[A, C[_], D[_]](initialMembers: Set[Uid])(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
): Membership[A, C, D] =
require(initialMembers.nonEmpty, "initial members can't be empty")
val unchanged = Membership.empty[A, C, D]
unchanged.copy(membersConsensus = unchanged.membersConsensus.reset(initialMembers),
innerConsensus = unchanged.innerConsensus.reset(initialMembers))

unchanged.copy(
membersConsensus = unchanged.membersConsensus.reset(initialMembers),
innerConsensus = unchanged.innerConsensus.reset(initialMembers)
)

def empty[A, C[_], D[_]]
(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
def empty[A, C[_], D[_]](using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
): Membership[A, C, D] =
Membership(
0,
Expand All @@ -117,20 +118,22 @@ object Membership {
List()
)

given lattice[A, C[_], D[_]]
(using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
given lattice[A, C[_], D[_]](using
Bottom[C[Set[Uid]]],
Bottom[D[A]],
Consensus[C],
Consensus[D],
Lattice[C[Set[Uid]]],
Lattice[D[A]],
): Lattice[Membership[A, C, D]] with
override def merge(left: Membership[A, C, D], right: Membership[A, C, D]): Membership[A, C, D] =
if left.counter > right.counter then left
else if right.counter > left.counter then right
else
require(left.currentMembers == right.currentMembers, s"left and right members need to be the same. Got: $left, $right")
require(
left.currentMembers == right.currentMembers,
s"left and right members need to be the same. Got: $left, $right"
)
Membership(
left.counter,
Lattice[C[Set[Uid]]].merge(left.membersConsensus, right.membersConsensus),
Expand Down

0 comments on commit d1ed843

Please sign in to comment.