text:
package org.apache.solr.client.solrj.impl;

import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.IsUpdateRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.cloud.Aliases;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.DocRouter;
import org.apache.solr.common.cloud.ImplicitDocRouter;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkCoreNodeProps;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.cloud.ZooKeeperException;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.Hash;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SolrjNamedThreadFactory;
import org.apache.solr.common.util.StrUtils;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * SolrJ client class to communicate with SolrCloud.
 * Instances of this class communicate with Zookeeper to discover
 * Solr endpoints for SolrCloud collections, and then use the
 * {@link LBHttpSolrClient} to issue requests.
 *
 * This class assumes the id field for your documents is called
 * 'id' - if this is not the case, you must set the right name
 * with {@link #setIdField(String)}.
 */
@SuppressWarnings("serial")
public class CloudSolrClient extends SolrClient {

  protected static final Logger log = LoggerFactory.getLogger(CloudSolrClient.class);

  private volatile ZkStateReader zkStateReader;
  private String zkHost; // the zk server connect string
  private int zkConnectTimeout = 10000;
  private int zkClientTimeout = 10000;
  private volatile String defaultCollection;
  private final LBHttpSolrClient lbClient;
  private final boolean shutdownLBHttpSolrServer;
  private HttpClient myClient;
  private final boolean clientIsInternal;
  //no of times collection state to be reloaded if stale state error is received
  private static final int MAX_STALE_RETRIES = 5;
  Random rand = new Random();

  private final boolean updatesToLeaders;
  private boolean parallelUpdates = true;
  private ExecutorService threadPool = ExecutorUtil
      .newMDCAwareCachedThreadPool(new SolrjNamedThreadFactory(
          "CloudSolrClient ThreadPool"));
  private String idField = "id";
  public static final String STATE_VERSION = "_stateVer_";
  private final Set<String> NON_ROUTABLE_PARAMS;
  {
    NON_ROUTABLE_PARAMS = new HashSet<>();
    NON_ROUTABLE_PARAMS.add(UpdateParams.EXPUNGE_DELETES);
    NON_ROUTABLE_PARAMS.add(UpdateParams.MAX_OPTIMIZE_SEGMENTS);
    NON_ROUTABLE_PARAMS.add(UpdateParams.COMMIT);
    NON_ROUTABLE_PARAMS.add(UpdateParams.WAIT_SEARCHER);
    NON_ROUTABLE_PARAMS.add(UpdateParams.OPEN_SEARCHER);

    NON_ROUTABLE_PARAMS.add(UpdateParams.SOFT_COMMIT);
    NON_ROUTABLE_PARAMS.add(UpdateParams.PREPARE_COMMIT);
    NON_ROUTABLE_PARAMS.add(UpdateParams.OPTIMIZE);

    // Not supported via SolrCloud
    // NON_ROUTABLE_PARAMS.add(UpdateParams.ROLLBACK);

  }
  private volatile long timeToLive = 60* 1000L;
  private volatile List<Object> locks = objectList(3);

  protected final Map<String, ExpiringCachedDocCollection> collectionStateCache = new ConcurrentHashMap<String, ExpiringCachedDocCollection>(){
    @Override
    public ExpiringCachedDocCollection get(Object key) {
      ExpiringCachedDocCollection val = super.get(key);
      if(val == null) return null;
      if(val.isExpired(timeToLive)) {
        super.remove(key);
        return null;
      }
      return val;
    }

  };

  class ExpiringCachedDocCollection {
    final DocCollection cached;
    long cachedAt;

    ExpiringCachedDocCollection(DocCollection cached) {
      this.cached = cached;
      this.cachedAt = System.currentTimeMillis();
    }

    boolean isExpired(long timeToLive) {
      return (System.currentTimeMillis() - cachedAt) > timeToLive;
    }
  }

  /**
   * Create a new client object that connects to Zookeeper and is always aware
   * of the SolrCloud state. If there is a fully redundant Zookeeper quorum and
   * SolrCloud has enough replicas for every shard in a collection, there is no
   * single point of failure. Updates will be sent to shard leaders by default.
   *
   * @param zkHost
   *          The client endpoint of the zookeeper quorum containing the cloud
   *          state. The full specification for this string is one or more comma
   *          separated HOST:PORT values, followed by an optional chroot value
   *          that starts with a forward slash. Using a chroot allows multiple
   *          applications to coexist in one ensemble. For full details, see the
   *          Zookeeper documentation. Some examples:
   *

   *          "host1:2181"
   *

   *          "host1:2181,host2:2181,host3:2181/mysolrchroot"
   *

   *          "zoo1.example.com:2181,zoo2.example.com:2181,zoo3.example.com:2181"
   */
  public CloudSolrClient(String zkHost) {
      this.zkHost = zkHost;
      this.clientIsInternal = true;
      this.myClient = HttpClientUtil.createClient(null);
      this.lbClient = new LBHttpSolrClient(myClient);
      this.lbClient.setRequestWriter(new BinaryRequestWriter());
      this.lbClient.setParser(new BinaryResponseParser());
      this.updatesToLeaders = true;
      shutdownLBHttpSolrServer = true;
      lbClient.addQueryParams(STATE_VERSION);
  }

  /**
   * Create a new client object that connects to Zookeeper and is always aware
   * of the SolrCloud state. If there is a fully redundant Zookeeper quorum and
   * SolrCloud has enough replicas for every shard in a collection, there is no
   * single point of failure. Updates will be sent to shard leaders by default.
   *
   * @param zkHost
   *          The client endpoint of the zookeeper quorum containing the cloud
   *          state. The full specification for this string is one or more comma
   *          separated HOST:PORT values, followed by an optional chroot value
   *          that starts with a forward slash. Using a chroot allows multiple
   *          applications to coexist in one ensemble. For full details, see the
   *          Zookeeper documentation. Some examples:
   *

   *          "host1:2181"
   *

   *          "host1:2181,host2:2181,host3:2181/mysolrchroot"
   *

   *          "zoo1.example.com:2181,zoo2.example.com:2181,zoo3.example.com:2181"
   * @param httpClient
   *          the {@link HttpClient} instance to be used for all requests. The
   *          provided httpClient should use a multi-threaded connection manager.
   */
  public CloudSolrClient(String zkHost, HttpClient httpClient)  {
    this.zkHost = zkHost;
    this.clientIsInternal = httpClient == null;
    this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient;
    this.lbClient = new LBHttpSolrClient(myClient);
    this.lbClient.setRequestWriter(new BinaryRequestWriter());
    this.lbClient.setParser(new BinaryResponseParser());
    this.updatesToLeaders = true;
    shutdownLBHttpSolrServer = true;
    lbClient.addQueryParams(STATE_VERSION);
  }

  /**
   * Create a new client object using multiple string values in a Collection
   * instead of a standard zkHost connection string. Note that this method will
   * not be used if there is only one String argument - that will use
   * {@link #CloudSolrClient(String)} instead.
   *
   * @param zkHosts
   *          A Java Collection (List, Set, etc) of HOST:PORT strings, one for
   *          each host in the zookeeper ensemble. Note that with certain
   *          Collection types like HashSet, the order of hosts in the final
   *          connect string may not be in the same order you added them.
   * @param chroot
   *          A chroot value for zookeeper, starting with a forward slash. If no
   *          chroot is required, use null.
   * @throws IllegalArgumentException
   *           if the chroot value does not start with a forward slash.
   * @see #CloudSolrClient(String)
   */
  public CloudSolrClient(Collection<String> zkHosts, String chroot) {
    this(zkHosts, chroot, null);
  }

  /**
   * Create a new client object using multiple string values in a Collection
   * instead of a standard zkHost connection string. Note that this method will
   * not be used if there is only one String argument - that will use
   * {@link #CloudSolrClient(String)} instead.
   *
   * @param zkHosts
   *          A Java Collection (List, Set, etc) of HOST:PORT strings, one for
   *          each host in the zookeeper ensemble. Note that with certain
   *          Collection types like HashSet, the order of hosts in the final
   *          connect string may not be in the same order you added them.
   * @param chroot
   *          A chroot value for zookeeper, starting with a forward slash. If no
   *          chroot is required, use null.
   * @param httpClient
   *          the {@link HttpClient} instance to be used for all requests. The provided httpClient should use a
   *          multi-threaded connection manager.
   * @throws IllegalArgumentException
   *           if the chroot value does not start with a forward slash.
   * @see #CloudSolrClient(String)
   */
  public CloudSolrClient(Collection<String> zkHosts, String chroot, HttpClient httpClient) {
    StringBuilder zkBuilder = new StringBuilder();
    int lastIndexValue = zkHosts.size() - 1;
    int i = 0;
    for (String zkHost : zkHosts) {
      zkBuilder.append(zkHost);
      if (i < lastIndexValue) {
        zkBuilder.append(",");
      }
      i++;
    }
    if (chroot != null) {
      if (chroot.startsWith("/")) {
        zkBuilder.append(chroot);
      } else {
        throw new IllegalArgumentException(
            "The chroot must start with a forward slash.");
      }
    }

    /* Log the constructed connection string and then initialize. */
    log.info("Final constructed zkHost string: " + zkBuilder.toString());

    this.zkHost = zkBuilder.toString();
    this.clientIsInternal = httpClient == null;
    this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient;
    this.lbClient = new LBHttpSolrClient(myClient);
    this.lbClient.setRequestWriter(new BinaryRequestWriter());
    this.lbClient.setParser(new BinaryResponseParser());
    this.updatesToLeaders = true;
    shutdownLBHttpSolrServer = true;
  }

  /**
   * @param zkHost
   *          A zookeeper client endpoint.
   * @param updatesToLeaders
   *          If true, sends updates only to shard leaders.
   * @see #CloudSolrClient(String) for full description and details on zkHost
   */
  public CloudSolrClient(String zkHost, boolean updatesToLeaders) {
    this(zkHost, updatesToLeaders, null);
  }

  /**
   * @param zkHost
   *          A zookeeper client endpoint.
   * @param updatesToLeaders
   *          If true, sends updates only to shard leaders.
   * @param httpClient
   *          the {@link HttpClient} instance to be used for all requests. The provided httpClient should use a
   *          multi-threaded connection manager.
   * @see #CloudSolrClient(String) for full description and details on zkHost
   */
  public CloudSolrClient(String zkHost, boolean updatesToLeaders, HttpClient httpClient) {
    this.zkHost = zkHost;
    this.clientIsInternal = httpClient == null;
    this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient;
    this.lbClient = new LBHttpSolrClient(myClient);
    this.lbClient.setRequestWriter(new BinaryRequestWriter());
    this.lbClient.setParser(new BinaryResponseParser());
    this.updatesToLeaders = updatesToLeaders;
    shutdownLBHttpSolrServer = true;
    lbClient.addQueryParams(STATE_VERSION);
  }

  /**Sets the cache ttl for DocCollection Objects cached  . This is only applicable for collections which are persisted outside of clusterstate.json
   * @param seconds ttl value in seconds
   */
  public void setCollectionCacheTTl(int seconds){
    assert seconds > 0;
    timeToLive = seconds*1000L;
  }

  /**
   * @param zkHost
   *          A zookeeper client endpoint.
   * @param lbClient
   *          LBHttpSolrServer instance for requests.
   * @see #CloudSolrClient(String) for full description and details on zkHost
   */
  public CloudSolrClient(String zkHost, LBHttpSolrClient lbClient) {
    this(zkHost, lbClient, true);
  }

  /**
   * @param zkHost
   *          A zookeeper client endpoint.
   * @param lbClient
   *          LBHttpSolrServer instance for requests.
   * @param updatesToLeaders
   *          If true, sends updates only to shard leaders.
   * @see #CloudSolrClient(String) for full description and details on zkHost
   */
  public CloudSolrClient(String zkHost, LBHttpSolrClient lbClient, boolean updatesToLeaders) {
    this.zkHost = zkHost;
    this.lbClient = lbClient;
    this.updatesToLeaders = updatesToLeaders;
    shutdownLBHttpSolrServer = false;
    this.clientIsInternal = false;
    lbClient.addQueryParams(STATE_VERSION);
  }

  public ResponseParser getParser() {
    return lbClient.getParser();
  }

  /**
   * Note: This setter method is not thread-safe.
   *
   * @param processor
   *          Default Response Parser chosen to parse the response if the parser
   *          were not specified as part of the request.
   * @see org.apache.solr.client.solrj.SolrRequest#getResponseParser()
   */
  public void setParser(ResponseParser processor) {
    lbClient.setParser(processor);
  }

  public RequestWriter getRequestWriter() {
    return lbClient.getRequestWriter();
  }

  public void setRequestWriter(RequestWriter requestWriter) {
    lbClient.setRequestWriter(requestWriter);
  }

  /**
   * @return the zkHost value used to connect to zookeeper.
   */
  public String getZkHost() {
    return zkHost;
  }

  public ZkStateReader getZkStateReader() {
    return zkStateReader;
  }

  /**
   * @param idField the field to route documents on.
   */
  public void setIdField(String idField) {
    this.idField = idField;
  }

  /**
   * @return the field that updates are routed on.
   */
  public String getIdField() {
    return idField;
  }

  /** Sets the default collection for request */
  public void setDefaultCollection(String collection) {
    this.defaultCollection = collection;
  }

  /** Gets the default collection for request */
  public String getDefaultCollection() {
    return defaultCollection;
  }

  /** Set the connect timeout to the zookeeper ensemble in ms */
  public void setZkConnectTimeout(int zkConnectTimeout) {
    this.zkConnectTimeout = zkConnectTimeout;
  }

  /** Set the timeout to the zookeeper ensemble in ms */
  public void setZkClientTimeout(int zkClientTimeout) {
    this.zkClientTimeout = zkClientTimeout;
  }

  /**
   * Connect to the zookeeper ensemble.
   * This is an optional method that may be used to force a connect before any other requests are sent.
   *
   */
  public void connect() {
    if (zkStateReader == null) {
      synchronized (this) {
        if (zkStateReader == null) {
          ZkStateReader zk = null;
          try {
            zk = new ZkStateReader(zkHost, zkClientTimeout, zkConnectTimeout);
            zk.createClusterStateWatchersAndUpdate();
            zkStateReader = zk;
          } catch (InterruptedException e) {
            zk.close();
            Thread.currentThread().interrupt();
            throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
          } catch (KeeperException e) {
            zk.close();
            throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
          } catch (Exception e) {
            if (zk != null) zk.close();
            // do not wrap because clients may be relying on the underlying exception being thrown
            throw e;
          }
        }
      }
    }
  }

  /**
   * Connect to a cluster.  If the cluster is not ready, retry connection up to a given timeout.
   * @param duration the timeout
   * @param timeUnit the units of the timeout
   * @throws TimeoutException if the cluster is not ready after the timeout
   * @throws InterruptedException if the wait is interrupted
   */
  public void connect(long duration, TimeUnit timeUnit) throws TimeoutException, InterruptedException {
    log.info("Waiting for {} {} for cluster at {} to be ready", duration, timeUnit, zkHost);
    long timeout = System.nanoTime() + timeUnit.toNanos(duration);
    while (System.nanoTime() < timeout) {
      try {
        connect();
        log.info("Cluster at {} ready", zkHost);
        return;
      }
      catch (RuntimeException e) {
        // not ready yet, then...
      }
      TimeUnit.MILLISECONDS.sleep(250);
    }
    throw new TimeoutException("Timed out waiting for cluster");
  }

  public void setParallelUpdates(boolean parallelUpdates) {
    this.parallelUpdates = parallelUpdates;
  }

  /**
   * Upload a set of config files to Zookeeper and give it a name
   *
   * NOTE: You should only allow trusted users to upload configs.  If you
   * are allowing client access to zookeeper, you should protect the
   * /configs node against unauthorised write access.
   *
   * @param configPath {@link java.nio.file.Path} to the config files
   * @param configName the name of the config
   * @throws IOException if an IO error occurs
   */
  public void uploadConfig(Path configPath, String configName) throws IOException {
    connect();
    zkStateReader.getConfigManager().uploadConfigDir(configPath, configName);
  }

  /**
   * Download a named config from Zookeeper to a location on the filesystem
   * @param configName    the name of the config
   * @param downloadPath  the path to write config files to
   * @throws IOException  if an I/O exception occurs
   */
  public void downloadConfig(String configName, Path downloadPath) throws IOException {
    connect();
    zkStateReader.getConfigManager().downloadConfigDir(configName, downloadPath);
  }

  private NamedList<Object> directUpdate(AbstractUpdateRequest request, String collection, ClusterState clusterState) throws SolrServerException {
    UpdateRequest updateRequest = (UpdateRequest) request;
    ModifiableSolrParams params = (ModifiableSolrParams) request.getParams();
    ModifiableSolrParams routableParams = new ModifiableSolrParams();
    ModifiableSolrParams nonRoutableParams = new ModifiableSolrParams();

    if(params != null) {
      nonRoutableParams.add(params);
      routableParams.add(params);
      for(String param : NON_ROUTABLE_PARAMS) {
        routableParams.remove(param);
      }
    }

    if (collection == null) {
      throw new SolrServerException("No collection param specified on request and no default collection has been set.");
    }

    //Check to see if the collection is an alias.
    Aliases aliases = zkStateReader.getAliases();
    if(aliases != null) {
      Map<String, String> collectionAliases = aliases.getCollectionAliasMap();
      if(collectionAliases != null && collectionAliases.containsKey(collection)) {
        collection = collectionAliases.get(collection);
      }
    }

    DocCollection col = getDocCollection(clusterState, collection,null);

    DocRouter router = col.getRouter();

    if (router instanceof ImplicitDocRouter) {
      // short circuit as optimization
      return null;
    }

    //Create the URL map, which is keyed on slice name.
    //The value is a list of URLs for each replica in the slice.
    //The first value in the list is the leader for the slice.
    Map<String,List<String>> urlMap = buildUrlMap(col);
    if (urlMap == null) {
      // we could not find a leader yet - use unoptimized general path
      return null;
    }

    NamedList<Throwable> exceptions = new NamedList<>();
    NamedList<NamedList> shardResponses = new NamedList<>();

    Map<String, LBHttpSolrClient.Req> routes = updateRequest.getRoutes(router, col, urlMap, routableParams, this.idField);
    if (routes == null) {
      return null;
    }

    long start = System.nanoTime();

    if (parallelUpdates) {
      final Map<String, Future<NamedList<?>>> responseFutures = new HashMap<>(routes.size());
      for (final Map.Entry<String, LBHttpSolrClient.Req> entry : routes.entrySet()) {
        final String url = entry.getKey();
        final LBHttpSolrClient.Req lbRequest = entry.getValue();
        try {
          MDC.put("CloudSolrClient.url", url);
          responseFutures.put(url, threadPool.submit(new Callable<NamedList<?>>() {
            @Override
            public NamedList<?> call() throws Exception {
              return lbClient.request(lbRequest).getResponse();
            }
          }));
        } finally {
          MDC.remove("CloudSolrClient.url");
        }
      }

      for (final Map.Entry<String, Future<NamedList<?>>> entry: responseFutures.entrySet()) {
        final String url = entry.getKey();
        final Future<NamedList<?>> responseFuture = entry.getValue();
        try {
          shardResponses.add(url, responseFuture.get());
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          throw new RuntimeException(e);
        } catch (ExecutionException e) {
          exceptions.add(url, e.getCause());
        }
      }

      if (exceptions.size() > 0) {
        Throwable firstException = exceptions.getVal(0);
        if(firstException instanceof SolrException) {
          SolrException e = (SolrException) firstException;
          throw new RouteException(ErrorCode.getErrorCode(e.code()), exceptions, routes);
        } else {
          throw new RouteException(ErrorCode.SERVER_ERROR, exceptions, routes);
        }
      }
    } else {
      for (Map.Entry<String, LBHttpSolrClient.Req> entry : routes.entrySet()) {
        String url = entry.getKey();
        LBHttpSolrClient.Req lbRequest = entry.getValue();
        try {
          NamedList<Object> rsp = lbClient.request(lbRequest).getResponse();
          shardResponses.add(url, rsp);
        } catch (Exception e) {
          if(e instanceof SolrException) {
            throw (SolrException) e;
          } else {
            throw new SolrServerException(e);
          }
        }
      }
    }

    UpdateRequest nonRoutableRequest = null;
    List<String> deleteQuery = updateRequest.getDeleteQuery();
    if (deleteQuery != null && deleteQuery.size() > 0) {
      UpdateRequest deleteQueryRequest = new UpdateRequest();
      deleteQueryRequest.setDeleteQuery(deleteQuery);
      nonRoutableRequest = deleteQueryRequest;
    }

    Set<String> paramNames = nonRoutableParams.getParameterNames();

    Set<String> intersection = new HashSet<>(paramNames);
    intersection.retainAll(NON_ROUTABLE_PARAMS);

    if (nonRoutableRequest != null || intersection.size() > 0) {
      if (nonRoutableRequest == null) {
        nonRoutableRequest = new UpdateRequest();
      }
      nonRoutableRequest.setParams(nonRoutableParams);
      List<String> urlList = new ArrayList<>();
      urlList.addAll(routes.keySet());
      Collections.shuffle(urlList, rand);
      LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(nonRoutableRequest, urlList);
      try {
        LBHttpSolrClient.Rsp rsp = lbClient.request(req);
        shardResponses.add(urlList.get(0), rsp.getResponse());
      } catch (Exception e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, urlList.get(0), e);
      }
    }

    long end = System.nanoTime();

    RouteResponse rr =  condenseResponse(shardResponses, (long)((end - start)/1000000));
    rr.setRouteResponses(shardResponses);
    rr.setRoutes(routes);
    return rr;
  }

  private Map<String,List<String>> buildUrlMap(DocCollection col) {
    Map<String, List<String>> urlMap = new HashMap<>();
    Collection<Slice> slices = col.getActiveSlices();
    Iterator<Slice> sliceIterator = slices.iterator();
    while (sliceIterator.hasNext()) {
      Slice slice = sliceIterator.next();
      String name = slice.getName();
      List<String> urls = new ArrayList<>();
      Replica leader = slice.getLeader();
      if (leader == null) {
        // take unoptimized general path - we cannot find a leader yet
        return null;
      }
      ZkCoreNodeProps zkProps = new ZkCoreNodeProps(leader);
      String url = zkProps.getCoreUrl();
      urls.add(url);
      Collection<Replica> replicas = slice.getReplicas();
      Iterator<Replica> replicaIterator = replicas.iterator();
      while (replicaIterator.hasNext()) {
        Replica replica = replicaIterator.next();
        if (!replica.getNodeName().equals(leader.getNodeName()) &&
            !replica.getName().equals(leader.getName())) {
          ZkCoreNodeProps zkProps1 = new ZkCoreNodeProps(replica);
          String url1 = zkProps1.getCoreUrl();
          urls.add(url1);
        }
      }
      urlMap.put(name, urls);
    }
    return urlMap;
  }

  public RouteResponse condenseResponse(NamedList response, long timeMillis) {
    RouteResponse condensed = new RouteResponse();
    int status = 0;
    Integer rf = null;
    Integer minRf = null;
    for(int i=0; i<response.size(); i++) {
      NamedList shardResponse = (NamedList)response.getVal(i);
      NamedList header = (NamedList)shardResponse.get("responseHeader");
      Integer shardStatus = (Integer)header.get("status");
      int s = shardStatus.intValue();
      if(s > 0) {
          status = s;
      }
      Object rfObj = header.get(UpdateRequest.REPFACT);
      if (rfObj != null && rfObj instanceof Integer) {
        Integer routeRf = (Integer)rfObj;
        if (rf == null || routeRf < rf)
          rf = routeRf;
      }
      minRf = (Integer)header.get(UpdateRequest.MIN_REPFACT);
    }

    NamedList cheader = new NamedList();
    cheader.add("status", status);
    cheader.add("QTime", timeMillis);
    if (rf != null)
      cheader.add(UpdateRequest.REPFACT, rf);
    if (minRf != null)
      cheader.add(UpdateRequest.MIN_REPFACT, minRf);

    condensed.add("responseHeader", cheader);
    return condensed;
  }

  public static class RouteResponse extends NamedList {
    private NamedList routeResponses;
    private Map<String, LBHttpSolrClient.Req> routes;

    public void setRouteResponses(NamedList routeResponses) {
      this.routeResponses = routeResponses;
    }

    public NamedList getRouteResponses() {
      return routeResponses;
    }

    public void setRoutes(Map<String, LBHttpSolrClient.Req> routes) {
      this.routes = routes;
    }

    public Map<String, LBHttpSolrClient.Req> getRoutes() {
      return routes;
    }

  }

  public static class RouteException extends SolrException {

    private NamedList<Throwable> throwables;
    private Map<String, LBHttpSolrClient.Req> routes;

    public RouteException(ErrorCode errorCode, NamedList<Throwable> throwables, Map<String, LBHttpSolrClient.Req> routes){
      super(errorCode, throwables.getVal(0).getMessage(), throwables.getVal(0));
      this.throwables = throwables;
      this.routes = routes;
    }

    public NamedList<Throwable> getThrowables() {
      return throwables;
    }

    public Map<String, LBHttpSolrClient.Req> getRoutes() {
      return this.routes;
    }
  }

  @Override
  public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException {
    SolrParams reqParams = request.getParams();

    if (collection == null)
      collection = (reqParams != null) ? reqParams.get("collection", getDefaultCollection()) : getDefaultCollection();
    return requestWithRetryOnStaleState(request, 0, collection);
  }

  /**
   * As this class doesn't watch external collections on the client side,
   * there's a chance that the request will fail due to cached stale state,
   * which means the state must be refreshed from ZK and retried.
   */
  protected NamedList<Object> requestWithRetryOnStaleState(SolrRequest request, int retryCount, String collection)
      throws SolrServerException, IOException {

    connect(); // important to call this before you start working with the ZkStateReader

    // build up a _stateVer_ param to pass to the server containing all of the
    // external collection state versions involved in this request, which allows
    // the server to notify us that our cached state for one or more of the external
    // collections is stale and needs to be refreshed ... this code has no impact on internal collections
    String stateVerParam = null;
    List<DocCollection> requestedCollections = null;
    if (collection != null && !request.getPath().startsWith("/admin")) { // don't do _stateVer_ checking for admin requests
      Set<String> requestedCollectionNames = getCollectionNames(getZkStateReader().getClusterState(), collection);

      StringBuilder stateVerParamBuilder = null;
      for (String requestedCollection : requestedCollectionNames) {
        // track the version of state we're using on the client side using the _stateVer_ param
        DocCollection coll = getDocCollection(getZkStateReader().getClusterState(), requestedCollection,null);
        int collVer = coll.getZNodeVersion();
        if (coll.getStateFormat()>1) {
          if(requestedCollections == null) requestedCollections = new ArrayList<>(requestedCollectionNames.size());
          requestedCollections.add(coll);

          if (stateVerParamBuilder == null) {
            stateVerParamBuilder = new StringBuilder();
          } else {
            stateVerParamBuilder.append("|"); // hopefully pipe is not an allowed char in a collection name
          }

          stateVerParamBuilder.append(coll.getName()).append(":").append(collVer);
        }
      }

      if (stateVerParamBuilder != null) {
        stateVerParam = stateVerParamBuilder.toString();
      }
    }

    if (request.getParams() instanceof ModifiableSolrParams) {
      ModifiableSolrParams params = (ModifiableSolrParams) request.getParams();
      if (stateVerParam != null) {
        params.set(STATE_VERSION, stateVerParam);
      } else {
        params.remove(STATE_VERSION);
      }
    } // else: ??? how to set this ???

    NamedList<Object> resp = null;
    try {
      resp = sendRequest(request, collection);
      //to avoid an O(n) operation we always add STATE_VERSION to the last and try to read it from there
      Object o = resp.get(STATE_VERSION, resp.size()-1);
      if(o != null && o instanceof Map) {
        //remove this because no one else needs this and tests would fail if they are comparing responses
        resp.remove(resp.size()-1);
        Map invalidStates = (Map) o;
        for (Object invalidEntries : invalidStates.entrySet()) {
          Map.Entry e = (Map.Entry) invalidEntries;
          getDocCollection(getZkStateReader().getClusterState(),(String)e.getKey(), (Integer)e.getValue());
        }

      }
    } catch (Exception exc) {

      Throwable rootCause = SolrException.getRootCause(exc);
      // don't do retry support for admin requests or if the request doesn't have a collection specified
      if (collection == null || request.getPath().startsWith("/admin")) {
        if (exc instanceof SolrServerException) {
          throw (SolrServerException)exc;
        } else if (exc instanceof IOException) {
          throw (IOException)exc;
        }else if (exc instanceof RuntimeException) {
          throw (RuntimeException) exc;
        }
        else {
          throw new SolrServerException(rootCause);
        }
      }

      int errorCode = (rootCause instanceof SolrException) ?
          ((SolrException)rootCause).code() : SolrException.ErrorCode.UNKNOWN.code;

      log.error("Request to collection {} failed due to ("+errorCode+
          ") {}, retry? "+retryCount, collection, rootCause.toString());

      boolean wasCommError =
          (rootCause instanceof ConnectException ||
              rootCause instanceof ConnectTimeoutException ||
              rootCause instanceof NoHttpResponseException ||
              rootCause instanceof SocketException);

      boolean stateWasStale = false;
      if (retryCount < MAX_STALE_RETRIES  &&
          requestedCollections != null    &&
          !requestedCollections.isEmpty() &&
          SolrException.ErrorCode.getErrorCode(errorCode) == SolrException.ErrorCode.INVALID_STATE)
      {
        // cached state for one or more external collections was stale
        // re-issue request using updated state
        stateWasStale = true;

        // just re-read state for all of them, which is a little heavy handed but hopefully a rare occurrence
        for (DocCollection ext : requestedCollections) {
          collectionStateCache.remove(ext.getName());
        }
      }

      // if we experienced a communication error, it's worth checking the state
      // with ZK just to make sure the node we're trying to hit is still part of the collection
      if (retryCount < MAX_STALE_RETRIES &&
          !stateWasStale &&
          requestedCollections != null &&
          !requestedCollections.isEmpty() &&
          wasCommError) {
        for (DocCollection ext : requestedCollections) {
          DocCollection latestStateFromZk = getDocCollection(zkStateReader.getClusterState(), ext.getName(),null);
          if (latestStateFromZk.getZNodeVersion() != ext.getZNodeVersion()) {
            // looks like we couldn't reach the server because the state was stale == retry
            stateWasStale = true;
            // we just pulled state from ZK, so update the cache so that the retry uses it
            collectionStateCache.put(ext.getName(), new ExpiringCachedDocCollection(latestStateFromZk));
          }
        }
      }

      if (requestedCollections != null) {
        requestedCollections.clear(); // done with this
      }

      // if the state was stale, then we retry the request once with new state pulled from Zk
      if (stateWasStale) {
        log.warn("Re-trying request to  collection(s) "+collection+" after stale state error from server.");
        resp = requestWithRetryOnStaleState(request, retryCount+1, collection);
      } else {
        if(exc instanceof SolrException) {
          throw exc;
        } if (exc instanceof SolrServerException) {
          throw (SolrServerException)exc;
        } else if (exc instanceof IOException) {
          throw (IOException)exc;
        } else {
          throw new SolrServerException(rootCause);
        }
      }
    }

    return resp;
  }

  protected NamedList<Object> sendRequest(SolrRequest request, String collection)
      throws SolrServerException, IOException {
    connect();

    ClusterState clusterState = zkStateReader.getClusterState();

    boolean sendToLeaders = false;
    List<String> replicas = null;

    if (request instanceof IsUpdateRequest) {
      if (request instanceof UpdateRequest) {
        NamedList<Object> response = directUpdate((AbstractUpdateRequest) request, collection, clusterState);
        if (response != null) {
          return response;
        }
      }
      sendToLeaders = true;
      replicas = new ArrayList<>();
    }

    SolrParams reqParams = request.getParams();
    if (reqParams == null) {
      reqParams = new ModifiableSolrParams();
    }
    List<String> theUrlList = new ArrayList<>();
    if (request.getPath().equals("/admin/collections")
        || request.getPath().equals("/admin/cores")) {
      Set<String> liveNodes = clusterState.getLiveNodes();
      for (String liveNode : liveNodes) {
        theUrlList.add(zkStateReader.getBaseUrlForNodeName(liveNode));
      }
    } else {

      if (collection == null) {
        throw new SolrServerException(
            "No collection param specified on request and no default collection has been set.");
      }

      Set<String> collectionNames = getCollectionNames(clusterState, collection);
      if (collectionNames.size() == 0) {
        throw new SolrException(ErrorCode.BAD_REQUEST,
            "Could not find collection: " + collection);
      }

      String shardKeys =  reqParams.get(ShardParams._ROUTE_);

      // TODO: not a big deal because of the caching, but we could avoid looking
      // at every shard
      // when getting leaders if we tweaked some things

      // Retrieve slices from the cloud state and, for each collection
      // specified,
      // add it to the Map of slices.
      Map<String,Slice> slices = new HashMap<>();
      for (String collectionName : collectionNames) {
        DocCollection col = getDocCollection(clusterState, collectionName, null);
        Collection<Slice> routeSlices = col.getRouter().getSearchSlices(shardKeys, reqParams , col);
        ClientUtils.addSlices(slices, collectionName, routeSlices, true);
      }
      Set<String> liveNodes = clusterState.getLiveNodes();

      List<String> leaderUrlList = null;
      List<String> urlList = null;
      List<String> replicasList = null;

      // build a map of unique nodes
      // TODO: allow filtering by group, role, etc
      Map<String,ZkNodeProps> nodes = new HashMap<>();
      List<String> urlList2 = new ArrayList<>();
      for (Slice slice : slices.values()) {
        for (ZkNodeProps nodeProps : slice.getReplicasMap().values()) {
          ZkCoreNodeProps coreNodeProps = new ZkCoreNodeProps(nodeProps);
          String node = coreNodeProps.getNodeName();
          if (!liveNodes.contains(coreNodeProps.getNodeName())
              || Replica.State.getState(coreNodeProps.getState()) != Replica.State.ACTIVE) continue;
          if (nodes.put(node, nodeProps) == null) {
            if (!sendToLeaders || coreNodeProps.isLeader()) {
              String url;
              if (reqParams.get(UpdateParams.COLLECTION) == null) {
                url = ZkCoreNodeProps.getCoreUrl(nodeProps.getStr(ZkStateReader.BASE_URL_PROP), collection);
              } else {
                url = coreNodeProps.getCoreUrl();
              }
              urlList2.add(url);
            } else {
              String url;
              if (reqParams.get(UpdateParams.COLLECTION) == null) {
                url = ZkCoreNodeProps.getCoreUrl(nodeProps.getStr(ZkStateReader.BASE_URL_PROP), collection);
              } else {
                url = coreNodeProps.getCoreUrl();
              }
              replicas.add(url);
            }
          }
        }
      }

      if (sendToLeaders) {
        leaderUrlList = urlList2;
        replicasList = replicas;
      } else {
        urlList = urlList2;
      }

      if (sendToLeaders) {
        theUrlList = new ArrayList<>(leaderUrlList.size());
        theUrlList.addAll(leaderUrlList);
      } else {
        theUrlList = new ArrayList<>(urlList.size());
        theUrlList.addAll(urlList);
      }
      if(theUrlList.isEmpty()) {
        for (String s : collectionNames) {
          if(s!=null) collectionStateCache.remove(s);
        }
        throw new SolrException(SolrException.ErrorCode.INVALID_STATE, "Not enough nodes to handle the request");
      }

      Collections.shuffle(theUrlList, rand);
      if (sendToLeaders) {
        ArrayList<String> theReplicas = new ArrayList<>(
            replicasList.size());
        theReplicas.addAll(replicasList);
        Collections.shuffle(theReplicas, rand);
        theUrlList.addAll(theReplicas);
      }

    }

    LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(request, theUrlList);
    LBHttpSolrClient.Rsp rsp = lbClient.request(req);
    return rsp.getResponse();
  }

  private Set<String> getCollectionNames(ClusterState clusterState,
                                         String collection) {
    // Extract each comma separated collection name and store in a List.
    List<String> rawCollectionsList = StrUtils.splitSmart(collection, ",", true);
    Set<String> collectionNames = new HashSet<>();
    // validate collections
    for (String collectionName : rawCollectionsList) {
      if (!clusterState.getCollections().contains(collectionName)) {
        Aliases aliases = zkStateReader.getAliases();
        String alias = aliases.getCollectionAlias(collectionName);
        if (alias != null) {
          List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
          collectionNames.addAll(aliasList);
          continue;
        }

          throw new SolrException(ErrorCode.BAD_REQUEST, "Collection not found: " + collectionName);
        }

      collectionNames.add(collectionName);
    }
    return collectionNames;
  }

  @Override
  public void close() throws IOException {
    shutdown();
  }

  @Override
  @Deprecated
  public void shutdown() {
    if (zkStateReader != null) {
      synchronized(this) {
        if (zkStateReader!= null)
          zkStateReader.close();
        zkStateReader = null;
      }
    }

    if (shutdownLBHttpSolrServer) {
      lbClient.shutdown();
    }

    if (clientIsInternal && myClient!=null) {
      HttpClientUtil.close(myClient);
    }

    if(this.threadPool != null && !this.threadPool.isShutdown()) {
      this.threadPool.shutdown();
    }
  }

  public LBHttpSolrClient getLbClient() {
    return lbClient;
  }

  public boolean isUpdatesToLeaders() {
    return updatesToLeaders;
  }

  /**If caches are expired they are refreshed after acquiring a lock.
   * use this to set the number of locks
   */
  public void setParallelCacheRefreshes(int n){ locks = objectList(n); }

  private static ArrayList<Object> objectList(int n) {
    ArrayList<Object> l =  new ArrayList<>(n);
    for(int i=0;i<n;i++) l.add(new Object());
    return l;
  }

  protected DocCollection getDocCollection(ClusterState clusterState, String collection, Integer expectedVersion) throws SolrException {
    if (collection == null) return null;
    DocCollection col = getFromCache(collection);
    if (col != null) {
      if (expectedVersion == null) return col;
      if (expectedVersion.intValue() == col.getZNodeVersion()) return col;
    }

    ClusterState.CollectionRef ref = clusterState.getCollectionRef(collection);
    if (ref == null) {
      //no such collection exists
      return null;
    }
    if (!ref.isLazilyLoaded()) {
      //it is readily available just return it
      return ref.get();
    }
    List locks = this.locks;
    final Object lock = locks.get(Math.abs(Hash.murmurhash3_x86_32(collection, 0, collection.length(), 0) % locks.size()));
    synchronized (lock) {
      //we have waited for sometime just check once again
      col = getFromCache(collection);
      if (col != null) {
        if (expectedVersion == null) return col;
        if (expectedVersion.intValue() == col.getZNodeVersion()) {
          return col;
        } else {
          collectionStateCache.remove(collection);
        }
      }
      col = ref.get();//this is a call to ZK
    }
    if (col == null) return null;
    if (col.getStateFormat() > 1) collectionStateCache.put(collection, new ExpiringCachedDocCollection(col));
    return col;
  }

  private DocCollection getFromCache(String c){
    ExpiringCachedDocCollection cachedState = collectionStateCache.get(c);
    return cachedState != null ? cachedState.cached : null;
  }

  /**
   * Useful for determining the minimum achieved replication factor across
   * all shards involved in processing an update request, typically useful
   * for gauging the replication factor of a batch.
   */
  @SuppressWarnings("rawtypes")
  public int getMinAchievedReplicationFactor(String collection, NamedList resp) {
    // it's probably already on the top-level header set by condense
    NamedList header = (NamedList)resp.get("responseHeader");
    Integer achRf = (Integer)header.get(UpdateRequest.REPFACT);
    if (achRf != null)
      return achRf.intValue();

    // not on the top-level header, walk the shard route tree
    Map<String,Integer> shardRf = getShardReplicationFactor(collection, resp);
    for (Integer rf : shardRf.values()) {
      if (achRf == null || rf < achRf) {
        achRf = rf;
      }
    }
    return (achRf != null) ? achRf.intValue() : -1;
  }

  /**
   * Walks the NamedList response after performing an update request looking for
   * the replication factor that was achieved in each shard involved in the request.
   * For single doc updates, there will be only one shard in the return value.
   */
  @SuppressWarnings("rawtypes")
  public Map<String,Integer> getShardReplicationFactor(String collection, NamedList resp) {
    connect();

    Map<String,Integer> results = new HashMap<String,Integer>();
    if (resp instanceof CloudSolrClient.RouteResponse) {
      NamedList routes = ((CloudSolrClient.RouteResponse)resp).getRouteResponses();
      ClusterState clusterState = zkStateReader.getClusterState();
      Map<String,String> leaders = new HashMap<String,String>();
      for (Slice slice : clusterState.getActiveSlices(collection)) {
        Replica leader = slice.getLeader();
        if (leader != null) {
          ZkCoreNodeProps zkProps = new ZkCoreNodeProps(leader);
          String leaderUrl = zkProps.getBaseUrl() + "/" + zkProps.getCoreName();
          leaders.put(leaderUrl, slice.getName());
          String altLeaderUrl = zkProps.getBaseUrl() + "/" + collection;
          leaders.put(altLeaderUrl, slice.getName());
        }
      }

      Iterator<Map.Entry<String,Object>> routeIter = routes.iterator();
      while (routeIter.hasNext()) {
        Map.Entry<String,Object> next = routeIter.next();
        String host = next.getKey();
        NamedList hostResp = (NamedList)next.getValue();
        Integer rf = (Integer)((NamedList)hostResp.get("responseHeader")).get(UpdateRequest.REPFACT);
        if (rf != null) {
          String shard = leaders.get(host);
          if (shard == null) {
            if (host.endsWith("/"))
              shard = leaders.get(host.substring(0,host.length()-1));
            if (shard == null) {
              shard = host;
            }
          }
          results.put(shard, rf);
        }
      }
    }
    return results;
  }
}


