You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2014/04/04 15:46:28 UTC

svn commit: r1584685 - in /uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima: analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java impl/ChildUimaContext_impl.java impl/RootUimaContext_impl.java impl/UimaContext_ImplBase.java

Author: schor
Date: Fri Apr  4 13:46:27 2014
New Revision: 1584685

URL: http://svn.apache.org/r1584685
Log:
[UIMA-3693][UIMA-3694] skip unneeded work, synch resolve imports, make uima-context fields final (preferable) or volatile

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/ChildUimaContext_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/RootUimaContext_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java?rev=1584685&r1=1584684&r2=1584685&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/analysis_engine/metadata/impl/FlowControllerDeclaration_impl.java Fri Apr  4 13:46:27 2014
@@ -112,15 +112,19 @@ public class FlowControllerDeclaration_i
    * @see FlowControllerDeclaration#resolveImports()
    */
   public void resolveImports() throws InvalidXMLException {
-    resolveImports(UIMAFramework.newDefaultResourceManager());
+    if (getImport() != null) {
+      resolveImports(UIMAFramework.newDefaultResourceManager());      
+    }
   }
 
   /*
    * (non-Javadoc)
    * 
    * @see FlowControllerDeclaration#resolveImports(org.apache.uima.resource.ResourceManager)
+   * Synchronized to support parallel initialization calls on primitive AEs, sharing
+   * a common Resource Manager, and perhaps common UIMA Contexts
    */
