2012-09-19 26 views
0

我使用memcached 1.4.7版和spymemcached 2.8.4作为客户端来设置并获取关键值。在多线程和高负载环境中使用时,spymemcached客户端无法设置缓存本身的值。使用多线程和高负载时,Spymemcached集不起作用

我正在运行我的负载测试程序,其中有40个长键,它们在20个工作线程中被等分。每个工作线程都会尝试在缓存中设置1M个密钥。因此有40个工作线程在运行。

在我的DefaultCache.java文件中,我创建了一个包含20个spymemcached客户端的连接池。每当工作线程尝试将密钥设置为缓存时,DefaultCache.java就会返回一个随机客户端,如getCache()方法中所示。

当我的程序退出时,它打印

总无钥匙装= 40000000

但是当我去的memcached telnet控制台,它总是惦记几千条记录。我也通过随机获取输出null的几个键来验证它。没有驱逐和cmd_set,curr_items,total_items每个都等于39.5M

这些丢失的密钥在缓存背后的原因是什么?

这是供参考的代码。

public class TestCacheLoader { 
public static final Long TOTAL_RECORDS = 40000000L; 
public static final Long LIMIT = 1000000L; 

public static void main(String[] args) { 
    long keyCount = loadKeyCacheData(); 
    System.out.println("Total no of keys loaded = " + keyCount); 
} 

public static long loadKeyCacheData() { 
    DefaultCache cache = new DefaultCache(); 
    List<Future<Long>> futureList = new ArrayList<Future<Long>>(); 
    ExecutorService executorThread = Executors.newFixedThreadPool(40); 
    long offset = 0; 
    long keyCount = 0; 
    long workerCount = 0; 
    try { 
     do { 
      List<Long> keyList = new ArrayList<Long>(LIMIT.intValue()); 
      for (long counter = offset; counter < (offset + LIMIT) && counter < TOTAL_RECORDS; counter++) { 
       keyList.add(counter); 
      } 
      if (keyList.size() != 0) { 
       System.out.println("Initiating a new worker thread " + workerCount++); 
       KeyCacheThread keyCacheThread = new KeyCacheThread(keyList, cache); 
       futureList.add(executorThread.submit(keyCacheThread)); 
      } 
      offset += LIMIT; 
     } while (offset < TOTAL_RECORDS); 
     for (Future<Long> future : futureList) { 
      keyCount += (Long) future.get(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     cache.shutdown(); 
    } 
    return keyCount; 
} 

}

class KeyCacheThread implements Callable<Long> { 
private List<Long> keyList; 
private DefaultCache cache; 

public KeyCacheThread(List<Long> keyList, DefaultCache cache) { 
    this.keyList = keyList; 
    this.cache = cache; 
} 

public Long call() { 
    return createKeyCache(); 
} 

public Long createKeyCache() { 
    String compoundKey = ""; 
    long keyCounter = 0; 
    System.out.println(Thread.currentThread() + " started to process " + keyList.size() + " keys"); 
    for (Long key : keyList) { 
     keyCounter++; 
     compoundKey = key.toString(); 
     cache.set(compoundKey, 0, key); 
    } 
    System.out.println(Thread.currentThread() + " processed = " + keyCounter + " keys"); 
    return keyCounter; 
} 

}

public class DefaultCache { 
private static final Logger LOGGER = Logger.getLogger(DefaultCache.class); 

private MemcachedClient[] clients; 

public DefaultCache() { 
    this.cacheNamespace = ""; 
    this.cacheName = "keyCache"; 
    this.addresses = "127.0.0.1:11211"; 
    this.cacheLookupTimeout = 3000; 
    this.numberOfClients = 20; 

    try { 
     LOGGER.debug("Cache initialization started for the cache : " + cacheName); 
     ConnectionFactory connectionFactory = new DefaultConnectionFactory(DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN, 
       DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE, DefaultHashAlgorithm.KETAMA_HASH) { 
      public NodeLocator createLocator(List<MemcachedNode> list) { 
       KetamaNodeLocator locator = new KetamaNodeLocator(list, DefaultHashAlgorithm.KETAMA_HASH); 
       return locator; 
      } 
     }; 

     clients = new MemcachedClient[numberOfClients]; 

     for (int i = 0; i < numberOfClients; i++) { 
      MemcachedClient client = new MemcachedClient(connectionFactory, AddrUtil.getAddresses(getServerAddresses(addresses))); 
      clients[i] = client; 
     } 
     LOGGER.debug("Cache initialization ended for the cache : " + cacheName); 
    } catch (IOException e) { 
     LOGGER.error("Exception occured while initializing cache : " + cacheName, e); 
     throw new CacheException("Exception occured while initializing cache : " + cacheName, e); 
    } 
} 

public Object get(String key) { 
    try { 
     return getCache().get(cacheNamespace + key); 
    } catch (Exception e) { 
     return null; 
    } 
} 

public void set(String key, Integer expiryTime, final Object value) { 
    getCache().set(cacheNamespace + key, expiryTime, value); 
} 

public Object delete(String key) { 
    return getCache().delete(cacheNamespace + key); 
} 

public void shutdown() { 
    for (MemcachedClient client : clients) { 
     client.shutdown(); 
    } 
} 

public void flush() { 
    for (MemcachedClient client : clients) { 
     client.flush(); 
    } 
} 

private MemcachedClient getCache() { 
    MemcachedClient client = null; 
    int i = (int) (Math.random() * numberOfClients); 
    client = clients[i]; 
    return client; 
} 

private String getServerAddresses(List<Address> addresses) { 
    StringBuilder addressStr = new StringBuilder(); 
    for (Address address : addresses) { 
     addressStr.append(address.getHost()).append(":").append(address.getPort()).append(" "); 
    } 
    return addressStr.toString().trim(); 
} 

}

+0

您确定这些密钥不是刚刚从缓存中被逐出吗? – mikewied

+0

是的,我可以看到从telnet stats命令显示我驱逐= 0 –

回答

0

我不知道,但它似乎与spymemcached库本身的问题。 我改变了DefaultCache.java文件的实现来使用xmemcached,并且一切都开始正常工作。现在我不会错过任何记录。远程登录统计显示匹配的设置命令数。

感谢您的耐心。

1

我看见了。原因是它们用于异步操作的反应堆模式。这意味着每1个连接1个工作线程。这1个线程是高负载和多线程机器下的bootleneck。 1个线程只能加载1个CPU,其余23个空闲。

我们想出了连接池,它增加了工作线程并允许使用更多的硬件电源。检出项目3levelmemcache at github