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

Timeout causes error #547

Closed
dhakehurst opened this issue Jun 3, 2024 · 6 comments
Closed

Timeout causes error #547

dhakehurst opened this issue Jun 3, 2024 · 6 comments

Comments

@dhakehurst
Copy link

I'm using version 5.1.1.Final

I find that if I set 'timeouts' the second of the requests in the following code fails.
I cannot see why ?
With no timeout (which defaults to -1) it works fine,
except on the occasion that the server does not respond - hence I want to use a timeout.

However, adding the timeout(s), of any value, causes a socket read-timeout on the second request ('query.submit')

javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: Read timed out
	at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:531)
	at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:300)
	at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:662)
...
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.base/sun.nio.ch.NioSocketImpl.timedRead(NioSocketImpl.java:278)
	at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:304)
// Use HttpClient instead of the default HttpUrlConnection
final ClientConfig clientConfig = new ClientConfig().connectorProvider(new ApacheConnectorProvider());

// Fixes Invalid cookie header: ... Invalid 'expires' attribute: Thu, 01 Dec 1994 16:00:00 GMT
final var reqCfg = RequestConfig.custom() //
			.setCookieSpec(CookieSpecs.STANDARD) //
			.setConnectionRequestTimeout(timeout_ms.orElse(-1)) //
			.setConnectTimeout(timeout_ms.orElse(-1)) //
			.setSocketTimeout(timeout_ms.orElse(-1)) //
			.build();
clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, reqCfg);
clientConfig.register(MultiPartFeature.class);

final ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.withConfig(clientConfig);
// IBM jazz-apps use JEE Form based authentication
clientBuilder.register(new JEEFormAuthenticator(baseUrl.toString(), userName, password));
final var oslcClient = new OslcClient(clientBuilder);

final var rootServicesUri = baseUrl + "/rootservices";
final Response response = oslcClient.getResource(rootServicesUri.toString(), OSLCConstants.CT_RDF);
final InputStream is = response.readEntity(InputStream.class);
final var rootServices = ModelFactory.createDefaultModel();
rootServices.read(is, rootServicesUri.toString());
final Property prop = rootServices.createProperty(OSLCConstants.OSLC_RM, RootServicesConstants.RM_ROOTSERVICES_CATALOG_PROP);
final Statement stmt = rootServices.getProperty((Resource) null, prop);
final var rmServiceCatalogUrl = stmt.getObject().toString();

final String serviceProviderUrl = oslcClient.lookupServiceProviderUrl(rmServiceCatalogUrl, projectName);
final String queryCapabilityUrl = oslcClient.lookupQueryCapability(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
final OslcQueryParameters queryParams = new OslcQueryParameters();
queryParams.setSelect("*");
final OslcQuery query = new OslcQuery(oslcClient, queryCapabilityUrl, queryParams);

final OslcQueryResult result = query.submit();
System.out.println(result.getRawResponse().readEntity(String.class));
@berezovskyi
Copy link
Contributor

@Jad-el-khoury have you encountered something similar?

@Jad-el-khoury
Copy link
Contributor

Does not look familiar to me, no.

@berezovskyi berezovskyi added the Type: Bug Something isn't working label Nov 11, 2024
@berezovskyi
Copy link
Contributor

@dhakehurst I am guess some (not completely correct) client instance reuse is happening. Did you manage to track down the origin of the defect? We will be glad to merge your fix. Apologies for not noticing your report earlier.

@berezovskyi
Copy link
Contributor

berezovskyi commented Nov 11, 2024

@Jad-el-khoury @dhakehurst I just checked and indeed we were using the builder incorrectly - we created a single instance of a client and persisted it. We should instead create a new client for every request. I think I was wary of the resource overhead and introduced this defect in a premature optimisation attempt. Here are the proposed changes:

  1. Store the builder in a field without building a client in the ctor.
  2. Remove getClient() method and introduce buildClient() - @Jad-el-khoury, how much of your code would it break?
  3. Call buildClient() in OslcClient.getWebResource()
  4. Call buildClient() in OslcClient.doRequest()
  5. Call buildClient() in OslcOAuthClient.performOAuthNegotiationInternal()
  6. Call buildClient() in OslcOAuthClient.getResource()

I don't have much time to make the PR and do testing (esp. around OAuth and cookie reuse) - happy to accept a PR.

@berezovskyi
Copy link
Contributor

berezovskyi commented Nov 12, 2024

I checked again and https://jakartaee.github.io/rest/apidocs/3.0.0/jakarta/ws/rs/client/Client.html does not recommend instantiating many Client instances, so in that regard we did not make a mistake - disregard my prior comment.

Will need a more in-depth look.

@berezovskyi
Copy link
Contributor

OSLC/lyo-samples#170 - verified on a real Jazz server to work fine https://github.com/OSLC/lyo-samples/actions/runs/11806371590/job/32890826618

Please observe that https://hc.apache.org/httpcomponents-client-5.4.x/current/httpclient5/apidocs/org/apache/hc/client5/http/config/RequestConfig.Builder.html#setConnectTimeout-org.apache.hc.core5.util.Timeout- is deprecated and the socket timeout was removed altogether from newer versions of the Apache client. Because we are using JAX-RS client facilities, we should be using their way of setting timeouts: https://jakartaee.github.io/rest/apidocs/3.0.0/jakarta/ws/rs/client/ClientBuilder.html#connectTimeout(long,java.util.concurrent.TimeUnit)

Please reopen the issue if you can still reproduce an error.

@berezovskyi berezovskyi removed the Type: Bug Something isn't working label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants