-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BP-62] Bookie server introduces the BatchedReadEntryProcessor to han…
…dle batch read request (#4187) Descriptions of the changes in this PR: This is the second PR for the batch read(#4051) feature. Bookie server introduces the BatchedReadEntryProcessor to handle batch read request
- Loading branch information
Showing
7 changed files
with
399 additions
and
15 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
109 changes: 109 additions & 0 deletions
109
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BatchedReadEntryProcessor.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,109 @@ | ||
/* | ||
* | ||
* 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.bookkeeper.proto; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.util.Recycler; | ||
import io.netty.util.ReferenceCounted; | ||
import java.util.concurrent.ExecutorService; | ||
import org.apache.bookkeeper.proto.BookieProtocol.BatchedReadRequest; | ||
import org.apache.bookkeeper.util.ByteBufList; | ||
|
||
public class BatchedReadEntryProcessor extends ReadEntryProcessor { | ||
|
||
private long maxBatchReadSize; | ||
|
||
public static BatchedReadEntryProcessor create(BatchedReadRequest request, | ||
BookieRequestHandler requestHandler, | ||
BookieRequestProcessor requestProcessor, | ||
ExecutorService fenceThreadPool, | ||
boolean throttleReadResponses, | ||
long maxBatchReadSize) { | ||
BatchedReadEntryProcessor rep = RECYCLER.get(); | ||
rep.init(request, requestHandler, requestProcessor); | ||
rep.fenceThreadPool = fenceThreadPool; | ||
rep.throttleReadResponses = throttleReadResponses; | ||
rep.maxBatchReadSize = maxBatchReadSize; | ||
requestProcessor.onReadRequestStart(requestHandler.ctx().channel()); | ||
return rep; | ||
} | ||
|
||
@Override | ||
protected ReferenceCounted readData() throws Exception { | ||
ByteBufList data = null; | ||
BatchedReadRequest batchRequest = (BatchedReadRequest) request; | ||
int maxCount = batchRequest.getMaxCount(); | ||
if (maxCount <= 0) { | ||
maxCount = Integer.MAX_VALUE; | ||
} | ||
long maxSize = Math.min(batchRequest.getMaxSize(), maxBatchReadSize); | ||
//See BookieProtoEncoding.ResponseEnDeCoderPreV3#encode on BatchedReadResponse case. | ||
long frameSize = 24 + 8 + 4; | ||
for (int i = 0; i < maxCount; i++) { | ||
try { | ||
ByteBuf entry = requestProcessor.getBookie().readEntry(request.getLedgerId(), request.getEntryId() + i); | ||
frameSize += entry.readableBytes() + 4; | ||
if (data == null) { | ||
data = ByteBufList.get(entry); | ||
} else { | ||
if (frameSize > maxSize) { | ||
entry.release(); | ||
break; | ||
} | ||
data.add(entry); | ||
} | ||
} catch (Throwable e) { | ||
if (data == null) { | ||
throw e; | ||
} | ||
break; | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
@Override | ||
protected BookieProtocol.Response buildReadResponse(ReferenceCounted data) { | ||
return ResponseBuilder.buildBatchedReadResponse((ByteBufList) data, (BatchedReadRequest) request); | ||
} | ||
|
||
protected void recycle() { | ||
request.recycle(); | ||
super.reset(); | ||
if (this.recyclerHandle != null) { | ||
this.recyclerHandle.recycle(this); | ||
} | ||
} | ||
|
||
private final Recycler.Handle<BatchedReadEntryProcessor> recyclerHandle; | ||
|
||
private BatchedReadEntryProcessor(Recycler.Handle<BatchedReadEntryProcessor> recyclerHandle) { | ||
this.recyclerHandle = recyclerHandle; | ||
} | ||
|
||
private static final Recycler<BatchedReadEntryProcessor> RECYCLER = new Recycler<BatchedReadEntryProcessor>() { | ||
@Override | ||
protected BatchedReadEntryProcessor newObject(Recycler.Handle<BatchedReadEntryProcessor> handle) { | ||
return new BatchedReadEntryProcessor(handle); | ||
} | ||
}; | ||
|
||
} |
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
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.