|
1 | 1 | package com.netflix.evcache.pool; |
2 | 2 |
|
3 | 3 | import java.util.List; |
| 4 | +import java.util.concurrent.BlockingQueue; |
4 | 5 | import java.util.concurrent.Future; |
| 6 | +import java.util.concurrent.LinkedBlockingQueue; |
| 7 | +import java.util.concurrent.RejectedExecutionHandler; |
| 8 | +import java.util.concurrent.ThreadPoolExecutor; |
| 9 | +import java.util.concurrent.TimeUnit; |
5 | 10 |
|
6 | 11 | import org.slf4j.Logger; |
7 | 12 | import org.slf4j.LoggerFactory; |
|
21 | 26 |
|
22 | 27 | public class EVCacheClientUtil { |
23 | 28 | private static Logger log = LoggerFactory.getLogger(EVCacheClientUtil.class); |
24 | | - private static final ChunkTranscoder ct = new ChunkTranscoder(); |
25 | | - public static EVCacheLatch add(String canonicalKey, CachedData cd, int timeToLive, EVCacheClientPool _pool, Policy policy) throws Exception { |
| 29 | + private final ChunkTranscoder ct = new ChunkTranscoder(); |
| 30 | + private final String _appName; |
| 31 | + private final DistributionSummary addDataSizeSummary; |
| 32 | + private final DistributionSummary addTTLSummary; |
| 33 | + private final DynamicBooleanProperty fixup; |
| 34 | + private final EVCacheClientPool _pool; |
| 35 | + private ThreadPoolExecutor threadPool = null; |
| 36 | + |
| 37 | + public EVCacheClientUtil(EVCacheClientPool pool) { |
| 38 | + this._pool = pool; |
| 39 | + this._appName = pool.getAppName(); |
| 40 | + this.addDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AddData-Size", _appName, null); |
| 41 | + this.addTTLSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AddData-TTL", _appName, null); |
| 42 | + this.fixup = EVCacheConfig.getInstance().getDynamicBooleanProperty(_appName + ".addOperation.fixup", Boolean.FALSE); |
| 43 | + int maxThreads = 10; |
| 44 | + |
| 45 | + RejectedExecutionHandler block = new RejectedExecutionHandler() { |
| 46 | + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { |
| 47 | + try { |
| 48 | + executor.getQueue().put( r ); |
| 49 | + } catch (InterruptedException e) { |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(1000); |
| 56 | + threadPool = new ThreadPoolExecutor(maxThreads, maxThreads * 2, 30, TimeUnit.SECONDS, queue); |
| 57 | + threadPool.setRejectedExecutionHandler(block); |
| 58 | + threadPool.prestartAllCoreThreads(); |
26 | 59 |
|
| 60 | + } |
| 61 | + |
| 62 | + public EVCacheLatch add(String canonicalKey, CachedData cd, int timeToLive, Policy policy) throws Exception { |
27 | 63 | if (cd == null) return null; |
28 | | - final String _appName = _pool.getAppName(); |
29 | | - final DistributionSummary addDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AddData-Size", _appName, null); |
30 | | - if (addDataSizeSummary != null) addDataSizeSummary.record(cd.getData().length); |
31 | | - final DistributionSummary addTTLSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AddData-TTL", _appName, null); |
32 | | - if (addTTLSummary != null) addTTLSummary.record(timeToLive); |
33 | | - final DynamicBooleanProperty fixup = EVCacheConfig.getInstance().getDynamicBooleanProperty(_appName + ".addOperation.fixup", Boolean.FALSE); |
| 64 | + addDataSizeSummary.record(cd.getData().length); |
| 65 | + addTTLSummary.record(timeToLive); |
34 | 66 |
|
35 | 67 | final EVCacheClient[] clients = _pool.getEVCacheClientForWrite(); |
36 | | - final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, clients.length, _pool.getAppName()){ |
| 68 | + final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, clients.length, _appName){ |
37 | 69 |
|
38 | 70 | @Override |
39 | 71 | public void onComplete(OperationFuture<?> operationFuture) throws Exception { |
40 | 72 | super.onComplete(operationFuture); |
41 | | - if (getPendingCount() == 0 && fixup.get()) { |
42 | | - final List<Future<Boolean>> futures = getAllFutures(); |
43 | | - int successCount = 0, failCount = 0; |
44 | | - for(int i = 0; i < futures.size() ; i++) { |
45 | | - final Future<Boolean> future = futures.get(i); |
46 | | - if(future instanceof EVCacheOperationFuture) { |
47 | | - final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)future; |
48 | | - if(f.getStatus().getStatusCode() == StatusCode.SUCCESS) { |
49 | | - successCount++; |
50 | | - if(log.isDebugEnabled()) log.debug("ADD : Success : APP " + _appName + ", key " + canonicalKey+ ", ServerGroup : " + f.getServerGroup().getName()); |
51 | | - } else { |
52 | | - failCount++; |
53 | | - if(log.isDebugEnabled()) log.debug("ADD : Fail : APP " + _appName + ", key : " + canonicalKey + ", ServerGroup : " + f.getServerGroup().getName()); |
54 | | - } |
55 | | - } |
| 73 | + if (getPendingFutureCount() == 0 && fixup.get()) { |
| 74 | + final RemoteRequest req = new RemoteRequest(this, canonicalKey, timeToLive); |
| 75 | + threadPool.submit(req); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + for (EVCacheClient client : clients) { |
| 81 | + final Future<Boolean> future = client.add(canonicalKey, timeToLive, cd, ct, latch); |
| 82 | + if(log.isDebugEnabled()) log.debug("ADD : Op Submitted : APP " + _appName + ", key " + canonicalKey + "; future : " + future); |
| 83 | + } |
| 84 | + return latch; |
| 85 | + } |
| 86 | + |
| 87 | + class RemoteRequest implements Runnable { |
| 88 | + private EVCacheLatchImpl latch; |
| 89 | + private String canonicalKey; |
| 90 | + private int timeToLive; |
| 91 | + public RemoteRequest(EVCacheLatchImpl latch, String canonicalKey, int timeToLive) { |
| 92 | + this.latch = latch; |
| 93 | + this.canonicalKey = canonicalKey; |
| 94 | + this.timeToLive = timeToLive; |
| 95 | + } |
| 96 | + public void run() { |
| 97 | + final List<Future<Boolean>> futures = latch.getAllFutures(); |
| 98 | + int successCount = 0, failCount = 0; |
| 99 | + for(int i = 0; i < futures.size() ; i++) { |
| 100 | + final Future<Boolean> future = futures.get(i); |
| 101 | + if(future instanceof EVCacheOperationFuture) { |
| 102 | + final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)future; |
| 103 | + if(f.getStatus().getStatusCode() == StatusCode.SUCCESS) { |
| 104 | + successCount++; |
| 105 | + if(log.isDebugEnabled()) log.debug("ADD : Success : APP " + _appName + ", key " + canonicalKey+ ", ServerGroup : " + f.getServerGroup().getName()); |
| 106 | + } else { |
| 107 | + failCount++; |
| 108 | + if(log.isDebugEnabled()) log.debug("ADD : Fail : APP " + _appName + ", key : " + canonicalKey + ", ServerGroup : " + f.getServerGroup().getName()); |
56 | 109 | } |
57 | | - |
58 | | - if(successCount > 0 && failCount > 0) { |
59 | | - CachedData readData = null; |
60 | | - for(int i = 0; i < futures.size(); i++) { |
61 | | - final Future<Boolean> evFuture = futures.get(i); |
62 | | - if(evFuture instanceof EVCacheOperationFuture) { |
63 | | - final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)evFuture; |
64 | | - if(f.getStatus().getStatusCode() == StatusCode.ERR_EXISTS) { |
65 | | - final EVCacheClient client = _pool.getEVCacheClient(f.getServerGroup()); |
66 | | - if(client != null) { |
67 | | - readData = client.get(canonicalKey, ct, false, false); |
68 | | - if(log.isDebugEnabled()) log.debug("Add : Read existing data for: APP " + _pool.getAppName() + ", key " + canonicalKey + "; ServerGroup : " + client.getServerGroupName()); |
69 | | - if(readData != null) { |
70 | | - break; |
71 | | - } else { |
72 | | - |
73 | | - } |
74 | | - } |
| 110 | + } |
| 111 | + } |
| 112 | + if(log.isDebugEnabled()) log.debug("ADD : Status: APP " + _appName + ", key : " + canonicalKey + ", failCount : " + failCount + "; successCount : " + successCount); |
| 113 | + |
| 114 | + if(successCount > 0 && failCount > 0) { |
| 115 | + CachedData readData = null; |
| 116 | + for(int i = 0; i < futures.size(); i++) { |
| 117 | + final Future<Boolean> evFuture = futures.get(i); |
| 118 | + if(evFuture instanceof EVCacheOperationFuture) { |
| 119 | + final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)evFuture; |
| 120 | + if(f.getStatus().getStatusCode() == StatusCode.ERR_EXISTS) { |
| 121 | + final EVCacheClient client = _pool.getEVCacheClient(f.getServerGroup()); |
| 122 | + if(client != null) { |
| 123 | + try { |
| 124 | + readData = client.get(canonicalKey, ct, false, false); |
| 125 | + } catch (Exception e) { |
| 126 | + log.error("Error readig the data", e); |
| 127 | + } |
| 128 | + if(log.isDebugEnabled()) log.debug("Add : Read existing data for: APP " + _appName + ", key " + canonicalKey + "; ServerGroup : " + client.getServerGroupName()); |
| 129 | + if(readData != null) { |
| 130 | + break; |
| 131 | + } else { |
| 132 | + |
75 | 133 | } |
76 | 134 | } |
77 | 135 | } |
78 | | - if(readData != null) { |
79 | | - for(int i = 0; i < futures.size(); i++) { |
80 | | - final Future<Boolean> evFuture = futures.get(i); |
81 | | - if(evFuture instanceof OperationFuture) { |
82 | | - final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)evFuture; |
83 | | - if(f.getStatus().getStatusCode() == StatusCode.SUCCESS) { |
84 | | - final EVCacheClient client = _pool.getEVCacheClient(f.getServerGroup()); |
85 | | - if(client != null) { |
86 | | - futures.remove(i); |
87 | | - client.set(canonicalKey, readData, timeToLive, this); |
88 | | - if(log.isDebugEnabled()) log.debug("Add: Fixup for : APP " + _pool.getAppName() + ", key " + canonicalKey + "; ServerGroup : " + client.getServerGroupName()); |
89 | | - EVCacheMetricsFactory.increment(_appName , null, client.getServerGroupName(), _appName + "-AddCall-FixUp"); |
90 | | - } |
| 136 | + } |
| 137 | + } |
| 138 | + if(readData != null) { |
| 139 | + for(int i = 0; i < futures.size(); i++) { |
| 140 | + final Future<Boolean> evFuture = futures.get(i); |
| 141 | + if(evFuture instanceof OperationFuture) { |
| 142 | + final EVCacheOperationFuture<Boolean> f = (EVCacheOperationFuture<Boolean>)evFuture; |
| 143 | + if(f.getStatus().getStatusCode() == StatusCode.SUCCESS) { |
| 144 | + final EVCacheClient client = _pool.getEVCacheClient(f.getServerGroup()); |
| 145 | + if(client != null) { |
| 146 | + try { |
| 147 | + client.set(canonicalKey, readData, timeToLive, latch); |
| 148 | + if(log.isDebugEnabled()) log.debug("Add: Fixup for : APP " + _appName + ", key " + canonicalKey + "; ServerGroup : " + client.getServerGroupName()); |
| 149 | + EVCacheMetricsFactory.increment(_appName , null, client.getServerGroupName(), _appName + "-AddCall-FixUp"); |
| 150 | + } catch (Exception e) { |
| 151 | + if(log.isDebugEnabled()) log.debug("Add: Fixup Error : APP " + _appName + ", key " + canonicalKey + "; ServerGroup : " + client.getServerGroupName(), e); |
91 | 152 | } |
92 | 153 | } |
93 | 154 | } |
94 | 155 | } |
95 | 156 | } |
96 | 157 | } |
97 | 158 | } |
98 | | - }; |
99 | | - |
100 | | - for (EVCacheClient client : clients) { |
101 | | - final Future<Boolean> future = client.add(canonicalKey, timeToLive, cd, ct, latch); |
102 | | - if(log.isDebugEnabled()) log.debug("ADD : Op Submitted : APP " + _pool.getAppName() + ", key " + canonicalKey + "; future : " + future); |
103 | 159 | } |
104 | | - return latch; |
105 | 160 | } |
106 | | - |
107 | | - |
108 | 161 | } |
0 commit comments