Skip to content

Commit

Permalink
feat: micro fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Nov 14, 2024
1 parent eb6106c commit 51459db
Showing 1 changed file with 0 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.sql.SQLException;
import java.time.Instant;
import java.util.Optional;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.LockProvider;
Expand All @@ -15,7 +14,6 @@
import tech.ydb.coordination.CoordinationClient;
import tech.ydb.coordination.CoordinationSession;
import tech.ydb.coordination.SemaphoreLease;
import tech.ydb.coordination.settings.DescribeSemaphoreMode;
import tech.ydb.core.Result;
import tech.ydb.jdbc.YdbConnection;

Expand All @@ -37,7 +35,6 @@ public YdbCoordinationServiceLockProvider(YdbConnection ydbConnection) {
this.coordinationClient = CoordinationClient.newClient(ydbConnection.getCtx().getGrpcTransport());
}

@PostConstruct
public void init() {
for (int i = 0; i < ATTEMPT_CREATE_NODE; i++) {
var status = coordinationClient.createNode(YDB_LOCK_NODE_NAME).join();
Expand Down Expand Up @@ -73,29 +70,6 @@ public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) {

logger.info("Instance[{}] is trying to become a leader...", instanceInfo);

var describeResult = coordinationSession.describeSemaphore(
lockConfiguration.getName(),
DescribeSemaphoreMode.WITH_OWNERS
).join();

if (describeResult.isSuccess()) {
var describe = describeResult.getValue();
var describePayload = new String(describe.getData(), StandardCharsets.UTF_8);

logger.debug("Received DescribeSemaphore[Name={}, Data={}]", describe.getName(), describePayload);

Instant createdLeaderTimestampUTC = Instant.parse(describePayload.split(",")[2].split("=")[1]);

if (now.isAfter(createdLeaderTimestampUTC.plus(lockConfiguration.getLockAtMostFor()))) {
var deleteResult = coordinationSession.deleteSemaphore(describe.getName(), true).join();
logger.debug("Delete semaphore[Name={}] result: {}", describe.getName(), deleteResult);
}
} else {
// no success, ephemeral semaphore is not created

logger.debug("Semaphore[Name={}] not found", lockConfiguration.getName());
}

Result<SemaphoreLease> semaphoreLease = coordinationSession.acquireEphemeralSemaphore(
lockConfiguration.getName(),
true,
Expand Down

0 comments on commit 51459db

Please sign in to comment.