Github user paul-rogers commented on a diff in the pull request:

    https://github.com/apache/drill/pull/701#discussion_r99454556
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/FunctionImplementationRegistry.java
 ---
    @@ -260,76 +293,101 @@ public RemoteFunctionRegistry 
getRemoteFunctionRegistry() {
       }
     
       /**
    -   * Attempts to load and register functions from remote function registry.
    -   * First checks if there is no missing jars.
    -   * If yes, enters synchronized block to prevent other loading the same 
jars.
    -   * Again re-checks if there are no missing jars in case someone has 
already loaded them (double-check lock).
    -   * If there are still missing jars, first copies jars to local udf area 
and prepares {@link JarScan} for each jar.
    -   * Jar registration timestamp represented in milliseconds is used as 
suffix.
    -   * Then registers all jars at the same time. Returns true when finished.
    -   * In case if any errors during jars coping or registration, logs errors 
and proceeds.
    +   * Purpose of this method is to synchronize remote and local function 
registries if needed
    +   * and to inform if function registry was changed after given version.
        *
    -   * If no missing jars are found, checks current local registry version.
    -   * Returns false if versions match, true otherwise.
    +   * To make synchronization as much light-weigh as possible, first only 
versions of both registries are checked
    +   * without any locking. If synchronization is needed, enters 
synchronized block to prevent others loading the same jars.
    +   * The need of synchronization is checked again (double-check lock) 
before comparing jars.
    +   * If any missing jars are found, they are downloaded to local udf area, 
each is wrapped into {@link JarScan}.
    +   * Once jar download is finished, all missing jars are registered in one 
batch.
    +   * In case if any errors during jars download / registration, these 
errors are logged.
        *
    -   * @param version local function registry version
    -   * @return true if new jars were registered or local function registry 
version is different, false otherwise
    +   * During registration local function registry is updated with remote 
function registry version it is synced with.
    +   * When at least one jar of the missing jars failed to download / 
register,
    +   * local function registry version are not updated but jars that where 
successfully downloaded / registered
    +   * are added to local function registry.
    +   *
    +   * If synchronization between remote and local function registry was not 
needed,
    +   * checks if given registry version matches latest sync version
    +   * to inform if function registry was changed after given version.
    +   *
    +   * @param version remote function registry local function registry was 
based on
    +   * @return true if remote and local function registries were 
synchronized after given version
        */
    -  public boolean loadRemoteFunctions(long version) {
    -    List<String> missingJars = getMissingJars(remoteFunctionRegistry, 
localFunctionRegistry);
    -    if (!missingJars.isEmpty()) {
    +  public boolean syncWithRemoteRegistry(long version) {
    +    if 
(doSyncFunctionRegistries(remoteFunctionRegistry.getRegistryVersion(), 
localFunctionRegistry.getVersion())) {
           synchronized (this) {
    -        missingJars = getMissingJars(remoteFunctionRegistry, 
localFunctionRegistry);
    -        if (!missingJars.isEmpty()) {
    -          logger.info("Starting dynamic UDFs lazy-init process.\n" +
    -              "The following jars are going to be downloaded and 
registered locally: " + missingJars);
    +        long localRegistryVersion = localFunctionRegistry.getVersion();
    +        if 
(doSyncFunctionRegistries(remoteFunctionRegistry.getRegistryVersion(), 
localRegistryVersion))  {
    +          DataChangeVersion remoteVersion = new DataChangeVersion();
    +          List<String> missingJars = 
getMissingJars(this.remoteFunctionRegistry, localFunctionRegistry, 
remoteVersion);
               List<JarScan> jars = Lists.newArrayList();
    -          for (String jarName : missingJars) {
    -            Path binary = null;
    -            Path source = null;
    -            URLClassLoader classLoader = null;
    -            try {
    -              binary = copyJarToLocal(jarName, remoteFunctionRegistry);
    -              source = copyJarToLocal(JarUtil.getSourceName(jarName), 
remoteFunctionRegistry);
    -              URL[] urls = {binary.toUri().toURL(), 
source.toUri().toURL()};
    -              classLoader = new URLClassLoader(urls);
    -              ScanResult scanResult = scan(classLoader, binary, urls);
    -              localFunctionRegistry.validate(jarName, scanResult);
    -              jars.add(new JarScan(jarName, scanResult, classLoader));
    -            } catch (Exception e) {
    -              deleteQuietlyLocalJar(binary);
    -              deleteQuietlyLocalJar(source);
    -              if (classLoader != null) {
    -                try {
    -                  classLoader.close();
    -                } catch (Exception ex) {
    -                  logger.warn("Problem during closing class loader for 
{}", jarName, e);
    +          if (!missingJars.isEmpty()) {
    +            logger.info("Starting dynamic UDFs lazy-init process.\n" +
    +                "The following jars are going to be downloaded and 
registered locally: " + missingJars);
    +            for (String jarName : missingJars) {
    +              Path binary = null;
    +              Path source = null;
    +              URLClassLoader classLoader = null;
    +              try {
    +                binary = copyJarToLocal(jarName, 
this.remoteFunctionRegistry);
    +                source = copyJarToLocal(JarUtil.getSourceName(jarName), 
this.remoteFunctionRegistry);
    +                URL[] urls = {binary.toUri().toURL(), 
source.toUri().toURL()};
    +                classLoader = new URLClassLoader(urls);
    +                ScanResult scanResult = scan(classLoader, binary, urls);
    +                localFunctionRegistry.validate(jarName, scanResult);
    +                jars.add(new JarScan(jarName, scanResult, classLoader));
    +              } catch (Exception e) {
    +                deleteQuietlyLocalJar(binary);
    --- End diff --
    
    Race condition?
    
    Suppose I have a query:
    ```
    SELECT fn1(a), fn2(b) FROM ...
    ```
    
    `fn1` is an existing Dynamic UDF (DUDF). `fn2()` is overloaded. We do the 
above check for `fn2`. In that check, we learn that `fn1` has been deleted. At 
this point, we have passed the checks for that function, will proceed to 
execute the query. `fn1` will then fail.
    
    Also, consider a different query:
    
    ```
    SELECT fn2(b), fn3(b) FROM ...
    ```
    
    Now, both functions are overloaded. Both will trigger the remote version 
check.
    
    In both cases, we have a non-atomic check; the query can be processed using 
two different versions of the remote registry.
    
    What we need to do is either:
    
    1. If we need to do a check, restart all function checks (or at least those 
that referred to a DUDF.)
    2. Do the version check once at the start of the query parse, and stick 
with that version for the entire query.
    
    Neither option is good. Option 1 causes the race conditions outlined here. 
Option 2 is slow.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to