Set update step info for new repositories (#1554)

Sets versions for repository update steps to newest versions for new repositories to prevent unnecessary updates.
This commit is contained in:
René Pfeuffer
2021-02-25 08:12:16 +01:00
committed by GitHub
parent 3a8d031ed5
commit 3c94ce91d6
5 changed files with 283 additions and 49 deletions

View File

@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.update;
import com.github.legman.Subscribe;
import sonia.scm.EagerSingleton;
import sonia.scm.HandlerEventType;
import sonia.scm.migration.RepositoryUpdateStep;
import sonia.scm.plugin.Extension;
import sonia.scm.repository.RepositoryEvent;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Extension @EagerSingleton
class RepositoryUpdateAfterCreationHook {
private final List<RepositoryUpdateStep> repositorySteps;
private final UpdateStepStore updateStepStore;
@Inject
RepositoryUpdateAfterCreationHook(Set<RepositoryUpdateStep> repositorySteps, UpdateStepStore updateStepStore) {
this.repositorySteps = determineStepsWithHighestVersion(repositorySteps);
this.updateStepStore = updateStepStore;
}
private ArrayList<RepositoryUpdateStep> determineStepsWithHighestVersion(Set<RepositoryUpdateStep> repositorySteps) {
List<RepositoryUpdateStep> temporarySteps = new ArrayList<>(repositorySteps);
temporarySteps.sort(Comparator.comparing(RepositoryUpdateStep::getTargetVersion).reversed());
Map<String, RepositoryUpdateStep> stepsForDataType = new HashMap<>();
temporarySteps.forEach(step -> stepsForDataType.put(step.getAffectedDataType(), step));
return new ArrayList<>(stepsForDataType.values());
}
@Subscribe
public void updateRepository(RepositoryEvent event) {
if (event.getEventType() == HandlerEventType.CREATE) {
repositorySteps.forEach(updateStep -> updateStepStore.storeExecutedUpdate(event.getItem().getId(), updateStep));
}
}
}

View File

@@ -31,8 +31,6 @@ import sonia.scm.migration.RepositoryUpdateStep;
import sonia.scm.migration.UpdateException;
import sonia.scm.migration.UpdateStep;
import sonia.scm.migration.UpdateStepTarget;
import sonia.scm.store.ConfigurationEntryStore;
import sonia.scm.store.ConfigurationEntryStoreFactory;
import sonia.scm.version.Version;
import javax.inject.Inject;
@@ -48,23 +46,19 @@ public class UpdateEngine {
public static final Logger LOG = LoggerFactory.getLogger(UpdateEngine.class);
private static final String STORE_NAME = "executedUpdates";
private final List<UpdateStepWrapper> steps;
private final ConfigurationEntryStore<UpdateVersionInfo> store;
private final ConfigurationEntryStoreFactory storeFactory;
private final RepositoryUpdateIterator repositoryUpdateIterator;
private final UpdateStepStore updateStepStore;
@Inject
public UpdateEngine(
Set<UpdateStep> globalSteps,
Set<RepositoryUpdateStep> repositorySteps,
ConfigurationEntryStoreFactory storeFactory,
RepositoryUpdateIterator repositoryUpdateIterator
) {
this.storeFactory = storeFactory;
RepositoryUpdateIterator repositoryUpdateIterator,
UpdateStepStore updateStepStore) {
this.repositoryUpdateIterator = repositoryUpdateIterator;
this.store = storeFactory.withType(UpdateVersionInfo.class).withName(STORE_NAME).build();
this.updateStepStore = updateStepStore;
this.steps = sortSteps(globalSteps, repositorySteps);
}
@@ -127,32 +121,12 @@ public class UpdateEngine {
}
}
private void storeNewVersion(ConfigurationEntryStore<UpdateVersionInfo> store, UpdateStepTarget updateStep) {
UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion());
store.put(updateStep.getAffectedDataType(), newVersionInfo);
}
private boolean notRunYet(UpdateStep updateStep) {
LOG.trace("checking whether to run update step for type {} and version {}",
updateStep.getAffectedDataType(),
updateStep.getTargetVersion()
);
return notRunYet(this.store, updateStep);
}
private boolean notRunYet(ConfigurationEntryStore<UpdateVersionInfo> store, UpdateStepTarget updateStep) {
UpdateVersionInfo updateVersionInfo = store.get(updateStep.getAffectedDataType());
if (updateVersionInfo == null) {
LOG.trace("no updates for type {} run yet; step will be executed", updateStep.getAffectedDataType());
return true;
}
boolean result = updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion());
LOG.trace("latest version for type {}: {}; step will be executed: {}",
updateStep.getAffectedDataType(),
updateVersionInfo.getLatestVersion(),
result
);
return result;
return updateStepStore.notRunYet(updateStep);
}
private abstract static class UpdateStepWrapper implements UpdateStepTarget {
@@ -219,7 +193,7 @@ public class UpdateEngine {
delegate.getClass().getName()
);
delegate.doUpdate();
storeNewVersion(store, delegate);
updateStepStore.storeExecutedUpdate(delegate);
}
void doUpdate(String repositoryId) {
@@ -261,7 +235,7 @@ public class UpdateEngine {
repositoryId
);
delegate.doUpdate(new RepositoryUpdateContext(repositoryId));
storeNewVersion(storeForRepository(repositoryId), delegate);
updateStepStore.storeExecutedUpdate(repositoryId, delegate);
}
}
@@ -271,11 +245,7 @@ public class UpdateEngine {
delegate.getTargetVersion(),
repositoryId
);
return UpdateEngine.this.notRunYet(storeForRepository(repositoryId), delegate);
}
private ConfigurationEntryStore<UpdateVersionInfo> storeForRepository(String repositoryId) {
return storeFactory.withType(UpdateVersionInfo.class).withName(STORE_NAME).forRepository(repositoryId).build();
return updateStepStore.notRunYet(repositoryId, delegate);
}
}
}

