Skip to content

Commit 203d0e1

Browse files
authored
Merge branch 'main' into iot_metrics
2 parents a61572c + a49c06c commit 203d0e1

File tree

17 files changed

+1252
-27
lines changed

17 files changed

+1252
-27
lines changed

NOTICE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
AWS Crt Java
22
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
SPDX-License-Identifier: Apache-2.0.
4+
5+
** XXHash - https://xxhash.com/
6+
Copyright (c) 2012-2021 Yann Collet
7+
All rights reserved.
8+
9+
BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
10+
11+
Redistribution and use in source and binary forms, with or without modification,
12+
are permitted provided that the following conditions are met:
13+
14+
* Redistributions of source code must retain the above copyright notice, this
15+
list of conditions and the following disclaimer.
16+
17+
* Redistributions in binary form must reproduce the above copyright notice, this
18+
list of conditions and the following disclaimer in the documentation and/or
19+
other materials provided with the distribution.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,6 @@ To debug native code with VSCode or CLion or any other IDE:
298298
3. Set the parameters to be the ones used by the ```mvn``` script, as per above
299299
4. Set the working directory to the `aws-crt-java` directory
300300
5. On windows, you will need to manually load the PDB via the Modules window in Visual Studio, as it is not embedded in the JAR. It will be in the ```target/cmake-build/lib/windows/<arch>``` folder.
301+
302+
## Attribution
303+
This library exposes native XXHash implementation (https://github.com/Cyan4973/xxHash) through JNI interface.

crt/aws-c-io

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package software.amazon.awssdk.crt.checksums;
6+
import software.amazon.awssdk.crt.CrtResource;
7+
8+
public class XXHash extends CrtResource {
9+
10+
private XXHash(long nativeHandle) {
11+
acquireNativeHandle(nativeHandle);
12+
}
13+
14+
/**
15+
* Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
16+
* Resources that wait are responsible for calling releaseReferences() manually.
17+
*/
18+
@Override
19+
protected boolean canReleaseReferencesImmediately() { return true; }
20+
21+
/**
22+
* Releases the instance's reference to the underlying native key pair
23+
*/
24+
@Override
25+
protected void releaseNativeHandle() {
26+
if (!isNull()) {
27+
xxHashRelease(getNativeHandle());
28+
}
29+
}
30+
31+
/**
32+
* Create new streaming XXHash64.
33+
* @param seed seed to use for the hash
34+
* @return new XXHash instance
35+
*/
36+
static public XXHash newXXHash64(long seed) {
37+
long nativeHandle = xxHash64Create(seed);
38+
return new XXHash(nativeHandle);
39+
}
40+
41+
42+
/**
43+
* Create new streaming XXHash64.
44+
* @return new XXHash instance
45+
*/
46+
static public XXHash newXXHash64() {
47+
long nativeHandle = xxHash64Create(0);
48+
return new XXHash(nativeHandle);
49+
}
50+
51+
/**
52+
* Create new streaming XXHash3_64.
53+
* @param seed seed to use for the hash
54+
* @return new XXHash instance
55+
*/
56+
static public XXHash newXXHash3_64(long seed) {
57+
long nativeHandle = xxHash364Create(seed);
58+
return new XXHash(nativeHandle);
59+
}
60+
61+
/**
62+
* Create new streaming XXHash3_64.
63+
* @return new XXHash instance
64+
*/
65+
static public XXHash newXXHash3_64() {
66+
long nativeHandle = xxHash364Create(0);
67+
if (nativeHandle != 0) {
68+
return new XXHash(nativeHandle);
69+
}
70+
71+
return null;
72+
}
73+
74+
/**
75+
* Create new streaming XXHash3_128.
76+
* @param seed seed to use for the hash
77+
* @return new XXHash instance
78+
*/
79+
static public XXHash newXXHash3_128(long seed) {
80+
long nativeHandle = xxHash3128Create(seed);
81+
if (nativeHandle != 0) {
82+
return new XXHash(nativeHandle);
83+
}
84+
85+
return null;
86+
}
87+
88+
/**
89+
* Create new streaming XXHash3_128.
90+
* @return new XXHash instance
91+
*/
92+
static public XXHash newXXHash3_128() {
93+
long nativeHandle = xxHash3128Create(0);
94+
if (nativeHandle != 0) {
95+
return new XXHash(nativeHandle);
96+
}
97+
98+
return null;
99+
}
100+
101+
/**
102+
* Update xxhash state from input
103+
* @param input input to update with
104+
*/
105+
public void update(byte[] input) {
106+
xxHashUpdate(getNativeHandle(), input);
107+
}
108+
109+
/**
110+
* Return digest for the current state of hash.
111+
* @return hash as bytes in big endian
112+
*/
113+
public byte[] digest() {
114+
return xxHashFinalize(getNativeHandle());
115+
}
116+
117+
/**
118+
* Oneshot compute XXHash64.
119+
* @param input input input to hash
120+
* @param seed seed
121+
* @return xxhash64 hash
122+
*/
123+
static public byte[] computeXXHash64(byte[] input, long seed) {
124+
return xxHash64Compute(input, seed);
125+
}
126+
127+
/**
128+
* Oneshot compute XXHash64.
129+
* @param input input input to hash
130+
* @return xxhash64 hash
131+
*/
132+
static public byte[] computeXXHash64(byte[] input) {
133+
return xxHash64Compute(input, 0);
134+
}
135+
136+
137+
/**
138+
* Oneshot compute XXHash3_64.
139+
* @param input input input to hash
140+
* @param seed seed
141+
* @return xxhash64 hash
142+
*/
143+
static public byte[] computeXXHash3_64(byte[] input, long seed) {
144+
return xxHash364Compute(input, seed);
145+
}
146+
147+
/**
148+
* Oneshot compute XXHash3_64.
149+
* @param input input input to hash
150+
* @return xxhash64 hash
151+
*/
152+
static public byte[] computeXXHash3_64(byte[] input) {
153+
return xxHash364Compute(input, 0);
154+
}
155+
156+
/**
157+
* Oneshot compute XXHash3_128.
158+
* @param input input input to hash
159+
* @param seed seed
160+
* @return xxhash64 hash
161+
*/
162+
static public byte[] computeXXHash3_128(byte[] input, long seed) {
163+
return xxHash3128Compute(input, seed);
164+
}
165+
166+
/**
167+
* Oneshot compute XXHash3_128.
168+
* @param input input input to hash
169+
* @return xxhash64 hash
170+
*/
171+
static public byte[] computeXXHash3_128(byte[] input) {
172+
return xxHash3128Compute(input, 0);
173+
}
174+
175+
/*******************************************************************************
176+
* native methods
177+
******************************************************************************/
178+
private static native byte[] xxHash64Compute(byte[] input, long seed);
179+
private static native byte[] xxHash364Compute(byte[] input, long seed);
180+
private static native byte[] xxHash3128Compute(byte[] input, long seed);
181+
182+
private static native long xxHash64Create(long seed);
183+
private static native long xxHash364Create(long seed);
184+
private static native long xxHash3128Create(long seed);
185+
private static native void xxHashRelease(long xxhash);
186+
187+
private static native void xxHashUpdate(long xxhash, byte[] input);
188+
private static native byte[] xxHashFinalize(long xxhash);
189+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package software.amazon.awssdk.crt.http;
6+
7+
import software.amazon.awssdk.crt.CrtRuntimeException;
8+
9+
import java.util.concurrent.CompletableFuture;
10+
11+
/**
12+
* Manages a Pool of HTTP/1.1 Streams. Creates and manages HTTP/1.1 connections
13+
* under the hood. Will grab a connection from HttpClientConnectionManager to
14+
* make request on it, and will return it back until the request finishes.
15+
*/
16+
public class Http1StreamManager implements AutoCloseable {
17+
18+
private HttpClientConnectionManager connectionManager = null;
19+
20+
/**
21+
* Factory function for Http1StreamManager instances
22+
*
23+
* @param options the connection manager options configure to connection manager under the hood
24+
* @return a new instance of an Http1StreamManager
25+
*/
26+
public static Http1StreamManager create(HttpClientConnectionManagerOptions options) {
27+
return new Http1StreamManager(options);
28+
}
29+
30+
private Http1StreamManager(HttpClientConnectionManagerOptions options) {
31+
this.connectionManager = HttpClientConnectionManager.create(options);
32+
}
33+
34+
public CompletableFuture<Void> getShutdownCompleteFuture() {
35+
return this.connectionManager.getShutdownCompleteFuture();
36+
}
37+
38+
/**
39+
* Request an HTTP/1.1 HttpStream from StreamManager.
40+
*
41+
* @param request HttpRequest. The Request to make to the Server.
42+
* @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop
43+
* @return A future for a HttpStream that will be completed when the stream is
44+
* acquired.
45+
* @throws CrtRuntimeException Exception happens from acquiring stream.
46+
*/
47+
public CompletableFuture<HttpStream> acquireStream(HttpRequest request,
48+
HttpStreamBaseResponseHandler streamHandler) {
49+
return this.acquireStream((HttpRequestBase) request, streamHandler);
50+
}
51+
52+
/**
53+
* Request an HTTP/1.1 HttpStream from StreamManager.
54+
*
55+
* @param request HttpRequestBase. The Request to make to the Server.
56+
* @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop
57+
* @return A future for a HttpStream that will be completed when the stream is
58+
* acquired.
59+
* @throws CrtRuntimeException Exception happens from acquiring stream.
60+
*/
61+
public CompletableFuture<HttpStream> acquireStream(HttpRequestBase request,
62+
HttpStreamBaseResponseHandler streamHandler) {
63+
CompletableFuture<HttpStream> completionFuture = new CompletableFuture<>();
64+
HttpClientConnectionManager connManager = this.connectionManager;
65+
66+
connManager.acquireConnection().whenComplete((conn, throwable) -> {
67+
if (throwable != null) {
68+
completionFuture.completeExceptionally(throwable);
69+
} else {
70+
try {
71+
HttpStreamBase stream = conn.makeRequest(request, new HttpStreamBaseResponseHandler() {
72+
@Override
73+
public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType,
74+
HttpHeader[] nextHeaders) {
75+
streamHandler.onResponseHeaders(stream, responseStatusCode, blockType, nextHeaders);
76+
}
77+
78+
@Override
79+
public void onResponseHeadersDone(HttpStreamBase stream, int blockType) {
80+
streamHandler.onResponseHeadersDone(stream, blockType);
81+
}
82+
83+
@Override
84+
public int onResponseBody(HttpStreamBase stream, byte[] bodyBytesIn) {
85+
return streamHandler.onResponseBody(stream, bodyBytesIn);
86+
}
87+
88+
@Override
89+
public void onResponseComplete(HttpStreamBase stream, int errorCode) {
90+
streamHandler.onResponseComplete(stream, errorCode);
91+
/* Release the connection back */
92+
connManager.releaseConnection(conn);
93+
}
94+
});
95+
completionFuture.complete((HttpStream) stream);
96+
/* Active the stream for user */
97+
try {
98+
stream.activate();
99+
} catch (CrtRuntimeException e) {
100+
/* If activate failed, complete callback will not be invoked */
101+
streamHandler.onResponseComplete(stream, e.errorCode);
102+
/* Release the connection back */
103+
connManager.releaseConnection(conn);
104+
}
105+
} catch (Exception ex) {
106+
connManager.releaseConnection(conn);
107+
completionFuture.completeExceptionally(ex);
108+
}
109+
}
110+
});
111+
return completionFuture;
112+
}
113+
114+
/**
115+
* @return concurrency metrics for the current manager
116+
*/
117+
public HttpManagerMetrics getManagerMetrics() {
118+
return this.connectionManager.getManagerMetrics();
119+
}
120+
121+
/**
122+
* @return maximum number of connections this manager will pool
123+
*/
124+
public int getMaxConnections() {
125+
return this.connectionManager.getMaxConnections();
126+
}
127+
128+
@Override
129+
public void close() {
130+
this.connectionManager.close();
131+
}
132+
}

src/main/java/software/amazon/awssdk/crt/http/Http2StreamManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ private Http2StreamManager(Http2StreamManagerOptions options) {
117117
proxyAuthorizationPassword != null ? proxyAuthorizationPassword.getBytes(UTF8) : null,
118118
noProxyHosts != null ? noProxyHosts.getBytes(UTF8) : null,
119119
connectionManagerOptions.isManualWindowManagement(),
120+
connectionManagerOptions.getWindowSize(),
120121
monitoringThroughputThresholdInBytesPerSecond,
121122
monitoringFailureIntervalInSeconds,
122123
maxConnections,
@@ -158,9 +159,8 @@ public CompletableFuture<Http2Stream> acquireStream(HttpRequest request,
158159
return this.acquireStream((HttpRequestBase) request, streamHandler);
159160
}
160161

161-
private CompletableFuture<Http2Stream> acquireStream(HttpRequestBase request,
162-
HttpStreamBaseResponseHandler streamHandler) {
163-
162+
public CompletableFuture<Http2Stream> acquireStream(HttpRequestBase request,
163+
HttpStreamBaseResponseHandler streamHandler) {
164164
CompletableFuture<Http2Stream> completionFuture = new CompletableFuture<>();
165165
AsyncCallback acquireStreamCompleted = AsyncCallback.wrapFuture(completionFuture, null);
166166
if (isNull()) {
@@ -259,6 +259,7 @@ private static native long http2StreamManagerNew(Http2StreamManager thisObj,
259259
byte[] proxyAuthorizationPassword,
260260
byte[] noProxyHosts,
261261
boolean isManualWindowManagement,
262+
long windowSize,
262263
long monitoringThroughputThresholdInBytesPerSecond,
263264
int monitoringFailureIntervalInSeconds,
264265
int maxConns,

0 commit comments

Comments
 (0)