Rack awareness helps Hadoop store data smartly across different racks. This keeps data safe and fast to access.
Rack awareness in HDFS in Hadoop
class RackAwarePlacementPolicy { // Map of DataNode to its rack Map<DataNode, String> nodeToRackMap = new HashMap<>(); // Method to get rack for a DataNode String getRack(DataNode node) { return nodeToRackMap.get(node); } // Method to choose DataNodes for block placement List<DataNode> chooseDataNodes(int numReplicas) { // Logic to pick nodes from different racks } }
This is a simplified view of how rack awareness is implemented in Hadoop.
Hadoop uses a network topology script to map nodes to racks automatically.
RackAwarePlacementPolicy policy = new RackAwarePlacementPolicy(); policy.nodeToRackMap.put(node1, "/rack1"); policy.nodeToRackMap.put(node2, "/rack2"); List<DataNode> chosenNodes = policy.chooseDataNodes(2);
// Edge case: Empty cluster RackAwarePlacementPolicy emptyPolicy = new RackAwarePlacementPolicy(); List<DataNode> chosenNodes = emptyPolicy.chooseDataNodes(3); // returns empty list
// Edge case: Only one rack available policy.nodeToRackMap.clear(); policy.nodeToRackMap.put(node1, "/rack1"); policy.nodeToRackMap.put(node2, "/rack1"); List<DataNode> chosenNodes = policy.chooseDataNodes(2); // both nodes from same rack
This program creates three data nodes on two racks. It then chooses two nodes for storing replicas, preferring different racks.
import java.util.*; class DataNode { String name; DataNode(String name) { this.name = name; } public String toString() { return name; } } class RackAwarePlacementPolicy { Map<DataNode, String> nodeToRackMap = new HashMap<>(); String getRack(DataNode node) { return nodeToRackMap.get(node); } List<DataNode> chooseDataNodes(int numReplicas) { List<DataNode> chosenNodes = new ArrayList<>(); Set<String> racksUsed = new HashSet<>(); for (DataNode node : nodeToRackMap.keySet()) { String rack = getRack(node); if (!racksUsed.contains(rack)) { chosenNodes.add(node); racksUsed.add(rack); if (chosenNodes.size() == numReplicas) { break; } } } // If not enough racks, fill with nodes from racks already used if (chosenNodes.size() < numReplicas) { for (DataNode node : nodeToRackMap.keySet()) { if (!chosenNodes.contains(node)) { chosenNodes.add(node); if (chosenNodes.size() == numReplicas) { break; } } } } return chosenNodes; } } public class RackAwarenessDemo { public static void main(String[] args) { DataNode node1 = new DataNode("Node1"); DataNode node2 = new DataNode("Node2"); DataNode node3 = new DataNode("Node3"); RackAwarePlacementPolicy policy = new RackAwarePlacementPolicy(); policy.nodeToRackMap.put(node1, "/rack1"); policy.nodeToRackMap.put(node2, "/rack2"); policy.nodeToRackMap.put(node3, "/rack1"); System.out.println("Before choosing nodes:"); for (var entry : policy.nodeToRackMap.entrySet()) { System.out.println(entry.getKey() + " on " + entry.getValue()); } List<DataNode> chosenNodes = policy.chooseDataNodes(2); System.out.println("\nChosen DataNodes for replicas:"); for (DataNode node : chosenNodes) { System.out.println(node + " on " + policy.getRack(node)); } } }
Time complexity: O(n) where n is number of data nodes, because it scans nodes to pick racks.
Space complexity: O(n) for storing node to rack mapping.
Common mistake: Not handling the case when fewer racks than replicas exist, causing all replicas on same rack.
Use rack awareness to improve fault tolerance and network efficiency compared to random placement.
Rack awareness places data copies on different racks to protect against rack failures.
It improves data availability and network usage in Hadoop clusters.
When racks are limited, replicas may share racks but still try to spread out.
Practice
Solution
Step 1: Understand rack awareness concept
Rack awareness means placing data copies on different racks to avoid data loss if one rack fails.Step 2: Identify the main goal
The goal is to improve fault tolerance by spreading replicas across racks.Final Answer:
To place data replicas on different racks to improve fault tolerance -> Option AQuick Check:
Rack awareness = fault tolerance [OK]
- Assuming all replicas are on the same rack
- Confusing rack awareness with encryption
- Thinking rack awareness reduces replica count
Solution
Step 1: Identify rack awareness configuration
Rack awareness requires a script that maps nodes to racks.Step 2: Recognize the correct property
The propertynet.topology.script.file.namepoints to this script.Final Answer:
Implement a script that maps nodes to racks and setnet.topology.script.file.name-> Option AQuick Check:
Rack mapping script =net.topology.script.file.name[OK]
- Setting replication to 1 disables replication, not rack awareness
- Using HDFS commands with rack options (not supported)
- Disabling rack awareness removes its benefits
Solution
Step 1: Recall HDFS replica placement policy
HDFS places one replica on the local node, one replica on a different rack, and the third replica on the same rack but a different node to balance fault tolerance and network usage.Step 2: Apply to 3 racks and replication factor 3
One replica is local, one is on a different rack, and the third is on the same rack but different node.Final Answer:
One replica on the local node, one on a different rack, and one on the same rack but different node -> Option DQuick Check:
Replica placement = local + different rack + same rack different node [OK]
- Placing all replicas on the same rack
- Placing two replicas on the same rack as local
- Ignoring rack awareness in placement
Solution
Step 1: Check rack awareness setup
If all replicas are on the same rack, the rack awareness script might be missing or incorrect, so HDFS cannot identify racks properly.Step 2: Rule out other causes
Replication factor 1 means only one replica, so multiple replicas on same rack is not possible. HDFS supports rack awareness. If all nodes are on the same rack physically, then placement is expected.Final Answer:
The rack awareness script is missing or misconfigured -> Option BQuick Check:
Missing script = replicas on same rack [OK]
- Assuming replication factor 1 causes multiple replicas
- Ignoring physical rack layout
- Believing HDFS lacks rack awareness support
Solution
Step 1: Understand rack awareness with limited racks
When racks are fewer than replication factor, HDFS tries to spread replicas across racks as much as possible.Step 2: Apply to 2 racks and replication factor 3
HDFS places one replica on each rack, and the third replica on any rack to ensure three copies exist.Final Answer:
Places one replica on each rack and the third replica on any rack to maximize availability -> Option CQuick Check:
Limited racks = spread as much as possible [OK]
- Thinking all replicas must be on different racks always
- Assuming failure if racks < replication factor
- Ignoring partial rack spreading
Ready to prove it?
Pass the test and this pattern moves unknown
