Skip to content

Commit

Permalink
Now reads are working, as is writing to the state machine, but writin…
Browse files Browse the repository at this point in the history
…g to the log is not.
  • Loading branch information
chelseatroy committed Jul 25, 2020
1 parent 6769696 commit a0331eb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
10 changes: 10 additions & 0 deletions logs/server_registry.txt
Expand Up @@ -50,3 +50,13 @@ every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
every localhost 10000
tom localhost 10001
29 changes: 23 additions & 6 deletions src/key_value_store.py
Expand Up @@ -31,7 +31,7 @@ def catch_up(self, path_to_logs=''):
f.close()

for command in log.split('\n'):
self.execute(command, term_absent=False, write=False)
self.write_to_state_machine(command, term_absent=False, write=False)

self.catch_up_successful = True

Expand All @@ -43,7 +43,7 @@ def get_latest_term(self):
return self.latest_term


def execute(self, string_operation, term_absent, write=True, path_to_logs=''):
def write_to_state_machine(self, string_operation, term_absent, write=True, path_to_logs=''):
print(string_operation)

if len(string_operation) == 0:
Expand All @@ -60,9 +60,7 @@ def execute(self, string_operation, term_absent, write=True, path_to_logs=''):
with self.client_lock:
self.latest_term = int(operands[term])

if operands[command] == "get":
response = self.get(operands[key])
elif operands[command] == "set":
if operands[command] == "set":
value = " ".join(operands[values:])

self.log.append(string_operation)
Expand All @@ -76,14 +74,33 @@ def execute(self, string_operation, term_absent, write=True, path_to_logs=''):
self.write_to_log(string_operation, term_absent=False, path_to_logs=path_to_logs)
self.delete(operands[key])
response = f"key {key} deleted"
else:
pass

return response

def read(self, string_operation):
print(string_operation)

if len(string_operation) == 0:
return

operands = string_operation.split(" ")
print("the read command is " + string_operation)
command, key, values = 0, 1, 2

response = "Sorry, I don't understand that command."

with self.client_lock:
if operands[command] == "get":
response = self.get(operands[key])
elif operands[command] == "show":
response = str(self.data)
else:
pass

return response


#used in leader server when client sends a command
def write_to_log(self, string_operation, term_absent, path_to_logs=''):
print(string_operation)
Expand Down
14 changes: 8 additions & 6 deletions src/server.py
Expand Up @@ -85,7 +85,7 @@ def mark_updated(self, server_name):
if trues > falses:
print("Committing entry: " + self.current_operation)
self.current_operation_committed = True
self.key_value_store.execute(self.current_operation, term_absent=True, write=False)
self.key_value_store.write_to_state_machine(self.current_operation, term_absent=True, write=False)
self.current_operation_committed = False
#how to get a message back to the client that it has been committed?

Expand Down Expand Up @@ -127,7 +127,7 @@ def respond(self, key_value_store, operation):
stringified_logs_to_append = string_operation.split(" ")[1]
print(stringified_logs_to_append)
logs_to_append = ast.literal_eval(stringified_logs_to_append)
[key_value_store.execute(log, term_absent=False) for log in logs_to_append]
[key_value_store.write_to_state_machine(log, term_absent=False) for log in logs_to_append]

response = "Append entries call successful!"

Expand All @@ -144,14 +144,16 @@ def respond(self, key_value_store, operation):
else:
if self.leader:
self.current_operation = string_operation
key_value_store.write_to_log(string_operation, term_absent=True)

if self.current_operation.split(" ")[0] in ["set", "delete"]:
key_value_store.write_to_log(string_operation, term_absent=True)
broadcast(self, with_return_address(self, "append_entries [" + self.current_operation + "]"))

while not self.current_operation_committed:
print("WAITITNG")
response = "Entry committed."
while not self.current_operation_committed:
print("WAITING")
response = "Entry committed."
else:
response = key_value_store.read(self.current_operation)
else:
response = "I am not the leader. Please leave me alone."

Expand Down
6 changes: 3 additions & 3 deletions tests/test_keyValueStore.py
Expand Up @@ -68,7 +68,7 @@ def test_execute_from_client(self):
self.clean_up_file(TEST_LOG_PATH)
kvs = KeyValueStore(server_name="DorianGray")

kvs.execute("set Sibyl cruelty", term_absent=True, write=True, path_to_logs=TEST_LOG_PATH)
kvs.write_to_state_machine("set Sibyl cruelty", term_absent=True, write=True, path_to_logs=TEST_LOG_PATH)

self.assert_on_file(path=TEST_LOG_PATH, length=1, lines="0 set Sibyl cruelty")
assert kvs.get("Sibyl") == "cruelty"
Expand All @@ -79,7 +79,7 @@ def test_execute_from_logs_upon_restart(self):
kvs.write_to_log(string_operation="0 set Sibyl cruelty", path_to_logs=TEST_LOG_PATH)
assert not kvs.get("Sibyl")

kvs.execute("0 set Sibyl cruelty", term_absent=False, write=False, path_to_logs=TEST_LOG_PATH)
kvs.write_to_state_machine("0 set Sibyl cruelty", term_absent=False, write=False, path_to_logs=TEST_LOG_PATH)

self.assert_on_file(path=TEST_LOG_PATH, length=1, lines="0 set Sibyl cruelty")
assert kvs.get("Sibyl") == "cruelty"
Expand All @@ -89,7 +89,7 @@ def test_execute_from_leader_catchup_command(self):
kvs = KeyValueStore(server_name="DorianGray")
assert not kvs.get("Sibyl")

kvs.execute("0 set Sibyl cruelty", term_absent=False, write=True, path_to_logs=TEST_LOG_PATH)
kvs.write_to_state_machine("0 set Sibyl cruelty", term_absent=False, write=True, path_to_logs=TEST_LOG_PATH)

self.assert_on_file(path=TEST_LOG_PATH, length=1, lines="0 set Sibyl cruelty")
assert kvs.get("Sibyl") == "cruelty"
Expand Down

0 comments on commit a0331eb

Please sign in to comment.