1
1
package tech .ydb .lock .provider ;
2
2
3
+ import java .nio .charset .StandardCharsets ;
3
4
import java .sql .SQLException ;
5
+ import java .time .Instant ;
4
6
import java .util .Optional ;
5
7
import javax .annotation .PreDestroy ;
6
8
import net .javacrumbs .shedlock .core .LockConfiguration ;
7
9
import net .javacrumbs .shedlock .core .LockProvider ;
8
10
import net .javacrumbs .shedlock .core .SimpleLock ;
11
+ import net .javacrumbs .shedlock .support .Utils ;
9
12
import org .slf4j .Logger ;
10
13
import org .slf4j .LoggerFactory ;
14
+ import tech .ydb .common .retry .RetryForever ;
11
15
import tech .ydb .coordination .CoordinationClient ;
16
+ import tech .ydb .coordination .CoordinationSession ;
12
17
import tech .ydb .coordination .SemaphoreLease ;
18
+ import tech .ydb .coordination .settings .CoordinationSessionSettings ;
19
+ import tech .ydb .core .Result ;
13
20
import tech .ydb .jdbc .YdbConnection ;
14
21
15
22
/**
@@ -32,10 +39,6 @@ public void init() {
32
39
for (int i = 0 ; i < ATTEMPT_CREATE_NODE ; i ++) {
33
40
var status = coordinationClient .createNode (YDB_LOCK_NODE_NAME ).join ();
34
41
35
- if (status .isSuccess ()) {
36
- return ;
37
- }
38
-
39
42
if (i == ATTEMPT_CREATE_NODE - 1 ) {
40
43
status .expectSuccess ("Failed created coordination service node: " + YDB_LOCK_NODE_NAME );
41
44
}
@@ -44,30 +47,60 @@ public void init() {
44
47
45
48
@ Override
46
49
public Optional <SimpleLock > lock (LockConfiguration lockConfiguration ) {
47
- var coordinationSession = coordinationClient .createSession (YDB_LOCK_NODE_NAME );
50
+ var now = Instant .now ();
51
+
52
+ String instanceInfo = "Hostname=" + Utils .getHostname () + ", " +
53
+ "Current PID=" + ProcessHandle .current ().pid () + ", " +
54
+ "CreatedAt=" + now ;
55
+
56
+ logger .info ("Instance[{}] is trying to become a leader..." , instanceInfo );
48
57
49
- coordinationSession .connect ().join ()
50
- .expectSuccess ("Failed creating coordination node session" );
58
+ var coordinationSession = coordinationClient .createSession (
59
+ YDB_LOCK_NODE_NAME , CoordinationSessionSettings .newBuilder ()
60
+ .withRetryPolicy (new RetryForever (500 ))
61
+ .build ()
62
+ );
51
63
52
- logger . debug ( "Created coordination node session" );
64
+ var statusCS = coordinationSession . connect (). join ( );
53
65
54
- var semaphoreLease = coordinationSession .acquireEphemeralSemaphore (lockConfiguration .getName (), true ,
55
- lockConfiguration .getLockAtMostFor ()).join ();
66
+ if (!statusCS .isSuccess ()) {
67
+ logger .info ("Failed creating coordination session [{}]" , coordinationSession );
68
+
69
+ return Optional .empty ();
70
+ }
71
+
72
+ logger .info ("Created coordination node session [{}]" , coordinationSession );
73
+
74
+ Result <SemaphoreLease > semaphoreLease = coordinationSession .acquireEphemeralSemaphore (
75
+ lockConfiguration .getName (),
76
+ true ,
77
+ instanceInfo .getBytes (StandardCharsets .UTF_8 ),
78
+ lockConfiguration .getLockAtMostFor ()
79
+ ).join ();
80
+
81
+ logger .debug (coordinationSession .toString ());
56
82
57
83
if (semaphoreLease .isSuccess ()) {
58
- logger .debug ("Semaphore acquired" );
84
+ logger .info ("Instance[{}] acquired semaphore[SemaphoreName={}]" , instanceInfo ,
85
+ semaphoreLease .getValue ().getSemaphoreName ());
59
86
60
- return Optional .of (new YdbSimpleLock (semaphoreLease .getValue ()));
87
+ return Optional .of (new YdbSimpleLock (semaphoreLease .getValue (), instanceInfo , coordinationSession ));
61
88
} else {
62
- logger .debug ("Semaphore is not acquired" );
89
+ logger .info ("Instance[{}] did not acquire semaphore" , instanceInfo );
90
+
63
91
return Optional .empty ();
64
92
}
65
93
}
66
94
67
- private record YdbSimpleLock (SemaphoreLease semaphoreLease ) implements SimpleLock {
95
+ private record YdbSimpleLock (SemaphoreLease semaphoreLease , String metaInfo ,
96
+ CoordinationSession coordinationSession ) implements SimpleLock {
68
97
@ Override
69
98
public void unlock () {
99
+ logger .info ("Instance[{}] released semaphore[SemaphoreName={}]" , metaInfo , semaphoreLease .getSemaphoreName ());
100
+
70
101
semaphoreLease .release ().join ();
102
+
103
+ coordinationSession .close ();
71
104
}
72
105
}
73
106
0 commit comments