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