-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sl_li
committed
Dec 9, 2024
1 parent
841d61f
commit a195c95
Showing
28 changed files
with
382 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
9 changes: 9 additions & 0 deletions
9
.../redis-core/src/main/java/com/ctrip/xpipe/redis/core/store/ratelimit/ReplDelayConfig.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,9 @@ | ||
package com.ctrip.xpipe.redis.core.store.ratelimit; | ||
|
||
public interface ReplDelayConfig { | ||
|
||
long getDelayMilli(); | ||
|
||
long getDelayBytes(); | ||
|
||
} |
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
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
33 changes: 33 additions & 0 deletions
33
...s-keeper/src/main/java/com/ctrip/xpipe/redis/keeper/config/DefaultKeeperCommonConfig.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,33 @@ | ||
package com.ctrip.xpipe.redis.keeper.config; | ||
|
||
import com.ctrip.xpipe.api.codec.Codec; | ||
import com.ctrip.xpipe.api.codec.GenericTypeReference; | ||
import com.ctrip.xpipe.api.config.Config; | ||
import com.ctrip.xpipe.api.config.ConfigProvider; | ||
import com.ctrip.xpipe.config.CompositeConfig; | ||
import com.ctrip.xpipe.config.DefaultFileConfig; | ||
import com.ctrip.xpipe.config.DefaultPropertyConfig; | ||
import com.ctrip.xpipe.redis.core.config.AbstractCoreConfig; | ||
|
||
import java.util.List; | ||
|
||
public class DefaultKeeperCommonConfig extends AbstractCoreConfig implements KeeperCommonConfig { | ||
|
||
private static String KEY_REPL_DELAY_CONFIG = "keeper.repl.delay.config"; | ||
|
||
private final GenericTypeReference<List<KeeperReplDelayConfig>> replDelayConfigListType = new GenericTypeReference<List<KeeperReplDelayConfig>>() {}; | ||
|
||
public DefaultKeeperCommonConfig(){ | ||
CompositeConfig compositeConfig = new CompositeConfig(); | ||
compositeConfig.addConfig(ConfigProvider.DEFAULT.getOrCreateConfig(ConfigProvider.COMMON_CONFIG)); | ||
compositeConfig.addConfig(new DefaultPropertyConfig()); | ||
setConfig(compositeConfig); | ||
} | ||
|
||
@Override | ||
public List<KeeperReplDelayConfig> getReplDelayConfigs() { | ||
String replDelayConfigInfo = getProperty(KEY_REPL_DELAY_CONFIG, "[]"); | ||
return Codec.DEFAULT.decode(replDelayConfigInfo, replDelayConfigListType); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
redis/redis-keeper/src/main/java/com/ctrip/xpipe/redis/keeper/config/KeeperCommonConfig.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,9 @@ | ||
package com.ctrip.xpipe.redis.keeper.config; | ||
|
||
import java.util.List; | ||
|
||
public interface KeeperCommonConfig { | ||
|
||
List<KeeperReplDelayConfig> getReplDelayConfigs(); | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
...redis-keeper/src/main/java/com/ctrip/xpipe/redis/keeper/config/KeeperReplDelayConfig.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,44 @@ | ||
package com.ctrip.xpipe.redis.keeper.config; | ||
|
||
public class KeeperReplDelayConfig { | ||
|
||
private String srcDc; | ||
|
||
private String destDc; | ||
|
||
private long delayMilli; | ||
|
||
private long delayBytes; | ||
|
||
public String getSrcDc() { | ||
return srcDc; | ||
} | ||
|
||
public void setSrcDc(String srcDc) { | ||
this.srcDc = srcDc; | ||
} | ||
|
||
public String getDestDc() { | ||
return destDc; | ||
} | ||
|
||
public void setDestDc(String destDc) { | ||
this.destDc = destDc; | ||
} | ||
|
||
public long getDelayMilli() { | ||
return delayMilli; | ||
} | ||
|
||
public void setDelayMilli(long delayMilli) { | ||
this.delayMilli = delayMilli; | ||
} | ||
|
||
public long getDelayBytes() { | ||
return delayBytes; | ||
} | ||
|
||
public void setDelayBytes(long delayBytes) { | ||
this.delayBytes = delayBytes; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../redis-keeper/src/main/java/com/ctrip/xpipe/redis/keeper/config/ReplDelayConfigCache.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,93 @@ | ||
package com.ctrip.xpipe.redis.keeper.config; | ||
|
||
import com.ctrip.xpipe.api.foundation.FoundationService; | ||
import com.ctrip.xpipe.api.lifecycle.TopElement; | ||
import com.ctrip.xpipe.concurrent.AbstractExceptionLogTask; | ||
import com.ctrip.xpipe.lifecycle.AbstractLifecycle; | ||
import com.ctrip.xpipe.utils.XpipeThreadFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
public class ReplDelayConfigCache extends AbstractLifecycle implements TopElement { | ||
|
||
private KeeperCommonConfig keeperConfig; | ||
|
||
private ScheduledExecutorService scheduled; | ||
|
||
private ScheduledFuture<?> future; | ||
|
||
private Map<String, KeeperReplDelayConfig> keeperReplDelayConfigMap; | ||
|
||
public ReplDelayConfigCache() { | ||
this(null); | ||
} | ||
|
||
@Autowired | ||
public ReplDelayConfigCache(KeeperCommonConfig keeperConfig) { | ||
this.keeperConfig = keeperConfig; | ||
this.keeperReplDelayConfigMap = new HashMap<>(); | ||
} | ||
|
||
private void refresh() { | ||
logger.debug("[refresh]"); | ||
List<KeeperReplDelayConfig> replDelayConfigs = keeperConfig.getReplDelayConfigs(); | ||
Map<String, KeeperReplDelayConfig> localReplDelayConfigMap = new HashMap<>(); | ||
String currentDc = FoundationService.DEFAULT.getDataCenter(); | ||
for (KeeperReplDelayConfig config: replDelayConfigs) { | ||
if (currentDc.equalsIgnoreCase(config.getSrcDc())) { | ||
localReplDelayConfigMap.put(config.getDestDc().toUpperCase(), config); | ||
} | ||
} | ||
this.keeperReplDelayConfigMap = localReplDelayConfigMap; | ||
} | ||
|
||
public KeeperReplDelayConfig getReplDelayConfig(String destIdc) { | ||
if (null == destIdc || this.keeperReplDelayConfigMap.isEmpty()) return null; | ||
return this.keeperReplDelayConfigMap.get(destIdc); | ||
} | ||
|
||
@Override | ||
protected void doInitialize() throws Exception { | ||
super.doInitialize(); | ||
this.scheduled = Executors.newScheduledThreadPool(1, XpipeThreadFactory.create("ReplDelayConfigCache")); | ||
} | ||
|
||
@Override | ||
protected void doStart() throws Exception { | ||
super.doStart(); | ||
this.future = scheduled.scheduleWithFixedDelay(new AbstractExceptionLogTask() { | ||
@Override | ||
protected void doRun() throws Exception { | ||
ReplDelayConfigCache.this.refresh(); | ||
} | ||
}, 5, 10, TimeUnit.SECONDS); | ||
} | ||
|
||
@Override | ||
protected void doStop() throws Exception { | ||
super.doStop(); | ||
if (null != future) { | ||
this.future.cancel(false); | ||
this.future = null; | ||
} | ||
} | ||
|
||
@Override | ||
protected void doDispose() throws Exception { | ||
super.doDispose(); | ||
if (null != scheduled) { | ||
this.scheduled.shutdownNow(); | ||
this.scheduled = null; | ||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...edis-keeper/src/main/java/com/ctrip/xpipe/redis/keeper/config/TestKeeperCommonConfig.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,12 @@ | ||
package com.ctrip.xpipe.redis.keeper.config; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class TestKeeperCommonConfig implements KeeperCommonConfig { | ||
|
||
@Override | ||
public List<KeeperReplDelayConfig> getReplDelayConfigs() { | ||
return Collections.emptyList(); | ||
} | ||
} |
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
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
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
Oops, something went wrong.