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

Cache index of WorkspaceLocatorImpl.load/save #188

Merged
merged 1 commit into from
Apr 7, 2020
Merged
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
39 changes: 39 additions & 0 deletions src/main/java/jenkins/branch/WorkspaceLocatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.base.Charsets;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.Computer;
import hudson.model.Item;
Expand Down Expand Up @@ -56,6 +57,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -120,6 +122,21 @@ enum Mode {
/** Same as {@link WorkspaceList#COMBINATOR}. */
private static final String COMBINATOR = System.getProperty(WorkspaceList.class.getName(), "@");

/**
* @see #indexCache()
* @see #load
* @see #save
*/
private final Map<VirtualChannel, IndexCacheEntry> indexCache = new WeakHashMap<>();
private static final class IndexCacheEntry {
final String workspaceRoot;
final Map<String, String> index;
IndexCacheEntry(String workspaceRoot, Map<String, String> index) {
this.workspaceRoot = workspaceRoot;
this.index = index;
}
}

@Override
public FilePath locate(TopLevelItem item, Node node) {
return locate(item, node, true);
Expand Down Expand Up @@ -213,7 +230,21 @@ private static FilePath locate(TopLevelItem item, String fullName, Node node, bo
}
}

private static Map<VirtualChannel, IndexCacheEntry> indexCache() {
return ExtensionList.lookupSingleton(WorkspaceLocatorImpl.class).indexCache;
}

private static Map<String, String> load(FilePath workspace) throws IOException, InterruptedException {
Map<VirtualChannel, IndexCacheEntry> _indexCache = indexCache();
IndexCacheEntry entry;
synchronized (_indexCache) {
entry = _indexCache.get(workspace.getChannel());
}
if (entry != null && entry.workspaceRoot.equals(workspace.getRemote())) {
LOGGER.log(Level.FINER, "cache hit on {0}", workspace);
return entry.index;
}
LOGGER.log(Level.FINER, "cache miss on {0}", workspace);
Map<String, String> map = new TreeMap<>();
FilePath index = workspace.child(INDEX_FILE_NAME);
if (index.exists()) {
Expand All @@ -231,6 +262,9 @@ private static Map<String, String> load(FilePath workspace) throws IOException,
}
}
}
synchronized (_indexCache) {
_indexCache.put(workspace.getChannel(), new IndexCacheEntry(workspace.getRemote(), map));
}
return map;
}

Expand All @@ -252,6 +286,11 @@ private static void save(Map<String, String> index, FilePath workspace) throws I
b.append(entry.getKey()).append('\n').append(entry.getValue()).append('\n');
}
workspace.child(INDEX_FILE_NAME).act(new WriteAtomic(b.toString()));
LOGGER.log(Level.FINER, "cache update on {0}", workspace);
Map<VirtualChannel, IndexCacheEntry> _indexCache = indexCache();
synchronized (_indexCache) {
_indexCache.put(workspace.getChannel(), new IndexCacheEntry(workspace.getRemote(), index));
}
}
private static final class WriteAtomic extends MasterToSlaveFileCallable<Void> {
private static final long serialVersionUID = 1;
Expand Down