Skip to content

Commit f5f0bb7

Browse files
[pos][native] Use okhttp instead of airlift.http in pos native
1 parent 16e42ab commit f5f0bb7

27 files changed

+1137
-1328
lines changed

presto-spark-base/pom.xml

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<air.main.basedir>${project.parent.basedir}</air.main.basedir>
1515
<air.check.skip-modernizer>true</air.check.skip-modernizer>
1616
<dep.jetty.version>9.4.55.v20240627</dep.jetty.version>
17+
<dep.okhttp.version>4.12.0</dep.okhttp.version>
18+
<dep.okio-jvm.version>3.9.1</dep.okio-jvm.version>
1719
</properties>
1820

1921
<dependencyManagement>
@@ -26,16 +28,6 @@
2628
<type>pom</type>
2729
<scope>import</scope>
2830
</dependency>
29-
<dependency>
30-
<groupId>com.facebook.airlift</groupId>
31-
<artifactId>http-client</artifactId>
32-
<version>0.216</version>
33-
</dependency>
34-
<dependency>
35-
<groupId>com.facebook.airlift</groupId>
36-
<artifactId>http-server</artifactId>
37-
<version>0.216</version>
38-
</dependency>
3931
<dependency>
4032
<groupId>com.facebook.airlift</groupId>
4133
<artifactId>event</artifactId>
@@ -51,16 +43,6 @@
5143
<artifactId>drift-codec</artifactId>
5244
<version>1.46</version>
5345
</dependency>
54-
<dependency>
55-
<groupId>com.facebook.drift</groupId>
56-
<artifactId>drift-client</artifactId>
57-
<version>1.46</version>
58-
</dependency>
59-
<dependency>
60-
<groupId>com.facebook.drift</groupId>
61-
<artifactId>drift-transport-spi</artifactId>
62-
<version>1.46</version>
63-
</dependency>
6446
</dependencies>
6547
</dependencyManagement>
6648

@@ -132,15 +114,6 @@
132114
</exclusions>
133115
</dependency>
134116

135-
<dependency>
136-
<groupId>com.facebook.drift</groupId>
137-
<artifactId>drift-transport-spi</artifactId>
138-
</dependency>
139-
140-
<dependency>
141-
<groupId>com.facebook.drift</groupId>
142-
<artifactId>drift-client</artifactId>
143-
</dependency>
144117
<dependency>
145118
<groupId>com.facebook.presto</groupId>
146119
<artifactId>presto-expressions</artifactId>
@@ -177,20 +150,15 @@
177150
</dependency>
178151

179152
<dependency>
180-
<groupId>com.facebook.airlift</groupId>
181-
<artifactId>http-client</artifactId>
182-
</dependency>
183-
184-
<dependency>
185-
<groupId>com.facebook.airlift</groupId>
186-
<artifactId>http-server</artifactId>
187-
<scope>provided</scope>
153+
<groupId>com.squareup.okhttp3</groupId>
154+
<artifactId>okhttp</artifactId>
155+
<version>${dep.okhttp.version}</version>
188156
</dependency>
189157

190158
<dependency>
191-
<groupId>com.facebook.airlift</groupId>
192-
<artifactId>event</artifactId>
193-
<scope>provided</scope>
159+
<groupId>com.squareup.okio</groupId>
160+
<artifactId>okio-jvm</artifactId>
161+
<version>${dep.okio-jvm.version}</version>
194162
</dependency>
195163

196164
<dependency>
@@ -295,6 +263,10 @@
295263
<artifactId>commons-text</artifactId>
296264
</dependency>
297265

