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 block of event bus #8

Merged
merged 2 commits into from
Nov 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/changelog/event_bus_lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- type: fixed
description: Block of event bus ([#8](https://github.com/scm-manager/scm-pushlog-plugin/pull/8))
92 changes: 0 additions & 92 deletions src/main/java/sonia/scm/pushlog/Pushlog.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,8 @@
public class Pushlog implements Serializable
{

/** Field description */
private static final int DATA_VERSION = 1;

/** Field description */
private static final long serialVersionUID = 6674106880546613670L;

//~--- constructors ---------------------------------------------------------

/**
* Constructs ...
*
*/
public Pushlog() {}

/**
* Constructs ...
*
*
* @param repositoryId
*/
public Pushlog(String repositoryId)
{
this.repositoryId = repositoryId;
}

//~--- methods --------------------------------------------------------------

/**
* Method description
*
*
* @param username
*
* @return
*/
public PushlogEntry createEntry(String username)
{
PushlogEntry entry = new PushlogEntry(++lastEntryId, username);
Expand All @@ -92,16 +59,6 @@ public PushlogEntry createEntry(String username)
return entry;
}

//~--- get methods ----------------------------------------------------------

/**
* Method description
*
*
* @param id
*
* @return
*/
public String get(String id)
{
String username = null;
Expand All @@ -119,23 +76,6 @@ public String get(String id)
return username;
}

/**
* Method description
*
*
* @return
*/
public int getDataVersion()
{
return dataVersion;
}

/**
* Method description
*
*
* @return
*/
public List<PushlogEntry> getEntries()
{
if (entries == null)
Expand All @@ -146,14 +86,6 @@ public List<PushlogEntry> getEntries()
return entries;
}

/**
* Method description
*
*
* @param id
*
* @return
*/
public PushlogEntry getEntry(long id)
{
PushlogEntry entry = null;
Expand All @@ -167,36 +99,12 @@ public PushlogEntry getEntry(long id)
break;
}
}

return entry;
}

/**
* Method description
*
*
* @return
*/
public String getRepositoryId()
{
return repositoryId;
}

//~--- fields ---------------------------------------------------------------

/** Field description */
@XmlElement(name = "data-version")
private int dataVersion = DATA_VERSION;

/** Field description */
@XmlElement(name = "entry")
private List<PushlogEntry> entries;

/** Field description */
@XmlElement(name = "last-entry-id")
private long lastEntryId = 0;

/** Field description */
@XmlElement(name = "repository-id")
private String repositoryId;
}
15 changes: 7 additions & 8 deletions src/main/java/sonia/scm/pushlog/PushlogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package sonia.scm.pushlog;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
Expand All @@ -32,6 +31,7 @@
import sonia.scm.store.DataStore;
import sonia.scm.store.DataStoreFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -49,7 +49,7 @@ public class PushlogManager {
private static final Object LOCK_STORE = new Object();
private static final Object LOCK_GET = new Object();

private final Map<String, Lock> locks = Maps.newHashMap();
private final Map<String, Lock> locks = new HashMap<>();
private final DataStoreFactory dataStoreFactory;

@Inject
Expand All @@ -61,12 +61,11 @@ public PushlogManager(DataStoreFactory dataStoreFactory) {
public void store(Pushlog pushlog, Repository repository) {
synchronized (LOCK_STORE) {
try {
logger.debug("store pushlog for repository {}",
pushlog.getRepositoryId());
logger.debug("store pushlog for repository {}", repository);
getDatastore(repository).put(NAME, pushlog);
} finally {
logger.trace("unlock repository {}", pushlog.getRepositoryId());
getLock(pushlog.getRepositoryId()).unlock();
logger.trace("unlock repository {}", repository);
getLock(repository.getId()).unlock();
}
}
}
Expand All @@ -75,7 +74,7 @@ public Pushlog get(Repository repository) {
Pushlog pushlog = getDatastore(repository).get(NAME);

if (pushlog == null) {
pushlog = new Pushlog(repository.getId());
pushlog = new Pushlog();
}

return pushlog;
Expand All @@ -90,7 +89,7 @@ public Pushlog getAndLock(Repository repository) {
synchronized (LOCK_GET) {
getLock(repository.getId()).lock();

logger.trace("lock pushlog for repository {}", repository.getId());
logger.trace("lock pushlog for repository {}", repository);

return get(repository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ private void doUpdate(String repositoryId) {
.forRepository(repositoryId)
.build();
store.getOptional(PUSHLOG_STORE_NAME)
.ifPresent(pushlog -> optimize(store, pushlog));
.ifPresent(pushlog -> optimize(store, repositoryId, pushlog));
}

private void optimize(DataStore<Pushlog> store, Pushlog pushlog) {
if (optimize(pushlog)) {
log.info("found illegal multiple pushlog entries for repository {}; cleaning up", pushlog.getRepositoryId());
private void optimize(DataStore<Pushlog> store, String repositoryId, Pushlog pushlog) {
if (optimize(repositoryId, pushlog)) {
log.info("found illegal multiple pushlog entries for repository {}; cleaning up", repositoryId);
store.put(PUSHLOG_STORE_NAME, pushlog);
}
}

@SuppressWarnings({"java:S3011", "unchecked"}) // reflection is used only for this update step and there should be acceptable
private boolean optimize(Pushlog pushlog) {
private boolean optimize(String repositoryId, Pushlog pushlog) {
Set<String> foundChangesets = new HashSet<>();
boolean errorsFound = false;
for (Iterator<PushlogEntry> entryIterator = pushlog.getEntries().iterator(); entryIterator.hasNext(); ) {
Expand All @@ -94,7 +94,7 @@ private boolean optimize(Pushlog pushlog) {
}
}
} catch (Exception e) {
log.warn("could not clean up pushlog for repository {}", pushlog.getRepositoryId(), e);
log.warn("could not clean up pushlog for repository {}", repositoryId, e);
return false;
}
}
Expand Down