Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.solr.handler.component;
  
  /*
   * 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.
  */
 
 import java.util.List;
 import java.util.Map;
 
A ShardHandlerFactory that extends HttpShardHandlerFactory and tracks requests made to nodes/shards such that interested parties can watch such requests and make assertions inside tests

This is a test helper only and should *not* be used for production.

 
 
   private Queue<ShardRequestAndParamsqueue;

  
Set the tracking queue for this factory. All the ShardHandler instances created from this factory will share the queue and call java.util.Queue.offer(java.lang.Object) with a TrackingShardHandlerFactory.ShardRequestAndParams instance whenever org.apache.solr.handler.component.ShardHandler.submit(org.apache.solr.handler.component.ShardRequest,java.lang.String,org.apache.solr.common.params.ModifiableSolrParams) is called before the request is actually submitted to the wrapped org.apache.solr.handler.component.HttpShardHandlerFactory instance.

If a tracking queue is already set then this call will overwrite and replace the previous queue with this one.

Parameters:
queue the java.util.Queue to be used for tracking shard requests
 
   public synchronized void setTrackingQueue(Queue<ShardRequestAndParamsqueue) {
     this. = queue;
   }

  

Returns:
the java.util.Queue being used for tracking, null if none has been set
 
   public synchronized Queue<ShardRequestAndParamsgetTrackingQueue() {
     return ;
   }

  

Returns:
true if a tracking queue has been set through setTrackingQueue(java.util.List,java.util.Queue), false otherwise
 
   public synchronized boolean isTracking() {
     return  != null;
   }
 
   @Override
   public ShardHandler getShardHandler() {
     final ShardHandlerFactory factory = this;
     final ShardHandler wrapped = super.getShardHandler();
     return new ShardHandler() {
       @Override
       public void checkDistributed(ResponseBuilder rb) {
         wrapped.checkDistributed(rb);
       }
 
       @Override
       public void submit(ShardRequest sreqString shardModifiableSolrParams params) {
         synchronized (TrackingShardHandlerFactory.this) {
           if (isTracking()) {
             .offer(new ShardRequestAndParams(sreqshardparams));
          }
        }
        wrapped.submit(sreqshardparams);
      }
      @Override
        return wrapped.takeCompletedIncludingErrors();
      }
      @Override
      public ShardResponse takeCompletedOrError() {
        return wrapped.takeCompletedOrError();
      }
      @Override
      public void cancelAll() {
        wrapped.cancelAll();
      }
      @Override
        return factory;
      }
    };
  }
  public void close() {
    super.close();
  }

  
Sets the tracking queue for all nodes participating in this cluster. Once this method returns, all search and core admin requests distributed to shards will be submitted to the given queue.

This is equivalent to calling: TrackingShardHandlerFactory.setTrackingQueue(cluster.getJettySolrRunners(), queue)

  public static void setTrackingQueue(MiniSolrCloudCluster clusterQueue<ShardRequestAndParamsqueue) {
    setTrackingQueue(cluster.getJettySolrRunners(), queue);
  }

  
Sets the tracking queue for all nodes participating in this cluster. Once this method returns, all search and core admin requests distributed to shards will be submitted to the given queue.

