-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[ISSUE#12359]Refactoring the historical configuration cleanup strategy with SPI #12367
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...c/main/java/com/alibaba/nacos/config/server/service/dump/DefaultHistoryConfigCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.config.server.service.dump; | ||
|
||
import com.alibaba.nacos.config.server.service.repository.HistoryConfigInfoPersistService; | ||
import com.alibaba.nacos.config.server.utils.TimeUtils; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import com.alibaba.nacos.sys.utils.ApplicationUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Timestamp; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
|
||
import static com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG; | ||
|
||
/** | ||
* The type Default history config cleaner. | ||
* | ||
* @author Sunrisea | ||
*/ | ||
public class DefaultHistoryConfigCleaner implements HistoryConfigCleaner { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHistoryConfigCleaner.class); | ||
|
||
private HistoryConfigInfoPersistService historyConfigInfoPersistService; | ||
|
||
private int retentionDays = 30; | ||
|
||
@Override | ||
public void cleanHistoryConfig() { | ||
try { | ||
Timestamp startTime = getBeforeStamp(TimeUtils.getCurrentTime(), 24 * getRetentionDays()); | ||
int pageSize = 1000; | ||
LOGGER.warn("clearConfigHistory, getBeforeStamp:{}, pageSize:{}", startTime, pageSize); | ||
getHistoryConfigInfoPersistService().removeConfigHistory(startTime, pageSize); | ||
} catch (Throwable e) { | ||
LOGGER.error("clearConfigHistory error : {}", e.toString()); | ||
} | ||
} | ||
|
||
private HistoryConfigInfoPersistService getHistoryConfigInfoPersistService() { | ||
if (historyConfigInfoPersistService == null) { | ||
historyConfigInfoPersistService = ApplicationUtils.getBean(HistoryConfigInfoPersistService.class); | ||
} | ||
return historyConfigInfoPersistService; | ||
} | ||
|
||
private Timestamp getBeforeStamp(Timestamp date, int step) { | ||
Calendar cal = Calendar.getInstance(); | ||
cal.setTime(date); | ||
cal.add(Calendar.HOUR_OF_DAY, -step); | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
return Timestamp.valueOf(format.format(cal.getTime())); | ||
} | ||
|
||
private int getRetentionDays() { | ||
String val = EnvUtil.getProperty("nacos.config.retention.days"); | ||
if (null == val) { | ||
return retentionDays; | ||
} | ||
|
||
int tmp = 0; | ||
try { | ||
tmp = Integer.parseInt(val); | ||
if (tmp > 0) { | ||
retentionDays = tmp; | ||
} | ||
} catch (NumberFormatException nfe) { | ||
FATAL_LOG.error("read nacos.config.retention.days wrong", nfe); | ||
} | ||
|
||
return retentionDays; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "nacos"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
config/src/main/java/com/alibaba/nacos/config/server/service/dump/HistoryConfigCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.config.server.service.dump; | ||
|
||
/** | ||
* The interface History config cleaner. | ||
* @author Sunrisea | ||
*/ | ||
public interface HistoryConfigCleaner { | ||
|
||
/** | ||
* Clean history config. | ||
*/ | ||
public void cleanHistoryConfig(); | ||
|
||
/** | ||
* Gets name. | ||
* | ||
* @return the name | ||
*/ | ||
public String getName(); | ||
} |
79 changes: 79 additions & 0 deletions
79
...rc/main/java/com/alibaba/nacos/config/server/service/dump/HistoryConfigCleanerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.config.server.service.dump; | ||
|
||
import com.alibaba.nacos.api.utils.StringUtils; | ||
import com.alibaba.nacos.core.config.AbstractDynamicConfig; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
|
||
/** | ||
* The type History config cleaner config. | ||
* @author Sunrisea | ||
*/ | ||
public class HistoryConfigCleanerConfig extends AbstractDynamicConfig { | ||
|
||
private static final String HISTORY_CONFIG_CLEANER = "historyConfigCleaner"; | ||
|
||
private static final HistoryConfigCleanerConfig INSTANCE = new HistoryConfigCleanerConfig(); | ||
|
||
private String activeHistoryConfigCleaner = "nacos"; | ||
|
||
private HistoryConfigCleanerConfig() { | ||
super(HISTORY_CONFIG_CLEANER); | ||
resetConfig(); | ||
} | ||
|
||
/** | ||
* Gets instance. | ||
* | ||
* @return the instance | ||
*/ | ||
public static HistoryConfigCleanerConfig getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
protected void getConfigFromEnv() { | ||
activeHistoryConfigCleaner = EnvUtil.getProperty("nacos.config.history.clear.name", String.class, "nacos"); | ||
if (StringUtils.isBlank(activeHistoryConfigCleaner)) { | ||
activeHistoryConfigCleaner = "nacos"; | ||
} | ||
} | ||
|
||
/** | ||
* Gets active history config cleaner. | ||
* | ||
* @return the active history config cleaner | ||
*/ | ||
public String getActiveHistoryConfigCleaner() { | ||
return activeHistoryConfigCleaner; | ||
} | ||
|
||
/** | ||
* Sets active history config cleaner. | ||
* | ||
* @param activeHistoryConfigCleaner the active history config cleaner | ||
*/ | ||
public void setActiveHistoryConfigCleaner(String activeHistoryConfigCleaner) { | ||
this.activeHistoryConfigCleaner = activeHistoryConfigCleaner; | ||
} | ||
|
||
@Override | ||
protected String printConfig() { | ||
return "activeHistoryConfigCleaner{ " + "activeHistoryConfigCleaner=" + activeHistoryConfigCleaner + "}"; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...c/main/java/com/alibaba/nacos/config/server/service/dump/HistoryConfigCleanerManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.config.server.service.dump; | ||
|
||
import com.alibaba.nacos.common.spi.NacosServiceLoader; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* The type History config cleaner manager. | ||
* | ||
* @author Sunrisea | ||
*/ | ||
public class HistoryConfigCleanerManager { | ||
|
||
private static HashMap<String, HistoryConfigCleaner> historyConfigCleanerMap = new HashMap<String, HistoryConfigCleaner>(); | ||
|
||
static { | ||
NacosServiceLoader.load(HistoryConfigCleaner.class).forEach(historyConfigCleaner -> { | ||
historyConfigCleanerMap.put(historyConfigCleaner.getName(), historyConfigCleaner); | ||
}); | ||
historyConfigCleanerMap.put("nacos", new DefaultHistoryConfigCleaner()); | ||
} | ||
|
||
/** | ||
* Gets history config cleaner. | ||
* | ||
* @param name the name | ||
* @return the history config cleaner | ||
*/ | ||
public static HistoryConfigCleaner getHistoryConfigCleaner(String name) { | ||
return historyConfigCleanerMap.getOrDefault(name, historyConfigCleanerMap.get("nacos")); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.when canExecute return false/true ,print log also ,like 'history config cleaner is not enable in current context' , 'history config cleaner is enable in current context, trying to run cleaner.'
2.when cleaner run finish ,print log like 'history cleaner finished successfully' and should catch exception when cleaner throw exception ,or it wont be scheduled next period.