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): report kraken version upgrade #142

Merged
merged 9 commits into from
Nov 14, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.consoleconnect.kraken.operator.controller.handler;

import com.consoleconnect.kraken.operator.controller.entity.EnvironmentEntity;
import com.consoleconnect.kraken.operator.controller.model.SystemInfo;
import com.consoleconnect.kraken.operator.controller.repo.EnvironmentRepository;
import com.consoleconnect.kraken.operator.controller.service.SystemInfoService;
import com.consoleconnect.kraken.operator.core.client.ClientEvent;
Expand All @@ -11,13 +12,15 @@
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.model.HttpResponse;
import com.consoleconnect.kraken.operator.core.repo.EnvironmentClientRepository;
import com.consoleconnect.kraken.operator.core.service.EventSinkService;
import com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -33,6 +36,7 @@ public class ClientHeartbeatEventHandler extends ClientEventHandler {
private final EnvironmentRepository environmentRepository;

private final SystemInfoService systemInfoService;
private final EventSinkService eventSinkService;

@Override
public ClientEventTypeEnum getEventType() {
Expand Down Expand Up @@ -75,7 +79,7 @@ public HttpResponse<Void> onEvent(String envId, String userId, ClientEvent event
environmentClientEntity.setUpdatedAt(instance.getUpdatedAt());
environmentClientEntity.setUpdatedBy(userId);
environmentClientRepository.save(environmentClientEntity);
updateAppVersion(envId, instance.getAppVersion());
updateAppVersion(envId, instance);
}
return HttpResponse.ok(null);
}
Expand All @@ -89,17 +93,32 @@ public Optional<EnvironmentEntity> queryEnvName(String envId) {
return Optional.empty();
}

private void updateAppVersion(String envId, String appVersion) {
private void updateAppVersion(String envId, ClientInstanceHeartbeat heartbeat) {
SystemInfo systemInfo = systemInfoService.find();
environmentRepository
.findById(UUID.fromString(envId))
.ifPresent(
env -> {
if (EnvNameEnum.STAGE.name().equalsIgnoreCase(env.getName())) {
systemInfoService.updateAppVersion(null, appVersion, null);
if (EnvNameEnum.STAGE.name().equalsIgnoreCase(env.getName())
&& !StringUtils.equalsIgnoreCase(
heartbeat.getAppVersion(), systemInfo.getStageAppVersion())) {
reportKrakenVersionUpgrade(EnvNameEnum.STAGE, heartbeat);
systemInfoService.updateAppVersion(null, heartbeat.getAppVersion(), null);
}
if (EnvNameEnum.PRODUCTION.name().equalsIgnoreCase(env.getName())) {
systemInfoService.updateAppVersion(null, null, appVersion);

if (EnvNameEnum.PRODUCTION.name().equalsIgnoreCase(env.getName())
&& !StringUtils.equalsIgnoreCase(
heartbeat.getAppVersion(), systemInfo.getProductionAppVersion())) {
reportKrakenVersionUpgrade(EnvNameEnum.PRODUCTION, heartbeat);
systemInfoService.updateAppVersion(null, null, heartbeat.getAppVersion());
}
});
}

private void reportKrakenVersionUpgrade(EnvNameEnum envName, ClientInstanceHeartbeat heartbeat) {
eventSinkService.reportKrakenVersionUpgradeResult(
envName,
heartbeat.getAppVersion(),
Optional.ofNullable(heartbeat.getStartUpAt()).orElse(heartbeat.getUpdatedAt()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.event.PlatformSettingCompletedEvent;
import com.consoleconnect.kraken.operator.core.exception.KrakenException;
import com.consoleconnect.kraken.operator.core.model.Metadata;
import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository;
import com.consoleconnect.kraken.operator.core.repo.UnifiedAssetRepository;
Expand Down Expand Up @@ -275,6 +274,6 @@ public SystemInfo find() {
t.setProductName(mgmtProperty.getProductName());
return t;
})
.orElseThrow(() -> KrakenException.notFound("System info not found"));
.orElse(new SystemInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.enums.UpgradeResultEventEnum;
import com.consoleconnect.kraken.operator.core.enums.UpgradeSourceEnum;
import com.consoleconnect.kraken.operator.core.event.PlatformSettingCompletedEvent;
Expand All @@ -18,9 +19,11 @@
import com.consoleconnect.kraken.operator.core.toolkit.Paging;
import java.time.ZonedDateTime;
import java.util.*;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
Expand All @@ -30,7 +33,7 @@

@Service
@Slf4j
@AllArgsConstructor
@RequiredArgsConstructor
public class TemplateIngestService {
private final DataIngestionJob dataIngestionJob;
private final ApplicationEventPublisher eventPublisher;
Expand All @@ -42,8 +45,13 @@ public class TemplateIngestService {
private final EventSinkService eventSinkService;
private final SystemInfoService systemInfoService;

@Value("${spring.build.version}")
private String buildVersion;

@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady(Object event) {
// trigger report kraken version upgrade
reportKrakenVersionUpgrade();
userService.initSystemUpgradeUser();
log.info("Platform Boot Up Event Received, event class:{}", event.getClass());
Paging<UnifiedAssetDto> assetDtoPaging =
Expand Down Expand Up @@ -107,4 +115,16 @@ private void reportFirstInstallation() {
reportEvent.setInstalledAt(ZonedDateTime.now());
});
}

private void reportKrakenVersionUpgrade() {
SystemInfo systemInfo = systemInfoService.find();
if (!StringUtils.equalsIgnoreCase(buildVersion, systemInfo.getControlAppVersion())) {
log.info(
"Kraken version upgrade report: old version {},current version {}",
systemInfo.getControlAppVersion(),
buildVersion);
eventSinkService.reportKrakenVersionUpgradeResult(
EnvNameEnum.CONTROL_PLANE, buildVersion, ZonedDateTime.now());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum ClientEventTypeEnum {
CLIENT_SERVER_API,
CLIENT_TEMPLATE_UPGRADE_RESULT,
CLIENT_SYSTEM_INFO,
CLIENT_MAPPER_VERSION
CLIENT_MAPPER_VERSION,
CLIENT_APP_VERSION_UPGRADE_RESULT
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class ClientInstanceHeartbeat {
private String role;
private String appVersion;
private ZonedDateTime updatedAt;
private ZonedDateTime startUpAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum MgmtEventType {
RESET,
CLIENT_HEART_BEAT,
CLIENT_SYSTEM_INFO,
TEMPLATE_UPGRADE_RESULT;
TEMPLATE_UPGRADE_RESULT,
CLIENT_APP_VERSION_UPGRADE_RESULT;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.consoleconnect.kraken.operator.core.event;

import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import java.time.ZonedDateTime;
import lombok.Data;

@Data
public class AppVersionUpgradeResultEvent {
private String appVersion;
EnvNameEnum envName;
private ZonedDateTime upgradeAt;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.consoleconnect.kraken.operator.core.service;

import static com.consoleconnect.kraken.operator.core.enums.MgmtEventType.CLIENT_APP_VERSION_UPGRADE_RESULT;

import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.entity.MgmtEventEntity;
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.enums.EventStatusType;
import com.consoleconnect.kraken.operator.core.enums.MgmtEventType;
import com.consoleconnect.kraken.operator.core.enums.UpgradeResultEventEnum;
import com.consoleconnect.kraken.operator.core.event.AppVersionUpgradeResultEvent;
import com.consoleconnect.kraken.operator.core.event.TemplateUpgradeResultEvent;
import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository;
import com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.function.Consumer;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -51,4 +56,17 @@ public void reportTemplateUpgradeResult(
mgmtEventEntity.setStatus(EventStatusType.WAIT_TO_SEND.name());
eventRepository.save(mgmtEventEntity);
}

public void reportKrakenVersionUpgradeResult(
EnvNameEnum envName, String appVersion, ZonedDateTime upgradeAt) {
MgmtEventEntity mgmtEventEntity = new MgmtEventEntity();
mgmtEventEntity.setEventType(CLIENT_APP_VERSION_UPGRADE_RESULT.name());
AppVersionUpgradeResultEvent appVersionUpgradeResultEvent = new AppVersionUpgradeResultEvent();
mgmtEventEntity.setPayload(appVersionUpgradeResultEvent);
appVersionUpgradeResultEvent.setUpgradeAt(upgradeAt);
appVersionUpgradeResultEvent.setAppVersion(appVersion);
appVersionUpgradeResultEvent.setEnvName(envName);
mgmtEventEntity.setStatus(EventStatusType.WAIT_TO_SEND.name());
eventRepository.save(mgmtEventEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ void givenTemplateUpgradeResult_whenReportTemplateUpgradeResult_thenOk() throws
EventStatusType.WAIT_TO_SEND,
PageRequest.of(0, 10));
MatcherAssert.assertThat(mgmtEventEntities, Matchers.hasSize(Matchers.greaterThanOrEqualTo(1)));
mgmtEventEntities.forEach(event -> mgmtEventRepository.delete(event));
mgmtEventRepository.deleteAll(mgmtEventEntities);
}

@Test
@Order(2)
void givenAppStartUp_whenReportKrakenVersion_thenOk() {
eventSinkService.reportKrakenVersionUpgradeResult(
EnvNameEnum.CONTROL_PLANE, "1.0.0", ZonedDateTime.now());
List<MgmtEventEntity> krakenVersionEntities =
eventSinkService.findByPage(
List.of(MgmtEventType.CLIENT_APP_VERSION_UPGRADE_RESULT),
EventStatusType.WAIT_TO_SEND,
PageRequest.of(0, 10));
MatcherAssert.assertThat(
krakenVersionEntities, Matchers.hasSize(Matchers.greaterThanOrEqualTo(1)));
mgmtEventRepository.deleteAll(krakenVersionEntities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.consoleconnect.kraken.operator.core.entity.AbstractEntity;
import jakarta.persistence.*;
import java.time.ZonedDateTime;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -24,4 +25,7 @@ public class InstanceHeartbeatEntity extends AbstractEntity {

@Column(name = "app_version", nullable = true)
private String appVersion;

@Column(name = "start_up_at", nullable = true, unique = false)
private ZonedDateTime startUpAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.time.ZonedDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -31,6 +33,11 @@ public HeartBeatService(final HeartbeatRepository heartbeatRepository) {
@Scheduled(cron = "${app.cron-job.push-heartbeat:-}")
@Transactional
public void heartBeat() {
InstanceHeartbeatEntity entity = builInstanceHeartbeatEntity();
heartbeatRepository.save(entity);
}

private InstanceHeartbeatEntity builInstanceHeartbeatEntity() {
ZonedDateTime now = DateTime.nowInUTC();
log.debug("Heartbeat at {}", now);
InstanceHeartbeatEntity entity =
Expand All @@ -47,6 +54,14 @@ public void heartBeat() {
});
entity.setAppVersion(buildVersion);
entity.setUpdatedAt(now);
heartbeatRepository.save(entity);
return entity;
}

@EventListener(classes = ApplicationReadyEvent.class)
public void onPlatformBootUp(Object event) {
log.info("Application started up, report start up event");
InstanceHeartbeatEntity instanceHeartbeatEntity = builInstanceHeartbeatEntity();
instanceHeartbeatEntity.setStartUpAt(ZonedDateTime.now());
heartbeatRepository.save(instanceHeartbeatEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class SyncProperty {
private MgmtPlane mgmtPlane = new MgmtPlane();
private List<String> acceptAssetKinds = List.of();
private boolean assetConfigOverwriteFlag = false;
private long synDelaySeconds = 60;

@Data
public static class ControlPlane {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void runIt() {
heartbeat.setRole(entity.getRole());
heartbeat.setAppVersion(entity.getAppVersion());
heartbeat.setUpdatedAt(entity.getUpdatedAt());
heartbeat.setStartUpAt(entity.getStartUpAt());
return heartbeat;
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public PushLogService(

@Scheduled(cron = "${app.cron-job.push-log:-}")
public void runIt() {
ZonedDateTime createdAt = ZonedDateTime.now().minusSeconds(10);
ZonedDateTime createdAt =
ZonedDateTime.now().minusSeconds(getAppProperty().getSynDelaySeconds());
List<ApiActivityLogEntity> logEntities =
apiActivityLogRepository
.findAllBySyncStatusAndCreatedAtBefore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public class PushMgmtEventService extends KrakenServerConnector {
TYPE_MAPPING.put(
MgmtEventType.TEMPLATE_UPGRADE_RESULT, ClientEventTypeEnum.CLIENT_TEMPLATE_UPGRADE_RESULT);
TYPE_MAPPING.put(MgmtEventType.CLIENT_HEART_BEAT, ClientEventTypeEnum.CLIENT_HEARTBEAT);
TYPE_MAPPING.put(
MgmtEventType.CLIENT_APP_VERSION_UPGRADE_RESULT,
ClientEventTypeEnum.CLIENT_APP_VERSION_UPGRADE_RESULT);
TYPE_MAPPING.put(MgmtEventType.CLIENT_SYSTEM_INFO, ClientEventTypeEnum.CLIENT_SYSTEM_INFO);
}

protected static final List<MgmtEventType> QUERY_EVENT_TYPES =
List.of(
MgmtEventType.TEMPLATE_UPGRADE_RESULT,
MgmtEventType.CLIENT_HEART_BEAT,
MgmtEventType.CLIENT_SYSTEM_INFO);
TYPE_MAPPING.keySet().stream().toList();

public PushMgmtEventService(
SyncProperty appProperty, WebClient webClient, EventSinkService eventSinkService) {
Expand Down
Loading