Giter Site home page Giter Site logo

paxos's Introduction

Basic Paxos in Scala

Demo

object Demo extends IOApp {

  implicit val longProposalId = new ProposalId[Long] {
    override def next(c: Long): Long = System.currentTimeMillis()
    override def next: Long          = System.currentTimeMillis()
  }

  implicit val nextDelay = FiniteDuration(20, TimeUnit.SECONDS)
  implicit val logger    = Slf4jLogger.getLogger[IO]
  val messenger          = new IOMessenger[Long, String]

  override def run(args: List[String]): IO[ExitCode] =
    for {
      _     <- logger.info("Running 9 nodes, three of them propose")
      node1 <- Node[IO, Long, String]("node1", 5, messenger)
      node2 <- Node[IO, Long, String]("node2", 5, messenger)
      node3 <- Node[IO, Long, String]("node3", 5, messenger)
      node4 <- Node[IO, Long, String]("node4", 5, messenger)
      node5 <- Node[IO, Long, String]("node5", 5, messenger)
      node6 <- Node[IO, Long, String]("node6", 5, messenger)
      node7 <- Node[IO, Long, String]("node7", 5, messenger)
      node8 <- Node[IO, Long, String]("node8", 5, messenger)
      node9 <- Node[IO, Long, String]("node9", 5, messenger)

      _ <- Traverse[List]
        .traverse(List(node1, node2, node3, node4, node5, node6, node7, node8, node9))(n => messenger.addNode(n))

      v1 <- node1.propose("cat").start
      v2 <- node2.propose("dog").start
      v3 <- node3.propose("fish").start
      v4 <- node4.listen.start
      v5 <- node5.listen.start
      v6 <- node6.listen.start
      v7 <- node7.listen.start
      v8 <- node8.listen.start
      v9 <- node9.listen.start

      values <- Traverse[List].traverse(List(v1, v2, v3, v4, v5, v6, v7, v8, v9))(_.join)

      _ <- logger.info(s"Accepted values by nodes ${values}")
    } yield ExitCode.Success

}

Running the demo

sbt "project example; run"

paxos's People

Contributors

alirezameskin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.