-  public void resolveImports(ResourceManager aResourceManager) throws InvalidXMLException {
+  public synchronized void resolveImports(ResourceManager aResourceManager) throws InvalidXMLException {
     Import theImport = getImport();
     if (theImport != null) {
       URL url = theImport.findAbsoluteUrl(aResourceManager);

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/ChildUimaContext_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/ChildUimaContext_impl.java?rev=1584685&r1=1584684&r2=1584685&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/ChildUimaContext_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/ChildUimaContext_impl.java Fri Apr  4 13:46:27 2014
@@ -41,24 +41,24 @@ public class ChildUimaContext_impl exten
   /**
    * Logger
    */
-  private Logger mLogger;
+  private volatile Logger mLogger;
 
   /**
    * Root Context (if root, points to self)
    */
-  private UimaContextAdmin mRootContext;
+  private final UimaContextAdmin mRootContext;
 
   /**
    * This Context's view of the Session object
    */
-  private SessionNamespaceView_impl mSessionNamespaceView;
+  private final SessionNamespaceView_impl mSessionNamespaceView;
 
   /**
    * ResourceManager used to locate and access external resources
    * Set non-null only for Pear resources contained in an aggregate
    */
   
-  private ResourceManager mPearResourceManager = null;
+  private volatile ResourceManager mPearResourceManager = null;
 
   /**
    * ref to the parent.  
@@ -73,14 +73,12 @@ public class ChildUimaContext_impl exten
    */
   public ChildUimaContext_impl(UimaContextAdmin aParentContext, String aContextName,
           Map<String, String> aSofaMappings) {
+    super(aParentContext.getQualifiedContextName() + aContextName + '/', aSofaMappings);
     mRootContext = aParentContext.getRootContext();
     mLogger = aParentContext.getRootContext().getLogger();
-    mQualifiedContextName = aParentContext.getQualifiedContextName() + aContextName + '/';
     mSessionNamespaceView = new SessionNamespaceView_impl(mRootContext.getSession(),
             mQualifiedContextName);
-    mSofaMappings = aSofaMappings;
     parentContext = aParentContext;
-    mExternalOverrides = aParentContext.getExternalOverrides();
   }
 
   /*

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/RootUimaContext_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/RootUimaContext_impl.java?rev=1584685&r1=1584684&r2=1584685&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/RootUimaContext_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/RootUimaContext_impl.java Fri Apr  4 13:46:27 2014
@@ -21,6 +21,7 @@ package org.apache.uima.impl;
 
 import java.util.TreeMap;
 
+import org.apache.uima.UIMAFramework;
 import org.apache.uima.UimaContext;
 import org.apache.uima.UimaContextAdmin;
 import org.apache.uima.internal.util.InstrumentationFacility_impl;
@@ -31,6 +32,7 @@ import org.apache.uima.resource.impl.Ses
 import org.apache.uima.util.InstrumentationFacility;
 import org.apache.uima.util.Logger;
 import org.apache.uima.util.ProcessTrace;
+import org.apache.uima.util.Settings;
 
 /**
  * Implementation of the root {@link UimaContext}. UIMA Contexts are arranged in a tree structure
@@ -42,30 +44,58 @@ public class RootUimaContext_impl extend
 
   /**
    * Logger
+   * 
+   * Volatile because mLogger can be updated on one thread and accessed on another
+   * 
    */
-  private Logger mLogger;
+  private volatile Logger mLogger;
 
   /**
    * ResourceManager used to locate and access external resources
    */
-  private ResourceManager mResourceManager;
+  private final ResourceManager mResourceManager;
 
   /**
    * ConfigurationManager used to access configuration parameter settings
    */
-  private ConfigurationManager mConfigurationManager;
+  private final ConfigurationManager mConfigurationManager;
 
   /**
    * Instrumentation Facility (wraps ProcessTrace)
    */
-  private InstrumentationFacility_impl mInstrumentationFacility = new InstrumentationFacility_impl(
+  final private InstrumentationFacility_impl mInstrumentationFacility = new InstrumentationFacility_impl(
           null);
 
   /**
    * Current Session
+   * 
+   * Has general setter and getter;
+   * marked volatile to allow effect of setting to be seen on another thread
    */
-  private Session mSession;
-
+  private volatile Session mSession;
+  
+  /**
+   * External parameter override specifications - held at the root context level
+   */
+  protected volatile Settings mExternalOverrides;
+  
+  public Settings getExternalOverrides() {
+    return mExternalOverrides;
+  }
+  
+  public void setExternalOverrides(Settings externalOverrides) {
+    mExternalOverrides = externalOverrides;
+  }
+
+  public RootUimaContext_impl() {
+    // ugly trick - passing parameters in thread local of one known caller,
+    // to allow these to be final,
+    // which causes a store memory barrier to be inserted for them
+    // which makes other accesses to them "safe" from other threads
+    //   without further synchronization
+    mResourceManager = UIMAFramework.newContextResourceManager.get();
+    mConfigurationManager = UIMAFramework.newContextConfigManager.get();
+  }
   /*
    * (non-Javadoc)
    * 
@@ -76,11 +106,9 @@ public class RootUimaContext_impl extend
   public void initializeRoot(Logger aLogger, ResourceManager aResourceManager,
           ConfigurationManager aConfigurationManager) {
     mLogger = aLogger;
-    mResourceManager = aResourceManager;
-    mConfigurationManager = aConfigurationManager;
-    mQualifiedContextName = "/";
+//    mResourceManager = aResourceManager;
+//    mConfigurationManager = aConfigurationManager;
     mSession = new Session_impl();
-    mSofaMappings = new TreeMap<String, String>();
   }
 
   /**

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java?rev=1584685&r1=1584684&r2=1584685&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java Fri Apr  4 13:46:27 2014
@@ -68,19 +68,29 @@ public abstract class UimaContext_ImplBa
    */
   private static final String LOG_RESOURCE_BUNDLE = "org.apache.uima.impl.log_messages";
   
+  private static final String[] EMPTY_STRINGS = new String[0];
+  
+  /**
+   * The ComponentInfoImpl class (an inner non-static class) has no fields and 
+   *   just one method that refers to fields in
+   *   this (containing, outer) class.  So it seems the method could just be used directly,
+   *   and putting it into an inner class is silly.
+   */
   final private ComponentInfo mComponentInfo = new ComponentInfoImpl();
-
+  
   /**
    * Fully-qualified name of this context.
    */
-  protected String mQualifiedContextName;
+  final protected String mQualifiedContextName;
 
   /**
    * Mapping between sofa names assigned by an aggregate engine to sofa names assigned by the
    * component engines. The key is the component sofa name and the value is the absolute sofa name
    * assigned by a top level aggregate in this process.
+   * 
+   * Multi-threading:  This map is constructed at Constructor time, and never updated, only referenced, subsequently.
    */
-  protected Map<String, String> mSofaMappings;
+  final protected Map<String, String> mSofaMappings;
 
   /**
    * Size of the CAS pool used to support the {@link #getEmptyCas(Class)} method.
@@ -96,24 +106,34 @@ public abstract class UimaContext_ImplBa
    * The value of this max is set by calling the annotator's method getCasInstancesRequired method,
    * a user-supplied method that is used to configure this.  The default is 1.
    */
-  protected int mCasPoolSize = 0;
+  protected volatile int mCasPoolSize = 0;
 
   /**
    * Performance tuning settings. Needed to specify CAS heap size for {@link #getEmptyCas(Class)}
    * method.
+   * 
+   * Set during initialize calls for Analysis Engine components.
+   * Referenced during later lazy creation of Cas Pool upon first getEmptyCas call.
+   * Not expected to be modified after set.
    */
-  private Properties mPerformanceTuningSettings;
+  private volatile Properties mPerformanceTuningSettings;
 
   /**
    * Whether the component that accesses the CAS pool is sofa-aware. Needed to determine which view
    * is returned by the {@link #getEmptyCas(Class)} method.
+   * 
+   * Set during initialize calls for Analysis Engine components.
+   * Referenced during later lazy creation of Cas Pool upon first getEmptyCas call.
+   * Not expected to be modified after set.
    */
-  private boolean mSofaAware;
+  private volatile boolean mSofaAware;
 
   /**
    * Keeps track of whether we've created a CAS pool yet, which happens on the first call to
    * {@link #getEmptyCas(Class)}.
    *   See http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java for why this is volatile
+   *   
+   * Ref'd and set inside getEmptyCas, part of lazy initialization of cas pool  
    */
   private volatile boolean mCasPoolCreated = false;
 
@@ -143,17 +163,31 @@ public abstract class UimaContext_ImplBa
   /**
    * Object that implements management interface to the AE.
    */
-  protected AnalysisEngineManagementImpl mMBean = new AnalysisEngineManagementImpl();
+  final protected AnalysisEngineManagementImpl mMBean = new AnalysisEngineManagementImpl();
 
   final private String uniqueIdentifier;
   
-  protected Settings mExternalOverrides;
+  /**
+   * Default constructor. 
+   * Only Called for creating a "Root" instance.
+   */
+  public UimaContext_ImplBase() { 
+    mQualifiedContextName = "/";  // This constructor for root call only
+    uniqueIdentifier = constructUniqueName();
+    mSofaMappings = new TreeMap<String, String>();
 
-  /*  Default constructor. Its main purpose is to create a UUID-like
-   *  unique name for this component.
-   *
+  }
+  
+  /**
+   * Constructor for non Root instances
    */
-  public UimaContext_ImplBase() {
+  public UimaContext_ImplBase(String contextName, Map<String, String> sofaMappings) {
+    mQualifiedContextName = contextName;
+    uniqueIdentifier = constructUniqueName();
+    mSofaMappings = sofaMappings;
+  }
+  
+  private String constructUniqueName() {
     //  Generate unique name for this component
     // this method generates less garbage than replaceAll, etc.
     String u = new UID().toString();
@@ -167,7 +201,7 @@ public abstract class UimaContext_ImplBa
         sb.setCharAt(i, '_');
       }
     }
-    uniqueIdentifier = sb.toString();
+    return sb.toString();
   }
   /* Returns a unique name of this component
    * 
@@ -450,7 +484,7 @@ public abstract class UimaContext_ImplBa
     ConfigurationGroup[] groups = getConfigurationManager().getConfigParameterDeclarations(
             getQualifiedContextName()).getConfigurationGroups();
     if (groups == null) {
-      return new String[0];
+      return EMPTY_STRINGS;
     } else {
       Set<String> names = new TreeSet<String>();
       for (int i = 0; i < groups.length; i++) {
@@ -471,7 +505,7 @@ public abstract class UimaContext_ImplBa
     ConfigurationParameter[] params = getConfigurationManager().getConfigParameterDeclarations(
             getQualifiedContextName()).getConfigurationParameters();
     if (params == null) {
-      return new String[0];
+      return EMPTY_STRINGS;
     } else {
       String[] names = new String[params.length];
       for (int i = 0; i < params.length; i++) {
@@ -490,7 +524,7 @@ public abstract class UimaContext_ImplBa
     ConfigurationGroup[] groups = getConfigurationManager().getConfigParameterDeclarations(
             getQualifiedContextName()).getConfigurationGroupDeclarations(aGroup);
     if (groups.length == 0) {
-      return new String[0];
+      return EMPTY_STRINGS;
     } else {
       List<String> names = new ArrayList<String>();
       ConfigurationParameter[] commonParams = getConfigurationManager()
@@ -518,7 +552,7 @@ public abstract class UimaContext_ImplBa
    * @see org.apache.uima.UimaContextAdmin#getExternalOverrides()
    */
   public Settings getExternalOverrides() {
-    return mExternalOverrides;
+    return getRootContext().getExternalOverrides();
   }
 
   /**
@@ -527,7 +561,7 @@ public abstract class UimaContext_ImplBa
    * @see org.apache.uima.UimaContextAdmin#setExternalOverrides(org.apache.uima.util.Settings)
    */
   public void setExternalOverrides(Settings externalOverrides) {
-    mExternalOverrides = externalOverrides; 
+    getRootContext().setExternalOverrides(externalOverrides);
   }
   
   /**