3535import java .util .Set ;
3636import java .util .concurrent .TimeUnit ;
3737import java .util .concurrent .TimeoutException ;
38+ import java .util .regex .Pattern ;
3839import org .apache .http .impl .client .CloseableHttpClient ;
3940import org .apache .lucene .tests .util .TestUtil ;
4041import org .apache .solr .client .solrj .SolrClient ;
7071import org .apache .solr .handler .admin .CollectionsHandler ;
7172import org .apache .solr .handler .admin .ConfigSetsHandler ;
7273import org .apache .solr .handler .admin .CoreAdminHandler ;
74+ import org .apache .solr .servlet .HttpSolrCall ;
75+ import org .apache .solr .util .LogLevel ;
76+ import org .apache .solr .util .LogListener ;
7377import org .junit .AfterClass ;
7478import org .junit .BeforeClass ;
7579import org .junit .Test ;
@@ -89,6 +93,15 @@ public class CloudHttp2SolrClientTest extends SolrCloudTestCase {
8993 private static CloudSolrClient httpBasedCloudSolrClient = null ;
9094 private static CloudSolrClient zkBasedCloudSolrClient = null ;
9195
96+ private static final Pattern PATTERN_WITH_COLLECTION =
97+ Pattern .compile (
98+ "path=/admin/collections.*params=\\ {[^}]*action=CLUSTERSTATUS"
99+ + "[^}]*collection=[^&}]+[^}]*\\ }" );
100+ private static final Pattern PATTERN_WITHOUT_COLLECTION =
101+ Pattern .compile (
102+ "path=/admin/collections.*params=\\ {[^}]*action=CLUSTERSTATUS"
103+ + "(?![^}]*collection=)[^}]*\\ }" );
104+
92105 @ BeforeClass
93106 public static void setupCluster () throws Exception {
94107 System .setProperty ("metricsEnabled" , "true" );
@@ -114,6 +127,7 @@ public static void setupCluster() throws Exception {
114127
115128 @ AfterClass
116129 public static void tearDownAfterClass () throws Exception {
130+
117131 if (httpBasedCloudSolrClient != null ) {
118132 try {
119133 httpBasedCloudSolrClient .close ();
@@ -246,6 +260,57 @@ public void testAliasHandling() throws Exception {
246260 2 , client .query (null , paramsWithMixedCollectionAndAlias ).getResults ().getNumFound ());
247261 }
248262
263+ @ Test
264+ @ LogLevel ("org.apache.solr.servlet.HttpSolrCall=DEBUG" )
265+ public void testHttpCspPerf () throws Exception {
266+
267+ String collectionName = "HTTPCSPTEST" ;
268+ CollectionAdminRequest .createCollection (collectionName , "conf" , 2 , 1 )
269+ .process (cluster .getSolrClient ());
270+ cluster .waitForActiveCollection (collectionName , 2 , 2 );
271+
272+ try (LogListener entireClusterStateLogs =
273+ LogListener .info (HttpSolrCall .class ).regex (PATTERN_WITHOUT_COLLECTION );
274+ LogListener collectionClusterStateLogs =
275+ LogListener .info (HttpSolrCall .class ).regex (PATTERN_WITH_COLLECTION );
276+ LogListener adminRequestLogs = LogListener .info (HttpSolrCall .class ).substring ("[admin]" );
277+ CloudSolrClient solrClient = createHttpCSPBasedCloudSolrClient (); ) {
278+ SolrInputDocument doc = new SolrInputDocument ("id" , "1" , "title_s" , "my doc" );
279+ solrClient .add (collectionName , doc );
280+ solrClient .commit (collectionName );
281+ for (int i = 0 ; i < 3 ; i ++) {
282+ assertEquals (
283+ 1 , solrClient .query (collectionName , params ("q" , "*:*" )).getResults ().getNumFound ());
284+ }
285+
286+ // 1 call to fetch entire cluster state via BaseHttpCSP.fetchLiveNodes()
287+ // 1 call to fetch CLUSTERSTATUS for collection via getDocCollection() (first collection
288+ // lookup)
289+ assertLogCount (adminRequestLogs , 2 );
290+ // 1 call to fetch CLUSTERSTATUS for collection via getDocCollection() (first collection
291+ // lookup)
292+ assertLogCount (collectionClusterStateLogs , 1 );
293+ // 1 call to fetch entire cluster state from HttpCSP.fetchLiveNodes()
294+ assertLogCount (entireClusterStateLogs , 1 );
295+ }
296+ }
297+
298+ private CloudSolrClient createHttpCSPBasedCloudSolrClient () {
299+ final List <String > solrUrls = new ArrayList <>();
300+ solrUrls .add (cluster .getJettySolrRunner (0 ).getBaseUrl ().toString ());
301+ return new CloudHttp2SolrClient .Builder (solrUrls ).build ();
302+ }
303+
304+ private void assertLogCount (LogListener logListener , int expectedCount ) {
305+ int logCount = logListener .getCount ();
306+ assertEquals (expectedCount , logCount );
307+ if (logCount > 0 ) {
308+ for (int i = 0 ; i < logCount ; i ++) {
309+ logListener .pollMessage ();
310+ }
311+ }
312+ }
313+
249314 @ Test
250315 public void testRouting () throws Exception {
251316 CollectionAdminRequest .createCollection ("routing_collection" , "conf" , 2 , 1 )
0 commit comments