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

[JENKINS-62757] Introduce fingerprint migration in External Storage API #4825

Merged
merged 18 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
23 changes: 21 additions & 2 deletions core/src/main/java/hudson/model/Fingerprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,10 @@ public Api getApi() {
* Loads a {@link Fingerprint} from the Storage with the given unique id.
* @return Loaded {@link Fingerprint}. {@code null} if the config file does not exist or
* malformed.
*
* In case an external storage is configured on top of a file system based storage:
* 1. External storage is polled to retrieve the fingerprint
* 2. If not found, then the local storage is polled to retrieve the fingerprint
*/
public static @CheckForNull Fingerprint load(@NonNull String id) throws IOException {
long start=0;
Expand All @@ -1324,7 +1328,17 @@ public Api getApi() {
}

Fingerprint loaded = FingerprintStorage.get().load(id);
initFacets(loaded);
if (loaded == null && !(FingerprintStorage.get() instanceof FileFingerprintStorage) &&
FingerprintStorage.getFileFingerprintStorage().isReady()) {
loaded = FingerprintStorage.getFileFingerprintStorage().load(id);
if (loaded != null) {
initFacets(loaded);
FingerprintStorage.get().save(loaded);
FingerprintStorage.getFileFingerprintStorage().delete(id);
}
} else {
initFacets(loaded);
}

if(logger.isLoggable(Level.FINE)) {
logger.fine("Loading fingerprint took " + (System.currentTimeMillis() - start) + "ms");
Expand Down Expand Up @@ -1357,12 +1371,17 @@ public Api getApi() {
}

/**
* Deletes the {@link Fingerprint} in the Storage with the given unique id.
* Deletes the {@link Fingerprint} in the configured storage with the given unique id.
*
* @since TODO
*/
public static void delete(@NonNull String id) throws IOException {
FingerprintStorage.get().delete(id);

if (!(FingerprintStorage.get() instanceof FileFingerprintStorage) &&
FingerprintStorage.getFileFingerprintStorage().isReady()) {
FingerprintStorage.getFileFingerprintStorage().delete(id);
}
}

/**
Expand Down
118 changes: 118 additions & 0 deletions test/src/test/java/jenkins/fingerprints/FingerprintStorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* The MIT License
*
* Copyright (c) 2020, Jenkins project 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 jenkins.fingerprints;

import hudson.Util;
import hudson.model.Fingerprint;
import hudson.model.FingerprintCleanupThreadTest;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;

public class FingerprintStorageTest {

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

@Test
public void testLoadingAndSavingLocalStorageFingerprintWithExternalStorage() throws IOException {
String id = Util.getDigestOf("testLoadingAndSavingLocalStorageFingerprintWithExternalStorage");
Fingerprint fingerprintSaved = new Fingerprint(null, "foo.jar", Util.fromHexString(id));
Fingerprint fingerprintLoaded = Fingerprint.load(id);
assertThat(fingerprintLoaded, is(not(nullValue())));
assertThat(fingerprintSaved.toString(), is(equalTo(fingerprintLoaded.toString())));

// After external storage is configured, check if local storage fingerprint is still accessible.
FingerprintStorage externalFingerprintStorage = configureExternalStorage();
fingerprintLoaded = Fingerprint.load(id);
assertThat(fingerprintLoaded, is(not(nullValue())));
assertThat(fingerprintSaved.toString(), is(equalTo(fingerprintLoaded.toString())));

// After loading the fingerprint, ensure it was moved to external storage.
fingerprintLoaded = externalFingerprintStorage.load(id);
assertThat(fingerprintLoaded, is(not(nullValue())));
assertThat(fingerprintSaved.toString(), is(equalTo(fingerprintLoaded.toString())));

// Ensure that the loaded fingerprint was deleted from local storage after being loaded.
fingerprintLoaded = FingerprintStorage.getFileFingerprintStorage().load(id);
assertThat(fingerprintLoaded, is(nullValue()));
}

@Test
public void testLoadingAndSavingFingerprintWithExternalStorage() throws IOException {
FingerprintStorage externalFingerprintStorage = configureExternalStorage();
String id = Util.getDigestOf("testLoadingAndSavingFingerprintWithExternalStorage");
Fingerprint fingerprintSaved = new Fingerprint(null, "bar.jar", Util.fromHexString(id));
Fingerprint fingerprintLoaded = externalFingerprintStorage.load(id);
assertThat(fingerprintLoaded, is(not(nullValue())));
assertThat(fingerprintSaved.toString(), is(equalTo(fingerprintLoaded.toString())));
}

@Test
public void testDeletingLocalStorageFingerprintWithExternalStorageBeforeMigration() throws IOException {
String id = Util.getDigestOf("testLoadingAndSavingLocalStorageFingerprintWithExternalStorage");
new Fingerprint(null, "foo.jar", Util.fromHexString(id));
configureExternalStorage();
Fingerprint.delete(id);
Fingerprint fingerprintLoaded = FingerprintStorage.getFileFingerprintStorage().load(id);
assertThat(fingerprintLoaded, is(nullValue()));
}

@Test
public void testDeletingLocalStorageFingerprintWithExternalStorageAfterMigration() throws IOException {
String id = Util.getDigestOf("testLoadingAndSavingLocalStorageFingerprintWithExternalStorage");
new Fingerprint(null, "foo.jar", Util.fromHexString(id));
FingerprintStorage externalFingerprintStorage = configureExternalStorage();
Fingerprint.load(id);
Fingerprint.delete(id);
Fingerprint fingerprintLoaded = externalFingerprintStorage.load(id);
assertThat(fingerprintLoaded, is(nullValue()));
}

@Test
public void testDeletingFingerprintWithExternalStorage() throws IOException {
configureExternalStorage();
String id = Util.getDigestOf("testLoadingAndSavingLocalStorageFingerprintWithExternalStorage");
configureExternalStorage();
new Fingerprint(null, "foo.jar", Util.fromHexString(id));
Fingerprint.delete(id);
Fingerprint fingerprintLoaded = Fingerprint.load(id);
assertThat(fingerprintLoaded, is(nullValue()));
}

private FingerprintStorage configureExternalStorage() {
FingerprintStorage fingerprintStorage = new FingerprintCleanupThreadTest.TestExternalFingerprintStorage();
GlobalFingerprintConfiguration.get().setStorage(fingerprintStorage);
return fingerprintStorage;
}

}