forked from apache/activemq-artemis
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1ba6cc
commit d67c08b
Showing
16 changed files
with
453 additions
and
113 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
51 changes: 51 additions & 0 deletions
51
...ns/src/main/java/org/apache/activemq/artemis/utils/actors/GroupingTasksProcessorBase.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,51 @@ | ||
/* | ||
* 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.actors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* this is for any ProcessorBase that should empty the queue, and execute things at once. | ||
* Example: a processorBase that should get all pending tasks, write them all on storage and issue a single storage sync at the end. | ||
*/ | ||
public abstract class GroupingTasksProcessorBase<T> extends ProcessorBase<T> { | ||
|
||
public GroupingTasksProcessorBase(Executor parent) { | ||
super(parent); | ||
} | ||
|
||
@Override | ||
protected void processQueue() { | ||
T task; | ||
List<T> pendingTasks = null; | ||
while (!yielded && !requestedForcedShutdown && (task = tasks.poll()) != null) { | ||
if (pendingTasks == null) { | ||
pendingTasks = new ArrayList<>(tasks.size() + 1); | ||
} | ||
pendingTasks.add(task); | ||
} | ||
if (pendingTasks != null) { | ||
doTasks(pendingTasks); | ||
} | ||
} | ||
|
||
protected abstract void doTasks(List<T> tasks); | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
...mis-commons/src/main/java/org/apache/activemq/artemis/utils/actors/TaskProcessorBase.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,44 @@ | ||
/* | ||
* 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.actors; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* this is for any ProcessorBase that will execute one item at a time. | ||
* processQueue will take one element each time and call doTask. | ||
*/ | ||
public abstract class TaskProcessorBase<T> extends ProcessorBase<T> { | ||
|
||
public TaskProcessorBase(Executor parent) { | ||
super(parent); | ||
} | ||
|
||
@Override | ||
protected void processQueue() { | ||
T task; | ||
//while the queue is not empty we process in order: | ||
//if requestedForcedShutdown==true than no new tasks will be drained from the tasks q. | ||
while (!yielded && !requestedForcedShutdown && (task = tasks.poll()) != null) { | ||
doTask(task); | ||
} | ||
} | ||
|
||
protected abstract void doTask(T task); | ||
|
||
} |
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
112 changes: 112 additions & 0 deletions
112
...s-commons/src/test/java/org/apache/activemq/artemis/utils/actors/GroupedExecutorTest.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,112 @@ | ||
/* | ||
* 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.actors; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.List; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.activemq.artemis.utils.ReusableLatch; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class GroupedExecutorTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
class GroupedStrings extends GroupingTasksProcessorBase<String> { | ||
|
||
Runnable left; | ||
|
||
public GroupedStrings(Executor parent, Runnable left) { | ||
super(parent); | ||
this.left = left; | ||
} | ||
|
||
List<String> lastExecution = null; | ||
|
||
@Override | ||
protected void doTasks(List<String> tasks) { | ||
lastExecution = tasks; | ||
} | ||
|
||
final Semaphore semaphore = new Semaphore(0); | ||
|
||
@Override | ||
protected void enter() { | ||
super.enter(); | ||
try { | ||
semaphore.acquire(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
protected void leave() { | ||
super.leave(); | ||
left.run(); | ||
} | ||
|
||
public void release() { | ||
semaphore.release(); | ||
} | ||
|
||
public void groupString(String value) { | ||
addTask(value); | ||
} | ||
} | ||
|
||
@Test | ||
public void groupStrings() throws InterruptedException { | ||
final int strings = 1000; | ||
final ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
try { | ||
ReusableLatch latchLeft = new ReusableLatch(1); | ||
final GroupedStrings groupedStrings = new GroupedStrings(executorService, latchLeft::countDown); | ||
|
||
for (int i = 0; i < strings; i++) { | ||
groupedStrings.groupString("String " + i); | ||
} | ||
groupedStrings.release(); | ||
assertTrue(latchLeft.await(10, TimeUnit.SECONDS)); | ||
assertNotNull(groupedStrings.lastExecution); | ||
assertEquals(strings, groupedStrings.lastExecution.size()); | ||
for (int i = 0; i < strings; i++) { | ||
assertEquals("String " + i, groupedStrings.lastExecution.get(i)); | ||
} | ||
latchLeft.setCount(1); | ||
groupedStrings.release(); | ||
assertTrue(latchLeft.await(10, TimeUnit.SECONDS)); | ||
assertNotNull(groupedStrings.lastExecution); | ||
// no executions, should still be the last one | ||
assertEquals(strings, groupedStrings.lastExecution.size()); | ||
} finally { | ||
executorService.shutdownNow(); | ||
} | ||
} | ||
|
||
} |
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.