lab/
lib.rs

1//! ## CSE 223B Labs
2//!
3//! Welcome to CSE 223B. Below you'll find some information on setting up an
4//! environment to complete the labs for the course. Instructions for each lab
5//! can be found at
6//!
7//! - [Lab 1](lab1)
8//! - [Lab 2](lab2)
9//! - [Lab 3](lab3)
10//!
11//! ## Programming Language
12//!
13//! You will write the labs in the [Rust](https://www.rust-lang.org/)
14//! programming language. It is a community-developed language originally
15//! started at Mozilla. Rust is a low-level language which eschews any kind of
16//! managed runtime like you might find in Java, Python, or Go. Rust's main
17//! selling point is it's **compile-time** [borrow
18//! checker](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
19//! that prevents invalid memory accesses at runtime.
20//!
21//! Here is some key documentation on the Rust programming language:
22//!
23//! - ["The Book"](https://doc.rust-lang.org/book/title-page.html)
24//! - [Rust By Example](https://doc.rust-lang.org/rust-by-example/)
25//! - [Learn Rust Page](https://www.rust-lang.org/learn)
26//! - [The Rust Playground](https://play.rust-lang.org/)
27//!
28//! You should be able to find a lot of documents about the Rust language on the
29//! web, especially from the official site. **[We highly recommend reading "The
30//! Book"](https://doc.rust-lang.org/book/title-page.html)** and following parts
31//! of the [Rustlings](https://github.com/rust-lang/rustlings/) course.
32//!
33//! **Rust takes time to understand and has a steep learning curve**. In the
34//! early stages of using the language, getting your program just to compile
35//! might take you 30 minutes or more. **Do not underestimate the amount of time
36//! you will need** to spend working on these assignments. Start early, **[read
37//! the book](https://doc.rust-lang.org/book/title-page.html)**, and search on
38//! the web if you have problems compiling your program. **Read the compiler
39//! error messages too**. The Rust compiler has some of the most helpful error
40//! messages of all modern languages. If that doesn't help, try **[reading the
41//! book again](https://doc.rust-lang.org/book/title-page.html)**. Finally, if
42//! you still can't figure out your issue ask for help on piazza or come to
43//! office hours if you truly feel stuck.
44//!
45//! **[READ THE BOOK](https://doc.rust-lang.org/book/title-page.html)**
46//!
47//! ---
48//!
49//! ### Install Rust
50//!
51//! We recommend using the `vscode DevContainer` as your development environment.
52//! You can find the setup instructions in the README file in the root of your
53//! repo. All rust tools are already installed in the container. If you want to
54//! install rust on your local machine, we recommend using the official
55//! [`rustup`](http://rustup.sh/) tool. This will install the latest stable Rust
56//! toolchain along with a number of programs useful for building applications
57//! with the language.
58//!
59//! #### Cargo
60//!
61//! The most important tool you'll need from the toolchain is `cargo`. `cargo`
62//! is a user-friendly wrapper around the rust compiler (rustc) and handles
63//! downloading, compiling, testing, formatting, checking, linting, documenting
64//! Rust code, and more.
65//!
66//! Cargo looks for a manifest file, `Cargo.toml` in your current directory to
67//! figure out exactly which files it needs to build, test, or run. The file can
68//! define dependencies, build options, metadata and many other options. You
69//! shouldn't need to modify any `Cargo.toml` files for these assignments.
70//!
71//! Here are some of the most useful cargo commands
72//!
73//! - `cargo build` will compile your code with no optimization
74//!     - `cargo build --release` compiles with optimizations (e.g. similar to
75//!       `gcc -O2`)
76//!     - all compilation output can be found in the `./target` directory
77//! - `cargo test` runs all of the tests defined in the projects
78//!     - `cargo test --tests` runs integration tests
79//!     - `cargo test -p <package>` runs all of the tests in `<package>`
80//! - `cargo fmt` will format your code according to standard Rust formatting
81//!   guidelines
82//! - `cargo check` looks for compilation errors in packages and dependencies.
83//!   It basically performs compilation without the final step of code
84//!   generation. It is faster than `cargo build` for checking if your code
85//!   compiles.
86//! - `cargo run` can run binaries defined in `Cargo.toml`
87//!     - `cargo run --bin <bin-name>` will run the `<bin-name>` binary
88//!     - pass arguments to the binary with `cargo run --bin <bin-name> --
89//!       <arg1> <arg2> ...`
90//!
91//! #### Clippy
92//!
93//! Rust also has a non-default code linting tool called Clippy. It can help
94//! point out simple optimizations and make your code more idiomatic. Clippy is
95//! entirely optional to use, but is immensely helpful in identifying use of
96//! anti-patterns in your code.
97//!
98//! - To install clippy: `rustup component add clippy`, then try running with
99//!   `cargo clippy`
100//!
101//! #### Basic Rust Concepts
102//!
103//! It is difficult to cover all of Rust's great (and not-so great) features. We
104//! recommend thoroughly reading through ["The
105//! Book"](https://doc.rust-lang.org/book/title-page.html) or [Rust By
106//! Example](https://doc.rust-lang.org/rust-by-example/) to learn the ins and
107//! outs of the language. Below we try to highlight some of the most important
108//! concepts you should understand.
109//!
110//! - Rust code is organized into *crates* and *modules*
111//!   - a set of modules make up a crate.
112//! - Compile, run, and format code with `cargo` (`build`, `run`, `fmt`)
113//! - Once cargo is installed, view documentation for the crate and dependencies
114//!   with `cargo doc --open`
115//! - In Rust, variables must be explicitly defined as mutable using the `mut`
116//!   keyword.
117//! - References to variables in Rust can be *owned* or *borrowed*.
118//! - `Vec<T>` is the standard type used to represent a series of items, similar
119//!   to C++. Array types (`[T; usize]`) are rarely used. Usually you should opt
120//!   for a slice (`&[T]`)
121//! - `std::collection::HashMap` or `std::collections::BTreeMap` can be used to
122//!   store a set of key-value pairs.
123//! - The `Result<T, E>` is the standard return type used for functions which
124//!   might return errors. When calling a function which returns a result, use
125//!   the `?` operator after the `()` to unwrap the result to type `T`
126//! - some built-in macros that you might find useful are `unimplemented!()`,
127//!   `todo!()`, `println!()`, `print!()`, and `format!()`.
128//! - you can write a loop using the `loop` keyword and run a function over an
129//!   iterator with `for x in a.iter()`
130//! - values are returned from functions by simply omitting the trailing
131//!   semicolon `;` at the end of a statement.
132//! - Store items on the heap using the `Box<T>` type.
133//! - You can write interfaces in Rust using the `trait` keyword, and then
134//! implement a trait for a specific struct with `impl <Trait> for <Struct> {
135//! ... }`
136//!
137//! ## The Tribbler Story
138//!
139//! Some cowboy programmer wrote a simple online microblogging service called
140//! Tribbler and, leveraging the power of the Web, it becomes quite popular.
141//! However, the program runs in a single process on a single machine; it does
142//! not scale, cannot support many concurrent connections, and is vulnerable to
143//! machine crashes. Knowing that you are taking the distributed computing
144//! system course at UCSD, he asks you for help. You answered his call and are
145//! starting work on this project.
146//!
147//! Your goal is to refactor Tribbler into a distributed system, making it more
148//! robust and scalable.
149//!
150//! ## Getting Started
151//!
152//! We will be using Github Classroom to distribute assignments and manage
153//! submissions. We will send out invitation links for each individual lab
154//! through Piazza/Canvas. Once you access these links, a GitHub repository
155//! containing Tribbler starter code will be created for you. It comes with
156//! necessary instructions on how to setup environment before you start, and
157//! submit your work when you are done.
158//!
159//! Once you download your first lab (and before you start working on it), you
160//! can run the basic version of Tribbler. The Tribbler project is written in
161//! Rust. To get started, run these commands from the command line:
162//!
163//! You can do some basic testing to see if the framework is in good shape:
164//!
165//! ```console
166//! $ cargo test -p tribbler
167//! ```
168//!
169//! The basic Tribbler service should now be installed on the system from your
170//! home directory. Let's give it a try:
171//!
172//! ```console
173//! $ cargo run --bin trib-front
174//! ```
175//!
176//! The program should show the URL it is running under.
177//!
178//! Open your browser and type in the address: `http://<host-name>:<port>`. For
179//! example, if you are using your local machine, and Tribbler is running on
180//! port 8080, then open `http://localhost:8080`. If you are using AWS EC2
181//! machine, you can use its public DNS name as host name (make sure to allow
182//! traffic on this port to your instance). You should see a list of Tribbler
183//! users. You can view their tribs and login as them (with no authentication).
184//!
185//! This is how Tribbler looks to users.  It is a single web page that performs
186//! AJAX calls (a type of web-based RPC) to the back-end web server. The
187//! webserver then in turn calls the Tribbler logic functions and returns the
188//! results back to the Web page in the browser.
189//!
190//! ## Source Code Organization
191//!
192//! The source code in the `trib` package repository is organized as follows:
193//!
194//! - `lab` has the skeleton code for completing the class assignments
195//! - [tribbler] defines the common Tribbler interfaces and data structures.
196//! - [tribbler::ref_impl] is a reference monolithic implementation of the
197//!   [Server](tribbler::trib::Server) interface. All the server logic runs in
198//!   one single process. It is not scalable and is vulnerable to machine
199//!   crashes.
200//! - [tribbler::storage] contains an in-memory thread-safe implementation of
201//!   the [tribbler::storage::Storage] interface. We will use this as the basic
202//!   building block for our back-end storage system.
203//! - [tribbler::addr] provides helper functions that check if an address
204//!   belongs to the machine that the program is running.
205//! - [tribbler::addr::rand] provides helper functions that generate a network
206//!   address with a random port number.
207//! - [tribbler::colon] provides helper functions that escape and unescape
208//!   colons in a string.
209//! - `lab/tests` provides several basic test cases for the interfaces.
210//! - `cmd/src/trib_front.rs` is the web-server launcher that you run.
211//! - `cmd/src/kv_client.rs` is a command line key-value RPC client for quick
212//!   testing.
213//! - `cmd/src/kv_server.rs` runs a key-value service as an RPC server.
214//! - `cmd/src/bins_client.rs` is a bin storage service client.
215//! - `cmd/src/bins_back.rs` is a bin storage service back-end launcher.
216//! - `cmd/src/bins_keep.rs` is a bin storage service keeper launcher.
217//! - `cmd/src/bins_mkcfg.rs` generates a bin storage configuration file.
218//! - `www/` contains the static files (html, css, js, etc.) for the web
219//!   front-end.
220//!
221//! **Don't be scared by the number of modules**. Most of the modules are very
222//! small, and you don't have to interact with all of them at once. All Rust
223//! language files under the `tribbler` directory are less than 2500 lines in
224//! total, so these packages aren't huge and intimidating.
225//!
226//! Throughout the entire lab, you do not need to (and should not) modify
227//! anything in the [tribbler] crate. If you feel that you have to change some
228//! code to complete your lab, please first discuss it with the TA. You are
229//! always welcome to read the code in the tribbler crate or read its
230//! [documentation][tribbler]. If you find a bug and report it, you might get
231//! some bonus credit.
232//!
233//! ## Your Job
234//!
235//! Your job is to complete the implementation of the `lab` package.
236//!
237//! It would be good practice for you to periodically commit your code into your
238//! git repo.
239//!
240//! ## Lab Roadmap
241//!
242//! - [Lab 1](crate::lab1). Wrap the key-value storage service with RPC so that
243//!   a remote client can store data remotely.
244//! - [Lab 2](crate::lab2). Reimplement the Tribbler service, splitting the
245//!   current Tribbler logic into stateless scalable front-ends and scalable
246//!   key-value store back-ends. The front-ends will call the back-ends via the
247//!   RPC mechanism implemented in Lab 1. When this lab is done, you will have
248//!   made both the front-end and the back-end scalable.
249//! - [Lab 3](crate::lab3). We make the back-ends fault-tolerent with
250//!   replication and by using techniques like distributed hash tables. At the
251//!   end of this lab, back-end servers can join, leave, or be killed, without
252//!   affecting the service.
253//!
254//! By the end of the labs, you will have an implementation of Tribbler that is
255//! scalable and fault-tolerant.
256//!
257//! ## Misc
258//!
259//! You can write your code on your own machine if you want to. See `rustup`
260//! language's [install](http://rustup.sh/) page for more information on how to
261//! install in different environments
262//!
263//! Unless noted by the TA or instructor, **no dependencies should be added to
264//! `Cargo.toml`**. If you feel you need to add an additional dependency to
265//! complete one of the lab assignments, consult with the instructor or TA.
266//!
267//! ## Ready?
268//!
269//! If you feel comfortable with the lab setup, continue on to [Lab 1](lab1).
270//!
271#![doc(
272    html_logo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Creative-Tail-Animal-penguin.svg/480px-Creative-Tail-Animal-penguin.svg.png"
273)]
274#![doc(
275    html_favicon_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Creative-Tail-Animal-penguin.svg/128px-Creative-Tail-Animal-penguin.svg.png?20160314145218"
276)]
277mod keeper;
278pub mod lab1;
279pub mod lab2;
280pub mod lab3;