Skip to content

Commit 9458d63

Browse files
author
Shih-Hao Yeh
committed
fixup with metaget
1 parent f9a6230 commit 9458d63

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

evcache-core/src/main/java/com/netflix/evcache/EVCacheImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public class EVCacheImpl implements EVCache, EVCacheImplMBean {
117117
private final Property<Boolean> bypassAddOpt;
118118
private final Property<Boolean> bypassFixup;
119119
private final Property<Boolean> fixupAsFail;
120+
private final Property<Boolean> fixupVer2;
120121

121122
EVCacheImpl(String appName, String cacheName, int timeToLive, Transcoder<?> transcoder, boolean enableZoneFallback,
122123
boolean throwException, EVCacheClientPoolManager poolManager) {
@@ -175,9 +176,10 @@ public class EVCacheImpl implements EVCache, EVCacheImplMBean {
175176
this.maxKeyLength = propertyRepository.get(_appName + ".max.key.length", Integer.class).orElseGet("evcache.max.key.length").orElse(200);
176177

177178
// bypass short-circuit optimization
178-
this.bypassAddOpt = propertyRepository.get(_appName + ".bypass.add.opt", Boolean.class).orElse(false);
179-
this.bypassFixup = propertyRepository.get(_appName + ".bypass.fixup", Boolean.class).orElse(false);
180-
this.fixupAsFail = propertyRepository.get(_appName + ".fixup.as.fail", Boolean.class).orElse(false);
179+
this.bypassAddOpt = propertyRepository.get(_appName + ".add.bypass.opt", Boolean.class).orElse(false);
180+
this.bypassFixup = propertyRepository.get(_appName + ".add.bypass.fixup", Boolean.class).orElse(false);
181+
this.fixupAsFail = propertyRepository.get(_appName + "add.fixup.as.fail", Boolean.class).orElse(false);
182+
this.fixupVer2 = propertyRepository.get(_appName + ".add.fixup.ver2", Boolean.class).orElse(false);
181183

182184
// if alias changes, refresh my pool to point to the correct alias app
183185
this.alias = propertyRepository.get("EVCacheClientPoolManager." + appName + ".alias", String.class);
@@ -3331,7 +3333,7 @@ protected <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeTo
33313333
cd = _pool.getEVCacheClientForRead().getTranscoder().encode(value);
33323334
}
33333335
if (clientUtil == null) clientUtil = new EVCacheClientUtil(_appName, _pool.getOperationTimeout().get());
3334-
latch = clientUtil.add(evcKey, cd, evcacheValueTranscoder, timeToLive, policy, clients, latchCount, fixup, bypassAddOpt.get(), fixupAsFail.get());
3336+
latch = clientUtil.add(evcKey, cd, evcacheValueTranscoder, timeToLive, policy, clients, latchCount, fixup, bypassAddOpt.get(), fixupAsFail.get(), fixupVer2.get());
33353337
if (event != null) {
33363338
event.setTTL(timeToLive);
33373339
event.setCachedData(cd);

evcache-core/src/main/java/com/netflix/evcache/pool/EVCacheClientUtil.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.concurrent.TimeUnit;
66

77
import com.netflix.evcache.EVCacheKey;
8+
import com.netflix.evcache.operation.EVCacheItem;
89
import net.spy.memcached.MemcachedClientIF;
910
import net.spy.memcached.transcoders.Transcoder;
1011
import org.slf4j.Logger;
@@ -49,7 +50,7 @@ public EVCacheClientUtil(String appName, long operationTimeout) {
4950
/**
5051
* TODO : once metaget is available we need to get the remaining ttl from an existing entry and use it
5152
*/
52-
public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcacheValueTranscoder, int timeToLive, Policy policy, final EVCacheClient[] clients, int latchCount, boolean fixMissing, boolean bypassAddOpt, boolean fixupAsFail) throws Exception {
53+
public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcacheValueTranscoder, int timeToLive, Policy policy, final EVCacheClient[] clients, int latchCount, boolean fixMissing, boolean bypassAddOpt, boolean fixupAsFail, boolean newFixup) throws Exception {
5354
if (cd == null) return null;
5455

5556
final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, latchCount, _appName);
@@ -83,11 +84,11 @@ public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcac
8384
return latch;
8485
}
8586
else {
86-
return fixup(client, clients, evcKey, timeToLive, policy, latch, fixupAsFail);
87+
return fixup(client, clients, evcKey, timeToLive, policy, latch, fixupAsFail, newFixup);
8788
}
8889
} else {
8990
if (log.isDebugEnabled()) log.debug("Add failed after first client. key: " + key + ", client : " + client);
90-
return fixup(client, clients, evcKey, timeToLive, policy, latch, fixupAsFail);
91+
return fixup(client, clients, evcKey, timeToLive, policy, latch, fixupAsFail, newFixup);
9192
}
9293
}
9394
if(firstStatus == null) firstStatus = Boolean.valueOf(status);
@@ -96,16 +97,28 @@ public EVCacheLatch add(EVCacheKey evcKey, final CachedData cd, Transcoder evcac
9697
return latch;
9798
}
9899

