Commit 50a4b0af authored by areej.mohammad's avatar areej.mohammad

project2

parent 22b48b55
transientworker @ 491fde3f
Subproject commit 491fde3f07465db4ae259db2f9df286fc7623f12
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>transientworker</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>transientworker</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* MIT License
*
* Copyright (c) 2019 Michael Pogrebinsky - Distributed Systems & Cloud Computing with Java
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
public class Application {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
Worker worker = new Worker();
worker.connectToZookeeper();
worker.work();
}
}
import org.apache.zookeeper.*;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
public class NodeAgent implements Watcher {
private static final String ZOOKEEPER_ADDRESS = "192.168.144.134:2181";
private static final int SESSION_TIMEOUT = 3000;
private static final String NODES_ROOT = "/nodes";
private static final String WORKER_JAR_PATH = "C:\\Users\\J.N\\IdeaProjects\\auto-healer-zookeper\\transientworker\\target\\transientworker-1.0-SNAPSHOT.jar";
private ZooKeeper zooKeeper;
private String myTasksPath;
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);
}
public void registerNodeAndWatchTasks() throws KeeperException, InterruptedException, IOException {
ensurePath(NODES_ROOT, CreateMode.PERSISTENT);
String ipAddress = InetAddress.getLocalHost().getHostAddress();
String nodePath = NODES_ROOT + "/node_" + ipAddress;
ensurePath(nodePath, CreateMode.PERSISTENT);
String activePath = nodePath + "/active";
if (zooKeeper.exists(activePath, false) == null) {
zooKeeper.create(activePath, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
this.myTasksPath = nodePath + "/tasks";
ensurePath(myTasksPath, CreateMode.PERSISTENT);
System.out.println("Node Registered: " + nodePath + ". Watching tasks at: " + myTasksPath);
watchMyTasks();
}
private void ensurePath(String path, CreateMode mode) throws KeeperException, InterruptedException {
if (zooKeeper.exists(path, false) == null) {
zooKeeper.create(path, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
}
}
private void watchMyTasks() throws KeeperException, InterruptedException {
List<String> tasks = zooKeeper.getChildren(myTasksPath, this);
System.out.println("Current tasks count: " + tasks.size());
}
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeChildrenChanged && event.getPath().equals(myTasksPath)) {
handleNewTask();
} else if (event.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Connected to Zookeeper");
}
}
private void handleNewTask() {
try {
startLocalWorker();
watchMyTasks(); // إعادة المراقبة
} catch (Exception e) { e.printStackTrace(); }
}
private void startLocalWorker() throws IOException {
File jarFile = new File(WORKER_JAR_PATH);
ProcessBuilder pb = new ProcessBuilder("java", "-jar", jarFile.getAbsolutePath());
pb.inheritIO();
pb.directory(jarFile.getParentFile());
System.out.println("New worker launched locally!");
pb.start();
}
public static void main(String[] args) throws Exception {
NodeAgent agent = new NodeAgent();
agent.connectToZookeeper();
agent.registerNodeAndWatchTasks();
synchronized (agent.zooKeeper) { agent.zooKeeper.wait(); }
}
}
\ No newline at end of file
/*
* MIT License
*
* Copyright (c) 2019 Michael Pogrebinsky - Distributed Systems & Cloud Computing with Java
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.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.144.134: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.1F;
private final Random random = new Random();
private ZooKeeper zooKeeper;
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("Working...");
LockSupport.parkNanos(1000);
if (random.nextFloat() < CHANCE_TO_FAIL) {
System.out.println("Critical error happened");
throw new RuntimeException("Oops");
}
}
}
private void addChildZnode() throws KeeperException, InterruptedException {
zooKeeper.create(AUTOHEALER_ZNODES_PATH + "/worker_",
new byte[]{},
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
}
}
artifactId=transientworker
groupId=org.example
version=1.0-SNAPSHOT
C:\Users\J.N\IdeaProjects\auto-healer-zookeper\transientworker\src\main\java\Application.java
C:\Users\J.N\IdeaProjects\auto-healer-zookeper\transientworker\src\main\java\NodeAgent.java
C:\Users\J.N\IdeaProjects\auto-healer-zookeper\transientworker\src\main\java\Worker.java
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