-
Notifications
You must be signed in to change notification settings - Fork 273
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
Avoid hash code collisions in build memory #360
Open
Jimilian
wants to merge
3
commits into
jenkinsci:master
Choose a base branch
from
Jimilian:hash_collision
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
*/ | ||
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.infradna.tool.bridge_method_injector.WithBridgeMethods; | ||
import com.sonyericsson.hudson.plugins.gerrit.trigger.diagnostics.BuildMemoryReport; | ||
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint.Entry; | ||
|
@@ -44,11 +45,10 @@ | |
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.HashMap; | ||
|
||
import static com.sonyericsson.hudson.plugins.gerrit.trigger.utils.Logic.shouldSkip; | ||
|
||
|
@@ -58,32 +58,7 @@ | |
* @author Robert Sandell <[email protected]> | ||
*/ | ||
public class BuildMemory { | ||
|
||
/** | ||
* Compares GerritTriggeredEvents using the Object.hashCode() method. This ensures that every event received from | ||
* Gerrit is kept track of individually. | ||
* | ||
* @author James E. Blair <[email protected]> | ||
*/ | ||
static class GerritTriggeredEventComparator implements Comparator<GerritTriggeredEvent> { | ||
@Override | ||
public int compare(GerritTriggeredEvent o1, GerritTriggeredEvent o2) { | ||
if (o1 == null && o2 == null) { | ||
return 0; | ||
} | ||
if (o1 != null && o2 == null) { | ||
return 1; | ||
} | ||
if (o1 == null && o2 != null) { | ||
return -1; | ||
} | ||
return Integer.valueOf(o1.hashCode()).compareTo(o2.hashCode()); | ||
} | ||
} | ||
|
||
private TreeMap<GerritTriggeredEvent, MemoryImprint> memory = | ||
new TreeMap<GerritTriggeredEvent, MemoryImprint>( | ||
new GerritTriggeredEventComparator()); | ||
private Map<GerritTriggeredEvent, MemoryImprint> memory = new HashMap<GerritTriggeredEvent, MemoryImprint>(); | ||
private static final Logger logger = LoggerFactory.getLogger(BuildMemory.class); | ||
|
||
/** | ||
|
@@ -96,6 +71,15 @@ public synchronized MemoryImprint getMemoryImprint(GerritTriggeredEvent event) { | |
return memory.get(event); | ||
} | ||
|
||
/** | ||
* Gets the entire memory representation for all events | ||
* @return memory representation for all events. | ||
*/ | ||
@VisibleForTesting | ||
Map<GerritTriggeredEvent, MemoryImprint> getMemory() { | ||
return memory; | ||
} | ||
|
||
/** | ||
* Tells if all triggered builds have started for a specific memory imprint. | ||
* | ||
|
111 changes: 111 additions & 0 deletions
111
...on/hudson/plugins/gerrit/trigger/gerritnotifier/model/BuildMemoryReportContainerTest.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,111 @@ | ||
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model; | ||
|
||
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeBasedEvent; | ||
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent; | ||
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.ItemGroup; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Arrays; | ||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Tests {@link BuildMemory} internal container. | ||
* The main requirement is not merging non-equal events with same hash code. | ||
* | ||
* @author Robert Sandell <[email protected]>. | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class BuildMemoryReportContainerTest { | ||
|
||
private final GerritTriggeredEvent o1; | ||
private final GerritTriggeredEvent o2; | ||
private final int expected; | ||
private final BuildMemory memory; | ||
|
||
static class PatchsetCreatedWithPredefinedHash extends PatchsetCreated { | ||
private final int hashCode; | ||
private final UUID uuid; | ||
|
||
private PatchsetCreatedWithPredefinedHash(int hashCode) { | ||
this.hashCode = hashCode; | ||
this.uuid = UUID.randomUUID(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o instanceof PatchsetCreatedWithPredefinedHash && | ||
uuid.equals(((PatchsetCreatedWithPredefinedHash) o).uuid); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PatchsetCreatedWithPredefinedHash{" + | ||
"uuid=" + uuid + | ||
'}'; | ||
} | ||
} | ||
|
||
/** | ||
* The different scenarios. | ||
* | ||
* @return a list of parameters to the constructor. | ||
* @see Parameterized | ||
*/ | ||
@Parameterized.Parameters(name = "{index}: {0}, {1} == {2}") | ||
public static Iterable<Object[]> permutations() { | ||
ChangeBasedEvent event1 = new PatchsetCreatedWithPredefinedHash(777); | ||
ChangeBasedEvent event2 = new PatchsetCreatedWithPredefinedHash(777); | ||
return Arrays.asList(new Object[][]{ | ||
{null, null, 1}, | ||
{null, event1, 2}, | ||
{event1, null, 2}, | ||
{event1, event1, 1}, | ||
{event2, event2, 1}, | ||
{event1, event2, 2}, | ||
{event2, event1, 2}, | ||
}); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* @param o1 first argument | ||
* @param o2 second argument | ||
* @param expected the expected result | ||
*/ | ||
public BuildMemoryReportContainerTest(GerritTriggeredEvent o1, GerritTriggeredEvent o2, int expected) { | ||
this.o1 = o1; | ||
this.o2 = o2; | ||
this.expected = expected; | ||
memory = new BuildMemory(); | ||
} | ||
|
||
/** | ||
* Tests the scenario on {@link BuildMemory}. | ||
* | ||
* @throws Exception if so | ||
*/ | ||
@Test | ||
public void testCompare() throws Exception { | ||
AbstractProject mock = Mockito.mock(AbstractProject.class); | ||
ItemGroup itemGroup = Mockito.mock(ItemGroup.class); | ||
Mockito.when(itemGroup.getFullName()).thenReturn("Parent"); | ||
Mockito.when(mock.getParent()).thenReturn(itemGroup); | ||
memory.triggered(o1, mock); | ||
memory.triggered(o2, mock); | ||
|
||
assertEquals(expected, memory.getMemory().size()); | ||
} | ||
} |
72 changes: 0 additions & 72 deletions
72
...udson/plugins/gerrit/trigger/gerritnotifier/model/GerritTriggeredEventComparatorTest.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be one, if they have the same hashcode, they essentially are the same event even though it is different object instances.
An event might be just one instance when a series of builds are triggered, but when those builds are restored from disk the events in those builds will be different instances, and the quick way to see that two instances respresents the same event is to use the
hashCode
.