-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARTEMIS-4569 Blocked Producers will hold runnables until messages are…
… consumed. When initially developed the expectation was that no more producers would keep connecting but in a scenario like this the consumers could actually give up and things will just accumulate on the server. We should cleanup these upon disconnect.
- Loading branch information
1 parent
1fef332
commit cedc050
Showing
13 changed files
with
514 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/runnables/RunnableList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.activemq.artemis.utils.runnables; | ||
|
||
import java.util.HashSet; | ||
import java.util.function.Consumer; | ||
|
||
public class RunnableList { | ||
|
||
private final HashSet<AtomicRunnable> list = new HashSet<>(); | ||
|
||
public RunnableList() { | ||
} | ||
|
||
public synchronized void add(AtomicRunnable runnable) { | ||
runnable.setAcceptedList(this); | ||
list.add(runnable); | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
|
||
public synchronized void remove(AtomicRunnable runnable) { | ||
list.remove(runnable); | ||
} | ||
|
||
public synchronized void cancel() { | ||
list.forEach(this::cancel); | ||
list.clear(); | ||
} | ||
|
||
private void cancel(AtomicRunnable atomicRunnable) { | ||
atomicRunnable.cancel(); | ||
} | ||
|
||
public void forEach(Consumer<AtomicRunnable> consumerRunnable) { | ||
list.forEach(consumerRunnable); | ||
} | ||
|
||
} |
135 changes: 135 additions & 0 deletions
135
...s-commons/src/test/java/org/apache/activemq/artemis/utils/runnables/RunnableListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.activemq.artemis.utils.runnables; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class RunnableListTest { | ||
|
||
HashSet<AtomicRunnable> masterList = new HashSet<>(); | ||
|
||
@Test | ||
public void testRunning() { | ||
AtomicInteger result = new AtomicInteger(); | ||
|
||
RunnableList listA = new RunnableList(); | ||
RunnableList listB = new RunnableList(); | ||
RunnableList listC = new RunnableList(); | ||
|
||
RunnableList[] lists = new RunnableList[]{listA, listB, listC}; | ||
for (RunnableList l : lists) { | ||
for (int i = 0; i < 10; i++) { | ||
AtomicRunnable runnable = new AtomicRunnable() { | ||
@Override | ||
public void atomicRun() { | ||
result.incrementAndGet(); | ||
masterList.remove(this); | ||
} | ||
}; | ||
addItem(l, runnable); | ||
} | ||
} | ||
|
||
Assert.assertEquals(30, masterList.size()); | ||
|
||
runList(listA); | ||
|
||
Assert.assertEquals(10, result.get()); | ||
|
||
Assert.assertEquals(20, masterList.size()); | ||
Assert.assertEquals(0, listA.size()); | ||
Assert.assertEquals(10, listB.size()); | ||
Assert.assertEquals(10, listC.size()); | ||
|
||
HashSet<AtomicRunnable> copyList = new HashSet<>(); | ||
copyList.addAll(masterList); | ||
|
||
copyList.forEach(r -> r.run()); | ||
|
||
for (RunnableList l : lists) { | ||
Assert.assertEquals(0, l.size()); | ||
} | ||
|
||
Assert.assertEquals(30, result.get()); | ||
} | ||
|
||
@Test | ||
public void testCancel() { | ||
AtomicInteger result = new AtomicInteger(); | ||
|
||
RunnableList listA = new RunnableList(); | ||
RunnableList listB = new RunnableList(); | ||
RunnableList listC = new RunnableList(); | ||
|
||
RunnableList[] lists = new RunnableList[]{listA, listB, listC}; | ||
for (RunnableList l : lists) { | ||
for (int i = 0; i < 10; i++) { | ||
AtomicRunnable runnable = new AtomicRunnable() { | ||
@Override | ||
public void atomicRun() { | ||
result.incrementAndGet(); | ||
masterList.remove(this); | ||
} | ||
}; | ||
addItem(l, runnable); | ||
} | ||
} | ||
|
||
Assert.assertEquals(30, masterList.size()); | ||
|
||
listA.cancel(); | ||
|
||
Assert.assertEquals(0, result.get()); | ||
|
||
Assert.assertEquals(20, masterList.size()); | ||
Assert.assertEquals(0, listA.size()); | ||
Assert.assertEquals(10, listB.size()); | ||
Assert.assertEquals(10, listC.size()); | ||
|
||
listB.cancel(); | ||
listC.cancel(); | ||
|
||
for (RunnableList l : lists) { | ||
Assert.assertEquals(0, l.size()); | ||
} | ||
|
||
Assert.assertEquals(0, masterList.size()); | ||
} | ||
|
||
// runs all AtomicRunnables inside the list | ||
private void runList(RunnableList list) { | ||
// make a copy of all the tasks to a new list | ||
ArrayList<AtomicRunnable> toRun = new ArrayList<>(); | ||
list.forEach(toRun::add); | ||
|
||
// run all the elements | ||
toRun.forEach(r -> r.run()); | ||
} | ||
|
||
private void addItem(RunnableList list, AtomicRunnable runnable) { | ||
list.add(runnable); | ||
runnable.setCancel(masterList::remove); | ||
masterList.add(runnable); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.