-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[Job](Fix)Improve Event Publishing with Timeout #45103
Changes from all commits
cffeac8
38b7220
5623459
7ddc38b
5636544
d224314
005b74a
68a38ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ | |
|
||
import com.lmax.disruptor.EventFactory; | ||
import com.lmax.disruptor.EventTranslatorVararg; | ||
import com.lmax.disruptor.RingBuffer; | ||
import com.lmax.disruptor.WaitStrategy; | ||
import com.lmax.disruptor.WorkHandler; | ||
import com.lmax.disruptor.dsl.Disruptor; | ||
|
@@ -28,6 +27,7 @@ | |
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Utility class for creating and managing a Disruptor instance. | ||
|
@@ -73,20 +73,42 @@ public void start() { | |
*/ | ||
public boolean publishEvent(Object... args) { | ||
try { | ||
RingBuffer<T> ringBuffer = disruptor.getRingBuffer(); | ||
// Check if the RingBuffer has enough capacity to reserve 10 slots for tasks | ||
// If there is insufficient capacity (less than 10 slots available) | ||
// log a warning and drop the current task | ||
if (!ringBuffer.hasAvailableCapacity(10)) { | ||
LOG.warn("ring buffer has no available capacity,task will be dropped," | ||
+ "please check the task queue size."); | ||
return false; | ||
// Set the timeout to 1 second, converted to nanoseconds for precision | ||
long timeoutInNanos = TimeUnit.SECONDS.toNanos(1); // Timeout set to 1 second | ||
long startTime = System.nanoTime(); // Record the start time | ||
|
||
// Loop until the timeout is reached | ||
while (System.nanoTime() - startTime < timeoutInNanos) { | ||
// Check if there is enough remaining capacity in the ring buffer | ||
// Adjusting to check if the required capacity is available (instead of hardcoding 1) | ||
if (disruptor.getRingBuffer().remainingCapacity() > 1) { | ||
// Publish the event if there is enough capacity | ||
disruptor.getRingBuffer().publishEvent(eventTranslator, args); | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("publishEvent success,the remaining buffer size is {}", | ||
disruptor.getRingBuffer().remainingCapacity()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of 'remainingCapacity'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simply to add debug logs. |
||
} | ||
return true; | ||
} | ||
|
||
// Wait for a short period before retrying | ||
try { | ||
Thread.sleep(10); // Adjust the wait time as needed (maybe increase if not high-frequency) | ||
} catch (InterruptedException e) { | ||
// Log the exception and return false if interrupted | ||
Thread.currentThread().interrupt(); // Restore interrupt status | ||
LOG.warn("Thread interrupted while waiting to publish event", e); | ||
return false; | ||
} | ||
} | ||
ringBuffer.publishEvent(eventTranslator, args); | ||
return true; | ||
|
||
// Timeout reached without publishing the event | ||
LOG.warn("Failed to publish event within the specified timeout (1 second)." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In log, it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes...done |
||
+ "Queue may be full. the remaining buffer size is {}", | ||
disruptor.getRingBuffer().remainingCapacity()); | ||
} catch (Exception e) { | ||
LOG.warn("Failed to publish event", e); | ||
// Handle the exception, e.g., retry or alert | ||
// Catching general exceptions to handle unexpected errors | ||
LOG.warn("Failed to publish event due to an unexpected error", e); | ||
} | ||
return false; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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.doris.job.executor; | ||
|
||
import org.apache.doris.job.task.AbstractTask; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
|
||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Log4j2 | ||
public class TaskProcessor { | ||
private ExecutorService executor; | ||
|
||
public TaskProcessor(int numberOfThreads, int queueSize, ThreadFactory threadFactory) { | ||
this.executor = new ThreadPoolExecutor( | ||
numberOfThreads, | ||
numberOfThreads, | ||
0L, TimeUnit.MILLISECONDS, | ||
new ArrayBlockingQueue<>(queueSize), | ||
threadFactory, | ||
new ThreadPoolExecutor.AbortPolicy() | ||
); | ||
} | ||
|
||
public boolean addTask(AbstractTask task) { | ||
try { | ||
executor.execute(() -> runTask(task)); | ||
log.info("Add task to executor, task id: {}", task.getTaskId()); | ||
return true; | ||
} catch (RejectedExecutionException e) { | ||
log.warn("Failed to add task to executor, task id: {}", task.getTaskId(), e); | ||
return false; | ||
} | ||
} | ||
|
||
public void shutdown() { | ||
log.info("Shutting down executor service..."); | ||
executor.shutdown(); | ||
try { | ||
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) { | ||
executor.shutdownNow(); | ||
} | ||
} catch (InterruptedException e) { | ||
executor.shutdownNow(); | ||
Thread.currentThread().interrupt(); | ||
} | ||
log.info("Executor service shut down successfully."); | ||
} | ||
|
||
private void runTask(AbstractTask task) { | ||
try { | ||
if (task == null) { | ||
log.warn("Task is null, ignore. Maybe it has been canceled."); | ||
return; | ||
} | ||
if (task.isCancelled()) { | ||
log.info("Task is canceled, ignore. Task id: {}", task.getTaskId()); | ||
return; | ||
} | ||
log.info("Start to execute task, task id: {}", task.getTaskId()); | ||
task.runTask(); | ||
} catch (Exception e) { | ||
log.warn("Execute task error, task id: {}", task.getTaskId(), e); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be concurrency? When making a judgment, it is greater than 1, and when publishing, there is no capacity left
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, only the time wheel is in use, and since the time wheel itself is single-threaded, there are no issues.