Skip to content

Commit

Permalink
LDEV-4356 - cache for a day and also cache the read method
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Aug 30, 2024
1 parent f421685 commit 3a7d457
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
93 changes: 50 additions & 43 deletions core/src/main/java/lucee/runtime/config/s3/BundleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

public final class BundleProvider extends DefaultHandler {
public static final int CONNECTION_TIMEOUT = 1000;
private static final long MAX_AGE = 10000;
private static final long MAX_AGE = 24 * 60 * 60 * 1000;
private static final int MAX_REDIRECTS = 10;

private static URL DEFAULT_PROVIDER_LIST = null;
Expand Down Expand Up @@ -148,6 +148,7 @@ public static URL getDefaultProviderDetailMvn() {
private final URL url;
private boolean insideContents;
private Map<String, Element> elements = new LinkedHashMap<>();
private List<Element> elementsSorted;
private Element element;
private boolean isTruncated;
private String lastKey;
Expand Down Expand Up @@ -244,7 +245,7 @@ public URL getBundleAsURL(BundleDefinition bd, boolean includeS3) throws Malform
// S3: we loop through all records and S3 and pick one
if (url == null) {
try {
for (Element e: read()) {
for (Element e: read(false)) {
if (bd.equals(e.getBundleDefinition())) {
url = e.getJAR();
if (url != null) return url;
Expand Down Expand Up @@ -456,50 +457,56 @@ private File deployBundledBundle(File bundleDirectory, String symbolicName, Stri
// 1146:size:305198;bundle:name:xmlgraphics.batik.awt.util;version:version EQ 1.8.0;;last-mod:{ts
// '2024-01-14 22:32:05'};

public List<Element> read() throws IOException, GeneralSecurityException, SAXException {
int count = 100;
URL url = null;

if (lastKey != null) url = new URL(this.url.toExternalForm() + "?marker=" + lastKey);

do {
if (url == null) url = isTruncated ? new URL(this.url.toExternalForm() + "?marker=" + this.lastKey) : this.url;
HTTPResponse rsp = HTTPEngine4Impl.get(url, null, null, BundleProvider.CONNECTION_TIMEOUT, true, null, null, null, null);
if (rsp != null) {
int sc = rsp.getStatusCode();
if (sc < 200 || sc >= 300) throw new IOException("unable to invoke [" + url + "], status code [" + sc + "]");
}
else {
throw new IOException("unable to invoke [" + url + "], no response.");
}
public List<Element> read(boolean flush) throws IOException, GeneralSecurityException, SAXException {
long now = System.currentTimeMillis();
if (elementsSorted == null) {
synchronized (elements) {
if (elementsSorted == null) {
int count = 100;
URL url = null;
if (lastKey != null) url = new URL(this.url.toExternalForm() + "?marker=" + lastKey);

do {
if (url == null) url = isTruncated ? new URL(this.url.toExternalForm() + "?marker=" + this.lastKey) : this.url;
HTTPResponse rsp = HTTPEngine4Impl.get(url, null, null, BundleProvider.CONNECTION_TIMEOUT, true, null, null, null, null);
if (rsp != null) {
int sc = rsp.getStatusCode();
if (sc < 200 || sc >= 300) throw new IOException("unable to invoke [" + url + "], status code [" + sc + "]");
}
else {
throw new IOException("unable to invoke [" + url + "], no response.");
}

Reader r = null;
try {
init(new InputSource(r = IOUtil.getReader(rsp.getContentAsStream(), (Charset) null)));
}
finally {
url = null;
IOUtil.close(r);
}
Reader r = null;
try {
init(new InputSource(r = IOUtil.getReader(rsp.getContentAsStream(), (Charset) null)));
}
finally {
url = null;
IOUtil.close(r);
}

}
while (isTruncated || --count == 0);
}
while (isTruncated || --count == 0);

List<Element> list = new ArrayList<>();
for (Element e: elements.values()) {
list.add(e);
}
List<Element> list = new ArrayList<>();
for (Element e: elements.values()) {
list.add(e);
}

Collections.sort(list, new Comparator<Element>() {
@Override
public int compare(Element l, Element r) {
int cmp = l.getBundleDefinition().getName().compareTo(r.getBundleDefinition().getName());
if (cmp != 0) return cmp;
return OSGiUtil.compare(l.getBundleDefinition().getVersion(), r.getBundleDefinition().getVersion());
Collections.sort(list, new Comparator<Element>() {
@Override
public int compare(Element l, Element r) {
int cmp = l.getBundleDefinition().getName().compareTo(r.getBundleDefinition().getName());
if (cmp != 0) return cmp;
return OSGiUtil.compare(l.getBundleDefinition().getVersion(), r.getBundleDefinition().getVersion());
}
});
elementsSorted = list;
}
}
});

return list;
}
return elementsSorted;
}

/**
Expand Down Expand Up @@ -779,7 +786,7 @@ public void createOSGiMavenMapping() throws IOException, GeneralSecurityExceptio
Set<String> has = new HashSet<>();
List<Info> infos;
Info info;
for (Element e: read()) {
for (Element e: read(true)) {
infos = mappings.get(e.bd.getName());
if (infos != null) continue;

Expand Down Expand Up @@ -816,7 +823,7 @@ else if (!info.isComplete()) {

public void whatcanBeRemovedFromS3() throws IOException, GeneralSecurityException, SAXException {
URL url;
for (Element e: read()) {
for (Element e: read(true)) {
url = getBundleAsURL(e.bd, false, null);

if (url != null) {
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.67-SNAPSHOT"/>
<property name="version" value="6.2.0.68-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.67-SNAPSHOT</version>
<version>6.2.0.68-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 3a7d457

Please sign in to comment.