forked from myYearbook/jira-gerrit-plugin
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve caching by leveraging atlassian-cache
This will enable caching in a clustered environment, such as Jira Data Center.
- Loading branch information
Showing
5 changed files
with
158 additions
and
187 deletions.
There are no files selected for viewing
105 changes: 0 additions & 105 deletions
105
src/main/java/com/meetme/plugins/jira/gerrit/data/IssueReviewsCache.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
src/main/java/com/meetme/plugins/jira/gerrit/data/IssueReviewsCacheLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.meetme.plugins.jira.gerrit.data; | ||
|
||
import com.meetme.plugins.jira.gerrit.data.dto.GerritChange; | ||
|
||
import com.atlassian.cache.CacheException; | ||
import com.atlassian.cache.CacheLoader; | ||
import com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritQueryException; | ||
import com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritQueryHandler; | ||
import com.sonyericsson.hudson.plugins.gerrit.gerritevents.ssh.Authentication; | ||
import com.sonyericsson.hudson.plugins.gerrit.gerritevents.ssh.SshException; | ||
|
||
import net.sf.json.JSONObject; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
public class IssueReviewsCacheLoader implements CacheLoader<String, List<GerritChange>> { | ||
private final Logger log = LoggerFactory.getLogger(IssueReviewsCacheLoader.class); | ||
private final GerritConfiguration configuration; | ||
|
||
public IssueReviewsCacheLoader(GerritConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public List<GerritChange> load(@Nonnull String key) { | ||
String query = String.format(Locale.US, configuration.getIssueSearchQuery(), key); | ||
|
||
try { | ||
return getReviewsFromGerrit(query); | ||
} catch (GerritQueryException e) { | ||
log.error("Error querying for issues", e); | ||
throw new CacheException("Error querying for issues: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
protected List<GerritChange> getReviewsFromGerrit(String searchQuery) throws GerritQueryException { | ||
List<GerritChange> changes; | ||
|
||
if (!configuration.isSshValid()) { | ||
// return Collections.emptyList(); | ||
throw new GerritConfiguration.NotConfiguredException("Not configured for SSH access"); | ||
} | ||
|
||
Authentication auth = new Authentication(configuration.getSshPrivateKey(), configuration.getSshUsername()); | ||
GerritQueryHandler query = new GerritQueryHandler(configuration.getSshHostname(), configuration.getSshPort(), null, auth); | ||
List<JSONObject> reviews; | ||
|
||
try { | ||
reviews = query.queryJava(searchQuery, false, true, false); | ||
} catch (SshException e) { | ||
throw new GerritQueryException("An ssh error occurred while querying for reviews.", e); | ||
} catch (IOException e) { | ||
throw new GerritQueryException("An error occurred while querying for reviews.", e); | ||
} | ||
|
||
changes = new ArrayList<>(reviews.size()); | ||
|
||
for (JSONObject obj : reviews) { | ||
if (obj.has("type") && "stats".equalsIgnoreCase(obj.getString("type"))) { | ||
// The final JSON object in the query results is just a set of statistics | ||
if (log.isDebugEnabled()) { | ||
log.trace("Results from QUERY: " + obj.optString("rowCount", "(unknown)") + " rows; runtime: " | ||
+ obj.optString("runTimeMilliseconds", "(unknown)") + " ms"); | ||
} | ||
continue; | ||
} | ||
|
||
changes.add(new GerritChange(obj)); | ||
} | ||
|
||
Collections.sort(changes); | ||
return changes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.