266+
<dependency>
267+
<groupId>org.apache.httpcomponents</groupId>
268+
<artifactId>httpcore</artifactId>
269+
</dependency>
298270
<!-- needed by tests at runtime -->
299271
<dependency>
300272
<groupId>javax.ws.rs</groupId>
@@ -350,6 +322,10 @@
350322
<artifactId>presto-hive</artifactId>
351323
<scope>test</scope>
352324
<exclusions>
325+
<exclusion>
326+
<groupId>com.facebook.airlift</groupId>
327+
<artifactId>http-client</artifactId>
328+
</exclusion>
353329
<exclusion>
354330
<groupId>org.xerial.snappy</groupId>
355331
<artifactId>snappy-java</artifactId>
@@ -382,6 +358,14 @@
382358
<artifactId>presto-tests</artifactId>
383359
<scope>test</scope>
384360
<exclusions>
361+
<exclusion>
362+
<groupId>com.facebook.airlift</groupId>
363+
<artifactId>http-client</artifactId>
364+
</exclusion>
365+
<exclusion>
366+
<groupId>com.facebook.airlift</groupId>
367+
<artifactId>http-server</artifactId>
368+
</exclusion>
385369
<exclusion>
386370
<groupId>javax.inject</groupId>
387371
<artifactId>javax.inject</artifactId>
@@ -410,6 +394,10 @@
410394
<artifactId>presto-function-namespace-managers</artifactId>
411395
<scope>test</scope>
412396
<exclusions>
397+
<exclusion>
398+
<groupId>com.facebook.airlift</groupId>
399+
<artifactId>http-client</artifactId>
400+
</exclusion>
413401
<exclusion>
414402
<groupId>com.google.protobuf</groupId>
415403
<artifactId>protobuf-java</artifactId>
@@ -466,6 +454,7 @@
466454
<ignoredNonTestScopedDependency>com.facebook.presto:presto-expressions</ignoredNonTestScopedDependency>
467455
<ignoredNonTestScopedDependency>it.unimi.dsi:fastutil</ignoredNonTestScopedDependency>
468456
<ignoredNonTestScopedDependency>com.facebook.airlift:log-manager</ignoredNonTestScopedDependency>
457+
<ignoredNonTestScopedDependency>com.squareup.okio:okio-jvm</ignoredNonTestScopedDependency>
469458
</ignoredNonTestScopedDependencies>
470459
<ignoredUnusedDeclaredDependencies>
471460
<ignoredUnusedDeclaredDependency>javax.ws.rs:javax.ws.rs-api</ignoredUnusedDeclaredDependency>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.spark.execution.http;
15+
16+
import com.facebook.airlift.json.JsonCodec;
17+
import com.facebook.presto.spark.execution.http.server.smile.BaseResponse;
18+
import com.google.common.collect.ImmutableListMultimap;
19+
import com.google.common.collect.ListMultimap;
20+
import okhttp3.Response;
21+
22+
import java.io.IOException;
23+
import java.util.List;
24+
25+
import static java.util.Objects.requireNonNull;
26+
27+
/**
28+
* Adapter class that bridges OkHttp Response to BaseResponse interface
29+
*/
30+
public class OkHttpBaseResponse<T>
31+
implements BaseResponse<T>
32+
{
33+
private final int statusCode;
34+
private final ListMultimap<OkHttpHeaderName, String> headers;
35+
private final T value;
36+
private final byte[] responseBytes;
37+
private final Exception exception;
38+
private final boolean hasValue;
39+
40+
public OkHttpBaseResponse(Response response, JsonCodec<T> codec)
41+
throws IOException
42+
{
43+
requireNonNull(response, "response is null");
44+
requireNonNull(codec, "codec is null");
45+
46+
this.statusCode = response.code();
47+
this.headers = buildHeaders(response);
48+
49+
// Calculate values first
50+
T tempValue = null;
51+
boolean tempHasValue = false;
52+
Exception tempException = null;
53+
byte[] tempResponseBytes;
54+
55+
if (response.body() != null) {
56+
tempResponseBytes = response.body().bytes();
57+
if (statusCode == 200 && tempResponseBytes.length > 0) {
58+
try {
59+
tempValue = codec.fromBytes(tempResponseBytes);
60+
tempHasValue = true;
61+
tempException = null;
62+
}
63+
catch (Exception e) {
64+
tempValue = null;
65+
tempHasValue = false;
66+
tempException = e;
67+
}
68+
}
69+
else {
70+
tempValue = null;
71+
tempHasValue = false;
72+
tempException = null;
73+
}
74+
}
75+
else {
76+
tempResponseBytes = new byte[0];
77+
tempValue = null;
78+
tempHasValue = false;
79+
tempException = null;
80+
}
81+
82+
// Assign to final fields only once
83+
this.responseBytes = tempResponseBytes;
84+
this.value = tempValue;
85+
this.hasValue = tempHasValue;
86+
this.exception = tempException;
87+
}
88+
89+
private static ListMultimap<OkHttpHeaderName, String> buildHeaders(Response response)
90+
{
91+
ImmutableListMultimap.Builder<OkHttpHeaderName, String> builder = ImmutableListMultimap.builder();
92+
93+
for (String name : response.headers().names()) {
94+
for (String value : response.headers().values(name)) {
95+
builder.put(OkHttpHeaderName.of(name), value);
96+
}
97+
}
98+
99+
return builder.build();
100+
}
101+
102+
@Override
103+
public int getStatusCode()
104+
{
105+
return statusCode;
106+
}
107+
108+
@Override
109+
public String getHeader(String name)
110+
{
111+
List<String> values = getHeaders(name);
112+
return values.isEmpty() ? null : values.get(0);
113+
}
114+
115+
@Override
116+
public List<String> getHeaders(String name)
117+
{
118+
return headers.get(OkHttpHeaderName.of(name));
119+
}
120+
121+
@Override
122+
public ListMultimap<OkHttpHeaderName, String> getHeaders()
123+
{
124+
return headers;
125+
}
126+
127+
@Override
128+
public boolean hasValue()
129+
{
130+
return hasValue;
131+
}
132+
133+
@Override
134+
public T getValue()
135+
{
136+
return value;
137+
}
138+
139+
@Override
140+
public int getResponseSize()
141+
{
142+
return responseBytes.length;
143+
}
144+
145+
@Override
146+
public byte[] getResponseBytes()
147+
{
148+
return responseBytes;
149+
}
150+
151+
@Override
152+
public Exception getException()
153+
{
154+
return exception;
155+
}
156+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.spark.execution.http;
15+
16+
import java.util.Objects;
17+
18+
/**
19+
* A replacement for airlift HeaderName that works with OkHttp
20+
*/
21+
public class OkHttpHeaderName
22+
{
23+
private final String name;
24+
25+
private OkHttpHeaderName(String name)
26+
{
27+
this.name = name.toLowerCase();
28+
}
29+
30+
public static OkHttpHeaderName of(String name)
31+
{
32+
return new OkHttpHeaderName(name);
33+
}
34+
35+
@Override
36+
public String toString()
37+
{
38+
return name;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o)
43+
{
44+
if (this == o) {
45+
return true;
46+
}
47+
if (o == null || getClass() != o.getClass()) {
48+
return false;
49+
}
50+
OkHttpHeaderName that = (OkHttpHeaderName) o;
51+
return Objects.equals(name, that.name);
52+
}
53+
54+
@Override
55+
public int hashCode()
56+
{
57+
return Objects.hash(name);
58+
}
59+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.spark.execution.http;
15+
16+
import com.facebook.presto.spark.execution.http.server.smile.BaseResponse;
17+
import okhttp3.Request;
18+
import okhttp3.Response;
19+
20+
import java.io.IOException;
21+
22+
public interface OkHttpResponseHandler<T>
23+
{
24+
BaseResponse<T> handle(Request request, Response response) throws IOException;
25+
}

0 commit comments

Comments
 (0)