/** * Registers the slave server with the coordinator * * @param masterHostName * @param servr KVServer used by this slave server (contains the hostName and a random port) * @throws UnknownHostException * @throws IOException * @throws KVException */ public void registerWithMaster(String masterHostName, SocketServer server) throws UnknownHostException, IOException, KVException { AutoGrader.agRegistrationStart(slaveID); Socket master = new Socket(masterHostName, 9090); KVMessage regMessage = new KVMessage("register", slaveID + "@" + server.getHostname() + ":" + server.getPort()); regMessage.sendMessage(master); master.close(); AutoGrader.agRegistrationStart(slaveID); }
/** * Load log and rebuild KVServer by iterating over log entries. You do not need to restore the * previous cache state (i.e. ignore GETS). * * @throws KVException if an error occurs in KVServer (though we expect none) */ public void rebuildServer() throws KVException { KVMessage interruptedTPCOperation = null; loadFromDisk(); for (int i = 0; i < entries.size(); i++) { // System.out.println("rebuild server " + i); KVMessage entry = entries.get(i); String msgType = entry.getMsgType(); if (msgType.equals("putreq") || msgType.equals("delreq")) { interruptedTPCOperation = entry; continue; } if (msgType.equals("commit") && interruptedTPCOperation != null) { if (interruptedTPCOperation.getMsgType().equals("delreq")) { kvServer.del(interruptedTPCOperation.getKey()); } else if (interruptedTPCOperation.getMsgType().equals("putreq")) { kvServer.put(interruptedTPCOperation.getKey(), interruptedTPCOperation.getValue()); } interruptedTPCOperation = null; } else if (msgType.equals("abort")) { interruptedTPCOperation = null; } } }
@Override public void run() { // Receive message from client // Implement me KVMessage msg = null; // Parse the message and do stuff String key = msg.getKey(); if (msg.getMsgType().equals("putreq")) { handlePut(msg, key); } else if (msg.getMsgType().equals("getreq")) { handleGet(msg, key); } else if (msg.getMsgType().equals("delreq")) { handleDel(msg, key); } else if (msg.getMsgType().equals("ignoreNext")) { // Set ignoreNext to true. PUT and DEL handlers know what to do. // Implement me // Send back an acknowledgment // Implement me } else if (msg.getMsgType().equals("commit") || msg.getMsgType().equals("abort")) { // Check in TPCLog for the case when SlaveServer is restarted // Implement me handleMasterResponse(msg, originalMessage, aborted); // Reset state // Implement me } // Finally, close the connection closeConn(); }
/** * Load log and rebuild KVServer by iterating over log entries. You do not need to restore the * previous cache state (i.e. ignore GETS). * * @throws KVException if an error occurs in KVServer (though we expect none) */ public void rebuildServer() throws KVException { // implement me try { loadFromDisk(); kvServer.wipeEverything(); KVMessage request = null; for (KVMessage log : entries) { if (log.getMsgType().equals(PUT_REQ) || log.getMsgType().equals(DEL_REQ)) { request = log; } else if (log.getMsgType().equals(COMMIT)) { if (request == null) { // Do nothing. If reached here, repeated COMMIT continue; } else if (request.getMsgType().equals(PUT_REQ)) { kvServer.put(request.getKey(), request.getValue()); } else if (request.getMsgType().equals(DEL_REQ)) { kvServer.del(request.getKey()); } } else if (log.getMsgType().equals(ABORT)) { request = null; } } } catch (Exception e) { // throw new KVException("Error: Rebuild failed"); } }