|
| 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.core; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | +import org.apache.solr.SolrTestCase; |
| 25 | +import org.apache.solr.SolrTestCaseJ4; |
| 26 | +import org.apache.solr.common.util.ObjectReleaseTracker; |
| 27 | +import org.apache.solr.handler.RequestHandlerBase; |
| 28 | +import org.apache.solr.handler.component.HttpShardHandlerFactory; |
| 29 | +import org.apache.solr.handler.component.ShardHandlerFactory; |
| 30 | +import org.apache.solr.metrics.SolrMetricsContext; |
| 31 | +import org.apache.solr.request.SolrQueryRequest; |
| 32 | +import org.apache.solr.response.SolrQueryResponse; |
| 33 | +import org.apache.solr.security.AuthorizationContext; |
| 34 | +import org.apache.solr.security.PermissionNameProvider; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** |
| 39 | + * Shutdown must run its trailing closes even when a close task outlives the closeThreadPool's |
| 40 | + * termination budget and the pool wait throws. Those closes are the only release path for the |
| 41 | + * container's metrics contexts; skipping them leaks the whole metrics tree, and in a test JVM the |
| 42 | + * leaked contexts are then charged to an unrelated suite by the object release tracker. |
| 43 | + */ |
| 44 | +public class CoreContainerShutdownTest extends SolrTestCase { |
| 45 | + |
| 46 | + private static final String SOLR_XML = |
| 47 | + "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 48 | + + "<solr>\n" |
| 49 | + + "<str name=\"configSetBaseDir\">${configsets:configsets}</str>\n" |
| 50 | + + "</solr>"; |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public static void beforeClass() { |
| 54 | + System.setProperty( |
| 55 | + "configsets", SolrTestCaseJ4.getFile("solr/configsets").toAbsolutePath().toString()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testShutdownRunsTrailingClosesWhenClosePoolTimesOut() throws Exception { |
| 60 | + Path solrHome = createTempDir(); |
| 61 | + CoreContainer cc = new CoreContainer(SolrXmlConfig.fromString(solrHome, SOLR_XML)); |
| 62 | + cc.load(); |
| 63 | + BlockingShardHandlerFactory blockingFactory = null; |
| 64 | + try { |
| 65 | + assertTrue( |
| 66 | + "the root metrics context is tracked after load", |
| 67 | + ObjectReleaseTracker.getTrackedObjectsOfType(SolrMetricsContext.class) |
| 68 | + .contains(cc.solrMetricsContext)); |
| 69 | + |
| 70 | + CloseTrackingHandler handler = new CloseTrackingHandler(); |
| 71 | + cc.getRequestHandlers().put("/close-tracking", handler); |
| 72 | + |
| 73 | + // Make one close task outlive the pool's whole termination budget (both 60 second |
| 74 | + // awaits), so the pool wait throws the way it did in the incident this test pins down. |
| 75 | + // This makes the test take about two minutes; the wait is the production timeout, |
| 76 | + // not a test sleep. |
| 77 | + blockingFactory = new BlockingShardHandlerFactory(cc.shardHandlerFactory); |
| 78 | + cc.shardHandlerFactory = blockingFactory; |
| 79 | + |
| 80 | + cc.shutdown(); |
| 81 | + |
| 82 | + assertTrue( |
| 83 | + "container handlers must be closed even when the close pool times out", |
| 84 | + handler.closed.get()); |
| 85 | + assertFalse( |
| 86 | + "the root metrics context must be released even when the close pool times out", |
| 87 | + ObjectReleaseTracker.getTrackedObjectsOfType(SolrMetricsContext.class) |
| 88 | + .contains(cc.solrMetricsContext)); |
| 89 | + } finally { |
| 90 | + if (blockingFactory != null) { |
| 91 | + blockingFactory.releaseClose(); |
| 92 | + // Wait for the blocked close task to finish so the delegate factory is closed |
| 93 | + // before the framework's post-class object release check runs. |
| 94 | + assertTrue(blockingFactory.awaitCloseFinished(30, TimeUnit.SECONDS)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** A container handler whose close is observable. */ |
| 100 | + private static final class CloseTrackingHandler extends RequestHandlerBase { |
| 101 | + final AtomicBoolean closed = new AtomicBoolean(); |
| 102 | + |
| 103 | + @Override |
| 104 | + public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {} |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getDescription() { |
| 108 | + return "test handler that records its close"; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public PermissionNameProvider.Name getPermissionName(AuthorizationContext request) { |
| 113 | + return PermissionNameProvider.Name.ALL; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void close() throws IOException { |
| 118 | + closed.set(true); |
| 119 | + super.close(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Wraps the real shard handler factory with a close that blocks until released, surviving the |
| 125 | + * pool's force-stop interrupt the way a stuck Jetty client close does, and only then delegates. |
| 126 | + */ |
| 127 | + private static final class BlockingShardHandlerFactory extends HttpShardHandlerFactory { |
| 128 | + private final ShardHandlerFactory delegate; |
| 129 | + private final CountDownLatch release = new CountDownLatch(1); |
| 130 | + private final CountDownLatch closeFinished = new CountDownLatch(1); |
| 131 | + |
| 132 | + BlockingShardHandlerFactory(ShardHandlerFactory delegate) { |
| 133 | + this.delegate = delegate; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void close() { |
| 138 | + try { |
| 139 | + boolean released = false; |
| 140 | + while (!released) { |
| 141 | + try { |
| 142 | + release.await(); |
| 143 | + released = true; |
| 144 | + } catch (InterruptedException e) { |
| 145 | + // Keep blocking: the pool's force-stop interrupts its workers, and a close stuck |
| 146 | + // inside the client shutdown survives that interrupt without finishing. |
| 147 | + } |
| 148 | + } |
| 149 | + delegate.close(); |
| 150 | + } finally { |
| 151 | + closeFinished.countDown(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + void releaseClose() { |
| 156 | + release.countDown(); |
| 157 | + } |
| 158 | + |
| 159 | + boolean awaitCloseFinished(long timeout, TimeUnit unit) throws InterruptedException { |
| 160 | + return closeFinished.await(timeout, unit); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments