/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.spark.util;

import scala.Function0;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;

/**
 * A lock-free implementation of a lazily-initialized variable.
 * If there are concurrent initializations then the `compute()` function may be invoked
 * multiple times. However, only a single `compute()` result will be stored and all readers
 * will receive the same result object instance.
 *
 * This may be helpful for avoiding deadlocks in certain scenarios where exactly-once
 * value computation is not a hard requirement.
 *
 * The main difference between this and [[BestEffortLazyVal]] is that:
 * [[BestEffortLazyVal]] serializes the cached value after computation, while
 * [[TransientBestEffortLazyVal]] always serializes the compute function.
 *
 * @note
 * This helper class has additional requirements on the compute function:
 *   1) The compute function MUST not return null;
 *   2) The computation failure is not cached.
 *
 * @note
 *   Scala 3 uses a different implementation of lazy vals which doesn't have this problem.
 *   Please refer to <a
 *   href="https://docs.scala-lang.org/scala3/reference/changed-features/lazy-vals-init.html">Lazy
 *   Vals Initialization</a> for more details.
 */
public class TransientBestEffortLazyVal<T> implements Serializable {
  private volatile Function0<T> compute;
  protected transient volatile T cached;

  private static final VarHandle HANDLE;
  static {
    try {
      HANDLE = MethodHandles.lookup()
        .in(TransientBestEffortLazyVal.class)
        .findVarHandle(TransientBestEffortLazyVal.class, "cached", Object.class);
    } catch (ReflectiveOperationException e) {
      throw new IllegalStateException("Failed to initialize VarHandle", e);
    }
  }

  public TransientBestEffortLazyVal(Function0<T> compute) {
    this.compute = compute;
  }

  public T apply() {
    T value = cached;
    if (value != null) {
      return value;
    }
    T newValue = compute.apply();
    assert newValue != null: "compute function cannot return null.";
    HANDLE.compareAndSet(this, null, newValue);
    return cached;
  }

  private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
    ois.defaultReadObject();
    cached = null;
  }
}