99-
private EVCacheLatch fixup(EVCacheClient sourceClient, EVCacheClient[] destClients, EVCacheKey evcKey, int timeToLive, Policy policy, EVCacheLatchImpl prevLatch, boolean fixupAsFail) {
100+
private EVCacheLatch fixup(EVCacheClient sourceClient, EVCacheClient[] destClients, EVCacheKey evcKey, int timeToLive, Policy policy, EVCacheLatchImpl prevLatch, boolean fixupAsFail, boolean newFixup) {
100101
if (log.isDebugEnabled()) log.debug("Trying to fix up!! destClient count = " + destClients.length);
101102
final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, destClients.length, _appName);
102103
try {
103-
final CachedData readData = sourceClient.get(evcKey.getDerivedKey(sourceClient.isDuetClient(), sourceClient.getHashingAlgorithm(), sourceClient.shouldEncodeHashKey(), sourceClient.getMaxDigestBytes(), sourceClient.getMaxHashLength(), sourceClient.getBaseEncoder()), ct, false, false);
104+
CachedData readData = null;
105+
int ttlToSet = timeToLive;
106+
if (newFixup) {
107+
final EVCacheItem<CachedData> obj = sourceClient.metaGet(evcKey.getDerivedKey(sourceClient.isDuetClient(), sourceClient.getHashingAlgorithm(), sourceClient.shouldEncodeHashKey(), sourceClient.getMaxDigestBytes(), sourceClient.getMaxHashLength(), sourceClient.getBaseEncoder()), ct, false, false);
108+
if (obj != null) {
109+
readData = obj.getData();
110+
ttlToSet = (int) obj.getItemMetaData().getSecondsLeftToExpire();
111+
if (log.isDebugEnabled()) log.debug("Applying new fixup. ttlToSet = " + ttlToSet);
112+
}
113+
}
114+
else {
115+
readData = sourceClient.get(evcKey.getDerivedKey(sourceClient.isDuetClient(), sourceClient.getHashingAlgorithm(), sourceClient.shouldEncodeHashKey(), sourceClient.getMaxDigestBytes(), sourceClient.getMaxHashLength(), sourceClient.getBaseEncoder()), ct, false, false);
116+
}
104117

105118
if(readData != null) {
106-
sourceClient.touch(evcKey.getDerivedKey(sourceClient.isDuetClient(), sourceClient.getHashingAlgorithm(), sourceClient.shouldEncodeHashKey(), sourceClient.getMaxDigestBytes(), sourceClient.getMaxHashLength(), sourceClient.getBaseEncoder()), timeToLive);
119+
sourceClient.touch(evcKey.getDerivedKey(sourceClient.isDuetClient(), sourceClient.getHashingAlgorithm(), sourceClient.shouldEncodeHashKey(), sourceClient.getMaxDigestBytes(), sourceClient.getMaxHashLength(), sourceClient.getBaseEncoder()), ttlToSet);
107120
for(EVCacheClient destClient : destClients) {
108-
destClient.set(evcKey.getDerivedKey(destClient.isDuetClient(), destClient.getHashingAlgorithm(), destClient.shouldEncodeHashKey(), destClient.getMaxDigestBytes(), destClient.getMaxHashLength(), destClient.getBaseEncoder()), readData, timeToLive, latch);
121+
destClient.set(evcKey.getDerivedKey(destClient.isDuetClient(), destClient.getHashingAlgorithm(), destClient.shouldEncodeHashKey(), destClient.getMaxDigestBytes(), destClient.getMaxHashLength(), destClient.getBaseEncoder()), readData, ttlToSet, latch);
109122
}
110123
}
111124
latch.await(_operationTimeout, TimeUnit.MILLISECONDS);

0 commit comments

Comments
 (0)