-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batch translog upload per x ms to allow high index throughput
Signed-off-by: Ashish Singh <ssashish@amazon.com>
- Loading branch information
Showing
4 changed files
with
154 additions
and
9 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
114 changes: 114 additions & 0 deletions
114
server/src/main/java/org/opensearch/common/util/concurrent/BufferedAsyncIOProcessor.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,114 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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. | ||
*/ | ||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.common.util.concurrent; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.collect.Tuple; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class BufferedAsyncIOProcessor<Item> extends AsyncIOProcessor<Item> { | ||
|
||
private final ThreadPool threadpool; | ||
private final TimeValue bufferInterval; | ||
|
||
protected BufferedAsyncIOProcessor( | ||
Logger logger, | ||
int queueSize, | ||
ThreadContext threadContext, | ||
ThreadPool threadpool, | ||
TimeValue bufferInterval | ||
) { | ||
super(logger, queueSize, threadContext); | ||
this.threadpool = threadpool; | ||
this.bufferInterval = bufferInterval; | ||
} | ||
|
||
@Override | ||
public void put(Item item, Consumer<Exception> listener) { | ||
Objects.requireNonNull(item, "item must not be null"); | ||
Objects.requireNonNull(listener, "listener must not be null"); | ||
|
||
try { | ||
long timeout = getPutBlockingTimeoutMillis(); | ||
if (timeout > 0) { | ||
if (!queue.offer(new Tuple<>(item, preserveContext(listener)), timeout, TimeUnit.MILLISECONDS)) { | ||
listener.accept(new OpenSearchRejectedExecutionException("failed to queue request, queue full")); | ||
} | ||
} else { | ||
queue.put(new Tuple<>(item, preserveContext(listener))); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
listener.accept(e); | ||
} | ||
|
||
scheduleRefresh(); | ||
} | ||
|
||
protected long getPutBlockingTimeoutMillis() { | ||
return -1L; | ||
} | ||
|
||
private void scheduleRefresh() { | ||
Runnable processor = () -> { | ||
final List<Tuple<Item, Consumer<Exception>>> candidates = new ArrayList<>(); | ||
// since we made the promise to process we gotta do it here at least once | ||
drainAndProcessAndRelease(candidates); | ||
while (queue.isEmpty() == false && promiseSemaphore.tryAcquire()) { | ||
// yet if the queue is not empty AND nobody else has yet made the promise to take over we continue processing | ||
drainAndProcessAndRelease(candidates); | ||
} | ||
}; | ||
|
||
if (promiseSemaphore.tryAcquire()) { | ||
try { | ||
threadpool.schedule(processor, this.bufferInterval, getBufferRefreshThreadPoolName()); | ||
} catch (Exception e) { | ||
logger.error("failed to schedule refresh"); | ||
promiseSemaphore.release(); | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
protected String getBufferRefreshThreadPoolName() { | ||
return ThreadPool.Names.TRANSLOG_SYNC; | ||
} | ||
|
||
} |
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