diff --git a/logs/server_registry.txt b/logs/server_registry.txt index 9cda444..bd50f18 100644 --- a/logs/server_registry.txt +++ b/logs/server_registry.txt @@ -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 diff --git a/src/key_value_store.py b/src/key_value_store.py index fda7c30..aab8ae7 100644 --- a/src/key_value_store.py +++ b/src/key_value_store.py @@ -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 @@ -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: @@ -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) @@ -76,6 +74,26 @@ 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: @@ -83,7 +101,6 @@ def execute(self, string_operation, term_absent, write=True, path_to_logs=''): 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) diff --git a/src/server.py b/src/server.py index c51ee25..da95e78 100644 --- a/src/server.py +++ b/src/server.py @@ -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? @@ -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!" @@ -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." diff --git a/tests/test_keyValueStore.py b/tests/test_keyValueStore.py index 27c10aa..855da03 100644 --- a/tests/test_keyValueStore.py +++ b/tests/test_keyValueStore.py @@ -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" @@ -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" @@ -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"