Lab Day II: Hack ZooKeeper

Objective

This specialized class event is designed to guide students through understanding and exploring the internals of Apache ZooKeeper, focusing on its core components like SessionTracker and the internal processing of ephemeral nodes. Students will compile ZooKeeper from source and delve into the codebase to gain a deeper understanding of how ZooKeeper functions under the hood.

We recommend you forming groups of 3-4 students to do this lab together. Each team plays against another team.

Prerequisites

  • Intermediate knowledge of Java, as ZooKeeper is primarily Java-based.
  • Basic familiarity with Git for cloning the repository.
  • Apache Maven (Java package manager) installed for building ZooKeeper from source. The latest version can be found on the official website.

Part 1: Setting Up

Install Apache Maven

Please follow the guide from the official website. For example, for installing on Ubuntu, run commandlines:

1
sudo apt install git maven

Compile ZooKeeper from source codes

  1. Clone the latest ZooKeeper codes from github
    1
    git clone https://github.com/apache/zookeeper.git
  2. Compile ZooKeeper codes
1
2
cd zookeeper
mvn clean install -DskipTests
  1. Follow the same steps in the previous lecture to configure ZooKeeper under conf/
    1
    2
    3
    tickTime=2000
    dataDir=/path/to/zookeeper/data
    clientPort=2181

Starting ZooKeeper Server and Test

  • Run the following command in the terminal:
    1
    2
    bin/zkServer.sh start
    bin/zkServer.sh status
  • Test commands using clients, see if the server can handle requests correctly:
    1
    2
    3
    bin/zkCli.sh -server 127.0.0.1:2181
    create /myfirstznode "My first ZooKeeper data"
    ls /
  • If you are interested, take a look at the log output under logs/ to see what happened inside the server.

Part 2: New Quest: Immortal Ephemeral Nodes

In this part you will learn how to change system behaviors. First let’s understand session management in ZooKeeper.

In ZooKeeper, a session represents a connection between a client and the server. Sessions are essential for maintaining the state of interactions, and their management is critical for the overall reliability and consistency of the system. ZooKeeper tracks sessions to manage ephemeral nodes, which are tied to the lifecycle of each session.

Overview of SessionTrackerImpl.java

SessionTrackerImpl.java (zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTrackerImpl.java) is part of the ZooKeeper server’s source code that specifically handles session tracking. It implements the SessionTracker interface, providing the functionality to track the creation, expiration, and renewal of sessions.

Key Responsibilities

  • Session Creation: When a new client connects to a ZooKeeper server, a session is created. SessionTrackerImpl assigns a unique session ID and tracks the session’s timeout settings.
  • Session Expiration: If a client does not send a heartbeat within the specified timeout period, SessionTrackerImpl marks the session as expired. This triggers the deletion of any ephemeral nodes associated with the session.
  • Session Renewal: Clients send heartbeats to keep their sessions active. SessionTrackerImpl updates its internal tracking to reflect the latest activity, preventing the session from expiring prematurely.

Core Components and Processes

Session Tracking Mechanism

  • Session Map: SessionTrackerImpl maintains a map of session IDs to session objects, which contain the session’s timeout information and last activity timestamp.
  • Timeout Handling: The implementation uses a timing wheel algorithm to efficiently manage session timeouts. This structure allows for quick updates and checks for session expiration.

Expiration and Cleanup

  • Expiration Queue: Sessions close to their expiration time are placed in a queue. A background thread periodically checks this queue to determine which sessions have expired.
  • Ephemeral Node Deletion: On session expiration, SessionTrackerImpl notifies the ZooKeeper server to remove any ephemeral nodes associated with the expired session.

Heartbeat Processing

  • Heartbeat Reception: Clients send periodic heartbeats to keep their sessions alive. SessionTrackerImpl processes these heartbeats, updating the session’s last activity timestamp.
  • Session Renewal Logic: The tracker uses the updated activity timestamp to adjust the session’s position in the expiration queue, effectively renewing the session.