text:
require 'ubiquo'

module UbiquoDesign
  class Engine < Rails::Engine
    include Ubiquo::Engine::Base

    paths['app/models'] << "app/models/widgets/"

    initializer :load_extensions do
      require 'ubiquo_design/extensions'
      require 'ubiquo_design/ubiquo_widgets'
      require 'ubiquo_design/render_page'
      require 'ubiquo_design/version'
      require 'ubiquo_design/cache_managers/base'
      require 'ubiquo_design/cache_expiration'
      require 'ubiquo_design/cache_rendering'
      require 'ubiquo_design/structure'
    end

    initializer :register_ubiquo_plugin do
      require 'ubiquo_design/init_settings.rb'
      _loader
    end

    initializer :ubiquo_assets_precompile do |app|
      app.config.assets.precompile += %w(ubiquo/ubiquo_design.*)
    end

    protected

    def _loader
      UbiquoDesign::Connectors.load!

      ActionController::Base.send(:include, UbiquoDesign::UbiquoWidgets)
      if ActionController::Base.perform_caching
        ActionController::Base.send(:include, UbiquoDesign::CacheRendering)
        ActiveRecord::Base.send(:include, UbiquoDesign::CacheExpiration::ActiveRecord)
      end
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
using System;

namespace Strings_and_Objects
{
    class Program
    {
        static void Main(string[] args)
        {
            // Problem:
            // Declare two string variables and assign them with "Hello" and "World".
            // Declare an object variable and assign it with the concatenation of the first two variables (mind adding an interval between).
            // Declare a third string variable and initialize it with the value of the object variable (you should perform type casting).

            string firstWord = Console.ReadLine();
            string secondWord = Console.ReadLine();

            string firstPluseSecondWord = firstWord + " " + secondWord;

            Console.WriteLine(firstPluseSecondWord);
        }
    }
}


