// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.actions;

import com.google.devtools.build.lib.util.Fingerprint;
import javax.annotation.Nullable;

/**
 * An implementation of {@link ActionAnalysisMetadata} that caches its {@linkplain #getKey key} so
 * that it is only computed once.
 */
public abstract class ActionKeyCacher implements ActionAnalysisMetadata {

  @Nullable private String cachedKey = null;

  @Override
  public final synchronized String getKey(ActionKeyContext actionKeyContext) {
    if (cachedKey == null) {
      try {
        Fingerprint fp = new Fingerprint();
        computeKey(actionKeyContext, fp);

        // Add a bool indicating whether the execution platform was set.
        fp.addBoolean(getExecutionPlatform() != null);
        if (getExecutionPlatform() != null) {
          // Add the execution platform information.
          getExecutionPlatform().addTo(fp);
        }

        // Compute the actual key and store it.
        cachedKey = fp.hexDigestAndReset();
      } catch (CommandLineExpansionException e) {
        cachedKey = KEY_ERROR;
      }
    }
    return cachedKey;
  }

  /**
   * See the javadoc for {@link Action} and {@link ActionAnalysisMetadata#getKey} for the contract
   * of this method.
   */
  protected abstract void computeKey(ActionKeyContext actionKeyContext, Fingerprint fp)
      throws CommandLineExpansionException;
}

@@ -982,16 +982,25 @@
 private 
+volatile 
 String c
@@ -1048,21 +1048,8 @@
 nal 
-synchronized 
 Stri
@@ -1120,24 +1120,89 @@
  == null) %7B%0A
+      synchronized (this) %7B%0A        if (cachedKey == null) %7B%0A    
       try %7B%0A
@@ -1209,16 +1209,20 @@
         
+  
+  
 Fingerpr
@@ -1257,16 +1257,20 @@
         
+   
+ 
 computeK
@@ -1296,16 +1296,20 @@
 , fp);%0A%0A
+    
         
@@ -1381,16 +1381,20 @@
         
+    
 fp.addBo
@@ -1436,24 +1436,28 @@
 l);%0A        
+    
 if (getExecu
@@ -1488,24 +1488,28 @@
 %7B%0A          
+  
+  
 // Add the e
@@ -1549,16 +1549,20 @@
         
+    
 getExecu
@@ -1595,19 +1595,27 @@
         
+   
+ 
 %7D%0A%0A
+    
         
@@ -1654,32 +1654,36 @@
 ore it.%0A        
+    
 cachedKey = fp.h
@@ -1708,16 +1708,20 @@
 ;%0A      
+ 
+   
 %7D catch 
@@ -1756,32 +1756,36 @@
 on e) %7B%0A        
+    
 cachedKey = KEY_
@@ -1791,16 +1791,38 @@
 _ERROR;%0A
+          %7D%0A        %7D%0A
       %7D%0A

Use a double check in getKey instead of synchronizing every call.