/*
 * Licensed to the University of California, Berkeley 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 tachyon.client;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.concurrent.ThreadSafe;

import com.google.common.base.Preconditions;

import tachyon.Constants;
import tachyon.client.block.BlockStoreContext;
import tachyon.client.file.FileSystemContext;
import tachyon.client.lineage.LineageContext;
import tachyon.conf.TachyonConf;
import tachyon.exception.PreconditionMessage;
import tachyon.util.ThreadFactoryUtils;
import tachyon.worker.ClientMetrics;

/**
 * A shared context in each client JVM. It provides common functionality such as the Tachyon
 * configuration and master address.
 */
@ThreadSafe
public final class ClientContext {
  private static ExecutorService sExecutorService;
  private static TachyonConf sTachyonConf;
  private static InetSocketAddress sMasterAddress;
  private static ClientMetrics sClientMetrics;
  private static boolean sInitialized;

  static {
    sInitialized = false;
    reset();
  }

  /**
   * Initializes the client context singleton.
   */
  public static synchronized void reset() {
    reset(new TachyonConf());
  }

  /**
   * Initializes the client context singleton with a given conf.
   *
   * @param conf the configuration of Tachyon
   */
  public static synchronized void reset(TachyonConf conf) {
    sTachyonConf = conf;

    String masterHostname = Preconditions.checkNotNull(sTachyonConf.get(Constants.MASTER_HOSTNAME));
    int masterPort = sTachyonConf.getInt(Constants.MASTER_RPC_PORT);

    sMasterAddress = new InetSocketAddress(masterHostname, masterPort);
    sClientMetrics = new ClientMetrics();

    if (sExecutorService != null) {
      sExecutorService.shutdownNow();
    }
    sExecutorService = Executors.newFixedThreadPool(
        sTachyonConf.getInt(Constants.USER_BLOCK_WORKER_CLIENT_THREADS),
        ThreadFactoryUtils.build("block-worker-heartbeat-%d", true));
    // If this isn't the first time setting the ClientContext, we should reset other contexts so
    // that they can see the latest changes
    if (sInitialized) {
      BlockStoreContext.INSTANCE.reset();
      FileSystemContext.INSTANCE.reset();
      LineageContext.INSTANCE.reset();
    }
    sInitialized = true;
  }

  /**
   * @return the {@link TachyonConf} for the client process
   */
  public static synchronized TachyonConf getConf() {
    checkContextInitialized();
    return sTachyonConf;
  }

  /**
   * @return the {@link ClientMetrics} for this client
   */
  public static synchronized ClientMetrics getClientMetrics() {
    checkContextInitialized();
    return sClientMetrics;
  }

  /**
   * @return the master address
   */
  public static synchronized InetSocketAddress getMasterAddress() {
    checkContextInitialized();
    return sMasterAddress;
  }

  /**
   * @return the executor service
   */
  public static synchronized ExecutorService getExecutorService() {
    checkContextInitialized();
    return sExecutorService;
  }

  private static void checkContextInitialized() {
    Preconditions.checkState(sInitialized, PreconditionMessage.CLIENT_CONTEXT_NOT_INITIALIZED);
  }

  private ClientContext() {} // prevent instantiation
}

@@ -1071,208 +1071,23 @@
 on.c
-lient.block.BlockStoreContext;%0Aimport tachyon.client.file.FileSystemContext;%0Aimport tachyon.client.lineage.LineageContext;%0Aimport tachyon.conf.TachyonConf;%0Aimport tachyon.exception.PreconditionMessage
+onf.TachyonConf
 ;%0Aim
@@ -1543,84 +1543,19 @@
 cs;%0A
-  private static boolean sInitialized;%0A%0A  static %7B%0A    sInitialized = false;
+%0A  static %7B
 %0A   
@@ -1627,37 +1627,38 @@
 n.%0A   */%0A  p
-ublic
+rivate
  static 
 synchronized
@@ -1641,37 +1641,24 @@
 vate static 
-synchronized 
 void reset()
@@ -1668,18 +1668,9 @@
     
-reset(new 
+s
 Tach
@@ -1680,229 +1680,28 @@
 Conf
-());%0A  %7D%0A%0A  /**%0A   * Initializes the client context singleton with a given conf.%0A   *%0A   * @param conf the configuration of Tachyon%0A   */%0A  public static synchronized void reset(TachyonConf conf) %7B%0A    sTachyonConf = conf
+ = new TachyonConf()
 ;%0A%0A 
@@ -1869,17 +1869,16 @@
 _PORT);%0A
-%0A
     sMas
@@ -1941,16 +1941,17 @@
 rPort);%0A
+%0A
     sCli
@@ -1989,88 +1989,8 @@
 );%0A%0A
-    if (sExecutorService != null) %7B%0A      sExecutorService.shutdownNow();%0A    %7D%0A
     
@@ -2185,327 +2185,8 @@
 ));%0A
-    // If this isn't the first time setting the ClientContext, we should reset other contexts so%0A    // that they can see the latest changes%0A    if (sInitialized) %7B%0A      BlockStoreContext.INSTANCE.reset();%0A      FileSystemContext.INSTANCE.reset();%0A      LineageContext.INSTANCE.reset();%0A    %7D%0A    sInitialized = true;%0A
   %7D%0A
@@ -2274,29 +2274,16 @@
  static 
-synchronized 
 TachyonC
@@ -2302,39 +2302,8 @@
 ) %7B%0A
-    checkContextInitialized();%0A
     
@@ -2411,29 +2411,16 @@
  static 
-synchronized 
 ClientMe
@@ -2450,39 +2450,8 @@
 ) %7B%0A
-    checkContextInitialized();%0A
     
@@ -2538,29 +2538,16 @@
  static 
-synchronized 
 InetSock
@@ -2581,39 +2581,8 @@
 ) %7B%0A
-    checkContextInitialized();%0A
     
@@ -2675,21 +2675,8 @@
 tic 
-synchronized 
 Exec
@@ -2718,213 +2718,31 @@
     
-checkContextInitialized();%0A    return sExecutorService;%0A  %7D%0A%0A  private static void checkContextInitialized() %7B%0A    Preconditions.checkState(sInitialized, PreconditionMessage.CLIENT_CONTEXT_NOT_INITIALIZED)
+return sExecutorService
 ;%0A  

remove concept of external resetting from ClientContext