Parameters:
runners a list of org.apache.solr.client.solrj.embedded.JettySolrRunner nodes
queue an implementation of java.util.Queue which accepts TrackingShardHandlerFactory.ShardRequestAndParams instances
  public static void setTrackingQueue(List<JettySolrRunnerrunnersQueue<ShardRequestAndParamsqueue) {
    for (JettySolrRunner runner : runners) {
      CoreContainer container = ((SolrDispatchFilterrunner.getDispatchFilter().getFilter()).getCores();
      ShardHandlerFactory factory = container.getShardHandlerFactory();
      assert factory instanceof TrackingShardHandlerFactory;
      TrackingShardHandlerFactory trackingShardHandlerFactory = (TrackingShardHandlerFactoryfactory;
      trackingShardHandlerFactory.setTrackingQueue(queue);
    }
  }
  public static class ShardRequestAndParams {
    public String shard;
    public ShardRequest sreq;
    public ShardRequestAndParams(ShardRequest sreqString shardModifiableSolrParams params) {
      this. = sreq;
      this. = params;
      this. = shard;
    }
    @Override
    public String toString() {
      return "ShardRequestAndParams{" +
          "shard='" +  + '\'' +
          ", sreq=" +  +
          ", params=" +  +
          '}';
    }
  }

  
A queue having helper methods to select requests by shard and purpose.

  public static class RequestTrackingQueue extends LinkedList<ShardRequestAndParams> {
    private final Map<StringList<ShardRequestAndParams>> requests = new ConcurrentHashMap<>();
    @Override
    public boolean offer(ShardRequestAndParams shardRequestAndParams) {
      List<ShardRequestAndParamslist = .get(shardRequestAndParams.shard);
      if (list == null) {
        list = new ArrayList<>();
      }
      list.add(shardRequestAndParams);
      .put(shardRequestAndParams.shardlist);
      return super.offer(shardRequestAndParams);
    }
    @Override
    public void clear() {
      .clear();
    }

    
Retrieve request recorded by this queue which were sent to given collection, shard and purpose

Parameters:
zkStateReader the org.apache.solr.common.cloud.ZkStateReader from which cluster state is read
collectionName the given collection name for which requests have to be extracted
shardId the given shard name for which requests have to be extracted
purpose the shard purpose
Returns:
instance of TrackingShardHandlerFactory.ShardRequestAndParams or null if none is found
Throws:
java.lang.RuntimeException if more than one request is found to the same shard with the same purpose
    public ShardRequestAndParams getShardRequestByPurpose(ZkStateReader zkStateReaderString collectionNameString shardIdint purposethrows RuntimeException {
      List<TrackingShardHandlerFactory.ShardRequestAndParamsshardRequests = getShardRequests(zkStateReadercollectionNameshardId);
      for (TrackingShardHandlerFactory.ShardRequestAndParams request : shardRequests) {
        if ((request.sreq.purpose & purpose) != 0) {
          result.add(request);
        }
      }
      if (result.size() > 1) {
        throw new RuntimeException("Multiple requests to the same shard with the same purpose were found. Requests: " + result);
      }
      return result.isEmpty() ? null : result.get(0);
    }

    
Retrieve all requests recorded by this queue which were sent to given collection and shard

Parameters:
zkStateReader the org.apache.solr.common.cloud.ZkStateReader from which cluster state is read
collectionName the given collection name for which requests have to be extracted
shardId the given shard name for which requests have to be extracted
Returns:
a list of TrackingShardHandlerFactory.ShardRequestAndParams or empty list if none are found
    public List<ShardRequestAndParamsgetShardRequests(ZkStateReader zkStateReaderString collectionNameString shardId) {
      DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
      assert collection != null;
      Slice slice = collection.getSlice(shardId);
      assert slice != null;
      for (Map.Entry<StringList<ShardRequestAndParams>> entry : .entrySet()) {
        // multiple shard addresses may be present separated by '|'
        List<Stringlist = StrUtils.splitSmart(entry.getKey(), '|');
        for (Map.Entry<StringReplicareplica : slice.getReplicasMap().entrySet()) {
          String coreUrl = new ZkCoreNodeProps(replica.getValue()).getCoreUrl();
          if (list.contains(coreUrl)) {
            return new ArrayList<>(entry.getValue());
          }
        }
      }
      return Collections.emptyList();
    }

    
Retrieves all core admin requests distributed to nodes by Collection API commands

Returns:
a list of TrackingShardHandlerFactory.ShardRequestAndParams or empty if none found
      List<ShardRequestAndParamsresults = new ArrayList<>();
      for (Map.Entry<StringList<ShardRequestAndParams>> entry : map.entrySet()) {
        for (ShardRequestAndParams shardRequestAndParams : entry.getValue()) {
          if (shardRequestAndParams.sreq.purpose == .) {
            results.add(shardRequestAndParams);
          }
        }
      }
      return results;
    }

    
Retrieves all requests recorded by this collection as a Map of shard address (string url) to a list of TrackingShardHandlerFactory.ShardRequestAndParams

Returns:
a java.util.concurrent.ConcurrentHashMap of url strings to TrackingShardHandlerFactory.ShardRequestAndParams objects or empty map if none have been recorded
      return ;
    }
  }
New to GrepCode? Check out our FAQ X