text:
package com.palantir.timelock.corruption.detection;

import com.palantir.common.concurrent.NamedThreadFactory;
import com.palantir.common.concurrent.PTExecutors;
import com.palantir.timelock.corruption.TimeLockCorruptionNotifier;
import com.palantir.timelock.corruption.handle.LocalCorruptionHandler;
import com.palantir.timelock.history.PaxosLogHistoryProvider;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public final class LocalCorruptionDetector implements CorruptionDetector {
    private static final Duration TIMELOCK_CORRUPTION_ANALYSIS_INTERVAL = Duration.ofMinutes(5);
    private static final String CORRUPTION_DETECTOR_THREAD_PREFIX = "timelock-corruption-detector";

    private final ScheduledExecutorService executor = PTExecutors.newSingleThreadScheduledExecutor(
            new NamedThreadFactory(CORRUPTION_DETECTOR_THREAD_PREFIX, true));
    private final LocalCorruptionHandler corruptionHandler;
    private final PaxosLogHistoryProvider historyProvider;
    private final LocalTimestampInvariantsVerifier timestampInvariantsVerifier;

    private volatile CorruptionStatus localCorruptionState = CorruptionStatus.HEALTHY;
    private volatile CorruptionHealthReport localCorruptionReport = CorruptionHealthReport.defaultHealthyReport();

    public static LocalCorruptionDetector create(
            PaxosLogHistoryProvider historyProvider,
            List<TimeLockCorruptionNotifier> corruptionNotifiers,
            LocalTimestampInvariantsVerifier timestampInvariants) {
        LocalCorruptionDetector localCorruptionDetector =
                new LocalCorruptionDetector(historyProvider, corruptionNotifiers, timestampInvariants);

        // TODO(mdaudali): Decide whether to re-enable once we've determined whether corruption detection will not kill
        // the node timelock is running on

        // localCorruptionDetector.scheduleWithFixedDelay();
        return localCorruptionDetector;
    }

    private LocalCorruptionDetector(
            PaxosLogHistoryProvider historyProvider,
            List<TimeLockCorruptionNotifier> corruptionNotifiers,
            LocalTimestampInvariantsVerifier timestampInvariantsVerifier) {

        this.historyProvider = historyProvider;
        this.timestampInvariantsVerifier = timestampInvariantsVerifier;
        this.corruptionHandler = new LocalCorruptionHandler(corruptionNotifiers);
    }

    @SuppressWarnings("UnusedMethod") // Remove when re-enabling local corruption
    private void scheduleWithFixedDelay() {
        executor.scheduleWithFixedDelay(
                () -> {
                    CorruptionHealthReport paxosRoundCorruptionReport = analyzeHistoryAndBuildCorruptionHealthReport();
                    CorruptionHealthReport timestampInvariantsReport =
                            timestampInvariantsVerifier.timestampInvariantsHealthReport();
                    localCorruptionReport = ImmutableCorruptionHealthReport.builder()
                            .from(paxosRoundCorruptionReport)
                            .putAllViolatingStatusesToNamespaceAndUseCase(
                                    timestampInvariantsReport.violatingStatusesToNamespaceAndUseCase())
                            .build();
                    processLocalHealthReport();
                },
                TIMELOCK_CORRUPTION_ANALYSIS_INTERVAL.getSeconds(),
                TIMELOCK_CORRUPTION_ANALYSIS_INTERVAL.getSeconds(),
                TimeUnit.SECONDS);
    }

    private CorruptionHealthReport analyzeHistoryAndBuildCorruptionHealthReport() {
        return HistoryAnalyzer.corruptionHealthReportForHistory(historyProvider.getHistory());
    }

    private void processLocalHealthReport() {
        localCorruptionState = getLocalCorruptionState(localCorruptionReport);
        if (localCorruptionState.shouldRejectRequests()) {
            corruptionHandler.notifyRemoteServersOfCorruption();
        }
    }

    CorruptionStatus getLocalCorruptionState(CorruptionHealthReport latestReport) {
        return latestReport.shouldRejectRequests()
                ? CorruptionStatus.DEFINITIVE_CORRUPTION_DETECTED_BY_LOCAL
                : localCorruptionState;
    }

    public CorruptionHealthReport corruptionHealthReport() {
        return localCorruptionReport;
    }

    @Override
    public boolean shouldRejectRequests() {
        return localCorruptionState.shouldRejectRequests();
    }
}


text:
namespace TimeZoneDb.ValueObjects.Specifications
{
    public struct DayOfYearSpecification
    {
        #region Ctors

        public DayOfYearSpecification(MonthOfYear monthOfYear, AbstractDayOfMonthSpecification dayOfMonthSpecification)
            : this()
        {
            MonthOfYear = monthOfYear;
            DayOfMonthSpecification = dayOfMonthSpecification;
        }

        #endregion

        #region Properties

        public MonthOfYear MonthOfYear { get; private set; }
        public AbstractDayOfMonthSpecification DayOfMonthSpecification { get; private set; }

        #endregion

        #region Conversion Methods

        /// <summary>
        ///     returns the explicit day of year this <see cref="DayOfYearSpecification" />
        ///     specifies for a given year
        /// </summary>
        /// <param name="year">the year for which the day of year should be calculated</param>
        /// <returns></returns>
        public int ToDayOfYear(int year)
        {
            return DayOfMonthSpecification.ToDayOfYear(year, MonthOfYear);
        }

        #endregion
    }
}


text:
begin
  require 'puppet/parser/functions/lib/prepare_cidr.rb'
rescue LoadError => e
  # puppet apply does not add module lib directories to the $LOAD_PATH (See
  # #4248). It should (in the future) but for the time being we need to be
  # defensive which is what this rescue block is doing.
  rb_file = File.join(File.dirname(__FILE__),'lib','prepare_cidr.rb')
  load rb_file if File.exists?(rb_file) or raise e
