<commit_before>/*
 * 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.accumulo.core.client.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.impl.thrift.ClientService;
import org.apache.accumulo.core.conf.Property;
import org.apache.accumulo.core.security.thrift.ThriftSecurityException;
import org.apache.accumulo.core.util.ArgumentChecker;
import org.apache.accumulo.core.util.ServerServices;
import org.apache.accumulo.core.util.ThriftUtil;
import org.apache.accumulo.core.util.UtilWaitThread;
import org.apache.accumulo.core.util.ServerServices.Service;
import org.apache.accumulo.core.zookeeper.ZooCache;
import org.apache.accumulo.core.zookeeper.ZooUtil;
import org.apache.log4j.Logger;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class ServerClient {
  private static final Logger log = Logger.getLogger(ServerClient.class);
  private static final Map<String,ZooCache> zooCaches = new HashMap<String,ZooCache>();
  
  private synchronized static ZooCache getZooCache(Instance instance) {
    ZooCache result = zooCaches.get(instance.getZooKeepers());
    if (result == null) {
      result = new ZooCache(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut(), null);
      zooCaches.put(instance.getZooKeepers(), result);
    }
    return result;
  }
  
  public static <T> T execute(Instance instance, ClientExecReturn<T,ClientService.Iface> exec) throws AccumuloException, AccumuloSecurityException {
    try {
      return executeRaw(instance, exec);
    } catch (ThriftSecurityException e) {
      throw new AccumuloSecurityException(e.user, e.code, e);
    } catch (AccumuloException e) {
      throw e;
    } catch (Exception e) {
      throw new AccumuloException(e);
    }
  }
  
  public static void execute(Instance instance, ClientExec<ClientService.Iface> exec) throws AccumuloException, AccumuloSecurityException {
    try {
      executeRaw(instance, exec);
    } catch (ThriftSecurityException e) {
      throw new AccumuloSecurityException(e.user, e.code, e);
    } catch (AccumuloException e) {
      throw e;
    } catch (Exception e) {
      throw new AccumuloException(e);
    }
  }
  
  public static <T> T executeRaw(Instance instance, ClientExecReturn<T,ClientService.Iface> exec) throws Exception {
    while (true) {
      ClientService.Iface client = null;
      try {
        client = ServerClient.getConnection(instance);
        return exec.execute(client);
      } catch (TTransportException tte) {
        log.debug("ClientService request failed, retrying ... ", tte);
        UtilWaitThread.sleep(100);
      } finally {
        if (client != null)
          ServerClient.close(client);
      }
    }
  }
  
  public static void executeRaw(Instance instance, ClientExec<ClientService.Iface> exec) throws Exception {
    while (true) {
      ClientService.Iface client = null;
      try {
        client = ServerClient.getConnection(instance);
        exec.execute(client);
        break;
      } catch (TTransportException tte) {
        log.debug("ClientService request failed, retrying ... ", tte);
        UtilWaitThread.sleep(100);
      } finally {
        if (client != null)
          ServerClient.close(client);
      }
    }
  }
  
  public static ClientService.Iface getConnection(Instance instance) throws TTransportException {
    ArgumentChecker.notNull(instance);
    // create list of servers
    ArrayList<ThriftTransportKey> servers = new ArrayList<ThriftTransportKey>();
    
    // add tservers
    
    ZooCache zc = getZooCache(instance);
    
    for (String tserver : zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTSERVERS)) {
      String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS + "/" + tserver;
      byte[] data = ZooUtil.getLockData(zc, path);
      if (data != null && !new String(data).equals("master"))
        servers.add(new ThriftTransportKey(new ServerServices(new String(data)).getAddressString(Service.TSERV_CLIENT), instance.getConfiguration().getPort(
            Property.TSERV_CLIENTPORT), instance.getConfiguration().getTimeInMillis(Property.GENERAL_RPC_TIMEOUT)));
    }
    
    boolean opened = false;
    try {
      TTransport socket = ThriftTransportPool.getInstance().getAnyTransport(servers);
      ClientService.Iface client = ThriftUtil.createClient(new ClientService.Client.Factory(), socket);
      opened = true;
      return client;
    } finally {
      if (!opened)
        log.warn("Failed to find an available server in the list of servers: " + servers);
    }
  }
  
  public static void close(ClientService.Iface iface) {
    TServiceClient client = (TServiceClient) iface;
    if (client != null && client.getInputProtocol() != null && client.getInputProtocol().getTransport() != null) {
      ThriftTransportPool.getInstance().returnTransport(client.getInputProtocol().getTransport());
    } else {
      log.debug("Attempt to close null connection to a server", new Exception());
    }
  }
}
<commit_msg>ACCUMULO-340 improve the error message, still hangs to preserve current behavior<commit_after>/*
 * 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.accumulo.core.client.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.impl.thrift.ClientService;
import org.apache.accumulo.core.conf.Property;
import org.apache.accumulo.core.security.thrift.ThriftSecurityException;
import org.apache.accumulo.core.util.ArgumentChecker;
import org.apache.accumulo.core.util.ServerServices;
import org.apache.accumulo.core.util.ServerServices.Service;
import org.apache.accumulo.core.util.ThriftUtil;
import org.apache.accumulo.core.util.UtilWaitThread;
import org.apache.accumulo.core.zookeeper.ZooCache;
import org.apache.accumulo.core.zookeeper.ZooUtil;
import org.apache.log4j.Logger;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class ServerClient {
  private static final Logger log = Logger.getLogger(ServerClient.class);
  private static final Map<String,ZooCache> zooCaches = new HashMap<String,ZooCache>();
  
  private synchronized static ZooCache getZooCache(Instance instance) {
    ZooCache result = zooCaches.get(instance.getZooKeepers());
    if (result == null) {
      result = new ZooCache(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut(), null);
      zooCaches.put(instance.getZooKeepers(), result);
    }
    return result;
  }
  
  public static <T> T execute(Instance instance, ClientExecReturn<T,ClientService.Iface> exec) throws AccumuloException, AccumuloSecurityException {
    try {
      return executeRaw(instance, exec);
    } catch (ThriftSecurityException e) {
      throw new AccumuloSecurityException(e.user, e.code, e);
    } catch (AccumuloException e) {
      throw e;
    } catch (Exception e) {
      throw new AccumuloException(e);
    }
  }
  
  public static void execute(Instance instance, ClientExec<ClientService.Iface> exec) throws AccumuloException, AccumuloSecurityException {
    try {
      executeRaw(instance, exec);
    } catch (ThriftSecurityException e) {
      throw new AccumuloSecurityException(e.user, e.code, e);
    } catch (AccumuloException e) {
      throw e;
    } catch (Exception e) {
      throw new AccumuloException(e);
    }
  }
  
  public static <T> T executeRaw(Instance instance, ClientExecReturn<T,ClientService.Iface> exec) throws Exception {
    while (true) {
      ClientService.Iface client = null;
      try {
        client = ServerClient.getConnection(instance);
        return exec.execute(client);
      } catch (TTransportException tte) {
        log.debug("ClientService request failed, retrying ... ", tte);
        UtilWaitThread.sleep(100);
      } finally {
        if (client != null)
          ServerClient.close(client);
      }
    }
  }
  
  public static void executeRaw(Instance instance, ClientExec<ClientService.Iface> exec) throws Exception {
    while (true) {
      ClientService.Iface client = null;
      try {
        client = ServerClient.getConnection(instance);
        exec.execute(client);
        break;
      } catch (TTransportException tte) {
        log.debug("ClientService request failed, retrying ... ", tte);
        UtilWaitThread.sleep(100);
      } finally {
        if (client != null)
          ServerClient.close(client);
      }
    }
  }
  
  static volatile boolean warnedAboutTServersBeingDown = false;

  public static ClientService.Iface getConnection(Instance instance) throws TTransportException {
    ArgumentChecker.notNull(instance);
    // create list of servers
    ArrayList<ThriftTransportKey> servers = new ArrayList<ThriftTransportKey>();
    
    // add tservers
    
    ZooCache zc = getZooCache(instance);
    
    for (String tserver : zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTSERVERS)) {
      String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS + "/" + tserver;
      byte[] data = ZooUtil.getLockData(zc, path);
      if (data != null && !new String(data).equals("master"))
        servers.add(new ThriftTransportKey(new ServerServices(new String(data)).getAddressString(Service.TSERV_CLIENT), instance.getConfiguration().getPort(
            Property.TSERV_CLIENTPORT), instance.getConfiguration().getTimeInMillis(Property.GENERAL_RPC_TIMEOUT)));
    }
    
    boolean opened = false;
    try {
      TTransport socket = ThriftTransportPool.getInstance().getAnyTransport(servers);
      ClientService.Iface client = ThriftUtil.createClient(new ClientService.Client.Factory(), socket);
      opened = true;
      return client;
    } finally {
      if (!opened) {
        if (!warnedAboutTServersBeingDown) {
          if (servers.isEmpty()) {
            log.warn("There are no tablet servers: check that zookeeper and tablet servers are running.");
          } else {
            log.warn("Failed to find an available server in the list of servers: " + servers);
          }
          warnedAboutTServersBeingDown = true;
        }
      }
    }
  }
  
  public static void close(ClientService.Iface iface) {
    TServiceClient client = (TServiceClient) iface;
    if (client != null && client.getInputProtocol() != null && client.getInputProtocol().getTransport() != null) {
      ThriftTransportPool.getInstance().returnTransport(client.getInputProtocol().getTransport());
    } else {
      log.debug("Attempt to close null connection to a server", new Exception());
    }
  }
}

{'fraction_non_alphanumeric': 0.0999287354501544, 'fraction_numerical': 0.0019795708290442633, 'mean_word_length': 3.6899368733754176, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6963760', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3538, 'n_words': 901}