Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): kraken-250 - add SchedLock support for app-agent schedulers (multiple instances) #292

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ app:
push-heartbeat: 0/20 * * * * *
push-log: 0/20 * * * * *
push-log-external-system: 0 0/1 * * * *
lock:
at-most-for: 2m
at-least-for: 10s
accept-asset-kinds:
- kraken.component.api
- kraken.component.api-target
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.consoleconnect.kraken.operator.data.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.ZonedDateTime;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "kraken_shed_lock")
@Getter
@Setter
public class ShedLock {
@Id
@Column(name = "name", length = 64, nullable = false)
private String name;

@Column(name = "lock_until", nullable = false)
private ZonedDateTime lockUntil;

@Column(name = "locked_at", nullable = false)
private ZonedDateTime lockedAt;

@Column(name = "locked_by", length = 255, nullable = false)
private String lockedBy;
}
10 changes: 10 additions & 0 deletions kraken-java-sdk/kraken-java-sdk-sync/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<version>2.0.0-snapshot.0</version>
</parent>
<dependencies>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>com.consoleconnect.kraken</groupId>
<artifactId>kraken-java-sdk-data</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.consoleconnect.kraken.operator.sync.config;

import com.consoleconnect.kraken.operator.sync.model.SyncProperty;
import javax.sql.DataSource;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "${app.cron-job.lock.at-most-for}")
public class SyncConfig {
private static final int SIZE = 16 * 1024 * 1024;

Expand All @@ -17,6 +25,16 @@ public SyncProperty syncProperty() {
return new SyncProperty();
}

@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(
JdbcTemplateLockProvider.Configuration.builder()
.withJdbcTemplate(new JdbcTemplate(dataSource))
.withTableName("kraken_shed_lock")
.usingDbTime()
.build());
}

@Bean
public WebClient webClient(SyncProperty syncProperty) {
ExchangeStrategies strategies =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -55,6 +56,10 @@ public ApiServerSynchronizeService(
this.applicationContext = applicationContext;
}

@SchedulerLock(
name = "synApiServerInfoLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.pull-api-server-info:-}")
public void synApiServerInfo() {
HttpResponse<List<SimpleApiServerDto>> res =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Optional;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -61,6 +62,10 @@ public GeneralSyncService(
this.applicationContext = applicationContext;
}

@SchedulerLock(
name = "syncServerAssetsLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.pull-server-assets:-}")
public void syncServerAssets() {
// 1. Query from client db to determine the latest updated time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit;
import java.time.ZonedDateTime;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -31,6 +32,10 @@ public HeartBeatCollectorService(
this.environmentClientRepository = environmentClientRepository;
}

@SchedulerLock(
name = "heartBeatCollectorLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-heartbeat-collector:-}")
public void runIt() {
ZonedDateTime now = ZonedDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.ZonedDateTime;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -55,6 +56,10 @@ public MgmtPullTemplateService(
this.eventSinkService = eventSinkService;
}

@SchedulerLock(
name = "pullMappingTemplateDetailsLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.download-mapping-template-content-from-mgmt:-}")
public void pullMappingTemplateDetails() {
Paging<UnifiedAssetDto> assetDtoPaging =
Expand Down Expand Up @@ -128,6 +133,10 @@ public void checkFirstStartUpAndExecuteInstalling(String id) {
installMappingTemplateViaMgmt(id);
}

@SchedulerLock(
name = "queryLatestProductReleaseLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.pull-latest-release-from-mgmt:-}")
public void queryLatestProductRelease() {
Paging<UnifiedAssetDto> assetDtoPaging =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down Expand Up @@ -100,6 +101,10 @@ protected void ingestData(UnifiedAssetDto dto) {
dataIngestionJob.ingestData(event);
}

@SchedulerLock(
name = "scheduledCheckLatestProductReleaseLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.pull-latest-release:-}")
public void scheduledCheckLatestProductRelease() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.ZonedDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -26,6 +27,10 @@ public PushHeartbeatService(
this.heartbeatRepository = heartbeatRepository;
}

@SchedulerLock(
name = "pushHeartbeatLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-heartbeat:-}")
public void runIt() {
ZonedDateTime now = ZonedDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository;
import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository;
import lombok.RequiredArgsConstructor;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -20,6 +21,10 @@ public class PushKrakenVersionService {
private final MgmtEventRepository eventRepository;
private final MgmtEventRepository mgmtEventRepository;

@SchedulerLock(
name = "pushKrakenVersionLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.sync-system-info-from-control-plane:-}")
public void runIt() {
// produce sync kraken info event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.ZonedDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -34,6 +35,10 @@ public PushLogService(
this.apiActivityLogRepository = apiActivityLogRepository;
}

@SchedulerLock(
name = "pushLogLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-log:-}")
public void runIt() {
ZonedDateTime createdAt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -50,6 +51,10 @@ public PushMgmtEventService(
this.eventSinkService = eventSinkService;
}

@SchedulerLock(
name = "pushMgmtEventLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-mgmt-event:-}")
public void pushMgmtEvent() {
List<MgmtEventEntity> mgmtEventEntities =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -36,6 +37,10 @@ public PushRunningMapperInfoService(
}

@Transactional
@SchedulerLock(
name = "pushRunningMapperInfoLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-running-mapper:-}")
public void runIt() {
ZonedDateTime now = ZonedDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.consoleconnect.kraken.operator.sync.model.SyncProperty;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
Expand All @@ -35,6 +36,10 @@ public ResetService(
this.unifiedAssetService = unifiedAssetService;
}

@SchedulerLock(
name = "scanEventLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.pull-reset-event:-}")
public void scanEvent() {
// search event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -71,6 +72,10 @@ public PushAPIActivityLogScheduler(
this.apiActivityLogRepository = apiActivityLogRepository;
}

@SchedulerLock(
name = "pushApiActivityLogToExternalSystemLock",
lockAtMostFor = "${app.cron-job.lock.at-most-for}",
lockAtLeastFor = "${app.cron-job.lock.at-least-for}")
@Scheduled(cron = "${app.cron-job.push-log-external-system:-}")
List<PushExternalSystemPayload> pushApiActivityLogToExternalSystem() {
Optional<MgmtEventEntity> entity =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ app:
- kraken.component.api-target
task:
enabled: false
cron-job:
lock:
at-most-for: 0s
at-least-for: 0s
Loading