Commit aa94992a authored by amir.yosef's avatar amir.yosef

Optimizing the nature of each class

parent 94618a3c
......@@ -10,7 +10,7 @@ import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class Client extends Thread {
public abstract class Client {
protected final CommandParser commandParser;
protected final Socket socket;
protected BufferedReader reader;
......@@ -27,7 +27,7 @@ public abstract class Client extends Thread {
this.socket = socket;
}
@Override
public void run() {
try (OutputStream outputStream = socket.getOutputStream()) {
if (reader == null) {
......
......@@ -12,7 +12,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MasterClient extends Client {
public final class MasterClient extends Client {
private static final Logger logger = Logger.getLogger(MasterClient.class.getName());
public MasterClient(Socket socket) {
......@@ -31,7 +31,7 @@ public class MasterClient extends Client {
commandHandler.execute();
}
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(),e);
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
......@@ -15,7 +15,7 @@ import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReplicaClient extends Client {
public final class ReplicaClient extends Client {
private final CommandByteCounter commandByteCounter;
private final ExecutorService executorService;
private static final Logger logger = Logger.getLogger(ReplicaClient.class.getName());
......@@ -38,7 +38,7 @@ public class ReplicaClient extends Client {
executorService.submit(command);
}
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(),e);
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
......
......@@ -4,7 +4,7 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CommandInvoker {
public final class CommandInvoker {
private static final Logger logger = Logger.getLogger(CommandInvoker.class.getName());
public static byte[] invoke(CommandExecutable<byte[]> command) throws IOException {
......
......@@ -4,7 +4,7 @@ import util.Response;
import java.util.List;
public class EchoCommand implements CommandExecutable<byte[]> {
public final class EchoCommand implements CommandExecutable<byte[]> {
private final List<String> args;
private final CommandValidator validator = CommandValidator.getInstance();
......
......@@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FullRsyncCommand implements CommandExecutable<byte[]> {
public final class FullRsyncCommand implements CommandExecutable<byte[]> {
private final SendToReplica replicaSender;
private final RdbFileInfo rdbFileInfo;
private final OutputStream outputStream;
......
......@@ -6,7 +6,7 @@ import util.Response;
import java.util.List;
public class GetCommand implements CommandExecutable<byte[]> {
public final class GetCommand implements CommandExecutable<byte[]> {
private final Storage storage = Storage.getInstance();
private final List<String> args;
private final CommandValidator validator = CommandValidator.getInstance();
......
......@@ -7,7 +7,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class InfoCommand implements CommandExecutable<byte[]> {
public final class InfoCommand implements CommandExecutable<byte[]> {
private final ServerInfo configuration = ServerInfo.getInstance();
private final CommandValidator validator = CommandValidator.getInstance();
List<String> args;
......
......@@ -3,7 +3,7 @@ package command;
import model.Command;
public class PingCommand implements CommandExecutable<byte[]> {
public final class PingCommand implements CommandExecutable<byte[]> {
@Override
public byte[] execute() {
......
......@@ -4,7 +4,7 @@ import model.Command;
import java.util.List;
public class ReplConfCommand implements CommandExecutable<byte[]> {
public final class ReplConfCommand implements CommandExecutable<byte[]> {
private final List<String> args;
......
......@@ -5,7 +5,7 @@ import util.Response;
import java.util.List;
public class SetCommand implements CommandExecutable<byte[]> {
public final class SetCommand implements CommandExecutable<byte[]> {
private static final byte[] OK_RESPONSE = "+OK\r\n".getBytes();
private static final byte[] ERROR_RESPONSE = Response.getResponse("wrong args");
......
......@@ -3,7 +3,7 @@ package command;
import java.util.Objects;
public class UnknownCommand implements CommandExecutable<byte[]> {
public final class UnknownCommand implements CommandExecutable<byte[]> {
private static String message;
public UnknownCommand(String message) {
......
......@@ -7,7 +7,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReplicaSetCommand implements CommandExecutable<Void> {
public final class ReplicaSetCommand implements CommandExecutable<Void> {
private final Storage storage = Storage.getInstance();
private final List<String> args;
private static final Logger logger = Logger.getLogger(ReplicaSetCommand.class.getName());
......
......@@ -16,7 +16,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClientCommandHandler {
public final class ClientCommandHandler {
private static final Logger logger = Logger.getLogger(ClientCommandHandler.class.getName());
private final List<String> commands;
private final OutputStream os;
......
......@@ -15,7 +15,7 @@ import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConnectionHandler {
public final class ConnectionHandler {
private static final Logger logger = Logger.getLogger(ConnectionHandler.class.getName());
private final Socket socket;
......
......@@ -12,7 +12,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReplicaCommandHandler implements CommandProcessable {
public final class ReplicaCommandHandler implements CommandProcessable {
private static final Logger logger = Logger.getLogger(ReplicaCommandHandler.class.getName());
private final List<String> commands;
private final ReplicaCommandFactory factory = ReplicaCommandFactory.getInstance();
......
......@@ -13,7 +13,7 @@ import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReplicaConnectionService {
public final class ReplicaConnectionService implements AutoCloseable {
private static final Logger logger = Logger.getLogger(ReplicaConnectionService.class.getName());
private final CommandParser commandParser;
private final int port;
......@@ -23,7 +23,7 @@ public class ReplicaConnectionService {
public ReplicaConnectionService(String[] masterPortAndHost, int port) throws IOException {
this.port = port;
this.commandParser = new CommandParser();
this.executorService = Executors.newCachedThreadPool();
this.executorService = Executors.newSingleThreadExecutor();
if (masterPortAndHost.length > 0) {
this.socket = new Socket(masterPortAndHost[0], Integer.parseInt(masterPortAndHost[1]));
......@@ -41,7 +41,7 @@ public class ReplicaConnectionService {
try {
logger.info("Ending handshake");
Client client = new ReplicaClient(bufferedReader, socket);
executorService.submit(client);
executorService.submit(client::run);
logger.info("ReplicaConnectionService: CONNECTION SUCCESS");
} catch (Exception e) {
throw new RuntimeException(e);
......@@ -57,6 +57,7 @@ public class ReplicaConnectionService {
}
}
@Override
public void close() {
executorService.shutdown();
if (socket != null && !socket.isClosed()) {
......
......@@ -24,7 +24,7 @@ public class SendToReplica implements Closeable {
private SendToReplica() {
this.connectedReplicas = new ConcurrentLinkedQueue<>();
this.commands = new ConcurrentLinkedQueue<>();
this.executorService = Executors.newSingleThreadExecutor();
this.executorService = Executors.newVirtualThreadPerTaskExecutor();
this.countCommands = new AtomicInteger(0);
}
......@@ -50,9 +50,9 @@ public class SendToReplica implements Closeable {
OutputStream outputStream = replica.os();
outputStream.write(commandBytes);
outputStream.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to send command to replica ", e);
connectedReplicas.remove(replica);
}
});
}
......
......@@ -12,7 +12,7 @@ import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server implements AutoCloseable {
public final class Server implements AutoCloseable {
private static final Logger logger = Logger.getLogger(Server.class.getName());
private final int PORT;
private final String role;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment