Commit 449ade2b authored by tammam.alsoleman's avatar tammam.alsoleman

write launchWorkersIfNecessary function

parent 01da7f15
...@@ -7,11 +7,22 @@ ...@@ -7,11 +7,22 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>autohealer</artifactId> <artifactId>autohealer</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <!-- تحديث الإصدار إلى 11 لتكون متوافقاً مع تكوين المكون الإضافي -->
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
...@@ -19,18 +30,40 @@ ...@@ -19,18 +30,40 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <version>3.11.0</version>
<configuration> <configuration>
<source>17</source> <!-- هذه الإعدادات غير ضرورية لأنها موجودة في properties -->
<target>17</target> </configuration>
</plugin>
<!-- إضافة maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- هذا سينشئ ملف JAR باسم محدد -->
<finalName>autohealer-1.0-SNAPSHOT-jar-with-dependencies</finalName>
<!-- هذا مهم ليشمل كل التبعيات -->
<appendAssemblyId>false</appendAssemblyId>
</configuration> </configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- ربط هذه المرحلة بمرحلة package -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project> </project>
\ No newline at end of file
...@@ -86,16 +86,52 @@ public class Autohealer implements Watcher { ...@@ -86,16 +86,52 @@ public class Autohealer implements Watcher {
// This event is triggered when workers are added or removed // This event is triggered when workers are added or removed
if (event.getPath() != null && event.getPath().equals(AUTOHEALER_ZNODES_PATH)) { if (event.getPath() != null && event.getPath().equals(AUTOHEALER_ZNODES_PATH)) {
System.out.println("Detected change in workers. Re-evaluating if new workers are needed..."); System.out.println("Detected change in workers. Re-evaluating if new workers are needed...");
try {
launchWorkersIfNecessary(); launchWorkersIfNecessary();
} catch (KeeperException | InterruptedException e) {
System.err.println("Error while handling workers change: " + e.getMessage());
e.printStackTrace();
}
} }
break; break;
} }
} }
private void launchWorkersIfNecessary() { private void launchWorkersIfNecessary() throws KeeperException, InterruptedException {
/** // Get current workers list and set a watcher for future changes
* Implement this method to watch and launch new workers if necessary List<String> currentWorkers = zooKeeper.getChildren(AUTOHEALER_ZNODES_PATH, this);
*/
int currentWorkerCount = currentWorkers.size();
System.out.println("Current worker count: " + currentWorkerCount +
", Target worker count: " + numberOfWorkers);
// Check if we need to start new workers
if (currentWorkerCount < numberOfWorkers) {
int workersToStart = numberOfWorkers - currentWorkerCount;
System.out.println("Starting " + workersToStart + " new worker(s)...");
for (int i = 0; i < workersToStart; i++) {
try {
startNewWorker();
// Small delay to prevent overwhelming the system
Thread.sleep(500);
} catch (IOException e) {
System.err.println("Failed to start worker " + (i + 1) + ": " + e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
System.err.println("Interrupted while starting workers");
Thread.currentThread().interrupt();
throw e;
}
}
System.out.println("Successfully started " + workersToStart + " worker(s)");
} else if (currentWorkerCount > numberOfWorkers) {
System.out.println("Warning: More workers (" + currentWorkerCount +
") than required (" + numberOfWorkers + "). " +
"Excess workers will expire naturally when they finish.");
} else {
System.out.println("Worker count is optimal. No action needed.");
}
} }
/** /**
......
...@@ -7,31 +7,57 @@ ...@@ -7,31 +7,57 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>transientworker</artifactId> <artifactId>transientworker</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <version>3.11.0</version>
<!-- الإعدادات غير ضرورية لأنها موجودة في properties -->
</plugin>
<!-- إضافة maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<configuration> <configuration>
<source>17</source> <archive>
<target>17</target> <manifest>
<mainClass>Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>transientworker-1.0-SNAPSHOT-jar-with-dependencies</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration> </configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project> </project>
\ 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