Commit 08925d8d authored by mohammad.salama's avatar mohammad.salama

Third Commit: code to use ssh is implmented , problem so many workers

parent 5d0042c4
...@@ -32,5 +32,15 @@ ...@@ -32,5 +32,15 @@
<artifactId>zookeeper</artifactId> <artifactId>zookeeper</artifactId>
<version>3.9.1</version> <version>3.9.1</version>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -17,12 +17,19 @@ public class Application implements Watcher { ...@@ -17,12 +17,19 @@ public class Application implements Watcher {
public static int numberOfInstances; public static int numberOfInstances;
public static String pathToFile = ""; public static String pathToFile = "";
private static final String RELATIVE_PATH_TO_JARS = "/out/artifacts/";
/**
* input for jars to execute must be their relative paths
* inside the out/artifact/ directory
* */
public static void main(String[] args) throws IOException, InterruptedException, KeeperException { public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
int currentServerPort = args.length == 3 ? Integer.parseInt(args[0]) : DEFAULT_PORT; int currentServerPort = args.length == 3 ? Integer.parseInt(args[0]) : DEFAULT_PORT;
numberOfInstances = args.length == 3 ? Integer.parseInt(args[1]) : 100; numberOfInstances = args.length == 3 ? Integer.parseInt(args[1]) : 10;
pathToFile = args.length == 3 ? args[2] : "D:\\HIAST\\FIY\\FS\\Distributed Systems\\Lab\\6\\DS-06\\Registration&Discovery-AutoHealer\\out\\artifacts\\TransientWorker_jar\\Registration&Discovery-AutoHealer.jar";
pathToFile = args.length == 3 ? args[2] : "TransientWorker_jar/Registration&Discovery-AutoHealer.jar";
pathToFile = System.getProperty("user.dir") + RELATIVE_PATH_TO_JARS + pathToFile;
Application application = new Application(); Application application = new Application();
ZooKeeper zooKeeper = application.connectToZookeeper(); ZooKeeper zooKeeper = application.connectToZookeeper();
......
...@@ -8,7 +8,7 @@ import java.util.List; ...@@ -8,7 +8,7 @@ import java.util.List;
public class LeaderElection implements Watcher { public class LeaderElection implements Watcher {
private static final String AUTOHEALER_ZNODES_PATH = "/physical_nodes"; private static final String PHYSICAL_ZNODES_PATH = "/physical_nodes";
private String currentZnodeName; private String currentZnodeName;
private ZooKeeper zooKeeper; private ZooKeeper zooKeeper;
...@@ -20,12 +20,15 @@ public class LeaderElection implements Watcher { ...@@ -20,12 +20,15 @@ public class LeaderElection implements Watcher {
} }
public void volunteerForLeadership() throws InterruptedException, KeeperException { public void volunteerForLeadership() throws InterruptedException, KeeperException, UnknownHostException {
String znodePrefix = AUTOHEALER_ZNODES_PATH + "/node_";
String znodeFullPath = zooKeeper.create(znodePrefix, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); String znodePrefix = PHYSICAL_ZNODES_PATH + "/node_";
String IP = InetAddress.getLocalHost().getHostAddress();
String username = System.getProperty("user.name")+"@"+IP;
String znodeFullPath = zooKeeper.create(znodePrefix, username.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(znodeFullPath); System.out.println(znodeFullPath);
this.currentZnodeName = znodeFullPath.replace(AUTOHEALER_ZNODES_PATH + "/", ""); this.currentZnodeName = znodeFullPath.replace(PHYSICAL_ZNODES_PATH + "/", "");
} }
public void reelectLeader() throws InterruptedException, KeeperException { public void reelectLeader() throws InterruptedException, KeeperException {
...@@ -35,20 +38,12 @@ public class LeaderElection implements Watcher { ...@@ -35,20 +38,12 @@ public class LeaderElection implements Watcher {
//this while to guarantee get predecessor even if it deleted just before zookeeper.exist //this while to guarantee get predecessor even if it deleted just before zookeeper.exist
while (predecessorStat == null) while (predecessorStat == null)
{ {
List<String> children = zooKeeper.getChildren(AUTOHEALER_ZNODES_PATH, false); List<String> children = zooKeeper.getChildren(PHYSICAL_ZNODES_PATH, false);
Collections.sort(children); Collections.sort(children);
String smallestChild = children.get(0); //the first element String smallestChild = children.get(0); //the first element
if (smallestChild.equals(currentZnodeName)) { if (smallestChild.equals(currentZnodeName)) {
System.out.println("I'm a leader"); System.out.println("I'm a leader");
try
{
String IP = InetAddress.getLocalHost().getHostAddress();
String username = System.getProperty("user.name")+"@"+IP;
zooKeeper.setData(AUTOHEALER_ZNODES_PATH + "/"+currentZnodeName , username.getBytes() , -1);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
onElectionCallback.onElectedToBeLeader(); onElectionCallback.onElectedToBeLeader();
return; return;
} }
...@@ -56,7 +51,7 @@ public class LeaderElection implements Watcher { ...@@ -56,7 +51,7 @@ public class LeaderElection implements Watcher {
System.out.println("I'm not a leader"); System.out.println("I'm not a leader");
int predecessorIndex = children.indexOf(currentZnodeName) - 1; int predecessorIndex = children.indexOf(currentZnodeName) - 1;
predecessorName = children.get(predecessorIndex); predecessorName = children.get(predecessorIndex);
predecessorStat = zooKeeper.exists(AUTOHEALER_ZNODES_PATH + "/" + predecessorName, this); predecessorStat = zooKeeper.exists(PHYSICAL_ZNODES_PATH + "/" + predecessorName, this);
} }
} }
onElectionCallback.onWorker(); onElectionCallback.onWorker();
......
...@@ -10,7 +10,7 @@ import java.util.List; ...@@ -10,7 +10,7 @@ import java.util.List;
public class ServiceRegistry implements Watcher public class ServiceRegistry implements Watcher
{ {
private static final String WORKERS_ZNODES_PATH = "/workers"; private static final String WORKERS_ZNODES_PATH = "/workers";
private static final String AUTOHEALER_ZNODES_PATH = "/physical_nodes"; private static final String PHYSICAL_ZNODES_PATH = "/physical_nodes";
private String pathToProgram = ""; private String pathToProgram = "";
private final ZooKeeper zooKeeper; private final ZooKeeper zooKeeper;
...@@ -34,8 +34,8 @@ public class ServiceRegistry implements Watcher ...@@ -34,8 +34,8 @@ public class ServiceRegistry implements Watcher
if (zooKeeper.exists(WORKERS_ZNODES_PATH, false) == null) { if (zooKeeper.exists(WORKERS_ZNODES_PATH, false) == null) {
zooKeeper.create(WORKERS_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zooKeeper.create(WORKERS_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} }
if (zooKeeper.exists(AUTOHEALER_ZNODES_PATH, false) == null) { if (zooKeeper.exists(PHYSICAL_ZNODES_PATH, false) == null) {
zooKeeper.create(AUTOHEALER_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zooKeeper.create(PHYSICAL_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} }
} }
catch (KeeperException | InterruptedException e) { catch (KeeperException | InterruptedException e) {
...@@ -47,11 +47,11 @@ public class ServiceRegistry implements Watcher ...@@ -47,11 +47,11 @@ public class ServiceRegistry implements Watcher
System.out.println(metadata); System.out.println(metadata);
if (this.currentZnode != null) if (this.currentZnode != null)
{ {
zooKeeper.setData(AUTOHEALER_ZNODES_PATH + "/"+currentZnode , metadata.getBytes() , -1); zooKeeper.setData(PHYSICAL_ZNODES_PATH + "/"+currentZnode , metadata.getBytes() , -1);
System.out.println("Already registered to service registry"); System.out.println("Already registered to service registry");
return; return;
} }
this.currentZnode = zooKeeper.create(AUTOHEALER_ZNODES_PATH + "/node_", metadata.getBytes(), this.currentZnode = zooKeeper.create(PHYSICAL_ZNODES_PATH + "/node_", metadata.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Registered to service registry"); System.out.println("Registered to service registry");
...@@ -85,14 +85,15 @@ public class ServiceRegistry implements Watcher ...@@ -85,14 +85,15 @@ public class ServiceRegistry implements Watcher
updateAddresses(); updateAddresses();
launchWorkersIfNecessary(); launchWorkersIfNecessary();
} }
private synchronized void updateAddresses() throws KeeperException, InterruptedException private synchronized void updateAddresses() throws KeeperException, InterruptedException
{ {
List<String> workerZnodes = zooKeeper.getChildren(AUTOHEALER_ZNODES_PATH, this); List<String> workerZnodes = zooKeeper.getChildren(PHYSICAL_ZNODES_PATH, this);
List<String> addresses = new ArrayList<>(workerZnodes.size()); List<String> addresses = new ArrayList<>(workerZnodes.size());
for (String workerZnode : workerZnodes) { for (String workerZnode : workerZnodes) {
String workerFullPath = AUTOHEALER_ZNODES_PATH + "/" + workerZnode; String workerFullPath = PHYSICAL_ZNODES_PATH + "/" + workerZnode;
Stat stat = zooKeeper.exists(workerFullPath, false); Stat stat = zooKeeper.exists(workerFullPath, false);
if (stat == null) { if (stat == null) {
continue; continue;
...@@ -107,12 +108,8 @@ public class ServiceRegistry implements Watcher ...@@ -107,12 +108,8 @@ public class ServiceRegistry implements Watcher
System.out.println("The cluster addresses are: " + this.allServiceAddresses); System.out.println("The cluster addresses are: " + this.allServiceAddresses);
} }
/**
* this method misses the implementation of ssh to use startNewWorker
* */
private void launchWorkersIfNecessary() throws KeeperException, InterruptedException, IOException { private void launchWorkersIfNecessary() throws KeeperException, InterruptedException, IOException {
List<String> physicalZnodes = zooKeeper.getChildren(AUTOHEALER_ZNODES_PATH, false); List<String> physicalZnodes = zooKeeper.getChildren(PHYSICAL_ZNODES_PATH, false);
List<String> workers = zooKeeper.getChildren(WORKERS_ZNODES_PATH , this); List<String> workers = zooKeeper.getChildren(WORKERS_ZNODES_PATH , this);
int neededInstances = numberOfInstances - workers.size(); int neededInstances = numberOfInstances - workers.size();
...@@ -125,7 +122,7 @@ public class ServiceRegistry implements Watcher ...@@ -125,7 +122,7 @@ public class ServiceRegistry implements Watcher
while (neededInstances > 0 && sortedWorkers.size()>0) while (neededInstances > 0 && sortedWorkers.size()>0)
{ {
Stat stat = zooKeeper.exists(AUTOHEALER_ZNODES_PATH+"/"+sortedWorkers.get(index) ,false); Stat stat = zooKeeper.exists(PHYSICAL_ZNODES_PATH+"/"+sortedWorkers.get(index) ,false);
if (stat == null) if (stat == null)
{ {
code++; code++;
...@@ -135,13 +132,9 @@ public class ServiceRegistry implements Watcher ...@@ -135,13 +132,9 @@ public class ServiceRegistry implements Watcher
} }
continue; continue;
} }
/*
* ssh to execute pathToProgram in the coherent IP for the node
* */
startNewWorker(sortedWorkers.get(index)); startNewWorker(sortedWorkers.get(index));
neededInstances--; neededInstances--;
index = (index+1)%sortedWorkers.size(); index = (index+1)%sortedWorkers.size();
/// System.out.println("Worker " + uuuu);
} }
} }
...@@ -160,15 +153,31 @@ public class ServiceRegistry implements Watcher ...@@ -160,15 +153,31 @@ public class ServiceRegistry implements Watcher
private void startNewWorker(String physicalNode) throws IOException, InterruptedException, KeeperException private void startNewWorker(String physicalNode) throws IOException, InterruptedException, KeeperException
{ {
File file = new File(pathToProgram); File file = new File(pathToProgram);
String command = "java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar " + file.getName() + " " + physicalNode;
String ip = new String(zooKeeper.getData(AUTOHEALER_ZNODES_PATH+"/"+physicalNode , false, null)); String remoteUser = new String(zooKeeper.getData(PHYSICAL_ZNODES_PATH+"/"+physicalNode , false, null));
System.out.println("Sendig job to " + ip);
/* String remoteDirectory = "/JavaJars/";
* must execute command over ssh here !!
* */ String remoteJarFilePath = remoteDirectory + file.getName();
String scpCommand = "scp " + pathToProgram + " " + remoteUser + ":" + remoteDirectory;
String sshCommand = "ssh " + remoteUser +
" 'java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar " +
remoteJarFilePath + " ' ";
Process scpProcess = Runtime.getRuntime().exec(scpCommand);
scpProcess.waitFor();
if (scpProcess.exitValue() == 0) {
Process sshProcess = Runtime.getRuntime().exec(sshCommand);
}
Runtime.getRuntime().exec(command, null, file.getParentFile()); /**
*String command = "java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar " + file.getName() + " " + physicalNode;
* System.out.println("Sendig job to " + ip + " of node : " + physicalNode);
* Runtime.getRuntime().exec(command, null, file.getParentFile());
*/
} }
@Override @Override
......
...@@ -3,10 +3,10 @@ import ch.qos.logback.core.FileAppender; ...@@ -3,10 +3,10 @@ import ch.qos.logback.core.FileAppender;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class MyClass { public class TestLogging {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class); private static final Logger logger = LoggerFactory.getLogger(TestLogging.class);
/*public static void changeLogFile(String newLogFile) { /* public static void changeLogFile(String newLogFile) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
FileAppender fileAppender = (FileAppender) loggerContext.getLogger("ROOT").getAppender("FILE"); FileAppender fileAppender = (FileAppender) loggerContext.getLogger("ROOT").getAppender("FILE");
fileAppender.setFile(newLogFile); fileAppender.setFile(newLogFile);
...@@ -16,15 +16,19 @@ public class MyClass { ...@@ -16,15 +16,19 @@ public class MyClass {
loggerContext.getLogger("ROOT").addAppender(fileAppender); loggerContext.getLogger("ROOT").addAppender(fileAppender);
}*/ }*/
public static void someMethod() { public static void tryLogging() {
logger.debug("Debug log message"); logger.debug("Debug log message");
logger.info("Info log message"); logger.info("Info log message");
logger.error("Error log message"); logger.error("Error log message");
} }
public static void main(String[] args) { public static void main(String[] args) {
someMethod(); //tryLogging();
//changeLogFile("logs/MyClassLogs.logs"); //changeLogFile("logs/MyClassLogs.logs");
// someMethod(); // someMethod();
String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory: " + workingDir);
/// out put : D:\HIAST\FIY\FS\Distributed Systems\Lab\6\DS-06\Registration&Discovery-AutoHealer
/// out/artifacts/TransientWorker_jar/Registration&Discovery-AutoHealer.jar
} }
} }
\ No newline at end of file
...@@ -17,7 +17,7 @@ public class TransientWorkerApplication ...@@ -17,7 +17,7 @@ public class TransientWorkerApplication
worker.work(nodeNum); worker.work(nodeNum);
} }
catch (Exception e) { catch (Exception e) {
System.exit(1);
} }
} }
} }
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <Configuration status="WARN">
<Appenders> <Appenders name="FILE" target="ch.qos.logback.core.FileAppender">
<Console name="Console" target="SYSTEM_OUT"> <file>logs/mylog.log</file>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="WARN"> <Root level="WARN">
<AppenderRef ref="Console"/> <AppenderRef ref="FILE"/>
</Root> </Root>
</Loggers> </Loggers>
</Configuration> </Configuration>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <Configuration status="WARN">
<Appenders> <Appenders name="FILE" target="ch.qos.logback.core.FileAppender">
<Console name="Console" target="SYSTEM_OUT"> <file>logs/mylog.log</file>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="WARN"> <Root level="WARN">
<AppenderRef ref="Console"/> <AppenderRef ref="FILE"/>
</Root> </Root>
</Loggers> </Loggers>
</Configuration> </Configuration>
\ No newline at end of file
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