end

module Puppet::Parser::Functions
  newfunction(:cidr_to_ipaddr, :type => :rvalue, :doc => <<-EOS
This function get cidr-notated IP addresses and return ip address.
EOS
  ) do |arguments|
    if arguments.size != 1
      raise(Puppet::ParseError, "cidr_to_ipaddr(): Wrong number of arguments " +
        "given (#{arguments.size} for 1)")
    end

    return prepare_cidr(arguments[0])[0]
  end
end


text:
/**
 *
 */
package edu.berkeley.nlp.classify;

import java.util.Arrays;
import java.util.List;

import edu.berkeley.nlp.util.Pair;

/**
 * @author adpauls
 *
 */
public class WordInSentence extends Pair<List<String>,Integer> {

	/**
	 * @param first
	 * @param second
	 */
	public WordInSentence(List<String> sentenceWords, Integer wordPos) {
		super(sentenceWords, wordPos);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param first
	 * @param second
	 */
	public WordInSentence(String sentence, Integer wordPos) {
		super(Arrays.asList(sentence.split(" ")), wordPos);
		// TODO Auto-generated constructor stub
	}

}


text:
require 'spec_helper'

describe "HostGroups" do

  before do
    integration_test
  end

  after do
    truncate_all_tables
  end

  describe "when not existing" do

