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

Add release history cleaning function #4813

Merged
merged 91 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 86 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
fe75b4d
add tech-support-qq-4.png
klboke May 16, 2019
99bf97a
Update README.md
klboke May 16, 2019
1579f41
Merge remote-tracking branch 'upstream/master'
klboke Nov 11, 2020
9f65eed
Merge remote-tracking branch 'upstream/master'
klboke Dec 9, 2020
d7d3fd9
Enhance the user experience in the scenario of submitting duplicate keys
klboke Dec 11, 2020
7329fab
Merge branch 'master' into master
nobodyiam Dec 12, 2020
5def448
Modify the key-value conflict exception prompt, adjust the code style
klboke Dec 12, 2020
6942564
Merge branch 'master' into master
nobodyiam Dec 12, 2020
0fc1f91
Merge remote-tracking branch 'upstream/master'
klboke Dec 19, 2020
61ad016
Merge remote-tracking branch 'origin/master'
klboke Dec 19, 2020
587ce33
rge remote-tracking branch 'upstream/master'
klboke Mar 1, 2021
9de6563
Merge remote-tracking branch 'upstream/master'
klboke Mar 8, 2021
9aab632
Merge remote-tracking branch 'upstream/master'
klboke Mar 25, 2021
050dd0a
Merge branch 'ctripcorp:master' into master
klboke May 10, 2021
5a64035
Merge branch 'master' of github.com:klboke/apollo
klboke May 10, 2021
0648fbd
Merge branch 'ctripcorp:master' into master
klboke May 19, 2021
35bd3a6
Merge branch 'master' of github.com:klboke/apollo
klboke May 19, 2021
bc8149b
Merge branch 'ctripcorp:master' into master
klboke Jun 8, 2021
e771bdd
Merge branch 'master' of github.com:klboke/apollo
klboke Jun 8, 2021
9a1ad83
Merge branch 'ctripcorp:master' into master
klboke Jun 25, 2021
3efef37
Merge branch 'master' of github.com:klboke/apollo
klboke Jun 25, 2021
1274281
Merge branch 'apolloconfig:master' into master
klboke Sep 1, 2021
a968932
Merge branch 'master' of github.com:klboke/apollo
klboke Sep 1, 2021
df8942c
Merge branch 'apolloconfig:master' into master
klboke Jan 7, 2022
f98175a
Merge branch 'master' of github.com:klboke/apollo
klboke Jan 7, 2022
710cdd6
Merge branch 'apolloconfig:master' into master
klboke Feb 18, 2022
3c162b6
Merge branch 'master' of github.com:klboke/apollo
klboke Feb 18, 2022
0101a3f
Merge branch 'apolloconfig:master' into master
klboke Jun 17, 2022
cc6f568
Merge branch 'master' of github.com:klboke/apollo
klboke Jun 17, 2022
21dd30c
Merge branch 'apolloconfig:master' into master
klboke Jun 21, 2022
c1eeb41
Merge branch 'master' of github.com:klboke/apollo
klboke Jun 21, 2022
e85d645
Merge branch 'apolloconfig:master' into master
klboke Jun 27, 2022
8b0056d
Merge branch 'master' of github.com:klboke/apollo
klboke Jun 27, 2022
347e07b
Merge branch 'apolloconfig:master' into master
klboke Jul 1, 2022
d8eb437
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 1, 2022
19bae52
Merge branch 'apolloconfig:master' into master
klboke Jul 4, 2022
7e40fa1
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 4, 2022
fbbb02d
Merge branch 'apolloconfig:master' into master
klboke Jul 6, 2022
19cee24
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 6, 2022
91ba3b7
Merge branch 'apolloconfig:master' into master
klboke Jul 8, 2022
94ad542
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 8, 2022
64e1b0b
Merge branch 'apolloconfig:master' into master
klboke Jul 19, 2022
36ce5e2
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 19, 2022
c1364c2
Merge branch 'apolloconfig:master' into master
klboke Jul 22, 2022
76ed2ea
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 22, 2022
db8cf3d
Merge branch 'apolloconfig:master' into master
klboke Jul 26, 2022
3e4616d
Merge branch 'master' of github.com:klboke/apollo
klboke Jul 26, 2022
47297cc
Merge branch 'apolloconfig:master' into master
klboke Jul 30, 2022
1c320e7
Merge branch 'apolloconfig:master' into master
klboke Aug 2, 2022
e723db5
Merge branch 'master' of github.com:klboke/apollo
klboke Aug 2, 2022
6be6a8b
Merge branch 'apolloconfig:master' into master
klboke Aug 8, 2022
014657d
Merge branch 'apolloconfig:master' into master
klboke Aug 12, 2022
370eec9
Merge branch 'master' of github.com:klboke/apollo
klboke Aug 12, 2022
dfa6c54
Merge branch 'apolloconfig:master' into master
klboke Sep 8, 2022
1a66d08
Merge branch 'master' of github.com:klboke/apollo
klboke Sep 8, 2022
cb2c22e
Merge branch 'apolloconfig:master' into master
klboke Sep 14, 2022
74af869
Merge branch 'master' of github.com:klboke/apollo
klboke Sep 14, 2022
a349f2e
Merge branch 'apolloconfig:master' into master
klboke Sep 22, 2022
7238296
Merge branch 'master' of github.com:klboke/apollo
klboke Sep 22, 2022
c38d00d
Merge branch 'apolloconfig:master' into master
klboke Oct 8, 2022
10cbbed
Merge branch 'master' of github.com:klboke/apollo
klboke Oct 8, 2022
601e17b
Merge branch 'apolloconfig:master' into master
klboke Oct 11, 2022
8fe2ce5
Merge branch 'master' of github.com:klboke/apollo
klboke Oct 11, 2022
04ea7ff
Merge branch 'apolloconfig:master' into master
klboke Dec 12, 2022
cfe577c
Merge branch 'master' of github.com:klboke/apollo
klboke Dec 12, 2022
4e9ba79
Merge branch 'apolloconfig:master' into master
klboke Dec 15, 2022
53d033a
Merge branch 'master' of github.com:klboke/apollo
klboke Dec 15, 2022
3789a6d
Merge branch 'apolloconfig:master' into master
klboke Jan 4, 2023
2a1f3bc
Merge branch 'master' of github.com:klboke/apollo
klboke Jan 10, 2023
0c7c63f
Merge branch 'apolloconfig:master' into master
klboke Mar 9, 2023
eaf3b3d
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 9, 2023
312abaa
Merge branch 'apolloconfig:master' into master
klboke Mar 13, 2023
f7c95f2
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 13, 2023
39cc719
Merge branch 'apolloconfig:master' into master
klboke Mar 15, 2023
e410062
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 15, 2023
bb2bf94
Merge branch 'apolloconfig:master' into master
klboke Mar 17, 2023
95cf566
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 17, 2023
0c10b9d
Merge branch 'apolloconfig:master' into master
klboke Mar 20, 2023
aa95cdd
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 20, 2023
3441587
Merge branch 'apolloconfig:master' into master
klboke Mar 21, 2023
a09b174
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 21, 2023
8419fca
Merge branch 'apolloconfig:master' into master
klboke Mar 23, 2023
d8d0664
Merge branch 'master' of github.com:klboke/apollo
klboke Mar 23, 2023
fa695d1
feat(apollo-biz): Add Clearing Function for Release History
klboke Mar 24, 2023
84decc0
doc(CHANGES.md): Update CHANGES.md
klboke Mar 24, 2023
c0754dd
test(apollo-biz): Added test case testCleanReleaseHistoryTransactiona…
klboke Mar 25, 2023
7fee80b
test(apollo-biz): Try to fix the test case AdminServiceTransactionTest
klboke Mar 26, 2023
b1d9dae
refactor(apollo-biz): Modify nativeSQL to JPA query
klboke Mar 26, 2023
4a4b928
doc(apollo-biz): Improve the documentation.
klboke Mar 26, 2023
7c7e7f0
refactor(apollo-biz): Optimize the logic of releaseHistoryRetentionLi…
klboke Mar 26, 2023
0743f96
Merge branch 'master' into release
klboke Mar 26, 2023
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Apollo 2.2.0
* [Misc dependency updates](https://github.com/apolloconfig/apollo/pull/4784)
* [Fix the problem that the deletion failure of the system rights management page does not prompt](https://github.com/apolloconfig/apollo/pull/4803)
* [Fix the issue of the system permission management page retrieving non-existent users](https://github.com/apolloconfig/apollo/pull/4802)
* [Add release history cleaning function](https://github.com/apolloconfig/apollo/pull/4813)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public class BizConfig extends RefreshableConfig {
private static final int DEFAULT_RELEASE_MESSAGE_NOTIFICATION_BATCH = 100;
private static final int DEFAULT_RELEASE_MESSAGE_NOTIFICATION_BATCH_INTERVAL_IN_MILLI = 100;//100ms
private static final int DEFAULT_LONG_POLLING_TIMEOUT = 60; //60s
public static final int DEFAULT_RELEASE_HISTORY_RETENTION_SIZE = -1;

private static final Gson GSON = new Gson();

klboke marked this conversation as resolved.
Show resolved Hide resolved
private static final Type namespaceValueLengthOverrideTypeReference =
new TypeToken<Map<Long, Integer>>() {
}.getType();
private static final Type releaseHistoryRetentionSizeOverrideTypeReference =
new TypeToken<Map<String, Integer>>() {
}.getType();

private final BizDBPropertySource propertySource;

Expand Down Expand Up @@ -154,6 +158,21 @@ public int accessKeyAuthTimeDiffTolerance() {
DEFAULT_ACCESS_KEY_AUTH_TIME_DIFF_TOLERANCE);
}

public int releaseHistoryRetentionSize() {
int count = getIntProperty("apollo.release-history.retention.size", DEFAULT_RELEASE_HISTORY_RETENTION_SIZE);
return checkInt(count, 1, Integer.MAX_VALUE, DEFAULT_RELEASE_HISTORY_RETENTION_SIZE);
klboke marked this conversation as resolved.
Show resolved Hide resolved
}

public Map<String, Integer> releaseHistoryRetentionSizeOverride() {
String overrideString = getValue("apollo.release-history.retention.size.override");
Map<String, Integer> releaseHistoryRetentionSizeOverride = Maps.newHashMap();
if (!Strings.isNullOrEmpty(overrideString)) {
releaseHistoryRetentionSizeOverride =
GSON.fromJson(overrideString, releaseHistoryRetentionSizeOverrideTypeReference);
}
return releaseHistoryRetentionSizeOverride;
klboke marked this conversation as resolved.
Show resolved Hide resolved
}

public int releaseMessageCacheScanInterval() {
int interval = getIntProperty("apollo.release-message-cache-scan.interval", DEFAULT_RELEASE_MESSAGE_CACHE_SCAN_INTERVAL);
return checkInt(interval, 1, Integer.MAX_VALUE, DEFAULT_RELEASE_MESSAGE_CACHE_SCAN_INTERVAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.ctrip.framework.apollo.biz.entity.ReleaseHistory;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -42,4 +44,9 @@ Page<ReleaseHistory> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(Stri
@Query("update ReleaseHistory set IsDeleted = true, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?4 where AppId=?1 and ClusterName=?2 and NamespaceName = ?3 and IsDeleted = false")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);

@Query(value = "select Id from ReleaseHistory where AppId=?1 and ClusterName=?2 and NamespaceName = ?3 and BranchName = ?4 and IsDeleted = false order by Id desc Limit 1 offset ?5", nativeQuery = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be optimized. When the releaseHistoryLimit (configuration upper limit is Integer.MAX_VALUE) is very large, the performance of this query will be relatively poor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok, too big value doesn't make sense here,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to use native query here as it may not work well with other databases. I can see the limit offset setup but I suppose it could be replaced with Pageable?

Optional<Long> findReleaseHistoryRetentionMaxId(String appId, String clusterName, String namespaceName, String branchName, int releaseHistoryLimit);

List<ReleaseHistory> findFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc(String appId, String clusterName, String namespaceName, String branchName, long maxId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,91 @@
*/
package com.ctrip.framework.apollo.biz.service;

import static com.ctrip.framework.apollo.biz.config.BizConfig.DEFAULT_RELEASE_HISTORY_RETENTION_SIZE;

import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.ReleaseHistory;
import com.ctrip.framework.apollo.biz.repository.ReleaseHistoryRepository;
import com.ctrip.framework.apollo.biz.repository.ReleaseRepository;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.collect.Queues;
import com.google.gson.Gson;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public class ReleaseHistoryService {
private static final Logger logger = LoggerFactory.getLogger(ReleaseHistoryService.class);
private static final Gson GSON = new Gson();
private static final int CLEAN_QUEUE_MAX_SIZE = 100;
private final BlockingQueue<ReleaseHistory> releaseClearQueue = Queues.newLinkedBlockingQueue(CLEAN_QUEUE_MAX_SIZE);
private final ExecutorService cleanExecutorService = Executors.newSingleThreadExecutor(
ApolloThreadFactory.create("ReleaseHistoryService", true));
private final AtomicBoolean cleanStopped = new AtomicBoolean(false);

private final ReleaseHistoryRepository releaseHistoryRepository;
private final ReleaseRepository releaseRepository;
private final AuditService auditService;
private final BizConfig bizConfig;
private final TransactionTemplate transactionManager;

public ReleaseHistoryService(
final ReleaseHistoryRepository releaseHistoryRepository,
final AuditService auditService) {
final ReleaseRepository releaseRepository,
final AuditService auditService,
final BizConfig bizConfig,
final TransactionTemplate transactionManager) {
this.releaseHistoryRepository = releaseHistoryRepository;
this.releaseRepository = releaseRepository;
this.auditService = auditService;
this.bizConfig = bizConfig;
this.transactionManager = transactionManager;
}

@PostConstruct
private void initialize() {
cleanExecutorService.submit(() -> {
while (!cleanStopped.get() && !Thread.currentThread().isInterrupted()) {
try {
ReleaseHistory releaseHistory = releaseClearQueue.poll(1, TimeUnit.SECONDS);
if (releaseHistory != null) {
this.cleanReleaseHistory(releaseHistory);
} else {
TimeUnit.MINUTES.sleep(1);
}
} catch (Throwable ex) {
logger.error("Clean releaseHistory failed", ex);
Tracer.logError(ex);
}
}
});
}

public Page<ReleaseHistory> findReleaseHistoriesByNamespace(String appId, String clusterName,
String namespaceName, Pageable
Expand Down Expand Up @@ -92,11 +147,67 @@ public ReleaseHistory createReleaseHistory(String appId, String clusterName, Str
auditService.audit(ReleaseHistory.class.getSimpleName(), releaseHistory.getId(),
Audit.OP.INSERT, releaseHistory.getDataChangeCreatedBy());

int releaseHistoryRetentionLimit = this.getReleaseHistoryRetentionLimit(releaseHistory);
if (releaseHistoryRetentionLimit != DEFAULT_RELEASE_HISTORY_RETENTION_SIZE) {
if (!releaseClearQueue.offer(releaseHistory)) {
logger.warn("releaseClearQueue is full, failed to add task to clean queue, " +
"clean queue max size:{}", CLEAN_QUEUE_MAX_SIZE);
}
}
return releaseHistory;
}

@Transactional
public int batchDelete(String appId, String clusterName, String namespaceName, String operator) {
return releaseHistoryRepository.batchDelete(appId, clusterName, namespaceName, operator);
}

private void cleanReleaseHistory(ReleaseHistory cleanRelease) {
String appId = cleanRelease.getAppId();
String clusterName = cleanRelease.getClusterName();
String namespaceName = cleanRelease.getNamespaceName();
String branchName = cleanRelease.getBranchName();

int retentionLimit = this.getReleaseHistoryRetentionLimit(cleanRelease);
//Second check, if retentionLimit is default value, do not clean
if (retentionLimit == DEFAULT_RELEASE_HISTORY_RETENTION_SIZE) {
return;
}

Optional<Long> maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(appId, clusterName, namespaceName, branchName, retentionLimit);
klboke marked this conversation as resolved.
Show resolved Hide resolved
if (!maxId.isPresent()) {
return;
}

boolean hasMore = true;
while (hasMore && !Thread.currentThread().isInterrupted()) {
List<ReleaseHistory> cleanReleaseHistoryList = releaseHistoryRepository.findFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc(
appId, clusterName, namespaceName, branchName, maxId.get());
Set<Long> releaseIds = cleanReleaseHistoryList.stream()
.map(ReleaseHistory::getReleaseId)
.collect(Collectors.toSet());

transactionManager.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
releaseHistoryRepository.deleteAll(cleanReleaseHistoryList);
releaseRepository.deleteAllById(releaseIds);
}
});
hasMore = cleanReleaseHistoryList.size() == 100;
}
klboke marked this conversation as resolved.
Show resolved Hide resolved
}

private int getReleaseHistoryRetentionLimit(ReleaseHistory releaseHistory) {
String overrideKey = String.format("%s+%s+%s+%s", releaseHistory.getAppId(),
releaseHistory.getClusterName(), releaseHistory.getNamespaceName(), releaseHistory.getBranchName());

Map<String, Integer> overrideMap = bizConfig.releaseHistoryRetentionSizeOverride();
return overrideMap.getOrDefault(overrideKey, bizConfig.releaseHistoryRetentionSize());
}

@PreDestroy
void stopClean() {
cleanStopped.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
)
public abstract class AbstractIntegrationTest {

protected static final String APP_ID = "kl-app";
protected static final String CLUSTER_NAME = "default";
protected static final String NAMESPACE_NAME = "application";
protected static final String BRANCH_NAME = "default";

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public void testReleaseMessageNotificationBatchWithInvalidNumber() throws Except
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
}

@Test
public void testReleaseHistoryRetentionLimit() {
int someLimit = 20;
when(environment.getProperty("apollo.release-history.retention.size")).thenReturn(String.valueOf(someLimit));

assertEquals(someLimit, bizConfig.releaseHistoryRetentionSize());
}

@Test
public void testReleaseHistoryRetentionLimitOverride() {
int someOverrideLimit = 10;
String overrideValueString = "{'a+b+c+b':10}";
when(environment.getProperty("apollo.release-history.retention.size.override")).thenReturn(overrideValueString);
int overrideValue = bizConfig.releaseHistoryRetentionSizeOverride().get("a+b+c+b");

assertEquals(someOverrideLimit, overrideValue);
}

@Test
public void testReleaseMessageNotificationBatchWithNAN() throws Exception {
String someNAN = "someNAN";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2023 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.biz.repository;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.ReleaseHistory;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

/**
* @author kl (http://kailing.pub)
* @since 2023/3/23
*/
public class ReleaseHistoryRepositoryTest extends AbstractIntegrationTest {

@Autowired
private ReleaseHistoryRepository releaseHistoryRepository;

@Test
@Sql(scripts = "/sql/release-history-test.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindReleaseHistoryRetentionMaxId() {
Optional<Long> maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, 1);
assertEquals(5, maxId.orElse(0L).longValue());

maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, 2);
assertEquals(4, maxId.orElse(0L).longValue());

maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, 5);
assertEquals(1, maxId.orElse(0L).longValue());

releaseHistoryRepository.deleteAll();
maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, 1);
assertFalse(maxId.isPresent());
}

@Test
@Sql(scripts = "/sql/release-history-test.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc() {

int releaseHistoryRetentionSize = 2;
Optional<Long> maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, releaseHistoryRetentionSize);
List<ReleaseHistory> releaseHistories = releaseHistoryRepository.findFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc(
APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, maxId.orElse(0L));
assertEquals(4, releaseHistories.size());

releaseHistoryRetentionSize = 1;
maxId = releaseHistoryRepository.findReleaseHistoryRetentionMaxId(APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, releaseHistoryRetentionSize);
releaseHistories = releaseHistoryRepository.findFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc(
APP_ID, CLUSTER_NAME, NAMESPACE_NAME, BRANCH_NAME, maxId.orElse(0L));
assertEquals(5, releaseHistories.size());
}
}
Loading