@@ -100,6 +100,22 @@ public <T> GetFuture<T> asyncGet(final String key, final Transcoder<T> tc) {
100100 throw new UnsupportedOperationException ("asyncGet" );
101101 }
102102
103+ // Returns 'true' if keys don't match and logs & reports the error.
104+ // Returns 'false' if keys match.
105+ // TODO: Consider removing this code once we've fixed the Wrong key bug(s)
106+ private boolean isWrongKeyReturned (String original_key , String returned_key ) {
107+ if (!original_key .equals (returned_key )) {
108+ // If they keys don't match, log the error along with the key owning host's information and stack trace.
109+ final String original_host = getHostNameByKey (original_key );
110+ final String returned_host = getHostNameByKey (returned_key );
111+ log .error ("Wrong key returned. Key - " + original_key + " (Host: " + original_host + ") ; Returned Key "
112+ + returned_key + " (Host: " + returned_host + ")" , new Exception ());
113+ client .reportWrongKeyReturned (original_host );
114+ return true ;
115+ }
116+ return false ;
117+ }
118+
103119 public <T > EVCacheOperationFuture <T > asyncGet (final String key , final Transcoder <T > tc , EVCacheGetOperationListener <T > listener ) {
104120 final CountDownLatch latch = new CountDownLatch (1 );
105121 final EVCacheOperationFuture <T > rv = new EVCacheOperationFuture <T >(key , latch , new AtomicReference <T >(null ), readTimeout .get ().intValue (), executorService , client );
@@ -126,10 +142,8 @@ public void receivedStatus(OperationStatus status) {
126142 @ SuppressWarnings ("unchecked" )
127143 public void gotData (String k , int flags , byte [] data ) {
128144
129- if (!key .equals (k )) {
130- log .error ("Wrong key returned. Key - " + key + "; Returned Key " + k );
131- return ;
132- }
145+ if (isWrongKeyReturned (key , k )) return ;
146+
133147 if (log .isDebugEnabled () && client .getPool ().getEVCacheClientPoolManager ().shouldLog (appName )) log .debug ("Read data : key " + key + "; flags : " + flags + "; data : " + data );
134148 if (data != null ) {
135149 if (log .isDebugEnabled () && client .getPool ().getEVCacheClientPoolManager ().shouldLog (appName )) log .debug ("Key : " + key + "; val size : " + data .length );
@@ -255,7 +269,8 @@ public void complete() {
255269 }
256270
257271 public void gotData (String k , int flags , long cas , byte [] data ) {
258- if (!key .equals (k )) log .warn ("Wrong key returned. Key - " + key + "; Returned Key " + k );
272+ if (isWrongKeyReturned (key , k )) return ;
273+
259274 if (data != null ) getDataSizeDistributionSummary (EVCacheMetricsFactory .GET_AND_TOUCH_OPERATION , EVCacheMetricsFactory .READ , EVCacheMetricsFactory .IPC_SIZE_INBOUND ).record (data .length );
260275 val = new CASValue <T >(cas , tc .decode (new CachedData (flags , data , tc .getMaxSize ())));
261276 }
@@ -602,6 +617,11 @@ public int getReadMetricMaxValue() {
602617 return maxReadDuration .get ().intValue ();
603618 }
604619
620+ private String getHostNameByKey (String key ) {
621+ MemcachedNode evcNode = getEVCacheNode (key );
622+ return getHostName (evcNode .getSocketAddress ());
623+ }
624+
605625 private String getHostName (SocketAddress sa ) {
606626 if (sa == null ) return null ;
607627 if (sa instanceof InetSocketAddress ) {
@@ -722,10 +742,8 @@ public void receivedStatus(OperationStatus status) {
722742 @ Override
723743 public void gotMetaData (String k , char flag , String fVal ) {
724744 if (log .isDebugEnabled ()) log .debug ("key " + k + "; val : " + fVal + "; flag : " + flag );
725- if (!key .equals (k )) {
726- log .error ("Wrong key returned. Expected Key - " + key + "; Returned Key " + k );
727- return ;
728- }
745+ if (isWrongKeyReturned (key , k )) return ;
746+
729747 switch (flag ) {
730748 case 's' :
731749 evItem .getItemMetaData ().setSizeInBytes (Integer .parseInt (fVal ));
@@ -765,10 +783,8 @@ public void gotMetaData(String k, char flag, String fVal) {
765783 @ Override
766784 public void gotData (String k , int flag , byte [] data ) {
767785 if (log .isDebugEnabled () && client .getPool ().getEVCacheClientPoolManager ().shouldLog (appName )) log .debug ("Read data : key " + k + "; flags : " + flag + "; data : " + data );
768- if (!key .equals (k )) {
769- log .error ("Wrong key returned. Expected Key - " + key + "; Returned Key " + k );
770- return ;
771- }
786+ if (isWrongKeyReturned (key , k )) return ;
787+
772788 if (data != null ) {
773789 if (log .isDebugEnabled () && client .getPool ().getEVCacheClientPoolManager ().shouldLog (appName )) log .debug ("Key : " + k + "; val size : " + data .length );
774790 getDataSizeDistributionSummary (EVCacheMetricsFactory .META_GET_OPERATION , EVCacheMetricsFactory .READ , EVCacheMetricsFactory .IPC_SIZE_INBOUND ).record (data .length );
0 commit comments