Example #1
0
 def test_read_write_chunk(self):
     cs = ChunkServer(env=env.MemEnv())
     cs.create_chunk("c1")
     msg = b"123456789"
     cs.write_chunk("c1", msg)
     res = cs.read_chunk("c1")
     self.assertEquals(msg, res)
Example #2
0
 def test_readrange(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     msg = "abcdefg"
     e.write(fd, msg)
     e.close(fd)
     fd = e.open("a.txt", "r")
     buf = e.readrange(fd, start_offset=1, end_offset=2)
     self.assertEquals(buf, msg[1:2])
Example #3
0
 def test_readall(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     msg = "abcdefg"
     e.write(fd, msg)
     e.close(fd)
     fd = e.open("a.txt", "r")
     buf = e.readall(fd)
     self.assertEquals(buf, msg)
Example #4
0
 def test_read_chunks(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     msg = "abcdefg"
     e.write(fd, msg)
     e.close(fd)
     fd = e.open("a.txt", "r")
     buf = "".join([e.read(fd, 1) for _ in range(len(msg))])
     self.assertEquals(buf, msg)
Example #5
0
 def test_remove(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     e.close(fd)
     fd = e.open("a.txt", "r")
     e.close(fd)
     e.remove("a.txt")
     with self.assertRaises(IOError):
         _ = e.open("a.txt", "r")
Example #6
0
 def test_seek(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     msg = "abcdefg"
     e.write(fd, msg)
     e.close(fd)
     fd = e.open("a.txt", "r")
     e.seek(fd, 2)
     buf = e.read(fd, 1)
     self.assertEquals(buf, msg[2])
Example #7
0
 def test_read_chunk_range(self):
     cs = ChunkServer(env=env.MemEnv())
     cs.create_chunk("c1")
     msg = b"123456789"
     cs.write_chunk("c1", msg)
     res = cs.read_chunk(
         chunkid="c1",
         start_offset=1,
         end_offset=3
     )
     self.assertEquals(res, msg[1:3])
Example #8
0
 def test_open_new(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
Example #9
0
 def test_open_close(self):
     e = env.MemEnv()
     fd = e.open("a.txt", "w+")
     e.close(fd)
     fd = e.open("a.txt", "r")
     e.close(fd)
Example #10
0
 def test_open_non_existent(self):
     e = env.MemEnv()
     with self.assertRaises(IOError):
         e.open("a.txt", "r")
Example #11
0
 def test_create_chunk(self):
     cs = ChunkServer(env=env.MemEnv())
     cs.create_chunk("c1")
     cs.write_chunk("c1", b"12345")
     _ = cs.read_chunk("c1")
Example #12
0
 def test_write_chunk_DNE(self):
     cs = ChunkServer(env=env.MemEnv())
     with self.assertRaises(IOError):
         cs.write_chunk("c1", b"12345")
Example #13
0
 def test_delete_chunk_DNE(self):
     cs = ChunkServer(env=env.MemEnv())
     with self.assertRaises(IOError):
         cs.delete_chunk("c1")
Example #14
0
 def test_delete_chunk(self):
     cs = ChunkServer(env=env.MemEnv())
     cs.create_chunk("c1")
     cs.delete_chunk("c1")
     with self.assertRaises(IOError):
         _ = cs.read_chunk("c1")
Example #15
0
def main():
    import sys
    import env

    args = sys.argv[1:]
    client_port = DEFAULT_CHUNK_SERVER_CLIENT_PORT
    master_addr = DEFAULT_CHUNK_SERVER_MASTER_ADDR
    for idx, arg in enumerate(args):
        if arg == "--client-port" and idx+1 < len(args):
            client_port = int(args[idx+1])
        if arg == "--master-addr" and idx+1 < len(args):
            l = args[idx+1].split(":")
            master_addr = (l[0], int(l[1]))

    print("Starting chunk server.")

    master_conn = socket.socket()
    try:
        master_conn.connect(master_addr)
    except socket.error as err:
        print("Unable to connect to master.")
        sys.exit(1)
    print("Connected to master at address {}".format(master_addr))

    client_listener = socket.socket()
    client_listener.bind(("", client_port))
    client_listener.listen(1)
    print("Listening for clients on port {}".format(client_port))

    readers = [client_listener, master_conn]
    chunkserver = ChunkServer(env=env.MemEnv())
    while True:
        rlist, _, _ = select.select(readers, [], [])
        for s in rlist:

            if s is client_listener:
                conn, addr = s.accept()
                print("Accepted client conn with {}".format(addr))
                readers.append(conn)
                continue

            msg = rpc.recvmsg(s)
            if not msg:
                print("Client {} closed conn.".format(s.getpeername()))
                s.close()
                readers.remove(s)
                continue

            method = msg["method"]
            print("RPC Call to method {}".format(method))

            resp = {}
            if method == "create_chunk":
                try:
                    chunkserver.create_chunk(msg["chunkid"])
                except Exception as err:
                    resp["error"] = str(err)
                    print(err)
            elif method == "delete_chunk":
                try:
                    chunkserver.delete_chunk(msg["chunkid"])
                except Exception as err:
                    resp["error"] = str(err)
                    print(err)
            elif method == "write_chunk":
                try:
                    chunkserver.write_chunk(
                        msg["chunkid"],
                        msg["data"]
                    )
                except Exception as err:
                    resp["error"] = str(err)
                    print(err)
            elif method == "read_chunk":
                try:
                    resp["chunk"] = chunkserver.read_chunk(msg["chunkid"])
                except Exception as err:
                    resp["error"] = str(err)
                    print(err)
            elif method == "get_client_port":
                resp["port"] = client_port
            elif method == "ping":
                resp["status"] = "ok"
            else:
                resp["error"] = "Unrecognized method: {}".format(method)
            rpc.sendmsg(s, resp)