-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.DesignDistributedLockManager(DLM).java
More file actions
203 lines (184 loc) · 7.98 KB
/
16.DesignDistributedLockManager(DLM).java
File metadata and controls
203 lines (184 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.util.*;
import java.util.concurrent.*;
import java.io.*;
/*
------------------------------------------------------------
LLD : Distributed Lock Manager (DLM)
------------------------------------------------------------
Functional:
- Acquire / Release locks on shared resources.
- One client holds a lock at a time.
- Locks expire automatically after timeout.
Non-Functional:
- Thread-safe using synchronized blocks.
- In-memory design for low latency.
- Auto-cleanup for expired locks.
*/
class DistributedLockManager {
// Map to store current active locks: resource -> (owner, timestamp)
private final Map<String, LockEntry> lockTable = new HashMap<>();
private final long timeout; // lock expiration time
private final Object lock = new Object(); // global synchronization object
// Constructor starts cleanup thread automatically
public DistributedLockManager(long timeoutSeconds) {
this.timeout = timeoutSeconds * 1000; // convert to milliseconds
Thread cleaner = new Thread(this::cleanupExpiredLocks);
cleaner.setDaemon(true);
cleaner.start();
}
// ----------------------------------------------------------
// Try acquiring a lock for a given resource and client
// ----------------------------------------------------------
public boolean acquireLock(String resource, String clientId) {
synchronized (lock) {
if (!lockTable.containsKey(resource)) {
// Lock is free — assign to client
lockTable.put(resource, new LockEntry(clientId, System.currentTimeMillis()));
System.out.println(clientId + " acquired lock on " + resource);
return true;
} else {
// Resource already locked
LockEntry entry = lockTable.get(resource);
long currentTime = System.currentTimeMillis();
if (currentTime - entry.timestamp > timeout) {
// Lock expired, reassign it
System.out.println(clientId + " took expired lock on " + resource);
lockTable.put(resource, new LockEntry(clientId, currentTime));
return true;
} else {
// Still valid, client must wait
System.out.println(clientId + " waiting... " + resource + " locked by " + entry.owner);
return false;
}
}
}
}
// ----------------------------------------------------------
// Release a lock if owned by the requesting client
// ----------------------------------------------------------
public void releaseLock(String resource, String clientId) {
synchronized (lock) {
if (lockTable.containsKey(resource) && lockTable.get(resource).owner.equals(clientId)) {
lockTable.remove(resource);
System.out.println(clientId + " released lock on " + resource);
}
}
}
// ----------------------------------------------------------
// Background thread to remove expired locks periodically
// ----------------------------------------------------------
private void cleanupExpiredLocks() {
while (true) {
synchronized (lock) {
long now = System.currentTimeMillis();
List<String> expired = new ArrayList<>();
for (Map.Entry<String, LockEntry> e : lockTable.entrySet()) {
if (now - e.getValue().timestamp > timeout) {
expired.add(e.getKey());
}
}
for (String r : expired) {
System.out.println("Lock on " + r + " expired and removed.");
lockTable.remove(r);
}
}
try {
Thread.sleep(1000); // run every 1 second
} catch (InterruptedException ignored) {}
}
}
// ----------------------------------------------------------
// Inner helper class representing a lock entry
// ----------------------------------------------------------
private static class LockEntry {
String owner;
long timestamp;
LockEntry(String owner, long timestamp) {
this.owner = owner;
this.timestamp = timestamp;
}
}
}
/*
------------------------------------------------------------
CLIENT SIMULATION SECTION
------------------------------------------------------------
Each client tries to acquire a shared lock on same resource.
If successful, holds it for 2 seconds then releases it.
*/
class ClientTask implements Runnable {
private final DistributedLockManager dlm;
private final String clientId;
private final String resource;
ClientTask(DistributedLockManager dlm, String clientId, String resource) {
this.dlm = dlm;
this.clientId = clientId;
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
if (dlm.acquireLock(resource, clientId)) {
try {
Thread.sleep(2000); // simulate work
} catch (InterruptedException ignored) {}
dlm.releaseLock(resource, clientId);
} else {
try {
Thread.sleep(1000); // retry later
} catch (InterruptedException ignored) {}
}
}
}
}
/*
------------------------------------------------------------
MAIN EXECUTION SECTION
------------------------------------------------------------
Runs multiple clients concurrently trying to acquire
and release same resource lock.
*/
public class Main {
public static void main(String[] args) {
DistributedLockManager dlm = new DistributedLockManager(4); // timeout 4 seconds
ExecutorService pool = Executors.newFixedThreadPool(3);
pool.submit(new ClientTask(dlm, "Client-1", "Resource-A"));
pool.submit(new ClientTask(dlm, "Client-2", "Resource-A"));
pool.submit(new ClientTask(dlm, "Client-3", "Resource-A"));
pool.shutdown();
}
}
/*
------------------------------------------------------------
EXPLANATION (INLINE SUMMARY)
------------------------------------------------------------
1. DistributedLockManager keeps lock states in-memory (HashMap).
2. acquireLock(): checks if resource is locked; if expired or free, grants it.
3. releaseLock(): deletes entry if owned by requesting client.
4. cleanupExpiredLocks(): runs every second, removes stale locks.
5. ClientTask: simulates independent clients trying to acquire same lock.
6. main(): runs 3 client threads on same resource — demonstrates concurrency.
SAMPLE OUTPUT:
Client-1 acquired lock on Resource-A
Client-2 waiting... Resource-A locked by Client-1
Client-3 waiting... Resource-A locked by Client-1
Client-1 released lock on Resource-A
Client-2 acquired lock on Resource-A
Client-3 waiting... Resource-A locked by Client-2
Lock on Resource-A expired and removed.
Client-3 took expired lock on Resource-A
Client-3 released lock on Resource-A
*/