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

fix: fix memory leak with a single client instance #174

Merged
merged 5 commits into from
Jan 23, 2025
Merged
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
9 changes: 5 additions & 4 deletions cadc-permissions-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ description = 'OpenCADC Permissions client library'
def git_url = 'https://github.com/opencadc/ac'

dependencies {
compile 'org.opencadc:cadc-permissions:[0.2,)'
compile 'org.opencadc:cadc-registry:[1.0,2.0)'
compile 'org.opencadc:cadc-util:[1.6,2.0)'
implementation 'org.opencadc:cadc-cdp:[1.4.0,2.0)'
implementation 'org.opencadc:cadc-permissions:[0.2,)'
implementation 'org.opencadc:cadc-registry:[1.0,2.0)'
implementation 'org.opencadc:cadc-util:[1.6,2.0)'

testCompile 'junit:junit:[4.0,)'
testImplementation 'junit:junit:[4.0,)'
}

apply from: '../opencadc.gradle'
2 changes: 1 addition & 1 deletion posix-mapper/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# tags with and without build number so operators use the versioned
# tag but we always keep a timestamped tag in case a semantic tag gets
# replaced accidentally
VER=0.2.1
VER=0.3.0
TAGS="${VER} ${VER}-$(date -u +"%Y%m%dT%H%M%S")"
unset VER
10 changes: 5 additions & 5 deletions posix-mapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ war {

dependencies {
implementation 'org.hibernate.orm:hibernate-core:6.2.0.Final'
implementation 'org.opencadc:cadc-util:[1.9.11,2.0)'
implementation 'org.opencadc:cadc-log:[1.1.6,2.0)'
implementation 'org.opencadc:cadc-log:[1.2.1,2.0)'
implementation 'org.opencadc:cadc-registry:[1.7.3,)'
implementation 'org.opencadc:cadc-vosi:[1.4.3,2.0)'
implementation 'org.opencadc:cadc-rest:[1.3.14,)'
implementation 'org.opencadc:cadc-util:[1.11.6,2.0)'
implementation 'org.opencadc:cadc-vosi:[1.4.8,2.0)'

testImplementation 'junit:junit:[4.0,)'

intTestCompile 'org.opencadc:cadc-test-vosi:[1.0.11,)'
intTestCompile 'org.apache.commons:commons-lang3:3.13.0'
intTestImplementation 'org.opencadc:cadc-test-vosi:[1.0.11,)'
intTestImplementation 'org.apache.commons:commons-lang3:3.13.0'

runtimeOnly 'org.opencadc:cadc-gms:[1.0.12,2.0)'
runtimeOnly 'org.opencadc:cadc-access-control-identity:[1.0.3,2.0)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "groups_gid_seq1")
@GenericGenerator(name = "groups_gid_seq1", type = GroupIDSequenceGenerator.class,
parameters = {
@org.hibernate.annotations.Parameter(name = GroupIDSequenceGenerator.SEQUENCE_PARAM,
value = "groups_gid_seq1"),
@org.hibernate.annotations.Parameter(name = GroupIDSequenceGenerator.INCREMENT_PARAM, value = "1"),
@org.hibernate.annotations.Parameter(name = GroupIDSequenceGenerator.INITIAL_PARAM, value = "900000")
})
@SequenceGenerator(name = "groups_gid_seq1", sequenceName = "groups_gid_seq1", allocationSize = 1)
private Integer gid;

@Column(unique = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private Properties properties() {
Properties properties = new Properties();
properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
properties.put("hibernate.connection.datasource", Postgres.DEFAULT_JNDI);
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
if (this.defaultSchema != null) {
properties.put("hibernate.default_schema", this.defaultSchema);
}
Expand Down Expand Up @@ -87,15 +86,8 @@ public void close(Session session) {
}

public <R> R inSession(Function<Session, R> function) {
Session session = null;
try {
session = open();
try (final Session session = open()) {
return function.apply(session);
} catch (Exception e) {
log.error(e);
throw e;
} finally {
close(session);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,23 @@

public abstract class PosixMapperAction extends RestAction {

protected PosixClient posixClient;
protected static final MultiValuedProperties POSIX_CONFIGURATION = PosixInitAction.getConfig();
protected static final String TSV_CONTENT_TYPE = "text/tab-separated-values";

// As the Postgres class uses a Factory pattern, a static client is created to prevent multiple factory instances
// being created that was causing a memory leak.
// jenkinsd (2024.12.23)
//
protected static final PosixClient POSIX_CLIENT =
pdowler marked this conversation as resolved.
Show resolved Hide resolved
new PostgresPosixClient(Postgres.instance(PosixMapperAction.POSIX_CONFIGURATION
.getFirstPropertyValue(PosixInitAction.SCHEMA_KEY))
.entityClass(User.class, Group.class)
.build());

protected PosixMapperAction() {
final Postgres postgres = Postgres.instance(PosixMapperAction.POSIX_CONFIGURATION
.getFirstPropertyValue(PosixInitAction.SCHEMA_KEY))
.entityClass(User.class, Group.class)
.build();
this.posixClient = new PostgresPosixClient(postgres);
}


@Override
public void initAction() throws Exception {
super.initAction();
Expand All @@ -121,10 +125,7 @@ private void checkAuthorization() {
}

protected GroupWriter getGroupWriter() throws IOException {
final String requestContentType = syncInput.getHeader("accept");
final String writeContentType = PosixMapperAction.TSV_CONTENT_TYPE.equals(requestContentType)
? PosixMapperAction.TSV_CONTENT_TYPE : "text/plain";
this.syncOutput.addHeader("content-type", writeContentType);
final String writeContentType = prepareContent();
final Writer writer = new BufferedWriter(new OutputStreamWriter(this.syncOutput.getOutputStream()));
if (PosixMapperAction.TSV_CONTENT_TYPE.equals(writeContentType)) {
return new TSVGroupWriter(writer);
Expand All @@ -134,10 +135,7 @@ protected GroupWriter getGroupWriter() throws IOException {
}

protected UserWriter getUserWriter() throws IOException {
final String requestContentType = syncInput.getHeader("accept");
final String writeContentType = PosixMapperAction.TSV_CONTENT_TYPE.equals(requestContentType)
? PosixMapperAction.TSV_CONTENT_TYPE : "text/plain";
this.syncOutput.addHeader("content-type", writeContentType);
final String writeContentType = prepareContent();
final Writer writer = new BufferedWriter(new OutputStreamWriter(this.syncOutput.getOutputStream()));
if (PosixMapperAction.TSV_CONTENT_TYPE.equals(writeContentType)) {
return new TSVUserWriter(writer);
Expand All @@ -146,9 +144,19 @@ protected UserWriter getUserWriter() throws IOException {
}
}

protected String prepareContent() {
final String requestContentType = syncInput.getHeader("accept");
final String writeContentType = PosixMapperAction.TSV_CONTENT_TYPE.equals(requestContentType)
? PosixMapperAction.TSV_CONTENT_TYPE : "text/plain";
this.syncOutput.addHeader("content-type", writeContentType);

return writeContentType;
}

/**
* Never used.
* @return null
*
* @return null
*/
@Override
protected InlineContentHandler getInlineContentHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public class GetAction extends PosixMapperAction {
@Override
public void doAction() throws Exception {
final GroupWriter groupWriter = getGroupWriter();
this.posixClient.writeGroups(groupWriter, groupParameters().toArray(new GroupURI[0]),
gidParameters().toArray(new Integer[0]));
PosixMapperAction.POSIX_CLIENT.writeGroups(groupWriter, groupParameters().toArray(new GroupURI[0]),
gidParameters().toArray(new Integer[0]));

syncOutput.getOutputStream().flush();
}
Expand All @@ -109,8 +109,8 @@ private List<Integer> gidParameters() {
gidConstraints = Collections.emptyList();
} else {
gidConstraints = gidStringParameters.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
.map(Integer::parseInt)
.collect(Collectors.toList());
}

return gidConstraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public class GetAction extends PosixMapperAction {
@Override
public void doAction() throws Exception {
final UserWriter userWriter = getUserWriter();
posixClient.writeUsers(userWriter, usernameParameters().toArray(new String[0]),
uidParameters().toArray(new Integer[0]));
PosixMapperAction.POSIX_CLIENT.writeUsers(userWriter, usernameParameters().toArray(new String[0]),
uidParameters().toArray(new Integer[0]));
syncOutput.getOutputStream().flush();
}

Expand Down
Loading