Skip to content

Commit 95a2651

Browse files
author
andrew mcdonald
committed
use com.github.benmanes.caffeine.cache.Cache
1 parent 8333a39 commit 95a2651

26 files changed

Lines changed: 104 additions & 122 deletions

File tree

core/src/main/java/org/apache/accumulo/core/clientImpl/bulk/BulkImport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@
8888
import org.slf4j.Logger;
8989
import org.slf4j.LoggerFactory;
9090

91+
import com.github.benmanes.caffeine.cache.Cache;
92+
import com.github.benmanes.caffeine.cache.Caffeine;
9193
import com.google.common.base.Preconditions;
92-
import com.google.common.cache.Cache;
93-
import com.google.common.cache.CacheBuilder;
9494
import com.google.common.collect.Sets;
9595

9696
public class BulkImport implements ImportDestinationArguments, ImportMappingOptions {
@@ -398,7 +398,7 @@ private static Cache<String,Long> getPopulatedFileLenCache(Path dir, List<FileSt
398398
Map<String,Long> absFileLens = new HashMap<>();
399399
fileLens.forEach((k, v) -> absFileLens.put(pathToCacheId(new Path(dir, k)), v));
400400

401-
Cache<String,Long> fileLenCache = CacheBuilder.newBuilder().build();
401+
Cache<String,Long> fileLenCache = Caffeine.newBuilder().build();
402402

403403
fileLenCache.putAll(absFileLens);
404404

core/src/main/java/org/apache/accumulo/core/data/InstanceId.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
import java.util.Objects;
2222
import java.util.UUID;
23-
import java.util.concurrent.ExecutionException;
2423

25-
import com.google.common.cache.Cache;
26-
import com.google.common.cache.CacheBuilder;
24+
import com.github.benmanes.caffeine.cache.Cache;
25+
import com.github.benmanes.caffeine.cache.Caffeine;
2726

2827
/**
2928
* A strongly typed representation of an Accumulo instance ID. The constructor for this class will
@@ -36,7 +35,7 @@ public class InstanceId extends AbstractId<InstanceId> {
3635
// cache is for canonicalization/deduplication of created objects,
3736
// to limit the number of InstanceId objects in the JVM at any given moment
3837
// WeakReferences are used because we don't need them to stick around any longer than they need to
39-
static final Cache<String,InstanceId> cache = CacheBuilder.newBuilder().weakValues().build();
38+
static final Cache<String,InstanceId> cache = Caffeine.newBuilder().weakValues().build();
4039

4140
private InstanceId(String canonical) {
4241
super(canonical);
@@ -49,12 +48,7 @@ private InstanceId(String canonical) {
4948
* @return InstanceId object
5049
*/
5150
public static InstanceId of(final String canonical) {
52-
try {
53-
return cache.get(canonical, () -> new InstanceId(canonical));
54-
} catch (ExecutionException e) {
55-
throw new AssertionError(
56-
"This should never happen: ID constructor should never return null.");
57-
}
51+
return cache.get(canonical, k -> new InstanceId(canonical));
5852
}
5953

6054
/**

core/src/main/java/org/apache/accumulo/core/data/NamespaceId.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
*/
1919
package org.apache.accumulo.core.data;
2020

21-
import java.util.concurrent.ExecutionException;
22-
23-
import com.google.common.cache.Cache;
24-
import com.google.common.cache.CacheBuilder;
21+
import com.github.benmanes.caffeine.cache.Cache;
22+
import com.github.benmanes.caffeine.cache.Caffeine;
2523

2624
/**
2725
* A strongly typed representation of a namespace ID. This class cannot be used to get a namespace
@@ -35,7 +33,7 @@ public class NamespaceId extends AbstractId<NamespaceId> {
3533
// cache is for canonicalization/deduplication of created objects,
3634
// to limit the number of NamespaceId objects in the JVM at any given moment
3735
// WeakReferences are used because we don't need them to stick around any longer than they need to
38-
static final Cache<String,NamespaceId> cache = CacheBuilder.newBuilder().weakValues().build();
36+
static final Cache<String,NamespaceId> cache = Caffeine.newBuilder().weakValues().build();
3937

4038
private NamespaceId(String canonical) {
4139
super(canonical);
@@ -48,11 +46,6 @@ private NamespaceId(String canonical) {
4846
* @return NamespaceId object
4947
*/
5048
public static NamespaceId of(final String canonical) {
51-
try {
52-
return cache.get(canonical, () -> new NamespaceId(canonical));
53-
} catch (ExecutionException e) {
54-
throw new AssertionError(
55-
"This should never happen: ID constructor should never return null.");
56-
}
49+
return cache.get(canonical, k -> new NamespaceId(canonical));
5750
}
5851
}

core/src/main/java/org/apache/accumulo/core/data/TableId.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
*/
1919
package org.apache.accumulo.core.data;
2020

21-
import java.util.concurrent.ExecutionException;
22-
23-
import com.google.common.cache.Cache;
24-
import com.google.common.cache.CacheBuilder;
21+
import com.github.benmanes.caffeine.cache.Cache;
22+
import com.github.benmanes.caffeine.cache.Caffeine;
2523

2624
/**
2725
* A strongly typed representation of a table ID. This class cannot be used to get a table ID from a
@@ -35,7 +33,7 @@ public class TableId extends AbstractId<TableId> {
3533
// cache is for canonicalization/deduplication of created objects,
3634
// to limit the number of TableId objects in the JVM at any given moment
3735
// WeakReferences are used because we don't need them to stick around any longer than they need to
38-
static final Cache<String,TableId> cache = CacheBuilder.newBuilder().weakValues().build();
36+
static final Cache<String,TableId> cache = Caffeine.newBuilder().weakValues().build();
3937

4038
private TableId(final String canonical) {
4139
super(canonical);
@@ -48,11 +46,6 @@ private TableId(final String canonical) {
4846
* @return TableId object
4947
*/
5048
public static TableId of(final String canonical) {
51-
try {
52-
return cache.get(canonical, () -> new TableId(canonical));
53-
} catch (ExecutionException e) {
54-
throw new AssertionError(
55-
"This should never happen: ID constructor should never return null.");
56-
}
49+
return cache.get(canonical, k -> new TableId(canonical));
5750
}
5851
}

core/src/main/java/org/apache/accumulo/core/file/FileOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.apache.hadoop.fs.FileSystem;
4040
import org.apache.hadoop.mapred.FileOutputCommitter;
4141

42-
import com.google.common.cache.Cache;
42+
import com.github.benmanes.caffeine.cache.Cache;
4343

4444
public abstract class FileOperations {
4545

core/src/main/java/org/apache/accumulo/core/file/blockfile/impl/CachableBlockFile.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Collections;
2727
import java.util.Map;
2828
import java.util.Objects;
29-
import java.util.concurrent.ExecutionException;
3029
import java.util.concurrent.atomic.AtomicReference;
3130
import java.util.function.Supplier;
3231

@@ -51,7 +50,7 @@
5150
import org.slf4j.Logger;
5251
import org.slf4j.LoggerFactory;
5352

54-
import com.google.common.cache.Cache;
53+
import com.github.benmanes.caffeine.cache.Cache;
5554

5655
/**
5756
* This is a wrapper class for BCFile that includes a cache for independent caches for datablocks
@@ -173,9 +172,15 @@ public static class Reader implements Closeable {
173172

174173
private long getCachedFileLen() throws IOException {
175174
try {
176-
return fileLenCache.get(cacheId, lengthSupplier::get);
177-
} catch (ExecutionException e) {
178-
throw new IOException("Failed to get " + cacheId + " len from cache ", e);
175+
return fileLenCache.get(cacheId, k -> {
176+
try {
177+
return lengthSupplier.get();
178+
} catch (IOException e) {
179+
throw new UncheckedIOException(e);
180+
}
181+
});
182+
} catch (UncheckedIOException e) {
183+
throw new IOException("Failed to get " + cacheId + " len from cache ", e.getCause());
179184
}
180185
}
181186

core/src/main/java/org/apache/accumulo/core/iterators/Combiner.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.util.List;
2828
import java.util.Map;
2929
import java.util.NoSuchElementException;
30-
import java.util.concurrent.ExecutionException;
3130

31+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3232
import org.apache.accumulo.core.client.IteratorSetting;
3333
import org.apache.accumulo.core.client.IteratorSetting.Column;
3434
import org.apache.accumulo.core.client.ScannerBase;
@@ -43,10 +43,10 @@
4343
import org.slf4j.Logger;
4444
import org.slf4j.LoggerFactory;
4545

46+
import com.github.benmanes.caffeine.cache.Cache;
47+
import com.github.benmanes.caffeine.cache.Caffeine;
4648
import com.google.common.annotations.VisibleForTesting;
4749
import com.google.common.base.Splitter;
48-
import com.google.common.cache.Cache;
49-
import com.google.common.cache.CacheBuilder;
5050
import com.google.common.collect.Lists;
5151

5252
/**
@@ -187,23 +187,20 @@ public void next() throws IOException {
187187

188188
@VisibleForTesting
189189
static final Cache<String,Boolean> loggedMsgCache =
190-
CacheBuilder.newBuilder().expireAfterWrite(1, HOURS).maximumSize(10000).build();
190+
Caffeine.newBuilder().expireAfterWrite(1, HOURS).maximumSize(10000).build();
191191

192+
@CanIgnoreReturnValue
192193
private void sawDelete() {
193194
if (isMajorCompaction && !reduceOnFullCompactionOnly) {
194-
try {
195-
loggedMsgCache.get(this.getClass().getName(), () -> {
196-
sawDeleteLog.error(
197-
"Combiner of type {} saw a delete during a"
198-
+ " partial compaction. This could cause undesired results. See"
199-
+ " ACCUMULO-2232. Will not log subsequent occurrences for at least 1 hour.",
200-
Combiner.this.getClass().getSimpleName());
201-
// the value is not used and does not matter
202-
return Boolean.TRUE;
203-
});
204-
} catch (ExecutionException e) {
205-
throw new RuntimeException(e);
206-
}
195+
loggedMsgCache.get(this.getClass().getName(), k -> {
196+
sawDeleteLog.error(
197+
"Combiner of type {} saw a delete during a"
198+
+ " partial compaction. This could cause undesired results. See"
199+
+ " ACCUMULO-2232. Will not log subsequent occurrences for at least 1 hour.",
200+
Combiner.this.getClass().getSimpleName());
201+
// the value is not used and does not matter
202+
return Boolean.TRUE;
203+
});
207204
}
208205
}
209206

core/src/main/java/org/apache/accumulo/core/summary/Gatherer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@
8484
import org.slf4j.Logger;
8585
import org.slf4j.LoggerFactory;
8686

87+
import com.github.benmanes.caffeine.cache.Cache;
8788
import com.google.common.base.Preconditions;
88-
import com.google.common.cache.Cache;
8989
import com.google.common.hash.Hashing;
9090

9191
/**

core/src/main/java/org/apache/accumulo/core/summary/SummaryReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.apache.hadoop.fs.Path;
4949
import org.apache.hadoop.io.WritableUtils;
5050

51-
import com.google.common.cache.Cache;
51+
import com.github.benmanes.caffeine.cache.Cache;
5252

5353
public class SummaryReader {
5454

core/src/main/java/org/apache/accumulo/core/util/tables/TableZooHelper.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import java.util.List;
2727
import java.util.Objects;
28-
import java.util.concurrent.ExecutionException;
2928

3029
import org.apache.accumulo.core.Constants;
3130
import org.apache.accumulo.core.client.NamespaceNotFoundException;
@@ -40,16 +39,16 @@
4039
import org.apache.accumulo.core.metadata.MetadataTable;
4140
import org.apache.accumulo.core.metadata.RootTable;
4241

43-
import com.google.common.cache.Cache;
44-
import com.google.common.cache.CacheBuilder;
42+
import com.github.benmanes.caffeine.cache.Cache;
43+
import com.github.benmanes.caffeine.cache.Caffeine;
4544

4645
public class TableZooHelper implements AutoCloseable {
4746

4847
private final ClientContext context;
4948
// Per instance cache will expire after 10 minutes in case we
5049
// encounter an instance not used frequently
5150
private final Cache<TableZooHelper,TableMap> instanceToMapCache =
52-
CacheBuilder.newBuilder().expireAfterAccess(10, MINUTES).build();
51+
Caffeine.newBuilder().expireAfterAccess(10, MINUTES).build();
5352

5453
public TableZooHelper(ClientContext context) {
5554
this.context = Objects.requireNonNull(context);
@@ -127,11 +126,7 @@ public TableMap getTableMap() {
127126
}
128127

129128
private TableMap getCachedTableMap() {
130-
try {
131-
return instanceToMapCache.get(this, () -> new TableMap(context));
132-
} catch (ExecutionException e) {
133-
throw new RuntimeException(e);
134-
}
129+
return instanceToMapCache.get(this, k -> new TableMap(context));
135130
}
136131

137132
public boolean tableNodeExists(TableId tableId) {

0 commit comments

Comments
 (0)