Commit 1ff4ee00 authored by amir.yosef's avatar amir.yosef

Update builder pattern

parent da928f4f
import director.Director;
import server.ReplicaConnectionService;
import server.Server; import server.Server;
import server.ServerBuilder; import server.ServerBuilder;
import java.io.IOException; import java.util.logging.Level;
import java.util.Arrays; import java.util.logging.Logger;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main { public class Main {
public static ExecutorService executor; private static final Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) { public static void main(String[] args) {
int availableProcessors = Runtime.getRuntime().availableProcessors(); logger.info("Starting application");
executor = Executors.newFixedThreadPool(availableProcessors);
Director director = new Director();
ServerBuilder builder = new ServerBuilder(args);
List<Callable<Void>> tasks = Arrays.asList(
() -> {
try { try {
ReplicaConnectionService service = null; ServerBuilder builder = new ServerBuilder(args);
director.buildReplica(builder, service);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
},
() -> {
try (Server server = builder.build()) { try (Server server = builder.build()) {
server.start(); server.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
);
try {
executor.invokeAll(tasks);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Task execution was interrupted", e);
} finally {
shutdownExecutor(executor);
}
}
private static void shutdownExecutor(ExecutorService executor) {
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
System.err.println("Executor did not terminate");
}
} }
} catch (InterruptedException e) { } catch (Exception e) {
executor.shutdownNow(); logger.log(Level.SEVERE, "An error occurred", e);
Thread.currentThread().interrupt();
} }
logger.info("Application shutdown complete");
} }
} }
\ No newline at end of file
...@@ -15,15 +15,26 @@ import java.util.logging.Logger; ...@@ -15,15 +15,26 @@ import java.util.logging.Logger;
public class Server implements AutoCloseable { public 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 ExecutorService executor; private final ExecutorService executor;
private final StorageManager manager = new StorageManager(); private final StorageManager manager;
private final ReplicaConnectionService replicaConnectionService;
public Server(int port) { Server(int port, String role, ReplicaConnectionService replicaConnectionService) {
logger.info("Creating Server instance");
this.PORT = port; this.PORT = port;
this.role = role;
this.executor = Executors.newVirtualThreadPerTaskExecutor(); this.executor = Executors.newVirtualThreadPerTaskExecutor();
this.manager = new StorageManager();
this.replicaConnectionService = replicaConnectionService;
} }
public void start() { public void start() {
logger.info("Starting server on port " + PORT + " with role " + role);
if ("slave".equals(role) && replicaConnectionService != null) {
replicaConnectionService.checkConnection();
}
try (ServerSocket serverSocket = new ServerSocket(PORT)) { try (ServerSocket serverSocket = new ServerSocket(PORT)) {
serverSocket.setReuseAddress(true); serverSocket.setReuseAddress(true);
logger.info("Server started on Port " + PORT); logger.info("Server started on Port " + PORT);
...@@ -61,13 +72,10 @@ public class Server implements AutoCloseable { ...@@ -61,13 +72,10 @@ public class Server implements AutoCloseable {
@Override @Override
public void close() { public void close() {
try { logger.info("Shutting down server");
if (!executor.isShutdown()) { if (!executor.isShutdown()) {
executor.shutdown(); executor.shutdown();
} }
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to shutdown executor", e);
}
manager.shutdown(); manager.shutdown();
} }
} }
...@@ -5,45 +5,56 @@ import util.Settings; ...@@ -5,45 +5,56 @@ import util.Settings;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.logging.Logger;
public class ServerBuilder { public class ServerBuilder {
private int port = 16379; private static final Logger logger = Logger.getLogger(ServerBuilder.class.getName());
private String role; private int port = 6379;
private final String[] masterPortAndHost; private String role = "master";
private final static ServerInfo info = ServerInfo.getInstance(); private String[] masterPortAndHost;
private final ServerInfo serverInfo;
private final RdbFileInfo rdbFileInfo;
private final Map<String, String> parameters; private final Map<String, String> parameters;
private static final RdbFileInfo rdbfileInfo = RdbFileInfo.getInstance(); private ReplicaConnectionService replicaConnectionService;
public ServerBuilder(String[] args) { public ServerBuilder(String[] args) {
logger.info("Initializing ServerBuilder");
this.parameters = Settings.extractArgs(args); this.parameters = Settings.extractArgs(args);
this.port = Settings.extractPort(parameters, port); this.port = Settings.extractPort(parameters, port);
this.masterPortAndHost = initializeMasterPortAndHost(); this.serverInfo = ServerInfo.getInstance();
this.rdbFileInfo = RdbFileInfo.getInstance();
initializeFromParameters();
} }
public ServerBuilder setPort(int port) { public ServerBuilder setPort(int port) {
logger.info("Setting port to " + port);
this.port = port; this.port = port;
return this; return this;
} }
public void setRole(String role) { public ServerBuilder setRole(String role) {
logger.info("Setting role to " + role);
this.role = role; this.role = role;
return this;
} }
public String[] getMasterPortAndHost() { public String[] getMasterPortAndHost() {
return Optional.ofNullable(masterPortAndHost).orElseGet(this::initializeMasterPortAndHost); return masterPortAndHost;
} }
public Server build() throws IOException { public Server build() throws IOException {
if (role == null) { logger.info("Building Server instance");
role = "master"; if ("slave".equals(role) && masterPortAndHost != null) {
this.replicaConnectionService = new ReplicaConnectionService(masterPortAndHost, port);
} }
return new Server(port); return new Server(port, role, replicaConnectionService);
} }
private String[] initializeMasterPortAndHost() { private void initializeFromParameters() {
rdbfileInfo.setFile(parameters); logger.info("Initializing from parameters");
return info.findRole(parameters); rdbFileInfo.setFile(parameters);
this.masterPortAndHost = serverInfo.findRole(parameters);
this.role = serverInfo.getRole();
logger.info("Initialized with role: " + 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