-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nio http server transport (#29587)
This commit is related to #28898. It adds an nio driven http server transport. Currently it only supports basic http features. Cors, pipeling, and read timeouts will need to be added in future PRs.
- Loading branch information
1 parent
6695d11
commit 99b9ab5
Showing
45 changed files
with
3,800 additions
and
731 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
47 changes: 47 additions & 0 deletions
47
libs/elasticsearch-nio/src/main/java/org/elasticsearch/nio/BytesWriteHandler.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,47 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.nio; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
public abstract class BytesWriteHandler implements ReadWriteHandler { | ||
|
||
private static final List<FlushOperation> EMPTY_LIST = Collections.emptyList(); | ||
|
||
public WriteOperation createWriteOperation(SocketChannelContext context, Object message, BiConsumer<Void, Throwable> listener) { | ||
assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + ByteBuffer[].class | ||
+ ". Found type: " + message.getClass() + "."; | ||
return new FlushReadyWrite(context, (ByteBuffer[]) message, listener); | ||
} | ||
|
||
public List<FlushOperation> writeToBytes(WriteOperation writeOperation) { | ||
assert writeOperation instanceof FlushReadyWrite : "Write operation must be flush ready"; | ||
return Collections.singletonList((FlushReadyWrite) writeOperation); | ||
} | ||
|
||
public List<FlushOperation> pollFlushOperations() { | ||
return EMPTY_LIST; | ||
} | ||
|
||
public void close() {} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
libs/elasticsearch-nio/src/main/java/org/elasticsearch/nio/FlushReadyWrite.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,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.nio; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.function.BiConsumer; | ||
|
||
public class FlushReadyWrite extends FlushOperation implements WriteOperation { | ||
|
||
private final SocketChannelContext channelContext; | ||
private final ByteBuffer[] buffers; | ||
|
||
FlushReadyWrite(SocketChannelContext channelContext, ByteBuffer[] buffers, BiConsumer<Void, Throwable> listener) { | ||
super(buffers, listener); | ||
this.channelContext = channelContext; | ||
this.buffers = buffers; | ||
} | ||
|
||
@Override | ||
public SocketChannelContext getChannel() { | ||
return channelContext; | ||
} | ||
|
||
@Override | ||
public ByteBuffer[] getObject() { | ||
return buffers; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
libs/elasticsearch-nio/src/main/java/org/elasticsearch/nio/ReadWriteHandler.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,71 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.nio; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Implements the application specific logic for handling inbound and outbound messages for a channel. | ||
*/ | ||
public interface ReadWriteHandler { | ||
|
||
/** | ||
* This method is called when a message is queued with a channel. It can be called from any thread. | ||
* This method should validate that the message is a valid type and return a write operation object | ||
* to be queued with the channel | ||
* | ||
* @param context the channel context | ||
* @param message the message | ||
* @param listener the listener to be called when the message is sent | ||
* @return the write operation to be queued | ||
*/ | ||
WriteOperation createWriteOperation(SocketChannelContext context, Object message, BiConsumer<Void, Throwable> listener); | ||
|
||
/** | ||
* This method is called on the event loop thread. It should serialize a write operation object to bytes | ||
* that can be flushed to the raw nio channel. | ||
* | ||
* @param writeOperation to be converted to bytes | ||
* @return the operations to flush the bytes to the channel | ||
*/ | ||
List<FlushOperation> writeToBytes(WriteOperation writeOperation); | ||
|
||
/** | ||
* Returns any flush operations that are ready to flush. This exists as a way to check if any flush | ||
* operations were produced during a read call. | ||
* | ||
* @return flush operations | ||
*/ | ||
List<FlushOperation> pollFlushOperations(); | ||
|
||
/** | ||
* This method handles bytes that have been read from the network. It should return the number of bytes | ||
* consumed so that they can be released. | ||
* | ||
* @param channelBuffer of bytes read from the network | ||
* @return the number of bytes consumed | ||
* @throws IOException if an exception occurs | ||
*/ | ||
int consumeReads(InboundChannelBuffer channelBuffer) throws IOException; | ||
|
||
void close() throws IOException; | ||
} |
Oops, something went wrong.