|
| 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 | + |
| 18 | +package org.apache.solr.cloud; |
| 19 | + |
| 20 | +import java.lang.invoke.MethodHandles; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.ExecutorService; |
| 25 | +import java.util.concurrent.Future; |
| 26 | +import java.util.concurrent.Semaphore; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.apache.solr.SolrTestCaseJ4; |
| 29 | +import org.apache.solr.client.solrj.SolrClient; |
| 30 | +import org.apache.solr.client.solrj.impl.CloudSolrClient; |
| 31 | +import org.apache.solr.client.solrj.request.CollectionAdminRequest; |
| 32 | +import org.apache.solr.client.solrj.request.QueryRequest; |
| 33 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 34 | +import org.apache.solr.common.SolrException; |
| 35 | +import org.apache.solr.common.util.ExecutorUtil; |
| 36 | +import org.apache.solr.common.util.NamedList; |
| 37 | +import org.apache.solr.core.SolrCore; |
| 38 | +import org.apache.solr.embedded.JettyConfig; |
| 39 | +import org.apache.solr.embedded.JettySolrRunner; |
| 40 | +import org.apache.solr.handler.component.SearchHandler; |
| 41 | +import org.apache.solr.request.SolrQueryRequest; |
| 42 | +import org.apache.solr.response.SolrQueryResponse; |
| 43 | +import org.slf4j.Logger; |
| 44 | +import org.slf4j.LoggerFactory; |
| 45 | + |
| 46 | +public class TestGracefulJettyShutdown extends SolrTestCaseJ4 { |
| 47 | + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 48 | + |
| 49 | + public void testSingleShardInFlightRequestsDuringShutDown() throws Exception { |
| 50 | + final String collection = getSaferTestName(); |
| 51 | + final String handler = "/foo"; |
| 52 | + |
| 53 | + final Semaphore handlerGate = new Semaphore(0); |
| 54 | + final Semaphore handlerSignal = new Semaphore(0); |
| 55 | + |
| 56 | + final ExecutorService exec = ExecutorUtil.newMDCAwareCachedThreadPool("client-requests"); |
| 57 | + final MiniSolrCloudCluster cluster = |
| 58 | + new MiniSolrCloudCluster(1, createTempDir(), JettyConfig.builder().build()); |
| 59 | + try { |
| 60 | + assertTrue( |
| 61 | + CollectionAdminRequest.createCollection(collection, "_default", 1, 1) |
| 62 | + .process(cluster.getSolrClient()) |
| 63 | + .isSuccess()); |
| 64 | + |
| 65 | + final JettySolrRunner nodeToStop = cluster.getJettySolrRunner(0); |
| 66 | + |
| 67 | + // register our custom handler with "all" (one) of our SolrCores |
| 68 | + for (String coreName : nodeToStop.getCoreContainer().getLoadedCoreNames()) { |
| 69 | + try (SolrCore core = nodeToStop.getCoreContainer().getCore(coreName)) { |
| 70 | + final BlockingSearchHandler h = new BlockingSearchHandler(handlerGate, handlerSignal); |
| 71 | + h.inform(core); |
| 72 | + core.registerRequestHandler(handler, h); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + final CloudSolrClient cloudClient = cluster.getSolrClient(); |
| 77 | + |
| 78 | + // add a few docs... |
| 79 | + cloudClient.add(collection, sdoc("id", "xxx", "foo_s", "aaa")); |
| 80 | + cloudClient.add(collection, sdoc("id", "yyy", "foo_s", "bbb")); |
| 81 | + cloudClient.add(collection, sdoc("id", "zzz", "foo_s", "aaa")); |
| 82 | + cloudClient.commit(collection); |
| 83 | + |
| 84 | + final List<Future<QueryResponse>> results = new ArrayList<>(13); |
| 85 | + |
| 86 | + try (SolrClient jettyClient = nodeToStop.newClient()) { |
| 87 | + final QueryRequest req = new QueryRequest(params("q", "foo_s:aaa")); |
| 88 | + req.setPath(handler); |
| 89 | + |
| 90 | + // check inflight requests using both clients... |
| 91 | + for (SolrClient client : Arrays.asList(cloudClient, jettyClient)) { |
| 92 | + results.add( |
| 93 | + exec.submit( |
| 94 | + () -> { |
| 95 | + return req.process(client, collection); |
| 96 | + })); |
| 97 | + } |
| 98 | + |
| 99 | + // wait for our handlers to indicate they have recieved the requests and started processing |
| 100 | + log.info("Waiting for signals from both requests"); |
| 101 | + assertTrue(handlerSignal.tryAcquire(2, 300, TimeUnit.SECONDS)); // safety valve |
| 102 | + |
| 103 | + // stop our node (via executor so it doesn't block) and open the gate for our handlers |
| 104 | + log.info("Stopping jetty node"); |
| 105 | + final Future<Boolean> stopped = |
| 106 | + exec.submit( |
| 107 | + () -> { |
| 108 | + nodeToStop.stop(); |
| 109 | + return true; |
| 110 | + }); |
| 111 | + log.info("Releasing gate for requests"); |
| 112 | + handlerGate.release(2); |
| 113 | + log.info("Released gate for requests"); |
| 114 | + |
| 115 | + // confirm success of requests |
| 116 | + assertEquals(2, results.size()); |
| 117 | + for (Future<QueryResponse> f : results) { |
| 118 | + final QueryResponse rsp = f.get(300, TimeUnit.SECONDS); // safety valve |
| 119 | + assertEquals(2, rsp.getResults().getNumFound()); |
| 120 | + } |
| 121 | + assertTrue(stopped.get(300, TimeUnit.SECONDS)); // safety valve |
| 122 | + } |
| 123 | + |
| 124 | + } finally { |
| 125 | + handlerGate.release(9999999); // safety valve |
| 126 | + handlerSignal.release(9999999); // safety valve |
| 127 | + cluster.shutdown(); |
| 128 | + ExecutorUtil.shutdownAndAwaitTermination(exec); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * On every request, prior to handling, releases a ticket to it's signal Semaphre, and then blocks |
| 134 | + * until it can acquire a ticket from it's gate semaphore. Has a safety valve that gives up on the |
| 135 | + * gate after 300 seconds |
| 136 | + */ |
| 137 | + private static final class BlockingSearchHandler extends SearchHandler { |
| 138 | + private final Semaphore gate; |
| 139 | + private final Semaphore signal; |
| 140 | + |
| 141 | + public BlockingSearchHandler(final Semaphore gate, final Semaphore signal) { |
| 142 | + super(); |
| 143 | + this.gate = gate; |
| 144 | + this.signal = signal; |
| 145 | + super.init(new NamedList<>()); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { |
| 150 | + log.info("Starting request"); |
| 151 | + signal.release(); |
| 152 | + if (gate.tryAcquire(300, TimeUnit.SECONDS)) { |
| 153 | + super.handleRequestBody(req, rsp); |
| 154 | + } else { |
| 155 | + log.error("Gate safety valve timeout"); |
| 156 | + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Gate timeout"); |
| 157 | + } |
| 158 | + log.info("Finishing request"); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments