|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.cluster.action.index; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionListener; |
| 22 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 23 | +import org.elasticsearch.cluster.action.index.MappingUpdatedAction.AdjustableSemaphore; |
| 24 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.index.Index; |
| 27 | +import org.elasticsearch.index.mapper.Mapping; |
| 28 | +import org.elasticsearch.test.ESTestCase; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 32 | + |
| 33 | +public class MappingUpdatedActionTests extends ESTestCase { |
| 34 | + |
| 35 | + public void testAdjustableSemaphore() { |
| 36 | + AdjustableSemaphore sem = new AdjustableSemaphore(1, randomBoolean()); |
| 37 | + assertEquals(1, sem.availablePermits()); |
| 38 | + assertTrue(sem.tryAcquire()); |
| 39 | + assertEquals(0, sem.availablePermits()); |
| 40 | + assertFalse(sem.tryAcquire()); |
| 41 | + assertEquals(0, sem.availablePermits()); |
| 42 | + |
| 43 | + // increase the number of max permits to 2 |
| 44 | + sem.setMaxPermits(2); |
| 45 | + assertEquals(1, sem.availablePermits()); |
| 46 | + assertTrue(sem.tryAcquire()); |
| 47 | + assertEquals(0, sem.availablePermits()); |
| 48 | + |
| 49 | + // release all current permits |
| 50 | + sem.release(); |
| 51 | + assertEquals(1, sem.availablePermits()); |
| 52 | + sem.release(); |
| 53 | + assertEquals(2, sem.availablePermits()); |
| 54 | + |
| 55 | + // reduce number of max permits to 1 |
| 56 | + sem.setMaxPermits(1); |
| 57 | + assertEquals(1, sem.availablePermits()); |
| 58 | + // set back to 2 |
| 59 | + sem.setMaxPermits(2); |
| 60 | + assertEquals(2, sem.availablePermits()); |
| 61 | + |
| 62 | + // take both permits and reduce max permits |
| 63 | + assertTrue(sem.tryAcquire()); |
| 64 | + assertTrue(sem.tryAcquire()); |
| 65 | + assertEquals(0, sem.availablePermits()); |
| 66 | + assertFalse(sem.tryAcquire()); |
| 67 | + sem.setMaxPermits(1); |
| 68 | + assertEquals(-1, sem.availablePermits()); |
| 69 | + assertFalse(sem.tryAcquire()); |
| 70 | + |
| 71 | + // release one permit |
| 72 | + sem.release(); |
| 73 | + assertEquals(0, sem.availablePermits()); |
| 74 | + assertFalse(sem.tryAcquire()); |
| 75 | + |
| 76 | + // release second permit |
| 77 | + sem.release(); |
| 78 | + assertEquals(1, sem.availablePermits()); |
| 79 | + assertTrue(sem.tryAcquire()); |
| 80 | + } |
| 81 | + |
| 82 | + public void testMappingUpdatedActionBlocks() throws Exception { |
| 83 | + List<ActionListener<Void>> inFlightListeners = new CopyOnWriteArrayList<>(); |
| 84 | + final MappingUpdatedAction mua = new MappingUpdatedAction(Settings.builder() |
| 85 | + .put(MappingUpdatedAction.INDICES_MAX_IN_FLIGHT_UPDATES_SETTING.getKey(), 1).build(), |
| 86 | + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) { |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void sendUpdateMapping(Index index, Mapping mappingUpdate, ActionListener<Void> listener) { |
| 90 | + inFlightListeners.add(listener); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + PlainActionFuture<Void> fut1 = new PlainActionFuture<>(); |
| 95 | + mua.updateMappingOnMaster(null, null, fut1); |
| 96 | + assertEquals(1, inFlightListeners.size()); |
| 97 | + assertEquals(0, mua.blockedThreads()); |
| 98 | + |
| 99 | + PlainActionFuture<Void> fut2 = new PlainActionFuture<>(); |
| 100 | + Thread thread = new Thread(() -> { |
| 101 | + mua.updateMappingOnMaster(null, null, fut2); // blocked |
| 102 | + }); |
| 103 | + thread.start(); |
| 104 | + assertBusy(() -> assertEquals(1, mua.blockedThreads())); |
| 105 | + |
| 106 | + assertEquals(1, inFlightListeners.size()); |
| 107 | + assertFalse(fut1.isDone()); |
| 108 | + inFlightListeners.remove(0).onResponse(null); |
| 109 | + assertTrue(fut1.isDone()); |
| 110 | + |
| 111 | + thread.join(); |
| 112 | + assertEquals(0, mua.blockedThreads()); |
| 113 | + assertEquals(1, inFlightListeners.size()); |
| 114 | + assertFalse(fut2.isDone()); |
| 115 | + inFlightListeners.remove(0).onResponse(null); |
| 116 | + assertTrue(fut2.isDone()); |
| 117 | + } |
| 118 | +} |
0 commit comments