Skip to content

Commit 4385fe6

Browse files
feat: added leader info in semaphore data
1 parent 50aba4b commit 4385fe6

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

shedlock-ydb/src/main/java/tech/ydb/lock/provider/YdbCoordinationServiceLockProvider.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.nio.charset.StandardCharsets;
44
import java.sql.SQLException;
5+
import java.time.Instant;
56
import java.util.Optional;
67
import javax.annotation.PostConstruct;
78
import javax.annotation.PreDestroy;
@@ -14,6 +15,7 @@
1415
import tech.ydb.coordination.CoordinationClient;
1516
import tech.ydb.coordination.CoordinationSession;
1617
import tech.ydb.coordination.SemaphoreLease;
18+
import tech.ydb.coordination.settings.DescribeSemaphoreMode;
1719
import tech.ydb.core.Result;
1820
import tech.ydb.jdbc.YdbConnection;
1921

@@ -25,7 +27,7 @@ public class YdbCoordinationServiceLockProvider implements LockProvider {
2527
private static final String YDB_LOCK_NODE_NAME = "shared-lock-ydb";
2628
private static final int ATTEMPT_CREATE_NODE = 10;
2729
private static final String INSTANCE_INFO =
28-
"{Hostname=" + Utils.getHostname() + ", " + "Current PID=" + ProcessHandle.current().pid() + "}";
30+
"Hostname=" + Utils.getHostname() + ", " + "Current PID=" + ProcessHandle.current().pid();
2931
private static final byte[] INSTANCE_INFO_BYTES = INSTANCE_INFO.getBytes(StandardCharsets.UTF_8);
3032

3133
private final YdbConnection ydbConnection;
@@ -66,7 +68,36 @@ public void init() {
6668

6769
@Override
6870
public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) {
69-
logger.info("Instance[{}] is trying to become a leader...", INSTANCE_INFO);
71+
var now = Instant.now();
72+
73+
String instanceInfo = "Hostname=" + Utils.getHostname() + ", " +
74+
"Current PID=" + ProcessHandle.current().pid() + ", " +
75+
"CreatedAt=" + now;
76+
77+
logger.info("Instance[{}] is trying to become a leader...", instanceInfo);
78+
79+
var describeResult = coordinationSession.describeSemaphore(
80+
lockConfiguration.getName(),
81+
DescribeSemaphoreMode.WITH_OWNERS
82+
).join();
83+
84+
if (describeResult.isSuccess()) {
85+
var describe = describeResult.getValue();
86+
var describePayload = new String(describe.getData(), StandardCharsets.UTF_8);
87+
88+
logger.debug("Received DescribeSemaphore[Name={}, Data={}]", describe.getName(), describePayload);
89+
90+
Instant createdLeaderTimestampUTC = Instant.parse(describePayload.split(",")[2].split("=")[1]);
91+
92+
if (now.isAfter(createdLeaderTimestampUTC.plus(lockConfiguration.getLockAtMostFor()))) {
93+
var deleteResult = coordinationSession.deleteSemaphore(describe.getName(), true).join();
94+
logger.debug("Delete semaphore[Name={}] result: {}", describe.getName(), deleteResult);
95+
}
96+
} else {
97+
// no success, ephemeral semaphore is not created
98+
99+
logger.debug("Semaphore[Name={}] not found", lockConfiguration.getName());
100+
}
70101

71102
Result<SemaphoreLease> semaphoreLease = coordinationSession.acquireEphemeralSemaphore(
72103
lockConfiguration.getName(),
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver
1+
spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver
2+
3+
logging.level.tech.ydb.lock.provider=debug

0 commit comments

Comments
 (0)