View File

@@ -0,0 +1,102 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.migration.RepositoryUpdateStep;
import sonia.scm.migration.UpdateStep;
import sonia.scm.migration.UpdateStepTarget;
import sonia.scm.store.ConfigurationEntryStore;
import sonia.scm.store.ConfigurationEntryStoreFactory;
import javax.inject.Inject;
class UpdateStepStore {
private static final String STORE_NAME = "executedUpdates";
private static final Logger LOG = LoggerFactory.getLogger(UpdateStepStore.class);
private final ConfigurationEntryStoreFactory storeFactory;
@Inject
UpdateStepStore(ConfigurationEntryStoreFactory storeFactory) {
this.storeFactory = storeFactory;
}
void storeExecutedUpdate(String repositoryId, RepositoryUpdateStep updateStep) {
ConfigurationEntryStore<UpdateVersionInfo> store = createRepositoryStore(repositoryId);
storeNewVersion(store, updateStep);
}
void storeExecutedUpdate(UpdateStep updateStep) {
ConfigurationEntryStore<UpdateVersionInfo> store = createGlobalStore();
storeNewVersion(store, updateStep);
}
boolean notRunYet(UpdateStep updateStep) {
return notRunYet(createGlobalStore(), updateStep);
}
boolean notRunYet(String repositoryId, RepositoryUpdateStep updateStep) {
return notRunYet(createRepositoryStore(repositoryId), updateStep);
}
private void storeNewVersion(ConfigurationEntryStore<UpdateVersionInfo> store, UpdateStepTarget updateStep) {
UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion());
store.put(updateStep.getAffectedDataType(), newVersionInfo);
}
private boolean notRunYet(ConfigurationEntryStore<UpdateVersionInfo> store, UpdateStepTarget updateStep) {
UpdateVersionInfo updateVersionInfo = store.get(updateStep.getAffectedDataType());
if (updateVersionInfo == null) {
LOG.trace("no updates for type {} run yet; step will be executed", updateStep.getAffectedDataType());
return true;
}
boolean result = updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion());
LOG.trace("latest version for type {}: {}; step will be executed: {}",
updateStep.getAffectedDataType(),
updateVersionInfo.getLatestVersion(),
result
);
return result;
}
private ConfigurationEntryStore<UpdateVersionInfo> createGlobalStore() {
return storeFactory
.withType(UpdateVersionInfo.class)
.withName(STORE_NAME)
.build();
}
private ConfigurationEntryStore<UpdateVersionInfo> createRepositoryStore(String repositoryId) {
return storeFactory
.withType(UpdateVersionInfo.class)
.withName(STORE_NAME)
.forRepository(repositoryId)
.build();
}
}