Skip to content

Commit d186619

Browse files
committed
[kv-interface] Fix the data race of the counters in the cached client.
1 parent a867191 commit d186619

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

java/kvstore-interface/src/main/java/cn/edu/nju/pasalab/db/cache/CachedClient.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Properties;
1313
import java.util.concurrent.Executors;
1414
import java.util.concurrent.ScheduledExecutorService;
15+
import java.util.concurrent.atomic.AtomicLong;
1516
import java.util.logging.Logger;
1617

1718
/**
@@ -46,8 +47,8 @@ public class CachedClient extends BasicKVDatabaseClient {
4647
private int expireNumThread;
4748
private int hmapConcurrency;
4849
private String dbClassName;
49-
private long queryCount = 0L;
50-
private long hitCount = 0L;
50+
private AtomicLong queryCount = new AtomicLong(0L);
51+
private AtomicLong hitCount = new AtomicLong(0L);
5152
private Logger logger = Logger.getLogger(this.getClass().getName());
5253

5354

@@ -59,14 +60,14 @@ public class CachedClient extends BasicKVDatabaseClient {
5960
@Override
6061
public byte[] get(byte[] key) throws Exception {
6162
assert cache != null;
62-
queryCount++;
63+
this.queryCount.getAndAdd(1L);
6364
byte[] result = cache.get(key);
6465
if (result == null) {
6566
result = db.get(key);
6667
if (result != null)
6768
cache.put(key, result);
6869
} else {
69-
hitCount++;
70+
this.hitCount.getAndAdd(1L);
7071
}
7172
return result;
7273
}
@@ -75,17 +76,21 @@ public byte[] get(byte[] key) throws Exception {
7576
public byte[][] getAll(byte keys[][]) throws Exception{
7677
byte[][] results = new byte[keys.length][];
7778
IntArrayList queryKeysIDs = new IntArrayList();
79+
long localQueryCount = 0L;
80+
long localHitCount = 0L;
7881

7982
for (int i = 0; i < keys.length; i++) {
80-
queryCount++;
83+
localQueryCount++;
8184
byte[] result = cache.get(keys[i]);
8285
if (result == null) {
8386
queryKeysIDs.add(i);
8487
} else {
8588
results[i] = result;
86-
hitCount++;
89+
localHitCount++;
8790
}
8891
}
92+
this.queryCount.addAndGet(localQueryCount);
93+
this.hitCount.addAndGet(localHitCount);
8994
byte[][] queryKeys = new byte[queryKeysIDs.size()][];
9095
for (int i = 0; i < queryKeysIDs.size(); i++) {
9196
queryKeys[i] = keys[queryKeysIDs.getInt(i)];
@@ -140,6 +145,7 @@ private void loadConfigurations(Properties conf) {
140145
this.hmapConcurrency = Integer.valueOf(conf.getProperty(CONF_HMAP_CONCURRENCY, DEFAULT_HMAP_CONCURRENCY));
141146
this.dbClassName = conf.getProperty(CONF_DB_BACKEND_CLASS_NAME);
142147
logger.info("Get configurations:" + conf);
148+
logger.info("Set cache capacity: " + this.cacheCapacityInBytes + " bytes");
143149
}
144150

145151
/**
@@ -192,8 +198,8 @@ public CacheStatsReportRunnable(long sleepTimeInSecond) {
192198
private void writeStatsToFile() {
193199
try {
194200
PrintWriter writer = new PrintWriter(new File(statsFilePath));
195-
long missCount = queryCount - hitCount;
196-
writer.println(String.format("CacheStats{hitCount=%d, missCount=%d, }", hitCount, missCount));
201+
long missCount = queryCount.get() - hitCount.get();
202+
writer.println(String.format("CacheStats{hitCount=%d, missCount=%d, }", hitCount.get(), missCount));
197203
writer.close();
198204
} catch (FileNotFoundException e) {
199205
e.printStackTrace();

0 commit comments

Comments
 (0)