Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6529] Use persistent sessionContext in AvaticaCommonsHttpClie… #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class AvaticaCommonsHttpClientImpl implements AvaticaHttpClient, HttpClie
protected CredentialsProvider credentialsProvider = null;
protected Lookup<AuthSchemeFactory> authRegistry = null;
protected Object userToken;
protected HttpClientContext context;

public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
Expand All @@ -109,23 +110,22 @@ protected void initializeClient(PoolingHttpClientConnectionManager pool,
HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(pool)
.setDefaultRequestConfig(requestConfig);
this.client = httpClientBuilder.build();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried setting these directly on the client object ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet but i will try.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried setting these directly on the client object, but I think it would require quite a lot of refactoring. For example, practically the whole AvaticaHttpClientFactoryImpl class should be rewritten.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.
Maybe in a followup ticket.

this.context = HttpClientContext.create();
// Set the credentials if they were provided.
if (null != this.credentialsProvider) {
context.setCredentialsProvider(credentialsProvider);
context.setAuthSchemeRegistry(authRegistry);
context.setAuthCache(authCache);
}
if (null != userToken) {
context.setUserToken(userToken);
}

}

@Override public byte[] send(byte[] request) {
while (true) {
HttpClientContext context = HttpClientContext.create();

// Set the credentials if they were provided.
if (null != this.credentialsProvider) {
context.setCredentialsProvider(credentialsProvider);
context.setAuthSchemeRegistry(authRegistry);
context.setAuthCache(authCache);
}

if (null != userToken) {
context.setUserToken(userToken);
}

ByteArrayEntity entity = new ByteArrayEntity(request, ContentType.APPLICATION_OCTET_STREAM);

// Create the client with the AuthSchemeRegistry and manager
Expand Down Expand Up @@ -184,6 +184,8 @@ CloseableHttpResponse execute(HttpPost post, HttpClientContext context)
throw new IllegalArgumentException("Unsupported authentiation type: " + authType);
}
this.authRegistry = authRegistryBuilder.build();
context.setCredentialsProvider(credentialsProvider);
context.setAuthSchemeRegistry(authRegistry);
}

@Override public void setGSSCredential(GSSCredential credential) {
Expand All @@ -205,6 +207,8 @@ CloseableHttpResponse execute(HttpPost post, HttpClientContext context)
((BasicCredentialsProvider) this.credentialsProvider)
.setCredentials(anyAuthScope, EmptyCredentials.INSTANCE);
}
context.setCredentialsProvider(credentialsProvider);
context.setAuthSchemeRegistry(authRegistry);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@
package org.apache.calcite.avatica.remote;

import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ConnectionConfig;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.NoHttpResponseException;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;

import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.ByteArrayInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -64,9 +72,11 @@ public class AvaticaCommonsHttpClientImplTest {

final AvaticaCommonsHttpClientImpl client =
spy(new AvaticaCommonsHttpClientImpl(new URL("http://127.0.0.1")));
client.setHttpClientPool(mock(PoolingHttpClientConnectionManager.class), mock(
ConnectionConfig.class));

doAnswer(failThenSucceed).when(client)
.execute(any(HttpPost.class), any(HttpClientContext.class));
.execute(any(HttpPost.class), eq(client.context));

when(badResponse.getCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE);

Expand Down Expand Up @@ -96,9 +106,11 @@ public class AvaticaCommonsHttpClientImplTest {

final AvaticaCommonsHttpClientImpl client =
spy(new AvaticaCommonsHttpClientImpl(new URL("http://127.0.0.1")));
client.setHttpClientPool(mock(PoolingHttpClientConnectionManager.class), mock(
ConnectionConfig.class));

doAnswer(failThenSucceed).when(client)
.execute(any(HttpPost.class), any(HttpClientContext.class));
.execute(any(HttpPost.class), eq(client.context));

when(badResponse.getCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE);

Expand All @@ -109,6 +121,63 @@ public class AvaticaCommonsHttpClientImplTest {
assertEquals("success", AvaticaUtils.newStringUtf8(responseBytes));
}

@Test
public void testPersistentContextReusedAcrossRequests() throws Exception {
final AvaticaCommonsHttpClientImpl client =
spy(new AvaticaCommonsHttpClientImpl(new URL("http://127.0.0.1")));
client.setHttpClientPool(mock(PoolingHttpClientConnectionManager.class), mock(
ConnectionConfig.class));

CloseableHttpResponse response = mock(CloseableHttpResponse.class);
when(response.getCode()).thenReturn(HttpURLConnection.HTTP_OK);

ByteArrayEntity entity = mock(ByteArrayEntity.class);
when(entity.getContent()).thenReturn(new ByteArrayInputStream(new byte[0]));
when(response.getEntity()).thenReturn(entity);

doReturn(response).when(client)
.execute(any(HttpPost.class), eq(client.context));

client.send(new byte[0]);
client.send(new byte[0]);

// Verify that the persistent context was reused and not created again
verify(client, times(2)).execute(any(HttpPost.class),
eq(client.context));
}

@Test
public void testPersistentContextThreadSafety() throws Exception {
final AvaticaCommonsHttpClientImpl client =
spy(new AvaticaCommonsHttpClientImpl(new URL("http://127.0.0.1")));
client.setHttpClientPool(mock(PoolingHttpClientConnectionManager.class), mock(
ConnectionConfig.class));

doReturn(mock(CloseableHttpResponse.class)).when(client)
.execute(any(HttpPost.class), eq(client.context));

Runnable requestTask = () -> {
try {
client.send(new byte[0]);
} catch (Exception e) {
fail("Threaded request failed with exception: " + e.getMessage());
}
};

int threadCount = 5;
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread(requestTask);
threads[i].start();
}

for (Thread thread : threads) {
thread.join();
}

verify(client, times(threadCount)).execute(any(HttpPost.class), eq(client.context));
}

}

// End AvaticaCommonsHttpClientImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ private static void setupUsers(File keytabDir) throws KrbException {
// Passes the GSSCredential into the HTTP client implementation
final AvaticaCommonsHttpClientImpl httpClient =
new AvaticaCommonsHttpClientImpl(httpServerUrl);
httpClient.setGSSCredential(credential);
httpClient.setHttpClientPool(pool, config);
httpClient.setGSSCredential(credential);
stoty marked this conversation as resolved.
Show resolved Hide resolved

return httpClient.send(new byte[0]);
}
Expand Down