Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
C
cluster-auto-healer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
cluster-auto-healer
Commits
e43c1d0a
Commit
e43c1d0a
authored
Dec 23, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
write TransientWorker class
parent
c91795eb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
0 deletions
+64
-0
TransientWorker.java
worker/src/main/java/worker/TransientWorker.java
+64
-0
No files found.
worker/src/main/java/worker/TransientWorker.java
0 → 100644
View file @
e43c1d0a
package
worker
;
import
org.apache.zookeeper.*
;
import
common.ZkConfig
;
import
java.io.IOException
;
import
java.util.Random
;
import
java.util.concurrent.locks.LockSupport
;
public
class
TransientWorker
{
private
static
final
float
CHANCE_TO_FAIL
=
0.1
F
;
private
final
Random
random
=
new
Random
();
private
ZooKeeper
zooKeeper
;
private
final
String
nodeId
;
public
TransientWorker
(
String
nodeId
)
{
this
.
nodeId
=
nodeId
;
}
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
<
1
)
{
System
.
out
.
println
(
"Usage: java -jar TransientWorker.jar <nodeId>"
);
System
.
exit
(
1
);
}
String
nodeId
=
args
[
0
];
TransientWorker
worker
=
new
TransientWorker
(
nodeId
);
try
{
worker
.
connectToZookeeper
();
worker
.
work
();
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"Worker on node ["
+
nodeId
+
"] crashed: "
+
e
.
getMessage
());
System
.
exit
(
1
);
}
}
public
void
connectToZookeeper
()
throws
IOException
{
// We can pass null for the watcher since the worker doesn't need to respond to events
this
.
zooKeeper
=
new
ZooKeeper
(
ZkConfig
.
ZOOKEEPER_ADDRESS
,
ZkConfig
.
SESSION_TIMEOUT
,
null
);
}
public
void
work
()
throws
KeeperException
,
InterruptedException
{
registerWorkerInCluster
();
while
(
true
)
{
System
.
out
.
println
(
"Worker on node ["
+
nodeId
+
"] is performing calculations..."
);
LockSupport
.
parkNanos
(
2_000_000_000L
);
// 2 seconds
if
(
random
.
nextFloat
()
<
CHANCE_TO_FAIL
)
{
System
.
out
.
println
(
"Critical error simulated."
);
throw
new
RuntimeException
(
"Oops! Worker crashed."
);
}
}
}
private
void
registerWorkerInCluster
()
throws
KeeperException
,
InterruptedException
{
String
path
=
ZkConfig
.
WORKERS_PARENT_PATH
+
"/worker_"
;
byte
[]
data
=
nodeId
.
getBytes
();
// Create ephemeral sequential node to signal presence
zooKeeper
.
create
(
path
,
data
,
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment