You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/02/18 03:55:49 UTC
[GitHub] [pulsar] BewareMyPower commented on a change in pull request #14346: [Pulsar-Functions]Remove unnecessary synchronized-block in FunctionCacheManagerImpl
BewareMyPower commented on a change in pull request #14346:
URL: https://github.com/apache/pulsar/pull/14346#discussion_r809655470
##########
File path: pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/functioncache/FunctionCacheManagerImpl.java
##########
@@ -116,43 +112,35 @@ public void registerFunctionInstanceWithArchive(String fid, String eid,
throw new NullPointerException("FunctionID not set");
}
- synchronized (cacheFunctions) {
- FunctionCacheEntry entry = cacheFunctions.get(fid);
+ FunctionCacheEntry entry = cacheFunctions.get(fid);
- if (null != entry) {
- entry.register(eid, Collections.singleton(narArchive), Collections.emptyList());
- return;
- }
+ if (null != entry) {
+ entry.register(eid, Collections.singleton(narArchive), Collections.emptyList());
+ return;
+ }
- // Create new cache entry
- try {
- cacheFunctions.put(fid, new FunctionCacheEntry(narArchive, eid, rootClassLoader, narExtractionDirectory));
- } catch (Throwable cause) {
- Exceptions.rethrowIOException(cause);
- }
+ // Create new cache entry
+ try {
+ cacheFunctions.put(fid, new FunctionCacheEntry(narArchive, eid, rootClassLoader, narExtractionDirectory));
+ } catch (Throwable cause) {
+ Exceptions.rethrowIOException(cause);
}
Review comment:
We should also use `computeIfAbsent` here.
##########
File path: pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/functioncache/FunctionCacheManagerImpl.java
##########
@@ -73,39 +71,37 @@ public void registerFunctionInstance(String fid,
throw new NullPointerException("FunctionID not set");
}
- synchronized (cacheFunctions) {
- FunctionCacheEntry entry = cacheFunctions.get(fid);
+ FunctionCacheEntry entry = cacheFunctions.get(fid);
- if (null == entry) {
- URL[] urls = new URL[requiredJarFiles.size() + requiredClasspaths.size()];
- int count = 0;
- try {
- // add jar files to urls
- for (String jarFile : requiredJarFiles) {
- urls[count++] = new File(jarFile).toURI().toURL();
- }
+ if (null == entry) {
+ URL[] urls = new URL[requiredJarFiles.size() + requiredClasspaths.size()];
+ int count = 0;
+ try {
+ // add jar files to urls
+ for (String jarFile : requiredJarFiles) {
+ urls[count++] = new File(jarFile).toURI().toURL();
+ }
- // add classpaths
- for (URL url : requiredClasspaths) {
- urls[count++] = url;
- }
+ // add classpaths
+ for (URL url : requiredClasspaths) {
+ urls[count++] = url;
+ }
- cacheFunctions.put(
+ cacheFunctions.put(
fid,
new FunctionCacheEntry(
- requiredJarFiles,
- requiredClasspaths,
- urls,
- eid, rootClassLoader));
- } catch (Throwable cause) {
- Exceptions.rethrowIOException(cause);
- }
Review comment:
We should use `computeIfAbsent` here. Because in the `if` branch, the composed `get` and `put` operations are not atomic.
We can use a `AtomicBoolean` to check if a new value has been put, like:
```java
final Map<String, String> map = new ConcurrentHashMap<>();
final AtomicBoolean flag = new AtomicBoolean(false);
final String value = map.computeIfAbsent("A", __ -> {
flag.set(true);
return "B";
});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org