我使用memcached版本1.4.7和spymemcached 2.8.4作为客户端来设置和获取键值。在多线程和高负载环境中使用时,spymemcached客户端无法在缓存本身中设置值。
我正在使用40M长密钥运行我的负载测试程序,这些密钥在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 :(得分:1)
我也看到了同样的情况。原因是它们用于异步操作的reactor模式。这意味着每1个连接有1个工作线程。这1个线程是高负载和多线程机器下的bootleneck。 1个线程只能加载1个CPU,其余23个空闲。
我们已经提出了增加工作线程并允许使用更多硬件电源的连接池。在 github 上查看 3levelmemcache 项目。
答案 1 :(得分:0)
我不确定,但似乎是spymemcached库本身的问题。 我更改了DefaultCache.java文件的实现以使用xmemcached,一切都开始正常工作。现在我没有错过任何记录。 telnet stats显示匹配的set命令数。
感谢您的耐心等待。