Skip to content

Commit

Permalink
#3238 - Content Sync make timeouts configurable (#3254)
Browse files Browse the repository at this point in the history
  • Loading branch information
YegorKozlov authored Feb 22, 2024
1 parent d8a3996 commit 0c4a9b1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
<!-- Keep this up to date! After a release, change the tag name to the latest release -->-

## Unreleased ([details][unreleased changes details])

#3238 - Content Sync make timeouts configurable
#3235 - Add an option to ignore selectors in the url.

## 6.3.8 - 2024-02-02
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.adobe.acs.commons.contentsync.RemoteInstance.CONNECT_TIMEOUT;
import static com.adobe.acs.commons.contentsync.RemoteInstance.SOCKET_TIMEOUT;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static org.apache.jackrabbit.JcrConstants.NT_UNSTRUCTURED;
import static org.apache.sling.jcr.resource.api.JcrResourceConstants.NT_SLING_FOLDER;
Expand All @@ -39,6 +41,8 @@ public class ConfigurationUtils {

public static final String UPDATE_STRATEGY_KEY = "update-strategy";
public static final String EVENT_USER_DATA_KEY = "event-user-data";
public static final String SO_TIMEOUT_STRATEGY_KEY = "soTimeout";
public static final String CONNECT_TIMEOUT_KEY = "connTimeout";

private ConfigurationUtils(){

Expand All @@ -49,6 +53,8 @@ public static Resource getSettingsResource(ResourceResolver resourceResolver) th
resourceProperties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED);
resourceProperties.put(UPDATE_STRATEGY_KEY, LastModifiedStrategy.class.getName());
resourceProperties.put(EVENT_USER_DATA_KEY, "changedByPageManagerCopy");
resourceProperties.put(SO_TIMEOUT_STRATEGY_KEY, SOCKET_TIMEOUT);
resourceProperties.put(CONNECT_TIMEOUT_KEY, CONNECT_TIMEOUT);
return ResourceUtil.getOrCreateResource(resourceResolver, SETTINGS_PATH, resourceProperties, NT_SLING_FOLDER, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.resource.ValueMap;

import javax.json.Json;
import javax.json.JsonObject;
Expand All @@ -45,32 +46,37 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.adobe.acs.commons.contentsync.ConfigurationUtils.CONNECT_TIMEOUT_KEY;
import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SO_TIMEOUT_STRATEGY_KEY;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;

/**
* HTTP connection to a remote AEM instance + some sugar methods to fetch data
*/
public class RemoteInstance implements Closeable {
private static final int CONNECT_TIMEOUT = 5000;
private static final int SOCKET_TIMEOUT = 60000;
static final int CONNECT_TIMEOUT = 5000;
static final int SOCKET_TIMEOUT = 300000;

private final CloseableHttpClient httpClient;
private final SyncHostConfiguration hostConfiguration;

public RemoteInstance(SyncHostConfiguration hostConfiguration) {
public RemoteInstance(SyncHostConfiguration hostConfiguration, ValueMap generalSettings) {
this.hostConfiguration = hostConfiguration;
this.httpClient = createHttpClient();
this.httpClient = createHttpClient(hostConfiguration, generalSettings);
}

private CloseableHttpClient createHttpClient() {
private CloseableHttpClient createHttpClient(SyncHostConfiguration hostConfiguration, ValueMap generalSettings) {
BasicCredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(hostConfiguration.getUsername(), hostConfiguration.getPassword()));
int soTimeout = generalSettings.get(SO_TIMEOUT_STRATEGY_KEY, SOCKET_TIMEOUT);
int connTimeout = generalSettings.get(CONNECT_TIMEOUT_KEY, CONNECT_TIMEOUT);
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(CONNECT_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(connTimeout)
.setSocketTimeout(soTimeout)
.setCookieSpec(CookieSpecs.STANDARD).build();
return
HttpClients.custom()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.contentloader.ContentImporter;
import org.apache.sling.jcr.contentloader.internal.ContentReaderWhiteboard;
Expand All @@ -51,15 +52,17 @@
import java.util.Arrays;
import java.util.List;

import static com.adobe.acs.commons.contentsync.ConfigurationUtils.CONNECT_TIMEOUT_KEY;
import static com.adobe.acs.commons.contentsync.ConfigurationUtils.HOSTS_PATH;
import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SETTINGS_PATH;
import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SO_TIMEOUT_STRATEGY_KEY;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -87,11 +90,13 @@ public void setUp() throws Exception {

String configPath = HOSTS_PATH + "/host1";
context.build().resource(configPath, "host", "http://localhost:4502", "username", "", "password", "");
context.build().resource(SETTINGS_PATH, SO_TIMEOUT_STRATEGY_KEY, 1000, CONNECT_TIMEOUT_KEY, "1000");
ValueMap generalSettings = context.resourceResolver().getResource(configPath).getValueMap();
SyncHostConfiguration hostConfiguration =
context.getService(ModelFactory.class)
.createModel(context.resourceResolver().getResource(configPath), SyncHostConfiguration.class);
ContentImporter contentImporter = context.registerInjectActivateService(new DefaultContentImporter());
remoteInstance = spy(new RemoteInstance(hostConfiguration));
remoteInstance = spy(new RemoteInstance(hostConfiguration, generalSettings));

contentSync = new ContentSync(remoteInstance, context.resourceResolver(), contentImporter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
String observationData = generalSettings.get(ConfigurationUtils.EVENT_USER_DATA_KEY, String.class);
String strategyPid = generalSettings.get(ConfigurationUtils.UPDATE_STRATEGY_KEY, String.class);
Resource cfg = resourceResolver.getResource(cfgPath);
SyncHostConfiguration hostConfig = cfg.adaptTo(SyncHostConfiguration.class);
Expand All @@ -85,7 +84,7 @@
ContentReader contentReader = new ContentReader(session);
UpdateStrategy updateStrategy = sling.getServices(UpdateStrategy.class, "(component.name=" + strategyPid + ")")[0];
try(RemoteInstance remoteInstance = new RemoteInstance(hostConfig)){
try(RemoteInstance remoteInstance = new RemoteInstance(hostConfig, generalSettings)){
ContentImporter importer = sling.getService(ContentImporter.class);
ContentSync contentSync = new ContentSync(remoteInstance, resourceResolver, importer);
ContentCatalog contentCatalog = new ContentCatalog(remoteInstance, catalogServlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,25 @@
sling:resourceType="granite/ui/components/coral/foundation/form/hidden"
name="./jcr:primaryType"
value="nt:unstructured"/>
<fieldset
jcr:primaryType="nt:unstructured"
jcr:title="Connection Properties"
sling:resourceType="granite/ui/components/coral/foundation/form/fieldset">
<items jcr:primaryType="nt:unstructured">
<socket-timeout
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Socket timeout in milliseconds"
name="./soTimeout"
value="300000"/>
<conn-timeout
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Connect timeout in milliseconds"
name="./connTimeout"
value="5000"/>
</items>
</fieldset>
</items>
<successresponse
jcr:primaryType="nt:unstructured"
Expand Down

0 comments on commit 0c4a9b1

Please sign in to comment.