mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-18 06:22:29 +00:00
[FIXED JENKINS-18065] made ALLRM.entrySet() smarter
This commit is contained in:
1 parent
8057217f1f
commit
54c084613f
5 files changed
+187
-11
No files matched your search
@@ -75,6 +75,9 @@ Upcoming changes</a>
|
||||
<li class=bug>
|
||||
Fixed "... looks to have already been deleted" bug in LogRotator
|
||||
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-22395">issue 22395</a>)
|
||||
<li class='major bug'>
|
||||
Fixed unnecessary eager loading of build records in certain code path.
|
||||
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-18065">issue 18065</a>)
|
||||
</ul>
|
||||
</div><!--=END=-->
|
||||
<h3><a name=v1.571>What's new in 1.571</a> (2014/07/07)</h3>
|
||||
|
||||
@@ -220,7 +220,7 @@ public class RunList<R extends Run> extends AbstractList<R> {
|
||||
|
||||
/**
|
||||
* Return only the most recent builds.
|
||||
* <em>Warning:</em> this method mutates the original list and then returns it.
|
||||
* <em>Warning:</em> this method mutates the original list and then returns it.AbstractLazyLoadRunMap
|
||||
* @param n a count
|
||||
* @return the n most recent builds
|
||||
* @since 1.507
|
||||
|
||||
@@ -109,6 +109,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
* Updated atomically. Once set to this field, the index object may not be modified.
|
||||
*/
|
||||
private volatile Index index = new Index();
|
||||
private LazyLoadRunMapEntrySet<R> entrySet = new LazyLoadRunMapEntrySet<R>(this);
|
||||
|
||||
/**
|
||||
* Pair of two maps into a single class, so that the changes can be made visible atomically,
|
||||
@@ -274,7 +275,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
@Override
|
||||
public Set<Entry<Integer, R>> entrySet() {
|
||||
assert baseDirInitialized();
|
||||
return Collections.unmodifiableSet(new BuildReferenceMapAdapter<R>(this,all()).entrySet());
|
||||
return entrySet;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,7 +349,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
}
|
||||
|
||||
public R get(int n) {
|
||||
return search(n,Direction.EXACT);
|
||||
return search(n, Direction.EXACT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -543,7 +544,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
}
|
||||
|
||||
public R getByNumber(int n) {
|
||||
return search(n,Direction.EXACT);
|
||||
return search(n, Direction.EXACT);
|
||||
}
|
||||
|
||||
public R put(R value) {
|
||||
@@ -551,7 +552,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
}
|
||||
|
||||
protected R _put(R value) {
|
||||
return put(getNumberOf(value),value);
|
||||
return put(getNumberOf(value), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -612,7 +613,7 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
|
||||
* @return
|
||||
* fully populated map.
|
||||
*/
|
||||
private TreeMap<Integer,BuildReference<R>> all() {
|
||||
/*package*/ TreeMap<Integer,BuildReference<R>> all() {
|
||||
if (!fullyLoaded) {
|
||||
synchronized (this) {
|
||||
if (!fullyLoaded) {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package jenkins.model.lazy;
|
||||
|
||||
import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
|
||||
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Set that backs {@link AbstractLazyLoadRunMap#entrySet()}.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
class LazyLoadRunMapEntrySet<R> extends AbstractSet<Entry<Integer,R>> {
|
||||
private final AbstractLazyLoadRunMap<R> owner;
|
||||
|
||||
/**
|
||||
* Lazily loaded all entries.
|
||||
*/
|
||||
private Set<Entry<Integer,R>> all;
|
||||
|
||||
LazyLoadRunMapEntrySet(AbstractLazyLoadRunMap<R> owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
private synchronized Set<Entry<Integer,R>> all() {
|
||||
if (all==null)
|
||||
all = new BuildReferenceMapAdapter<R>(owner,owner.all()).entrySet();
|
||||
return all;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return all().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return owner.newestBuild()==null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
if (o instanceof Entry) {
|
||||
Entry e = (Entry) o;
|
||||
Object k = e.getKey();
|
||||
if (k instanceof Integer) {
|
||||
return owner.getByNumber((Integer)k).equals(e.getValue());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<Integer,R>> iterator() {
|
||||
return new Iterator<Entry<Integer,R>>() {
|
||||
R last = null;
|
||||
R next = owner.newestBuild();
|
||||
|
||||
public boolean hasNext() {
|
||||
return next!=null;
|
||||
}
|
||||
|
||||
public Entry<Integer,R> next() {
|
||||
last = next;
|
||||
if (last!=null) {
|
||||
next = owner.search(owner.getNumberOf(last)-1, Direction.DESC);
|
||||
} else
|
||||
throw new NoSuchElementException();
|
||||
return entryOf(last);
|
||||
}
|
||||
|
||||
private Entry<Integer, R> entryOf(R r) {
|
||||
return new SimpleEntry<Integer, R>(owner.getNumberOf(r),r);
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (last==null)
|
||||
throw new UnsupportedOperationException();
|
||||
owner.removeValue(last);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return all().toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return all().toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Entry<Integer, R> integerREntry) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
if (o instanceof Entry) {
|
||||
Entry e = (Entry) o;
|
||||
return owner.removeValue((R)e.getValue());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
@@ -39,6 +40,7 @@ import java.util.logging.Level;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.jvnet.hudson.test.Bug;
|
||||
import org.jvnet.hudson.test.Issue;
|
||||
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
@@ -206,9 +208,9 @@ public class AbstractLazyLoadRunMapTest extends Assert {
|
||||
|
||||
a.get(1).asserts(1,"A");
|
||||
assertNull(a.get(2));
|
||||
a.get(3).asserts(3,"B");
|
||||
a.get(3).asserts(3, "B");
|
||||
assertNull(a.get(4));
|
||||
a.get(5).asserts(5,"C");
|
||||
a.get(5).asserts(5, "C");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -216,7 +218,7 @@ public class AbstractLazyLoadRunMapTest extends Assert {
|
||||
FakeMap a = localBuilder.addBoth(1, "A").addBoth(3, "B").addBoth(5, "C").addBoth(7, "D").make();
|
||||
|
||||
// we should be using the cache to find the entry efficiently
|
||||
a.search(6, Direction.ASC).asserts(7,"D");
|
||||
a.search(6, Direction.ASC).asserts(7, "D");
|
||||
a.search(2, Direction.DESC).asserts(1, "A");
|
||||
}
|
||||
|
||||
@@ -234,7 +236,7 @@ public class AbstractLazyLoadRunMapTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void bogusCache2() throws IOException {
|
||||
FakeMap a = localBuilder.addBogusCache(1,3,"A").make();
|
||||
FakeMap a = localBuilder.addBogusCache(1, 3, "A").make();
|
||||
assertNull(a.get(1));
|
||||
a.get(3).asserts(3,"A");
|
||||
}
|
||||
@@ -294,7 +296,7 @@ public class AbstractLazyLoadRunMapTest extends Assert {
|
||||
.addUnloadable("F")
|
||||
.addUnloadable("G")
|
||||
.add(200,"H")
|
||||
.add(201,"I");
|
||||
.add(201, "I");
|
||||
FakeMap map = f.make();
|
||||
|
||||
Build x = map.search(Integer.MAX_VALUE, Direction.DESC);
|
||||
@@ -362,4 +364,63 @@ public class AbstractLazyLoadRunMapTest extends Assert {
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
|
||||
@Issue("JENKINS-18065")
|
||||
@Test
|
||||
public void entrySetIterator() {
|
||||
Iterator<Entry<Integer, Build>> itr = a.entrySet().iterator();
|
||||
|
||||
// iterator, when created fresh, shouldn't force loading everything
|
||||
// this involves binary searching, so it can load several.
|
||||
assertTrue(a.getLoadedBuilds().size() < 3);
|
||||
|
||||
// check if the first entry is legit
|
||||
assertTrue(itr.hasNext());
|
||||
Entry<Integer, Build> e = itr.next();
|
||||
assertEquals((Integer)5,e.getKey());
|
||||
e.getValue().asserts(5, "C");
|
||||
|
||||
// now that the first entry is returned, we expect there to be two loaded
|
||||
assertTrue(a.getLoadedBuilds().size() < 3);
|
||||
|
||||
// check if the second entry is legit
|
||||
assertTrue(itr.hasNext());
|
||||
e = itr.next();
|
||||
assertEquals((Integer)3, e.getKey());
|
||||
e.getValue().asserts(3,"B");
|
||||
|
||||
// repeat the process for the third one
|
||||
assertTrue(a.getLoadedBuilds().size() <= 3);
|
||||
|
||||
// check if the third entry is legit
|
||||
assertTrue(itr.hasNext());
|
||||
e = itr.next();
|
||||
assertEquals((Integer) 1, e.getKey());
|
||||
e.getValue().asserts(1,"A");
|
||||
|
||||
assertFalse(itr.hasNext());
|
||||
assertEquals(3, a.getLoadedBuilds().size());
|
||||
}
|
||||
|
||||
@Issue("JENKINS-18065")
|
||||
@Test
|
||||
public void entrySetEmpty() {
|
||||
// entrySet().isEmpty() shouldn't cause full data load
|
||||
assertFalse(a.entrySet().isEmpty());
|
||||
assertTrue(a.getLoadedBuilds().size() < 3);
|
||||
}
|
||||
|
||||
@Issue("JENKINS-18065")
|
||||
@Test
|
||||
public void entrySetSize() {
|
||||
assertEquals(3, a.entrySet().size());
|
||||
assertEquals(0, b.entrySet().size());
|
||||
}
|
||||
|
||||
@Issue("JENKINS-18065")
|
||||
@Test
|
||||
public void entrySetContains() {
|
||||
for (Entry<Integer, Build> e : a.entrySet()) {
|
||||
assertTrue(a.entrySet().contains(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user