    it "returns nil on find" do
      Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should be_nil
    end

    it "can be created" do
      hg = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
      hg.save.should be_true
    end

  end

  describe "when existing" do

    before do
      @hg = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
    end

    it "can be found" do
      Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should_not be_nil
    end

    it "can have its name changed" do
      @hg.name = 'rubix_spec_host_group_2'
      @hg.save
      Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should     be_nil
      Rubix::HostGroup.find(:name => 'rubix_spec_host_group_2').should_not be_nil
    end

    it "can be destroyed" do
      @hg.destroy
      Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should be_nil
    end
  end

end


text:
using System;

namespace De.Osthus.Ambeth.Collections
{
    public interface IListElem<V>
    {
        Object ListHandle { get; set; }

        IListElem<V> Prev { get; set; }

        IListElem<V> Next { get; set; }

        V ElemValue { get; set; }
    }
}


text:
class BubbleSort {

  def bubbleSort[T](arrayToBeSorted: Array[T])(implicit o: Ordering[T]): Array[T] = {

    import o._
    val consecutiveIndices = (arrayToBeSorted.indices, arrayToBeSorted.indices drop 1).zipped
    var hasChanged = true

    while (hasChanged) {

      hasChanged = false
      consecutiveIndices foreach { (i1, i2) =>

        if (arrayToBeSorted(i1) > arrayToBeSorted(i2)) {
          hasChanged = true
          val temporaryVariable = arrayToBeSorted(i1)
          arrayToBeSorted(i1) = arrayToBeSorted(i2)
          arrayToBeSorted(i2) = temporaryVariable
        }
      }
    }

    arrayToBeSorted
  }
}


text:
These are the commands in the `wheels generate` namespace. These commands are called by some of
the top level commands but you can use them directly to speed up your development process.

## `wheels generate`

The `wheels generate` command is what CommandBox calls a namespace and contains many sub commands beneath it. It's main purpose is to isolate those sub commands so there is no name collisions. It also has an alias of `wheels g` which allows you to shorten the commands you have to type. However, we have opted to show all the commands with their full names in the list below.

## `wheels generate app-wizard`

Creates a new CFWheels application using our wizard to gather all the necessary information. This is the recommended route to start a new application.

This command will ask for:

* An Application Name (a new directory will be created with this name)
* Template to use
* A reload Password
* A datasource name
* What local cfengine you want to run
* If using Lucee, do you want to setup a local h2 dev database
* Do you want to initialize the app as a ForgeBox module

```
wheels new
wheels g app-wizard
wheels generate app-wizard
```

All these three commands are equivalent and will call the same wizard. The wizard in turn gathers all the required data and passes it all off to the `wheels generate app` command to do all the heavy lifting.

Let's take a look at the wizard pages after issuing the `wheels new` command:

![wheels new - step 1](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.30.34 PM.png>)

You can accept the name offered or change it to whatever name you like. We try to clean up the name and take out special characters and spaces if we need to.

![wheels new - step 2](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.31.01 PM.png>)

You can select a template to use. If you are reading this before CFWheels 2.4 is released you may want to select the Bleeding Edge Base Template.

![wheels new - step 3](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.32.14 PM.png>)

You can set what you want to use as your reload password or accept the default. Please make sure to change this before you go into production. Ideally this should be kept out of your source repository by using something like the (CFWheels DotEnvSettings Plugin)\[[https://www.forgebox.io/view/cfwheels-dotenvsettings](https://www.forgebox.io/view/cfwheels-dotenvsettings)].

![wheels new - step 4](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.32.43 PM.png>)

The datasource is something you'll have to take care of unless you opt to use the H2 Embedded database in a Lucee server. Here you can define the datasource name if you would like to use something different than the application name.

![wheels new - step 5](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.33.18 PM.png>)

In this step you can choose what CF Engine and version to launch. Lucee has an embedded SQL compliant database server called H2. So if you use one of the Lucee options you can specify to use the H2 database as well. Notice the last item allows you to specify the module slug or URI to a CF Engine not on the list.

![wheels new - step 6](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.34.17 PM.png>)

On this step you are asked if you'd like to use the H2 Database, in which case we can set everything up for you, or if you would prefer to use another database engine and will take care of setting up the database yourself.

![wheels new - step 7](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.34.44 PM.png>)

On this last step, you are asked if you want us to include a box.json file so you can eventually submit this to ForgeBox.io for sharing with he world.

![wheels new - step 8](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.35.09 PM.png>)

This is the confirmation screen that shows all the choices you've made and gives you one last chance to bail out. If you choose to continue, the choices you've made will be sent over to the `wheels g app` command to do the actual app creation.

![wheels new - final screen](<../.gitbook/assets/Screen Shot 2022-06-18 at 12.35.40 PM.png>)

If you opted to continue you'll see a bunch of things scroll across your screen as the various items are downloaded and configured. Eventually you will see this status screen letting you know that everything was installed properly.

## `wheels generate app`

Create a blank CFWheels app from one of our app templates or a template using a valid Endpoint ID which can come from ForgeBox, HTTP/S, git, github, etc.

By default an app named MyCFWheelsApp will be created in a sub directory called MyCFWheelsApp.

The most basic call...
```
wheels generate app
```

This can be shortened to...
```
wheels g app
```

Here are the basic templates that are available for you that come from ForgeBox
- CFWheels Base Template - Stable (default)
- CFWheels Base Template - Bleeding Edge
- CFWheels Template - HelloWorld
- CFWheels Template - HelloDynamic
- CFWheels Template - HelloPages
- CFWheels Example App
- CFWheels - TodoMVC - HTMX - Demo App

```
wheels create app template=base
```
The template parameter can also be any valid Endpoint ID, which includes a Git repo or HTTP URL pointing to a package. So you can use this to publish your own templates to use to start new projects.

```
wheels create app template=http://site.com/myCustomAppTemplate.zip
```

| Parameter      | Required | Default        | Description                                           |
| -------------- | -------- | -------------- | ----------------------------------------------------- |
| name           | false    | MyCFWheelsApp  | The name of the app you want to create                |
| template       | false    | base template  | The name of the app template to use                   |
| directory      | false    | mycfwheelsapp/ | The directory to create the app in                    |
| reloadPassword | false    | ChangeMe       | The reload password to set for the app                |
| datasourceName | false    | MyCFWheelsApp  | The datasource name to set for the app                |
| cfmlEngine     | false    | Lucee          | The CFML engine to use for the app                    |
| setupH2        | false    | false          | Setup the H2 database for development                 |
| init           | false    | false          | "init" the directory as a package if it isn't already |
| force          | false    | false          | Force installation into an none empty directory       |

## `wheels generate route`

Adds a default resources Route to the routes table. All the normal CRUD routes are automatically added.

```
wheels generate route [objectname]
```

| Parameter  | Required | Default | Description                                         |
| ---------- | -------- | ------- | --------------------------------------------------- |
| objectname | true     |         | The name of the resource to add to the routes table |

## `wheels generate controller`

I generate a controller in the `controllers/` directory. You can either pass in a list of actions to stub out or the standard CRUD methods will be generated.

Create a user controller with full CRUD methods
```
wheels generate controller user
```

Create a user object with just "index" and "customaction" methods
```
wheels generate controller user index,customaction
```

| Parameter  | Required | Default | Description                                                        |
| ---------- | -------- | ------- | ------------------------------------------------------------------ |
| name       | true     |         | Name of the controller to create without the .cfc                  |
| actionList | false    |         | optional list of actions, comma delimited                          |
| directory  | false    |         | if for some reason you don't have your controllers in controllers/ |

## `wheels generate model`

This command generates a model in the `models/` folder and creates the associated DB Table using migrations.

Create "users" table and "User.cfc" in models:
```
wheels generate model user
```

Create just "User.cfc" in models:
```
wheels generate model user false
```

| Parameter  | Required | Default | Description                                                                    |
| ---------- | -------- | ------- | ------------------------------------------------------------------------------ |
| name       | true     |         | Name of the model to create without the .cfc: assumes singular                 |
| db         | false    |         | Boolean attribute specifying if the database table should be generated as well |

## `wheels generate property`

This command generates a dbmigration file to add a property to an object and scaffold into _form.cfm and show.cfm if they exist (i.e, wheels generate property table columnName columnType).

Create the a string/textField property called firstname on the User model:
```
wheels generate property user firstname
```

Create a boolean/Checkbox property called isActive on the User model with a default of 0:
```
wheels generate property user isActive boolean
```

Create a boolean/Checkbox property called hasActivated on the User model with a default of 1 (i.e, true):
```
wheels generate property user isActive boolean 1
```

 Create a datetime/datetimepicker property called lastloggedin on the User model:
 ```
 wheels generate property user lastloggedin datetime
 ```

 All columnType options:
 biginteger,binary,boolean,date,datetime,decimal,float,integer,string,limit,text,time,timestamp,uuid

| Parameter  | Required | Default | Description                                                           |
| ---------- | -------- | ------- |---------------------------------------------------------------------- |
| name       | true     |         | Table Name                                                            |
| columnName | false    |         | Name of Column                                                        |
| columnType | false    |         | Type of Column on of biginteger, binary, boolean, date, datetime, decimal, float,integer, string, limit, text, time, timestamp, uuid    |
| default    | false    |         | Default Value for column                                              |
| null       | false    |         | Whether to allow null values |
| limit      | false    |         | character or integer size limit for column |
| precision  | false    |         | precision value for decimal columns, i.e. number of digits the column can hold |
| scale      | false    |         | scale value for decimal columns, i.e. number of digits that can be placed to the right of the decimal point (must be less than or equal to precision) |

## `wheels generate view`

This command generates a view file in the `views/` directory when specifying the object name and the action. If a directory for the object does not exist a subdirectory will be created in the `views/` directory and the action NAME.cfm file place into it.

 Create a default file called show.cfm without a template
 ```
 wheels generate view user show
 ```

 Create a default file called show.cfm using the default CRUD template
 ```
 wheels generate view user show crud/show
 ```

| Parameter  | Required | Default | Description                                                           |
| ---------- | -------- | ------- |---------------------------------------------------------------------- |
| objectname | true     |         | View path folder, i.e user                                            |
| name       | true     |         | Name of the file to create, i.e, edit                                 |
| template   | false    |         | template (used in Scaffolding) - options crud/_form,crud/edit,crud/index,crud/new,crud/show |

## `wheels generate test`

This command generates a test stub in `/test/TYPE/NAME.cfc`.

```
wheels generate test model user
```

```
wheels generate test controller users
```

```
wheels generate test view users edit
```

| Parameter | Required | Default | Description |
| ---------- | -------- | ------- | ----------- |
| type | true | | Type of test to generate. Options are model, controller, and view |
| objectname | true | | View path folder or name of object, i.e user |
| name | true | | Name of the action/view |


text:
package com.github.terma.fastselect;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static junit.framework.Assert.assertEquals;

@SuppressWarnings("WeakerAccess")
public class FastSelectMultiByteTest {

    @Test
    public void selectByNegative() {
        FastSelect<Data> database = new FastSelectBuilder<>(Data.class).blockSize(1).create();
        database.addAll(asList(
                new Data((byte) -1),
                new Data((byte) Byte.MIN_VALUE),
                new Data((byte) -12)));

        assertEquals(singletonList(new Data((byte) -1)),
                database.select(new MultiByteRequest("value", (byte) -1)));

        assertEquals(singletonList(new Data(Byte.MIN_VALUE)),
                database.select(new MultiByteRequest("value", Byte.MIN_VALUE)));
    }

    @Test
    public void selectByZero() {
        FastSelect<Data> database = new FastSelectBuilder<>(Data.class).blockSize(1).create();
        database.addAll(asList(
                new Data((byte) 0),
                new Data((byte) 91),
                new Data((byte) 89)));

        List result = database.select(new MultiByteRequest("value", (byte) 0));

        assertEquals(singletonList(new Data((byte) 0)), result);
    }

    @Test
    public void selectByAge() {
        FastSelect<Data> database = new FastSelectBuilder<>(Data.class).blockSize(1).create();
        database.addAll(asList(
                new Data((byte) Byte.MAX_VALUE),
                new Data((byte) 0),
                new Data((byte) Byte.MIN_VALUE)));

        assertEquals(singletonList(new Data(Byte.MAX_VALUE)),
                database.select(new MultiByteRequest("value", (byte) Byte.MAX_VALUE)));

        assertEquals(singletonList(new Data(Byte.MIN_VALUE)),
                database.select(new MultiByteRequest("value", (byte) Byte.MIN_VALUE)));
    }

    @Test
    public void selectByMultipleValuesAsOr() {
        FastSelect<Data> database = new FastSelectBuilder<>(Data.class).blockSize(1).create();
        database.addAll(asList(
                new Data(new byte[]{1, 2}),
                new Data(new byte[]{11, 12}),
                new Data(new byte[]{33, 12}),
                new Data(new byte[]{Byte.MAX_VALUE, -1}),
                new Data(new byte[]{1, 0})
        ));

        List<Data> r = database.select(
                new MultiByteRequest("value", (byte) 1, (byte) 12, (byte) 2));

        assertEquals(asList(
                new Data((byte) 1, (byte) 2),
                new Data((byte) 11, (byte) 12),
                new Data((byte) 33, (byte) 12),
                new Data((byte) 1, (byte) 0)
        ), r);
    }

    @Test
    public void restore() {
        FastSelect<Data> database = new FastSelectBuilder<>(Data.class).blockSize(1).create();
        database.addAll(asList(
                new Data(new byte[]{Byte.MIN_VALUE, -1, 0}),
                new Data(new byte[]{Byte.MAX_VALUE, 0, 1})));

        List<Data> r = database.select(new MultiByteRequest("value", (byte) 0));

        assertEquals(r, asList(
                new Data(new byte[]{Byte.MIN_VALUE, -1, 0}),
                new Data(new byte[]{Byte.MAX_VALUE, 0, 1})
        ));
    }

    public static class Data {

        public byte[] value;

        @SuppressWarnings("unused")
        public Data() {
        }

        Data(byte... value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "{" + "value=" + Arrays.toString(value) + '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Data that = (Data) o;
            return Arrays.equals(value, that.value);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(value);
        }
    }

}


text:
namespace RestaurantManager.Engine
{
    using System;
    using System.Collections.Generic;
    using RestaurantManager.Interfaces.Engine;

    public class Command : ICommand
    {
        private const char CommandNameSeparator = '(';
        private const char CommandParameterSeparator = ';';
        private const char CommandValueSeparator = '=';

        private string name;
        private IDictionary<string, string> parameters = new Dictionary<string, string>();

        public Command(string input)
        {
            this.TranslateInput(input);
        }

        public string Name
        {
            get
            {
                return this.name;
            }

            private set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentNullException("The command name is required.");
                }

                this.name = value;
            }
        }

        public IDictionary<string, string> Parameters
        {
            get
            {
                return this.parameters;
            }

            private set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("The command parameters are required.");
                }

                this.parameters = value;
            }
        }

