Coder Social home page Coder Social logo

workshop2's Introduction

1. Start Up

Pre-request

  • Make
  • Ubuntu
  • Docker

We use docker compose to start up the lab environment.

Start up four container: master1, master2, worker, zookeeper.

make start-up

Check the status of the containers:

docker ps

The output should be like this:

CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                   NAMES
93e4b1d54a7e   myapp/master:latest   "/app/bin/master"        39 seconds ago   Up 35 seconds                                                           master1       
ac11bbef0daf   myapp/master:latest   "/app/bin/master"        39 seconds ago   Up 35 seconds                                                           master2       
b274b83c3c53   zookeeper             "/docker-entrypoint.…"   39 seconds ago   Up 36 seconds   2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                  zookeeper     
e75c2c49fda1   myapp/worker:latest   "/app/bin/worker"        39 seconds ago   Up 36 seconds                                                           worker    

If there is any container is not running, you can restart it by:

make stop-and-remove-all
make start-up

2. REMOTE PROCEDURE CALLS

The unit test defined in ./master/rpc_test.go simulates a rpc call from master to worker.

We can test the RPC by running:

make rpc-call

The output should be like this:

docker-compose exec worker /bin/bash -c "go test -v ./master"
=== RUN   TestPerformTask
2024/05/01 08:22:42 We got: Hello, worker! gatech
--- PASS: TestPerformTask (0.00s)
PASS
ok      github.com/chiwency/workshop2/master    0.004s

We can see the test passed, and the master send "Hello, worker!" and received the response from worker.

2024/05/01 08:22:42 We got: Hello, worker! gatech

3. LEADER ELECTION

The master take election every 5 seconds to choose a leader. Once a leader is elected, it will do a rpc call ts address as the input, and print the log to show it's leadership. We can see the log by running:

make log

or you can see the any of the container log by:

make log-master1
make log-master2
make log-worker

The output should be like this:

master1  | 2024/05/01 08:32:32 I am the follower
master1  | 2024/05/01 08:32:37 I am the follower
master1  | 2024/05/01 08:32:42 I am the follower
master1  | 2024/05/01 08:32:47 I am the follower

master2  | 2024/05/01 08:32:45 I am the leader
master2  | 2024/05/01 08:32:45 I am the leader
master2  | 2024/05/01 08:32:45 I am the leader
master2  | 2024/05/01 08:32:45 I am the leader
master2  | 2024/05/01 08:32:45 I am the leader

worker  | 2024/05/01 08:19:30 server listening at [::]:7070
worker  | 2024/05/01 08:19:31 Performing task: leader_election
worker  | 2024/05/01 08:22:42 Performing task: 1

4.Failover

If you find the master2 is the leader, you can stop it by:

make stop-master2

After a while, you can then see the log of master1:

make log-master1

The output should be like this:

master1  | 2024/05/01 08:32:32 I am the follower
master1  | 2024/05/01 08:32:37 I am the follower
master1  | 2024/05/01 08:32:42 I am the follower
master1  | 2024/05/01 08:32:47 I am the follower
master1  | 2024/05/01 08:32:52 I am the leader
master1  | 2024/05/01 08:32:52 I am the leader
master1  | 2024/05/01 08:32:52 I am the leader
master1  | 2024/05/01 08:32:52 I am the leader
master1  | 2024/05/01 08:32:52 I am the leader
master1  | 2024/05/01 08:32:52 I am the leader

You can see the master1 become the leader after master2 is stopped.

Then you can restart master2 by:

make start-master2

After a while, you can then see the log of master1:

make log-master1

Or if you find the master1 is the leader, vice versa.

5.Clean UP

To clean up the environment, you can run:

make clean

workshop2's People

Contributors

chiwency avatar

Watchers

 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.