Commit 0dd9439b authored by mohammad.salama's avatar mohammad.salama

AutoHealer with Worker

parents
<component name="ArtifactManager">
<artifact type="jar" name="AutoHealer:jar">
<output-path>$PROJECT_DIR$/out/artifacts/AutoHealer_jar</output-path>
<root id="archive" name="ZooKeeper-AutoHealer.jar">
<element id="module-output" name="ZooKeeper-AutoHealer" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/slf4j-api-1.7.30.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/logback-classic-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" path-in-jar="/" />
</root>
</artifact>
</component>
\ No newline at end of file
<component name="ArtifactManager">
<artifact type="jar" name="Worker:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Worker_jar</output-path>
<root id="archive" name="ZooKeeper-AutoHealer.jar">
<element id="module-output" name="ZooKeeper-AutoHealer" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/slf4j-api-1.7.30.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/logback-classic-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/../zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.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="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ZooKeeper-AutoHealer.iml" filepath="$PROJECT_DIR$/ZooKeeper-AutoHealer.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ArtifactsWorkspaceSettings">
<artifacts-to-build>
<artifact name="AutoHealer:jar" />
<artifact name="Worker:jar" />
</artifacts-to-build>
</component>
<component name="ChangeListManager">
<list default="true" id="d4a0b476-92e2-4923-ba69-0da7a4ae9e1f" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/artifacts/AutoHealer_jar.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/artifacts/Worker_jar.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Jars/AutoHealer.jar" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Jars/Worker.jar" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Jars/zcommand.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/ZooKeeper-AutoHealer.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/AutoHealer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/AutoHealerApplication.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/META-INF/MANIFEST.MF" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Worker.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/WorkerApplication.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2af5JJ6ctL38H2cJCshk5GneZV0" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/HIAST/FIY/FS/Distributed Systems/Lab/6/zookeeper-3.9.1-dependency&quot;,
&quot;project.structure.last.edited&quot;: &quot;Artifacts&quot;,
&quot;project.structure.proportion&quot;: &quot;0.15&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;
}
}</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="d4a0b476-92e2-4923-ba69-0da7a4ae9e1f" name="Changes" comment="" />
<created>1704696720218</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1704696720218</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
</component>
</project>
\ No newline at end of file
File added
D:
cd HIAST\FIY\FS\Distributed Systems\Lab\6\ZooKeeper-AutoHealer\Jars
java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar AutoHealer.jar 10 ".\Worker.jar"
Open "Jars" Folder, inside there are two jars and command.txt
command.txt has the commands to run the jars.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/../zookeeper-3.9.1-dependency" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/../zookeeper-3.9.1-dependency" recursive="false" />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
Manifest-Version: 1.0
Main-Class: WorkerApplication
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.zookeeper.*;
public class AutoHealer implements Watcher
{
private static final String ZOOKEEPER_ADDRESS = "192.168.184.10:2181";
private static final int SESSION_TIMEOUT = 3000;
// Parent Znode where each worker stores an ephemeral child to indicate it is alive
private static final String AUTOHEALER_ZNODES_PATH = "/workers";
// Path to the worker jar
private final String pathToProgram;
// The number of worker instances we need to maintain at all times
private final int numberOfWorkers;
private ZooKeeper zooKeeper;
public AutoHealer(int numberOfWorkers, String pathToProgram) {
this.numberOfWorkers = numberOfWorkers;
this.pathToProgram = pathToProgram;
}
public void startWatchingWorkers() throws KeeperException, InterruptedException {
if (zooKeeper.exists(AUTOHEALER_ZNODES_PATH, this) == null) {
zooKeeper.create(AUTOHEALER_ZNODES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
launchWorkersIfNecessary();
}
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);
}
public void run() throws InterruptedException {
synchronized (zooKeeper) {
zooKeeper.wait();
}
}
public void close() throws InterruptedException {
zooKeeper.close();
}
@Override
public void process(WatchedEvent event) {
switch (event.getType())
{
case None: {
if (event.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to Zookeeper");
} else {
synchronized (zooKeeper) {
System.out.println("Disconnected from Zookeeper event");
zooKeeper.notifyAll();
}
}
break;
}
case NodeChildrenChanged:{
if (event.getPath().equals(AUTOHEALER_ZNODES_PATH))
{
try
{
launchWorkersIfNecessary();
}
catch (KeeperException | InterruptedException e) {
System.out.println("Failed to launch workers: " + e.getMessage());
}
}
break;
}
}
}
private void launchWorkersIfNecessary() throws KeeperException, InterruptedException
{
List<String> workerZnodes = zooKeeper.getChildren(AUTOHEALER_ZNODES_PATH, this);
AtomicInteger existingWorkersNumber = new AtomicInteger(workerZnodes.size());
if (existingWorkersNumber.get() > 0)
{
System.out.println("Last Child Znode Now is : " + workerZnodes.get(existingWorkersNumber.get() -1));
}
while (existingWorkersNumber.get() < numberOfWorkers )
{
Thread thread = new Thread(()->
{
try {
startNewWorker();
existingWorkersNumber.getAndIncrement();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
thread.start();
}
/*while (existingWorkersNumber < numberOfWorkers )
{
try {
startNewWorker();
existingWorkersNumber++;
} catch (IOException e) {
//throw new RuntimeException(e);
}
}*/
while (existingWorkersNumber.get() > numberOfWorkers)
{
String worker = workerZnodes.get(0);
String workerPath = AUTOHEALER_ZNODES_PATH + "/" + worker;
System.out.println("delete node: " + AUTOHEALER_ZNODES_PATH + "/" + worker);
zooKeeper.delete(workerPath, -1);
existingWorkersNumber.getAndDecrement();
workerZnodes.remove(0);
}
}
/**
* Helper method to start a single worker
* @throws IOException
*/
private void startNewWorker() throws IOException {
File file = new File(pathToProgram);
String command = "java -Dorg.slf4j.simpleLogger.defaultLogLevel=off -jar " + file.getName();
System.out.println(String.format("Launching worker instance : %s ", command));
Runtime.getRuntime().exec(command, null, file.getParentFile());
}
}
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
public class AutoHealerApplication
{
public static void main(String[] args) throws IOException, InterruptedException, KeeperException
{
if (args.length != 2) {
System.out.println("Expecting parameters <number of workers> <path to worker jar file>");
System.exit(1);
}
int numberOfWorkers = Integer.parseInt(args[0]);
String pathToWorkerProgram = args[1];
AutoHealer autohealer = new AutoHealer(numberOfWorkers, pathToWorkerProgram);
autohealer.connectToZookeeper();
autohealer.startWatchingWorkers();
autohealer.run();
autohealer.close();
}
}
Manifest-Version: 1.0
Main-Class: WorkerApplication
import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.locks.LockSupport;
public class Worker
{
private static final String ZOOKEEPER_ADDRESS = "192.168.184.10:2181";
private static final int SESSION_TIMEOUT = 3000;
// Parent Znode where each worker stores an ephemeral child to indicate it is alive
private static final String AUTOHEALER_ZNODES_PATH = "/workers";
private static final float CHANCE_TO_FAIL = 0.001F;
private final Random random = new Random();
private ZooKeeper zooKeeper;
private String myName = "";
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, event -> {
});
}
public void work() throws KeeperException, InterruptedException {
addChildZnode();
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() throws KeeperException, InterruptedException {
myName= zooKeeper.create(AUTOHEALER_ZNODES_PATH + "/worker_",
new byte[]{},
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
}
}
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
public class WorkerApplication
{
public static void main(String[] args)
{
Worker worker = new Worker();
try {
worker.connectToZookeeper();
} catch (IOException e) {
throw new RuntimeException(e);
}
try
{
worker.work();
}
catch (Exception e) {
return;
}
}
}
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