<commit_before>/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.client;

import alluxio.Configuration;
import alluxio.Constants;
import alluxio.util.ThreadFactoryUtils;
import alluxio.worker.ClientMetrics;

import com.google.common.base.Preconditions;

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

import javax.annotation.concurrent.ThreadSafe;

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

  static {
    init();
  }

  /**
   * Initializes the client context singleton, bringing all non-Alluxio configuration state in sync
   * with the current Alluxio configuration.
   *
   * This method is useful for updating parts of {@link ClientContext} which depend on
   * {@link Configuration} when {@link Configuration} is changed, e.g. the master hostname or port.
   * This method requires that configuration has been initialized.
   */
  public static void init() {
    sBlockClientExecutorService = Executors
        .newFixedThreadPool(Configuration.getInt(Constants.USER_BLOCK_WORKER_CLIENT_THREADS),
            ThreadFactoryUtils.build("block-worker-heartbeat-%d", true));
    sFileClientExecutorService = Executors
        .newFixedThreadPool(Configuration.getInt(Constants.USER_FILE_WORKER_CLIENT_THREADS),
            ThreadFactoryUtils.build("file-worker-heartbeat-%d", true));
    sClientMetrics = new ClientMetrics();

    String masterHostname =
        Preconditions.checkNotNull(Configuration.get(Constants.MASTER_HOSTNAME));
    int masterPort = Configuration.getInt(Constants.MASTER_RPC_PORT);
    sMasterAddress = new InetSocketAddress(masterHostname, masterPort);
  }
<commit_msg>Clean up ClientContext executors on re-initialization<commit_after>/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.client;

import alluxio.Configuration;
import alluxio.Constants;
import alluxio.util.ThreadFactoryUtils;
import alluxio.worker.ClientMetrics;

import com.google.common.base.Preconditions;

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

import javax.annotation.concurrent.ThreadSafe;

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

  static {
    init();
  }

  /**
   * Initializes the client context singleton, bringing all non-Alluxio configuration state in sync
   * with the current Alluxio configuration.
   *
   * This method is useful for updating parts of {@link ClientContext} which depend on
   * {@link Configuration} when {@link Configuration} is changed, e.g. the master hostname or port.
   * This method requires that configuration has been initialized.
   */
  public static void init() {
    // If this is a re-initialization, we must shut down the previous executors.
    if (sBlockClientExecutorService != null) {
      sBlockClientExecutorService.shutdownNow();
    }
    sBlockClientExecutorService = Executors
        .newFixedThreadPool(Configuration.getInt(Constants.USER_BLOCK_WORKER_CLIENT_THREADS),
            ThreadFactoryUtils.build("block-worker-heartbeat-%d", true));
    if (sFileClientExecutorService != null) {
      sFileClientExecutorService.shutdownNow();
    }
    sFileClientExecutorService = Executors
        .newFixedThreadPool(Configuration.getInt(Constants.USER_FILE_WORKER_CLIENT_THREADS),
            ThreadFactoryUtils.build("file-worker-heartbeat-%d", true));
    sClientMetrics = new ClientMetrics();

    String masterHostname =
        Preconditions.checkNotNull(Configuration.get(Constants.MASTER_HOSTNAME));
    int masterPort = Configuration.getInt(Constants.MASTER_RPC_PORT);
    sMasterAddress = new InetSocketAddress(masterHostname, masterPort);
  }

{'fraction_non_alphanumeric': 0.07360358677377171, 'fraction_numerical': 0.0014944890715486642, 'mean_word_length': 4.542443064182194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140339', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1367, 'n_words': 453}