-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]> Signed-off-by: Andriy Redko <[email protected]> Signed-off-by: Andriy Redko <[email protected]>
- Loading branch information
Showing
5 changed files
with
150 additions
and
4 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
62 changes: 62 additions & 0 deletions
62
...transport-netty4/src/main/java/org/opensearch/transport/Netty4NioServerSocketChannel.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,62 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.transport; | ||
|
||
import io.netty.channel.socket.InternetProtocolFamily; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.util.internal.SocketUtils; | ||
import io.netty.util.internal.logging.InternalLogger; | ||
import io.netty.util.internal.logging.InternalLoggerFactory; | ||
|
||
import java.nio.channels.ServerSocketChannel; | ||
import java.nio.channels.SocketChannel; | ||
import java.nio.channels.spi.SelectorProvider; | ||
import java.util.List; | ||
|
||
public class Netty4NioServerSocketChannel extends NioServerSocketChannel { | ||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Netty4NioServerSocketChannel.class); | ||
|
||
public Netty4NioServerSocketChannel() { | ||
super(); | ||
} | ||
|
||
public Netty4NioServerSocketChannel(SelectorProvider provider) { | ||
super(provider); | ||
} | ||
|
||
public Netty4NioServerSocketChannel(SelectorProvider provider, InternetProtocolFamily family) { | ||
super(provider, family); | ||
} | ||
|
||
public Netty4NioServerSocketChannel(ServerSocketChannel channel) { | ||
super(channel); | ||
} | ||
|
||
@Override | ||
protected int doReadMessages(List<Object> buf) throws Exception { | ||
SocketChannel ch = SocketUtils.accept(javaChannel()); | ||
|
||
try { | ||
if (ch != null) { | ||
buf.add(new Netty4NioSocketChannel(this, ch)); | ||
return 1; | ||
} | ||
} catch (Throwable t) { | ||
logger.warn("Failed to create a new channel from an accepted socket.", t); | ||
|
||
try { | ||
ch.close(); | ||
} catch (Throwable t2) { | ||
logger.warn("Failed to close a socket.", t2); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
server/src/test/java/org/opensearch/common/bytes/ByteBuffersBytesReferenceTests.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,79 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.common.bytes; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
||
import org.hamcrest.Matchers; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
public class ByteBuffersBytesReferenceTests extends AbstractBytesReferenceTestCase { | ||
@ParametersFactory | ||
public static Collection<Object[]> allocator() { | ||
return Arrays.asList( | ||
new Object[] { (Function<Integer, ByteBuffer>) ByteBuffer::allocateDirect }, | ||
new Object[] { (Function<Integer, ByteBuffer>) ByteBuffer::allocate } | ||
); | ||
} | ||
|
||
private final Function<Integer, ByteBuffer> allocator; | ||
|
||
public ByteBuffersBytesReferenceTests(Function<Integer, ByteBuffer> allocator) { | ||
this.allocator = allocator; | ||
} | ||
|
||
@Override | ||
protected BytesReference newBytesReference(int length) throws IOException { | ||
return newBytesReference(length, randomInt(length)); | ||
} | ||
|
||
@Override | ||
protected BytesReference newBytesReferenceWithOffsetOfZero(int length) throws IOException { | ||
return newBytesReference(length, 0); | ||
} | ||
|
||
private BytesReference newBytesReference(int length, int offset) throws IOException { | ||
// we know bytes stream output always creates a paged bytes reference, we use it to create randomized content | ||
final ByteBuffer buffer = allocator.apply(length + offset); | ||
for (int i = 0; i < length + offset; i++) { | ||
buffer.put((byte) random().nextInt(1 << 8)); | ||
} | ||
assertEquals(length + offset, buffer.limit()); | ||
buffer.flip().position(offset); | ||
|
||
BytesReference ref = BytesReference.fromByteBuffer(buffer); | ||
assertEquals(length, ref.length()); | ||
assertTrue(ref instanceof BytesArray); | ||
assertThat(ref.length(), Matchers.equalTo(length)); | ||
return ref; | ||
} | ||
|
||
public void testArray() throws IOException { | ||
int[] sizes = { 0, randomInt(PAGE_SIZE), PAGE_SIZE, randomIntBetween(2, PAGE_SIZE * randomIntBetween(2, 5)) }; | ||
|
||
for (int i = 0; i < sizes.length; i++) { | ||
BytesArray pbr = (BytesArray) newBytesReference(sizes[i]); | ||
byte[] array = pbr.array(); | ||
assertNotNull(array); | ||
assertEquals(sizes[i], array.length - pbr.offset()); | ||
assertSame(array, pbr.array()); | ||
} | ||
} | ||
|
||
public void testArrayOffset() throws IOException { | ||
int length = randomInt(PAGE_SIZE * randomIntBetween(2, 5)); | ||
BytesArray pbr = (BytesArray) newBytesReferenceWithOffsetOfZero(length); | ||
assertEquals(0, pbr.offset()); | ||
} | ||
} |