1、 Spinnaker Labs, Inc.,Google Cluster Computing Faculty Training Workshop,Module II: Student Background Knowledge,This presentation includes course content University of Washington Redistributed under the Creative Commons Attribution 3.0 license. All other contents:, Spinnaker Labs, Inc.,Background T
2、opics,Programming Languages Systems: Operating Systems File Systems Networking Databases, Spinnaker Labs, Inc.,Programming Languages,MapReduce is based on functional programming map and fold FP is taught in one quarter, but not reinforced “Crash course” necessary Worksheets to pose short problems in
3、 terms of map and fold Immutable data a key concept, Spinnaker Labs, Inc.,Multithreaded programming,Taught in OS course at Washington Not a prerequisite! Students need to understand multiple copies of same method running in parallel, Spinnaker Labs, Inc.,File Systems,Necessary to understand GFS Comp
4、arison to NFS, other distributed file systems relevant, Spinnaker Labs, Inc.,Networking,TCP/IP Concepts of “connection,” network splits, other failure modes Bandwidth issues, Spinnaker Labs, Inc.,Other Systems Topics,Process Scheduling Synchronization Memory coherency, Spinnaker Labs, Inc.,Databases
5、,Concept of shared consistency model Consensus ACID characteristics Journaling Multi-phase commit processes, Spinnaker Labs, Inc.,Parallelization & Synchronization, Spinnaker Labs, Inc.,Parallelization Idea,Parallelization is “easy” if processing can be cleanly split into n units:, Spinnaker Labs, I
6、nc.,Parallelization Idea (2),In a parallel computation, we would like to have as many threads as we have processors. e.g., a four-processor computer would be able to run four threads at the same time., Spinnaker Labs, Inc.,Parallelization Idea (3), Spinnaker Labs, Inc.,Parallelization Idea (4), Spin
7、naker Labs, Inc.,Parallelization Pitfalls,But this model is too simple! How do we assign work units to worker threads? What if we have more work units than threads? How do we aggregate the results at the end? How do we know all the workers have finished? What if the work cannot be divided into compl
8、etely separate tasks?,What is the common theme of all of these problems?, Spinnaker Labs, Inc.,Parallelization Pitfalls (2),Each of these problems represents a point at which multiple threads must communicate with one another, or access a shared resource.Golden rule: Any memory that can be used by m
9、ultiple threads must have an associated synchronization system!, Spinnaker Labs, Inc.,What is Wrong With This?,Thread 1: void foo() x+;y = x; ,Thread 2: void bar() y+;x+; ,If the initial state is y = 0, x = 6, what happens after these threads finish running?,Multithreaded = Unpredictability,When we
10、run a multithreaded program, we dont know what order threads run in, nor do we know when they will interrupt one another.,Thread 1: void foo() eax = memx;inc eax;memx = eax;ebx = memx;memy = ebx; ,Thread 2: void bar() eax = memy;inc eax;memy = eax;eax = memx;inc eax;memx = eax; ,Many things that loo
11、k like “one step” operations actually take several steps under the hood:, Spinnaker Labs, Inc.,Multithreaded = Unpredictability,This applies to more than just integers:Pulling work units from a queue Reporting work back to master unit Telling another thread that it can begin the “next phase” of proc
12、essing All require synchronization!, Spinnaker Labs, Inc.,Synchronization Primitives,A synchronization primitive is a special shared variable that guarantees that it can only be accessed atomically. Hardware support guarantees that operations on synchronization primitives only ever take one step,Sem
13、aphores,A semaphore is a flag that can be raised or lowered in one step Semaphores were flags that railroad engineers would use when entering a shared track,Only one side of the semaphore can ever be red! (Can both be green?), Spinnaker Labs, Inc.,Semaphores,set() and reset() can be thought of as lo
14、ck() and unlock() Calls to lock() when the semaphore is already locked cause the thread to block.Pitfalls: Must “bind” semaphores to particular objects; must remember to unlock correctly, Spinnaker Labs, Inc.,The “Corrected” Example,Thread 1:void foo() sem.lock();x+;y = x;sem.unlock(); ,Thread 2:voi
15、d bar() sem.lock();y+;x+;sem.unlock(); ,Global var “Semaphore sem = new Semaphore();” guards access to x & y, Spinnaker Labs, Inc.,Condition Variables,A condition variable notifies threads that a particular condition has been met Inform another thread that a queue now contains elements to pull from
16、(or that its empty request more elements!)Pitfall: What if nobodys listening?,The final example,Thread 1:void foo() sem.lock();x+;y = x;fooDone = true;sem.unlock();fooFinishedCV.notify(); ,Thread 2:void bar() sem.lock();while(!fooDone) fooFinishedCV.wait(sem);y+;x+;sem.unlock(); ,Global vars: Semaph
17、ore sem = new Semaphore(); ConditionVar fooFinishedCV = new ConditionVar(); boolean fooDone = false;, Spinnaker Labs, Inc.,Barriers,A barrier knows in advance how many threads it should wait for. Threads “register” with the barrier when they reach it, and fall asleep. Barrier wakes up all registered
18、 threads when total count is correctPitfall: What happens if a thread takes a long time?, Spinnaker Labs, Inc.,Too Much Synchronization? Deadlock,Synchronization becomes even more complicated when multiple locks can be usedCan cause entire system to “get stuck”,Thread A: semaphore1.lock(); semaphore
19、2.lock(); /* use data guarded by semaphores */ semaphore1.unlock(); semaphore2.unlock();,Thread B: semaphore2.lock(); semaphore1.lock(); /* use data guarded by semaphores */ semaphore1.unlock(); semaphore2.unlock();,(Image: RPI CSCI.4210 Operating Systems notes), Spinnaker Labs, Inc.,And if you thou
20、ght I was joking, Spinnaker Labs, Inc.,The Moral: Be Careful!,Synchronization is hard Need to consider all possible shared state Must keep locks organized and use them consistently and correctly Knowing there are bugs may be tricky; fixing them can be even worse! Keeping shared state to a minimum re
21、duces total system complexity, Spinnaker Labs, Inc.,Fundamentals of Networking, Spinnaker Labs, Inc.,Sockets: The Internet = tubes?,A socket is the basic network interface Provides a two-way “pipe” abstraction between two applications Client creates a socket, and connects to the server, who receives
22、 a socket representing the other side, Spinnaker Labs, Inc.,Ports,Within an IP address, a port is a sub-address identifying a listening program Allows multiple clients to connect to a server at once, Spinnaker Labs, Inc.,Example: Web Server (1/3),The server creates a listener socket attached to a sp
23、ecific port. 80 is the agreed-upon port number for web traffic., Spinnaker Labs, Inc.,Example: Web Server (2/3),The client-side socket is still connected to a port, but the OS chooses a random unused port numberWhen the client requests a URL (e.g., “”), its OS uses a system called DNS to find its IP
24、 address., Spinnaker Labs, Inc.,Example: Web Server (3/3),Server chooses a randomly-numbered port to handle this particular client Listener is ready for more incoming connections, while we process the current connection in parallel,What makes this work?,Underneath the socket layer are several more p
25、rotocols Most important are TCP and IP (which are used hand-in-hand so often, theyre often spoken of as one protocol: TCP/IP),Even more low-level protocols handle how data is sent over Ethernet wires, or how bits are sent through the air using 802.11 wireless, Spinnaker Labs, Inc.,IP: The Internet P
26、rotocol,Defines the addressing scheme for computers Encapsulates internal data in a “packet”Does not provide reliability Just includes enough information for the data to tell routers where to send it, Spinnaker Labs, Inc.,TCP: Transmission Control Protocol,Built on top of IP Introduces concept of “c
27、onnection” Provides reliability and ordering, Spinnaker Labs, Inc.,Why is This Necessary?,Not actually tube-like “underneath the hood” Unlike phone system (circuit switched), the packet switched Internet uses many routes at once, Spinnaker Labs, Inc.,Networking Issues,If a party to a socket disconne
28、cts, how much data did they receive? Did they crash? Or did a machine in the middle? Can someone in the middle intercept/modify our data? Traffic congestion makes switch/router topology important for efficient throughput, Spinnaker Labs, Inc.,Final Thoughts,Various background topics fit into this course Two examples highlighted Other background topics may benefit from expansion, worksheets, reinforcement,