        public static Command Parse(string input)
        {
            return new Command(input);
        }

        private void TranslateInput(string input)
        {
            int parametersBeginning = input.IndexOf(CommandNameSeparator);

            this.Name = input.Substring(0, parametersBeginning);
            var parametersKeysAndValues = input.Substring(parametersBeginning + 1, input.Length - parametersBeginning - 2)
                .Split(new[] { CommandParameterSeparator }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var parameter in parametersKeysAndValues)
            {
                var split = parameter.Split(new[] { CommandValueSeparator }, StringSplitOptions.RemoveEmptyEntries);
                this.Parameters.Add(split[0], split[1]);
            }
        }
    }
}


text:
class CommentsController < ApplicationController
  before_filter :authenticate_user!

  def create
    commentable = find_commentable

    if (commentable.respond_to?("private?") && commentable.private? && !commentable.pass?(params[:pass]))
      flash[:error] = "You are not authorized to comment this #{commentable.class.to_s.downcase}."
      redirect_to :root
      return
    end

    comment = commentable.comments.build(params[:comment])
    comment.user = current_user

    if comment.save
      commentable.subscribe(current_user.id) if commentable.respond_to?(:subscribe)
      path = nil
      if params[:pass].blank?
        path = polymorphic_path(commentable, :anchor => :comments)
      else
        path = polymorphic_path(commentable, :pass => params[:pass], :anchor => :comments)
      end

      title = "#{current_user.username} commented #{commentable.class.to_s}#{commentable.id}"
      if commentable.respond_to?(:notify)
        Resque.enqueue(CommentNotifier, commentable.class.to_s, commentable.id, current_user.id, path, title, shortify(comment.text))
      end
    else
      flash[:error] = "There was a problem saving the comment."
    end
    redirect_to polymorphic_path(commentable, :pass => params[:pass])
  end

  protected
  def shortify(comment)
    short = comment[0,80]
    if short.length < comment.length
      short += "..."
    end
    short
  end

  def find_commentable
    params.each do |name, value|
      if name =~ /(.+)_id$/
        return $1.classify.constantize.find(value)
      end
    end
    nil
  end
end