Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SliceExecutor and QueueSizeBasedExecutor #12285

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.lucene.search;

import java.util.Collection;
import java.util.concurrent.ThreadPoolExecutor;

/**
Expand All @@ -30,31 +29,15 @@ class QueueSizeBasedExecutor extends SliceExecutor {

private final ThreadPoolExecutor threadPoolExecutor;

public QueueSizeBasedExecutor(ThreadPoolExecutor threadPoolExecutor) {
QueueSizeBasedExecutor(ThreadPoolExecutor threadPoolExecutor) {
super(threadPoolExecutor);
this.threadPoolExecutor = threadPoolExecutor;
}

@Override
public void invokeAll(Collection<? extends Runnable> tasks) {
int i = 0;

for (Runnable task : tasks) {
boolean shouldExecuteOnCallerThread = false;

// Execute last task on caller thread
if (i == tasks.size() - 1) {
shouldExecuteOnCallerThread = true;
}

if (threadPoolExecutor.getQueue().size()
>= (threadPoolExecutor.getMaximumPoolSize() * LIMITING_FACTOR)) {
shouldExecuteOnCallerThread = true;
}

processTask(task, shouldExecuteOnCallerThread);

++i;
}
boolean shouldExecuteOnCallerThread(int index, int numTasks) {
return super.shouldExecuteOnCallerThread(index, numTasks)
|| threadPoolExecutor.getQueue().size()
>= (threadPoolExecutor.getMaximumPoolSize() * LIMITING_FACTOR);
}
}
57 changes: 17 additions & 40 deletions lucene/core/src/java/org/apache/lucene/search/SliceExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.lucene.search;

import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;

Expand All @@ -28,54 +29,30 @@
class SliceExecutor {
private final Executor executor;

public SliceExecutor(Executor executor) {
this.executor = executor;
SliceExecutor(Executor executor) {
this.executor = Objects.requireNonNull(executor, "Executor is null");
}

public void invokeAll(Collection<? extends Runnable> tasks) {

if (tasks == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need this null check for tasks any more? Maybe replace it with an assertion given this is all package private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is going to throw NPE two lines below once we loop through it, I am not sure I see value in checking that here, do you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm allergic to NPEs in general... But maybe that's not a problem now with nicer messages from later JDKs? Up to you :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see, I was thinking of replacing the if with Objects.requireNonNull which would still throw NPE, but you would have preferred IAE then I guess. Given that this is package private and we really can't ever have a null list, I would skip checking.

throw new IllegalArgumentException("Tasks is null");
}

if (executor == null) {
throw new IllegalArgumentException("Executor is null");
}

final void invokeAll(Collection<? extends Runnable> tasks) {
int i = 0;

for (Runnable task : tasks) {
boolean shouldExecuteOnCallerThread = false;

// Execute last task on caller thread
if (i == tasks.size() - 1) {
shouldExecuteOnCallerThread = true;
if (shouldExecuteOnCallerThread(i, tasks.size())) {
task.run();
} else {
try {
executor.execute(task);
} catch (
@SuppressWarnings("unused")
RejectedExecutionException e) {
task.run();
}
}

processTask(task, shouldExecuteOnCallerThread);
++i;
}
;
}

// Helper method to execute a single task
protected void processTask(final Runnable task, final boolean shouldExecuteOnCallerThread) {
if (task == null) {
throw new IllegalArgumentException("Input is null");
}

if (!shouldExecuteOnCallerThread) {
try {
executor.execute(task);

return;
} catch (
@SuppressWarnings("unused")
RejectedExecutionException e) {
// Execute on caller thread
}
}

task.run();
boolean shouldExecuteOnCallerThread(int index, int numTasks) {
// Execute last task on caller thread
return index == numTasks - 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,15 @@ private void runSliceExecutorTest(ThreadPoolExecutor service, boolean useRandomS
}
}

private class RandomBlockingSliceExecutor extends SliceExecutor {
private static class RandomBlockingSliceExecutor extends SliceExecutor {

public RandomBlockingSliceExecutor(Executor executor) {
RandomBlockingSliceExecutor(Executor executor) {
super(executor);
}

@Override
public void invokeAll(Collection<? extends Runnable> tasks) {

for (Runnable task : tasks) {
boolean shouldExecuteOnCallerThread = random().nextBoolean();

processTask(task, shouldExecuteOnCallerThread);
}
boolean shouldExecuteOnCallerThread(int index, int numTasks) {
return random().nextBoolean();
}
}
}