Commit a029c01e authored by mohammad.salama's avatar mohammad.salama

Third Commit : Working with Scheduling and Logging

parent 750bb9e5
<component name="ArtifactManager">
<artifact type="jar" name="AutoHealer:jar">
<output-path>$PROJECT_DIR$/out/artifacts/AutoHealer_jar</output-path>
<root id="archive" name="Registration&amp;Discovery-AutoHealer.jar">
<element id="module-output" name="Registration&amp;Discovery-AutoHealer" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-handler/4.1.94.Final/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-buffer/4.1.94.Final/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-codec/4.1.94.Final/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/yetus/audience-annotations/0.12.0/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper-jute/3.9.1/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-common/4.1.94.Final/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-classes-epoll/4.1.94.Final/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-classes/2.0.61.Final/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.32/slf4j-api-1.7.32.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-unix-common/4.1.94.Final/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper/3.9.1/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-core/1.2.10/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-resolver/4.1.94.Final/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-epoll/4.1.94.Final/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport/4.1.94.Final/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
</root>
</artifact>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
File added
to Run AutoHealer.jar:
java -jar AutoHealer.jar <port_number> <number_of_workers_to_maintain_on_cluster> <username@host_ip>
and Worker.jar must be with in the same directory as AutoHealer.jar
logs are written in logs/app.log
we must start windows first then virtual machines
SSH Command FAILURE !!:
from windows to windows
from linux to windows
from linux to linux
if we started at windows first then every thing works fine.
how Linux Must be configured:
AutoHealer.jar and Worker.jar must be on linux at : /root/AutoHealer/.
cd /root/AutoHealer/
java -jar AutoHealer.jar <port_number> <number_of_workers_to_maintain_on_cluster> <username@host_ip>
\ No newline at end of file
File added
package AutoHealerAndLoadBalancing;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -17,18 +18,15 @@ public class Application implements Watcher {
public static int numberOfInstances;
public static String pathToFile = "";
private static final String RELATIVE_PATH_TO_JARS = "/out/artifacts/";
private final Logger logger = LoggerFactory.getLogger(Application.class);
/**
* 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 {
int currentServerPort = args.length == 3 ? Integer.parseInt(args[0]) : DEFAULT_PORT;
numberOfInstances = args.length == 3 ? Integer.parseInt(args[1]) : 4;
pathToFile = args.length == 3 ? args[2] : "TransientWorker_jar/Registration&Discovery-AutoHealer.jar";
pathToFile = System.getProperty("user.dir") + RELATIVE_PATH_TO_JARS + pathToFile;
String IP = args.length == 3 ? args[2] : "M_Salameh@127.0.0.1";
pathToFile = System.getProperty("user.dir") + "Worker.jar";
Application application = new Application();
ZooKeeper zooKeeper = application.connectToZookeeper();
......@@ -38,7 +36,7 @@ public class Application implements Watcher {
OnElectionAction onElectionAction = new OnElectionAction(serviceRegistry, currentServerPort);
LeaderElection leaderElection = new LeaderElection(zooKeeper, onElectionAction);
leaderElection.volunteerForLeadership();
leaderElection.volunteerForLeadership(IP);
leaderElection.reelectLeader();
application.run();
......@@ -66,14 +64,14 @@ public class Application implements Watcher {
switch (watchedEvent.getType()) {
case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to Zookeeper");
logger.info("Successfully connected to Zookeeper");
} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
synchronized (zooKeeper) {
System.out.println("Disconnected from Zookeeper");
logger.warn("Disconnected from Zookeeper");
zooKeeper.notifyAll();
}
} else if (watchedEvent.getState() == Event.KeeperState.Closed) {
System.out.println("Closed Successfully");
logger.info("Closed Successfully");
}
break;
}
......
package AutoHealerAndLoadBalancing;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
......@@ -11,7 +14,8 @@ public class LeaderElection implements Watcher {
private static final String ELECTION_NAMESPACE = "/election";
private String currentZnodeName;
private ZooKeeper zooKeeper;
private final Logger logger = LoggerFactory.getLogger(LeaderElection.class);
private String IP = "";
private OnElectionCallback onElectionCallback;
public LeaderElection(ZooKeeper zooKeeper, OnElectionCallback onElectionCallback)
......@@ -21,14 +25,13 @@ public class LeaderElection implements Watcher {
}
public void volunteerForLeadership() throws InterruptedException, KeeperException, UnknownHostException {
public void volunteerForLeadership(String IP) throws InterruptedException, KeeperException, UnknownHostException {
String znodePrefix = ELECTION_NAMESPACE + "/c_";
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);
this.IP = IP;
String znodeFullPath = zooKeeper.create(znodePrefix, IP.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//System.out.println(znodeFullPath);
logger.info("My Node under Election is" + znodeFullPath);
this.currentZnodeName = znodeFullPath.replace(ELECTION_NAMESPACE + "/", "");
}
......@@ -43,23 +46,22 @@ public class LeaderElection implements Watcher {
Collections.sort(children);
String smallestChild = children.get(0); //the first element
if (smallestChild.equals(currentZnodeName)) {
System.out.println("I'm a leader");
if (smallestChild.equals(currentZnodeName))
{
logger.info("I am LEADER");
onElectionCallback.onElectedToBeLeader();
return;
}
else {
System.out.println("I'm not a leader");
else
{
logger.info("I am NOT LEADER");
int predecessorIndex = children.indexOf(currentZnodeName) - 1;
predecessorName = children.get(predecessorIndex);
predecessorStat = zooKeeper.exists(ELECTION_NAMESPACE + "/" + predecessorName, this);
}
}
onElectionCallback.onWorker();
System.out.println("Watching znode " + predecessorName);
System.out.println();
onElectionCallback.onWorker(IP);
logger.info("Watching znode " + predecessorName + " in ELECTION");
}
@Override
......@@ -71,7 +73,9 @@ public class LeaderElection implements Watcher {
{
reelectLeader();
}
catch (InterruptedException | KeeperException e) {
catch (InterruptedException | KeeperException e)
{
logger.error("Can Not Handle Node Deletion in Election");
throw new RuntimeException(e);
}
break;
......
package AutoHealerAndLoadBalancing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class NodeSorting
{
private static final Logger logger = LoggerFactory.getLogger(NodeSorting.class);
private static class Pair
{
String node;
......@@ -21,9 +27,8 @@ public class NodeSorting
public static List<String> sort(List<byte[]> workers , List<String> Nodes)
{
Map<String , Integer> map = new HashMap<>();
System.out.println("physical nodes are : " + Nodes);
System.out.println("jars running are : " + workers.size());
//System.out.println("physical nodes are : " + Nodes);
logger.info("physical nodes are : " + Nodes);
for (byte[] worker : workers)
{
String s = new String(worker);
......@@ -52,12 +57,10 @@ public class NodeSorting
};
List<String> ans = new ArrayList<>();
Collections.sort(temp, comparator);
System.out.println(temp);
for (Pair pair : temp)
{
ans.add(pair.node);
}
//System.out.println(ans);
return ans;
}
}
import org.apache.zookeeper.KeeperException;
package AutoHealerAndLoadBalancing;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OnElectionAction implements OnElectionCallback
{
private final Logger logger = LoggerFactory.getLogger(OnElectionAction.class);
private final ServiceRegistry serviceRegistry;
private final int port;
......@@ -24,16 +25,15 @@ public class OnElectionAction implements OnElectionCallback
}
@Override
public void onWorker()
public void onWorker(String IP)
{
try
{
String IP = InetAddress.getLocalHost().getHostAddress();
String username = System.getProperty("user.name");
serviceRegistry.registerToCluster(username+"@"+IP);
serviceRegistry.registerToCluster(IP);
}
catch (InterruptedException | KeeperException | UnknownHostException e)
catch (InterruptedException | KeeperException e)
{
logger.error("Could Not Register To Cluster");
e.printStackTrace();
}
......
package AutoHealerAndLoadBalancing;
public interface OnElectionCallback {
void onElectedToBeLeader();
void onWorker();
void onWorker(String IP);
}
package AutoHealerAndLoadBalancing;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
......@@ -12,6 +15,7 @@ public class ServiceRegistry implements Watcher
private static final String WORKERS_ZNODES_PATH = "/workers";
private static final String PHYSICAL_ZNODES_PATH = "/physical_nodes";
private final Logger logger = LoggerFactory.getLogger(ServiceRegistry.class);
private String pathToProgram = "";
private final ZooKeeper zooKeeper;
......@@ -38,20 +42,23 @@ public class ServiceRegistry implements Watcher
zooKeeper.create(PHYSICAL_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
catch (KeeperException | InterruptedException e) {
catch (KeeperException | InterruptedException e)
{
logger.error("Could NOT Create Service Registry Znode");
e.printStackTrace();
}
}
public void registerToCluster(String metadata) throws KeeperException, InterruptedException {
if (this.currentZnode != null) {
System.out.println("Already registered to service registry");
if (this.currentZnode != null)
{
logger.info("Already registered to service registry");
return;
}
this.currentZnode = zooKeeper.create(PHYSICAL_ZNODES_PATH + "/physical_node_", metadata.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Registered to service registry");
logger.info("Registered to service registry");
}
public void registerForUpdates() {
......@@ -59,7 +66,9 @@ public class ServiceRegistry implements Watcher
{
masterJob();
}
catch (KeeperException | InterruptedException | IOException e) {
catch (KeeperException | InterruptedException | IOException e)
{
logger.error("Could Not Do LEADER JOB !");
e.printStackTrace();
}
}
......@@ -74,6 +83,7 @@ public class ServiceRegistry implements Watcher
}
catch (KeeperException | InterruptedException e)
{
logger.error("Could Not UN-Register From Cluster !!");
e.printStackTrace();
}
}
......@@ -102,7 +112,7 @@ public class ServiceRegistry implements Watcher
}
this.allServiceAddresses = Collections.unmodifiableList(addresses);
System.out.println("The cluster addresses are: " + this.allServiceAddresses);
logger.info("The cluster addresses are: " + this.allServiceAddresses);
}
private void launchWorkersIfNecessary() throws KeeperException, InterruptedException, IOException
......@@ -111,8 +121,6 @@ public class ServiceRegistry implements Watcher
List<String> workers = zooKeeper.getChildren(WORKERS_ZNODES_PATH, this);
//System.out.println("Total workers (before editing) = " + workers.size());
for (String worker : workers)
{
Stat stat = zooKeeper.exists(WORKERS_ZNODES_PATH + "/" + worker, false);
......@@ -124,7 +132,6 @@ public class ServiceRegistry implements Watcher
String node = new String(zooKeeper.getData(WORKERS_ZNODES_PATH + "/" + worker, false, stat));
if (!physicalZnodes.contains(node))
{
//System.out.println("Physical Node is shut down !!");
workers.remove(worker);
}
}
......@@ -145,7 +152,6 @@ public class ServiceRegistry implements Watcher
if (neededInstances <= 0) return;
System.out.println("Needed Instances is : " + neededInstances);
int index = 0;
......@@ -174,38 +180,20 @@ public class ServiceRegistry implements Watcher
Stat stat = zooKeeper.exists(WORKERS_ZNODES_PATH+"/"+worker ,false);
if (stat == null) continue;
ans.add(zooKeeper.getData(WORKERS_ZNODES_PATH+"/"+worker,false,stat ));
// String jj = new String(ans.get(ans.size()-1));
// System.out.println("worker : " +worker + " belongs to : " + jj);
}
return ans;
}
private void startNewWorker(String physicalNode) throws IOException, InterruptedException, KeeperException
{
File file = new File(pathToProgram);
String remoteUser = new String(zooKeeper.getData(PHYSICAL_ZNODES_PATH+"/"+physicalNode , false, null));
/**String remoteDirectory = "/JavaJars/";
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);
}*/
String command = "java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar " + file.getName() + " " + physicalNode;
System.out.println("Sending job to " + remoteUser + " of node : " + physicalNode);
Runtime.getRuntime().exec(command, null, file.getParentFile());
String remoteJarFilePath = "/root/AutoHealer/Worker.jar"; //+ file.getName();
logger.info("Sending To : " + remoteUser);
String sshCommand = "ssh " + remoteUser + " \"java -jar " + remoteJarFilePath + " " +physicalNode+"\"";
Runtime.getRuntime().exec(sshCommand);
}
@Override
......@@ -219,7 +207,9 @@ public class ServiceRegistry implements Watcher
{
masterJob();
}
catch (InterruptedException | KeeperException | IOException e) {
catch (InterruptedException | KeeperException | IOException e)
{
logger.error("Could NOT Handle Node Children Changed Event!");
throw new RuntimeException(e);
}
}
......
package AutoHealerAndLoadBalancing;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Random;
......@@ -12,6 +16,8 @@ public class TransientWorker
private static final String ZOOKEEPER_ADDRESS = "192.168.184.10:2181";
private static final int SESSION_TIMEOUT = 3000;
private final Logger logger = LoggerFactory.getLogger(TransientWorker.class);
// Parent Znode where each worker stores an ephemeral child to indicate it is alive
private static final String WORKERS_ZNODES_PATH = "/workers";
......@@ -29,19 +35,10 @@ public class TransientWorker
public void work(String nodeNum) throws KeeperException, InterruptedException {
addChildZnode(nodeNum);
System.out.println(myName + " is Working...");
logger.info(myName + " is Working...");
Thread.sleep(20000);
System.out.println(myName + " encountered Critical error happened");
logger.warn(myName + " encountered Critical error happened");
throw new RuntimeException(myName + " : Oops");
/*while (true)
{
System.out.println(myName + " is Working...");
LockSupport.parkNanos(1000L);
if (random.nextFloat() < CHANCE_TO_FAIL) {
System.out.println(myName + " encountered Critical error happened");
throw new RuntimeException(myName + " : Oops");
}
}*/
}
private void addChildZnode(String nodeNumber) throws KeeperException, InterruptedException {
......
package AutoHealerAndLoadBalancing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class TransientWorkerApplication
{
private static final Logger logger = LoggerFactory.getLogger(TransientWorkerApplication.class);
public static void main(String[] args)
{
TransientWorker worker = new TransientWorker();
......@@ -9,14 +15,19 @@ public class TransientWorkerApplication
try
{
worker.connectToZookeeper();
} catch (IOException e) {
}
catch (IOException e)
{
logger.warn("Cannot Connect To ZooKeeper");
throw new RuntimeException(e);
}
try
{
worker.work(nodeNum);
}
catch (Exception e) {
catch (Exception e)
{
logger.error("Worker Shut Down");
System.exit(1);
}
}
......
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.FileAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
public class TestLogging {
private static final Logger logger = LoggerFactory.getLogger(TestLogging.class);
/* public static void changeLogFile(String newLogFile) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
FileAppender fileAppender = (FileAppender) loggerContext.getLogger("ROOT").getAppender("FILE");
fileAppender.setFile(newLogFile);
fileAppender.start();
loggerContext.getLogger("ROOT").detachAppender("FILE");
loggerContext.getLogger("ROOT").addAppender(fileAppender);
}*/
public static void tryLogging() {
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
}
public static void main(String[] args) throws InterruptedException, IOException {
String remoteDirectory = "/JavaJars/";
String RELATIVE_PATH_TO_JARS = "/out/artifacts/";
String pathToFile = args.length == 3 ? args[2] : "TransientWorker_jar/Registration&Discovery-AutoHealer.jar";
pathToFile = System.getProperty("user.dir") + RELATIVE_PATH_TO_JARS + pathToFile;
File file = new File(pathToFile);
String remoteUser = "root@192.168.184.10";
String pathToProgram = System.getProperty("user.dir") + RELATIVE_PATH_TO_JARS + pathToFile;
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);
}
}
}
\ No newline at end of file
Manifest-Version: 1.0
Main-Class: TransientWorkerApplication
Main-Class: AutoHealerAndLoadBalancing.TransientWorkerApplication
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders name="FILE" target="ch.qos.logback.core.FileAppender">
<file>logs/mylog.log</file>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
<configuration>
<property name="HOME_LOG" value="logs/app.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${HOME_LOG}</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<!-- Log Level: ERROR, WARN, INFO, DEBUG, TRACE-->
<logger name="AutoHealerAndLoadBalancing" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="error">
<appender-ref ref="FILE"/>
</root>
</configuration>
\ No newline at end of file
Manifest-Version: 1.0
Main-Class: TransientWorkerApplication
Main-Class: AutoHealerAndLoadBalancing.TransientWorkerApplication
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders name="FILE" target="ch.qos.logback.core.FileAppender">
<file>logs/mylog.log</file>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
<configuration>
<property name="HOME_LOG" value="logs/app.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${HOME_LOG}</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<!-- Log Level: ERROR, WARN, INFO, DEBUG, TRACE-->
<logger name="AutoHealerAndLoadBalancing" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="error">
<appender-ref ref="FILE"/>
</root>
</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