Exercise Task: Modify ZooKeeper codes to Make Session Immortal (Thus Ephemeral Nodes)

Modify SessionTrackerImpl.java codes, so sessions will not expire after clients disconnect.

Hint: you may want to take a look on codes related to sessionExpiryQueue

To test, run:

1
2
bin/zkCli.sh -server 127.0.0.1:2181
create -e /zombie

quit the client, wait 30s and start a new one:

1
2
bin/zkCli.sh -server 127.0.0.1:2181
ls /

If you succeeded, the ephemeral node should still exist, instead of disappearing after timeout

1
[zombie, zookeeper]

Bounty Job: Zombie Wars

We will announce the task in the middle of class.

Part 3: Zombie Wars — Attack vs. Defense!

It’s time to battle! Each team will compete as either a Zombie Maker (Attacker) or a Zombie Killer (Defender). Your goal is to break or restore ZooKeeper’s session expiration mechanism by modifying its internal Java implementation.

Game Rules

Team Zombie (Attacker)

  • You may only modify ExpiryQueue.java.
  • Your goal is to prevent selected expired sessions from being cleaned up, leaving behind zombie ephemeral nodes.
  • At least 80% of expired sessions must still be cleaned up correctly. You cannot simply disable session expiration for everyone!

Team Survivor (Defender)

  • You may only modify SessionTrackerImpl.java.
  • Your goal is to detect and eliminate zombies created by the attacker’s modified ExpiryQueue.
  • You must preserve normal ZooKeeper behavior, including keeping healthy sessions alive.

General Rules

  • Each team submits one attack file and one defense file.
  • Both files must compile with the provided ZooKeeper version.
  • ZooKeeper must remain operational. Crashing the server, prematurely expiring healthy sessions, or breaking normal node operations is not allowed.
  • You cannot modify any other source files.

How Battles Work

The instructor’s automated judge combines the attack code from one team with the defense code from another team.

For each matchup, the judge runs two experiments:

  1. Attack Only: Run the attacker’s ExpiryQueue.java with the original ZooKeeper SessionTrackerImpl.java. Measure how many zombie sessions the attack creates.
  2. Attack vs. Defense: Run the same attack with the opponent’s modified SessionTrackerImpl.java. Measure how many zombies remain.

The judge automatically creates ephemeral nodes, crashes clients, waits for session expiration, and verifies the results.

Scoring

Metric Points
Attacker: Each surviving zombie after defense +1
Defender: Each zombie eliminated compared with Attack Only +1
Invalid attack (over 20% zombies or broken normal behavior) 0 for both
Invalid defense (healthy sessions killed or other required functionality broken) 0 for both

Example: An attack creates 4 zombies. The defender eliminates 3, leaving 1.

  • Attacker earns 1 point.
  • Defender earns 3 points.

Teams compete against multiple opponents. The team with the highest total score wins!

Run the Automated Judge

Download the provided judge package. On a Linux machine with Python 3.12+, Java, Maven, and Git installed, run:

1
2
3
4
5
6
7
python3 judge.py \
--repo /path/to/zookeeper \
--revision YOUR_FIXED_COMMIT \
--attack teamA/ExpiryQueue.java \
--defense teamB/SessionTrackerImpl.java \
--cases 20 \
--out result.json

Replace YOUR_FIXED_COMMIT with the ZooKeeper commit specified by the instructor. --repo points to your local ZooKeeper repository.

The script automatically builds ZooKeeper, runs both experiments, and saves the scores to result.json.

Check the following fields:

1
2
3
4
5
6
{
"attack_valid": true,
"defense_valid": true,
"attack_score": 1,
"defense_score": 3
}

You can test your implementations locally before submitting.

Challenge: Can you design an attack that defeats other teams’ defenses? Can your defense survive attacks you have never seen before?

—>

Resources