|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.client.solrj.io.stream; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | +import org.apache.solr.SolrTestCaseJ4; |
| 25 | +import org.apache.solr.client.solrj.io.Tuple; |
| 26 | +import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; |
| 27 | +import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter; |
| 28 | +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; |
| 29 | +import org.apache.solr.client.solrj.request.CollectionAdminRequest; |
| 30 | +import org.apache.solr.cloud.SolrCloudTestCase; |
| 31 | +import org.apache.solr.common.SolrException; |
| 32 | +import org.apache.solr.common.cloud.Replica; |
| 33 | +import org.apache.solr.common.params.CommonParams; |
| 34 | +import org.apache.solr.common.params.MultiMapSolrParams; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** Tests behaviors of CloudSolrStream when the cluster is behaving badly. */ |
| 39 | +@SolrTestCaseJ4.SuppressSSL |
| 40 | +public class BadClusterTest extends SolrCloudTestCase { |
| 41 | + |
| 42 | + private static final String collection = "streams"; |
| 43 | + private static final String id = "id"; |
| 44 | + |
| 45 | + private static final StreamFactory streamFactory = |
| 46 | + new StreamFactory().withFunctionName("search", CloudSolrStream.class); |
| 47 | + |
| 48 | + private static String zkHost; |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void configureCluster() throws Exception { |
| 52 | + configureCluster(1) |
| 53 | + .addConfig( |
| 54 | + "conf", |
| 55 | + getFile("solrj") |
| 56 | + .toPath() |
| 57 | + .resolve("solr") |
| 58 | + .resolve("configsets") |
| 59 | + .resolve("streaming") |
| 60 | + .resolve("conf")) |
| 61 | + .configure(); |
| 62 | + |
| 63 | + CollectionAdminRequest.createCollection(collection, "conf", 1, 1) |
| 64 | + .process(cluster.getSolrClient()); |
| 65 | + cluster.waitForActiveCollection(collection, 1, 1); |
| 66 | + |
| 67 | + zkHost = cluster.getZkServer().getZkAddress(); |
| 68 | + streamFactory.withCollectionZkHost(collection, zkHost); |
| 69 | + } |
| 70 | + |
| 71 | + // test order is important because the cluster progressively gets worse, but it is only created |
| 72 | + // once in BeforeClass as in other tests |
| 73 | + // ordering can not be strictly enforced with JUnit annotations because of parallel executions, so |
| 74 | + // we have this aggregated test instead |
| 75 | + @Test |
| 76 | + public void testBadCluster() throws Exception { |
| 77 | + testEmptyCollection(); |
| 78 | + testAllNodesDown(); |
| 79 | + testClusterShutdown(); |
| 80 | + } |
| 81 | + |
| 82 | + private void testEmptyCollection() throws Exception { |
| 83 | + CloudSolrStream stream = new CloudSolrStream(buildSearchExpression(), streamFactory); |
| 84 | + assertEquals(0, getTuples(stream).size()); |
| 85 | + } |
| 86 | + |
| 87 | + private void testAllNodesDown() throws Exception { |
| 88 | + |
| 89 | + CloudSolrStream stream = new CloudSolrStream(buildSearchExpression(), streamFactory); |
| 90 | + cluster.expireZkSession(cluster.getReplicaJetty(getReplicas().get(0))); |
| 91 | + |
| 92 | + try { |
| 93 | + getTuples(stream); |
| 94 | + fail("Expected IOException"); |
| 95 | + } catch (IOException ioe) { |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void testClusterShutdown() throws Exception { |
| 100 | + |
| 101 | + CloudSolrStream stream = new CloudSolrStream(buildSearchExpression(), streamFactory); |
| 102 | + cluster.shutdown(); |
| 103 | + |
| 104 | + try { |
| 105 | + getTuples(stream); |
| 106 | + fail("Expected IOException: SolrException: TimeoutException"); |
| 107 | + } catch (IOException ioe) { |
| 108 | + SolrException se = (SolrException) ioe.getCause(); |
| 109 | + TimeoutException te = (TimeoutException) se.getCause(); |
| 110 | + assertNotNull(te); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private StreamExpression buildSearchExpression() { |
| 115 | + StreamExpression expression = new StreamExpression("search"); |
| 116 | + expression.addParameter(collection); |
| 117 | + expression.addParameter(new StreamExpressionNamedParameter(CommonParams.Q, "*:*")); |
| 118 | + expression.addParameter(new StreamExpressionNamedParameter(CommonParams.FL, id)); |
| 119 | + expression.addParameter(new StreamExpressionNamedParameter(CommonParams.SORT, id + " asc")); |
| 120 | + return expression; |
| 121 | + } |
| 122 | + |
| 123 | + private List<Replica> getReplicas() throws IOException { |
| 124 | + return TupleStream.getReplicas(zkHost, collection, null, new MultiMapSolrParams(Map.of())); |
| 125 | + } |
| 126 | + |
| 127 | + private List<Tuple> getTuples(TupleStream tupleStream) throws IOException { |
| 128 | + tupleStream.open(); |
| 129 | + List<Tuple> tuples = new ArrayList<>(); |
| 130 | + for (; ; ) { |
| 131 | + Tuple t = tupleStream.read(); |
| 132 | + if (t.EOF) { |
| 133 | + break; |
| 134 | + } else { |
| 135 | + tuples.add(t); |
| 136 | + } |
| 137 | + } |
| 138 | + tupleStream.close(); |
| 139 | + return tuples; |
| 140 | + } |
| 141 | +} |
0 commit comments