BewareMyPower commented on a change in pull request #14346:
URL: https://github.com/apache/pulsar/pull/14346#discussion_r809860614
##########
File path:
pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/functioncache/FunctionCacheManagerImpl.java
##########
@@ -72,87 +70,70 @@ public void registerFunctionInstance(String fid,
if (fid == null) {
throw new NullPointerException("FunctionID not set");
}
+ 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;
+ }
- synchronized (cacheFunctions) {
- 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();
- }
-
- // add classpaths
- for (URL url : requiredClasspaths) {
- urls[count++] = url;
- }
-
- cacheFunctions.put(
- fid,
- new FunctionCacheEntry(
+ FunctionCacheEntry entry = cacheFunctions.putIfAbsent(
+ fid,
+ new FunctionCacheEntry(
requiredJarFiles,
requiredClasspaths,
urls,
eid, rootClassLoader));
- } catch (Throwable cause) {
- Exceptions.rethrowIOException(cause);
- }
- } else {
+ if (entry != null) {
entry.register(
- eid,
- requiredJarFiles,
- requiredClasspaths);
+ eid,
+ requiredJarFiles,
+ requiredClasspaths);
}
+ } catch (Throwable cause) {
+ Exceptions.rethrowIOException(cause);
}
}
@Override
public void registerFunctionInstanceWithArchive(String fid, String eid,
- String narArchive, String
narExtractionDirectory) throws IOException {
+ String narArchive,
+ String
narExtractionDirectory) throws IOException {
if (fid == null) {
throw new NullPointerException("FunctionID not set");
}
-
- synchronized (cacheFunctions) {
- FunctionCacheEntry entry = cacheFunctions.get(fid);
-
+ // Create new cache entry.
+ try {
+ FunctionCacheEntry entry = cacheFunctions.putIfAbsent(fid, new
FunctionCacheEntry(narArchive, eid,
+ rootClassLoader, narExtractionDirectory));
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);
}
+ } catch (Throwable cause) {
+ Exceptions.rethrowIOException(cause);
}
}
@Override
- public void unregisterFunctionInstance(String fid,
- String eid) {
- synchronized (cacheFunctions) {
- FunctionCacheEntry entry = cacheFunctions.get(fid);
-
- if (null != entry) {
- if (entry.unregister(eid)) {
- cacheFunctions.remove(fid);
- entry.close();
- }
+ public void unregisterFunctionInstance(String fid, String eid) {
+ FunctionCacheEntry entry = cacheFunctions.get(fid);
+ if (null != entry) {
+ if (entry.unregister(eid)) {
+ cacheFunctions.remove(fid);
+ entry.close();
}
}
Review comment:
I think it's the core problem here. Without the `synchronized` keyword,
it's hard to make this method thread safe. Because the first `get` call here
might return an outdated `FunctionCacheEntry` object, if we called the `remove`
method, the newly added `FunctionCacheEntry` might be removed unexpectedly.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]