-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractNodeLeader.java
More file actions
102 lines (87 loc) · 3.42 KB
/
AbstractNodeLeader.java
File metadata and controls
102 lines (87 loc) · 3.42 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
package invite.cron;
import lombok.SneakyThrows;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class AbstractNodeLeader {
private static final Log LOG = LogFactory.getLog(AbstractNodeLeader.class);
private final String lockName;
private final DataSource dataSource;
protected AbstractNodeLeader(String lockName, DataSource dataSource) {
this.lockName = lockName;
this.dataSource = dataSource;
}
@SneakyThrows
public void perform(String name, Executable executable) {
Connection conn = null;
boolean lockAcquired = false;
try {
conn = dataSource.getConnection();
lockAcquired = tryGetLock(conn, lockName);
if (!lockAcquired) {
LOG.info(String.format("Another node is running %s, skipping this one", name));
//Might be that there is a lock not cleaned up due to VM crash
this.cleanupStaleLocks(conn, 60);
return;
}
LOG.info(String.format("Lock acquired for %s", name));
executable.execute();
LOG.info(String.format("Executable %s completed successfully", name));
} catch (Throwable e) {
LOG.error(String.format("Error occurred in %s", name), e);
} finally {
if (lockAcquired) {
try {
releaseLock(conn, lockName);
LOG.info(String.format("Lock released for %s", name));
} catch (Exception e) {
LOG.error(String.format("Failed to release lock %s", name), e);
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ignored) {
//Can't do anything about this
LOG.warn(String.format("Failed to close lock %s", name));
}
}
}
}
protected boolean tryGetLock(Connection conn, String name) throws Exception {
try {
conn.setAutoCommit(false);
try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO distributed_locks (lock_name, acquired_at) VALUES (?, NOW())")) {
ps.setString(1, name);
ps.executeUpdate();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
// Duplicate key or other constraint violation means lock is held
return false;
}
} finally {
conn.setAutoCommit(true);
}
}
private void releaseLock(Connection conn, String name) throws Exception {
try (PreparedStatement ps = conn.prepareStatement(
"DELETE FROM distributed_locks WHERE lock_name = ?")) {
ps.setString(1, name);
ps.executeUpdate();
}
}
private void cleanupStaleLocks(Connection conn, int timeoutMinutes) throws Exception {
try (PreparedStatement ps = conn.prepareStatement(
"DELETE FROM distributed_locks WHERE acquired_at < NOW() - INTERVAL ? MINUTE")) {
ps.setInt(1, timeoutMinutes);
ps.executeUpdate();
}
}
}