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