Author: frm
Date: Thu Jul 27 09:35:38 2017
New Revision: 1803154
URL: http://svn.apache.org/viewvc?rev=1803154&view=rev
Log:
OAK-6458 - Extract manifest checking logic in ManifestChecker
Added:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java
(with props)
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java
(with props)
Modified:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/AbstractFileStore.java
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
Modified:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/AbstractFileStore.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/AbstractFileStore.java?rev=1803154&r1=1803153&r2=1803154&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/AbstractFileStore.java
(original)
+++
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/AbstractFileStore.java
Thu Jul 27 09:35:38 2017
@@ -24,6 +24,7 @@ import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.file.Files;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -70,12 +71,6 @@ public abstract class AbstractFileStore
private static final String MANIFEST_FILE_NAME = "manifest";
/**
- * This value can be used as an invalid store version, since the store
- * version is defined to be strictly greater than zero.
- */
- private static final int INVALID_STORE_VERSION = 0;
-
- /**
* The minimum supported store version. It is possible for an
implementation
* to support in a transparent and backwards-compatible way older versions
* of a repository. In this case, the minimum supported store version
@@ -83,7 +78,7 @@ public abstract class AbstractFileStore
* implementation. The minimum store version has to be greater than zero
and
* less than or equal to the maximum store version.
*/
- static final int MIN_STORE_VERSION = 1;
+ private static final int MIN_STORE_VERSION = 1;
/**
* The maximum supported store version. It is possible for an
implementation
@@ -93,9 +88,18 @@ public abstract class AbstractFileStore
* implementation. The maximum supported store version has to be greater
* than zero and greater than or equal to the minimum store version.
*/
- static final int MAX_STORE_VERSION = 2;
+ private static final int MAX_STORE_VERSION = 2;
- protected static boolean notEmptyDirectory(File path) {
+ static ManifestChecker newManifestChecker(File directory) {
+ return ManifestChecker.newManifestChecker(
+ new File(directory, MANIFEST_FILE_NAME),
+ notEmptyDirectory(directory),
+ MIN_STORE_VERSION,
+ MAX_STORE_VERSION
+ );
+ }
+
+ private static boolean notEmptyDirectory(File path) {
Collection<File> entries = FileUtils.listFiles(path, new String[]
{"tar"}, false);
checkArgument(entries != null, "{} is not a directory, or an I/O error
occurred", path);
return entries.size() > 0;
@@ -149,46 +153,6 @@ public abstract class AbstractFileStore
return new SegmentNotFoundException(id, e);
}
- File getManifestFile() {
- return new File(directory, MANIFEST_FILE_NAME);
- }
-
- Manifest openManifest() throws IOException {
- File file = getManifestFile();
-
- if (file.exists()) {
- return Manifest.load(file);
- }
-
- return null;
- }
-
- static Manifest checkManifest(Manifest manifest) throws
InvalidFileStoreVersionException {
- if (manifest == null) {
- throw new InvalidFileStoreVersionException("Using oak-segment-tar,
but oak-segment should be used");
- }
-
- int storeVersion = manifest.getStoreVersion(INVALID_STORE_VERSION);
-
- // A store version less than or equal to the highest invalid value
means
- // that something or someone is messing up with the manifest. This
error
- // is not recoverable and is thus represented as an ISE.
-
- if (storeVersion <= INVALID_STORE_VERSION) {
- throw new IllegalStateException("Invalid store version");
- }
-
- if (storeVersion < MIN_STORE_VERSION) {
- throw new InvalidFileStoreVersionException("Using a too recent
version of oak-segment-tar");
- }
-
- if (storeVersion > MAX_STORE_VERSION) {
- throw new InvalidFileStoreVersionException("Using a too old
version of oak-segment tar");
- }
-
- return manifest;
- }
-
@Nonnull
public CacheStatsMBean getSegmentCacheStats() {
return segmentCache.getCacheStats();
Modified:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java?rev=1803154&r1=1803153&r2=1803154&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
(original)
+++
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
Thu Jul 27 09:35:38 2017
@@ -192,13 +192,7 @@ public class FileStore extends AbstractF
builder.getCacheManager(),
builder.getStatsProvider());
- Manifest manifest = Manifest.empty();
-
- if (notEmptyDirectory(directory)) {
- manifest = checkManifest(openManifest());
- }
-
- saveManifest(manifest);
+ newManifestChecker(directory).checkAndUpdateManifest();
this.stats = new FileStoreStats(builder.getStatsProvider(), this, 0);
this.tarFiles = TarFiles.builder()
@@ -257,16 +251,6 @@ public class FileStore extends AbstractF
return this;
}
- private void saveManifest(Manifest manifest) throws IOException {
- // Always update the store version to the maximum supported store
- // version. In doing so, we prevent older implementations from
tampering
- // with the store's data, which from this moment on could be written in
- // a format that an older implementation might not be able to
- // understand.
- manifest.setStoreVersion(MAX_STORE_VERSION);
- manifest.save(getManifestFile());
- }
-
@Nonnull
private Supplier<RecordId> initialNode() {
return new Supplier<RecordId>() {
Added:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java?rev=1803154&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java
(added)
+++
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java
Thu Jul 27 09:35:38 2017
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.io.File;
+import java.io.IOException;
+
+class ManifestChecker {
+
+ static ManifestChecker newManifestChecker(File path, boolean shouldExist,
int minStoreVersion, int maxStoreVersion) {
+ checkArgument(path != null, "path");
+ checkArgument(minStoreVersion > 0, "minStoreVersion");
+ checkArgument(maxStoreVersion > 0, "maxStoreVersion");
+ return new ManifestChecker(path, shouldExist, minStoreVersion,
maxStoreVersion);
+ }
+
+ private final File file;
+
+ private final boolean shouldExist;
+
+ private final int minStoreVersion;
+
+ private final int maxStoreVersion;
+
+ private ManifestChecker(File file, boolean shouldExist, int
minStoreVersion, int maxStoreVersion) {
+ this.file = file;
+ this.shouldExist = shouldExist;
+ this.minStoreVersion = minStoreVersion;
+ this.maxStoreVersion = maxStoreVersion;
+ }
+
+ void checkAndUpdateManifest() throws IOException,
InvalidFileStoreVersionException {
+ Manifest manifest = openManifest();
+ checkManifest(manifest);
+ updateManifest(manifest);
+ }
+
+ void checkManifest() throws IOException, InvalidFileStoreVersionException {
+ checkManifest(openManifest());
+ }
+
+ private Manifest openManifest() throws IOException,
InvalidFileStoreVersionException {
+ if (file.exists()) {
+ return Manifest.load(file);
+ }
+ if (shouldExist) {
+ throw new InvalidFileStoreVersionException("Using oak-segment-tar,
but oak-segment should be used");
+ }
+ return Manifest.empty();
+ }
+
+ private void checkManifest(Manifest manifest) throws
InvalidFileStoreVersionException {
+ checkStoreVersion(manifest.getStoreVersion(maxStoreVersion));
+ }
+
+ private void checkStoreVersion(int storeVersion) throws
InvalidFileStoreVersionException {
+ // A store version less than or equal to the highest invalid value
means
+ // that something or someone is messing up with the manifest. This
error
+ // is not recoverable and is thus represented as an ISE.
+ if (storeVersion <= 0) {
+ throw new IllegalStateException("Invalid store version");
+ }
+ if (storeVersion < minStoreVersion) {
+ throw new InvalidFileStoreVersionException("Using a too recent
version of oak-segment-tar");
+ }
+ if (storeVersion > maxStoreVersion) {
+ throw new InvalidFileStoreVersionException("Using a too old
version of oak-segment tar");
+ }
+ }
+
+ private void updateManifest(Manifest manifest) throws IOException {
+ // Always update the store version to the maximum supported store
+ // version. In doing so, we prevent older implementations from
tampering
+ // with the store's data, which from this moment on could be written in
+ // a format that an older implementation might not be able to
+ // understand.
+ manifest.setStoreVersion(maxStoreVersion);
+ manifest.save(file);
+ }
+
+}
Propchange:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ManifestChecker.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java?rev=1803154&r1=1803153&r2=1803154&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
(original)
+++
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
Thu Jul 27 09:35:38 2017
@@ -63,9 +63,7 @@ public class ReadOnlyFileStore extends A
ReadOnlyFileStore(FileStoreBuilder builder) throws
InvalidFileStoreVersionException, IOException {
super(builder);
- if (notEmptyDirectory(directory)) {
- checkManifest(openManifest());
- }
+ newManifestChecker(directory).checkManifest();
tarFiles = TarFiles.builder()
.withDirectory(directory)
Added:
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java?rev=1803154&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java
(added)
+++
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java
Thu Jul 27 09:35:38 2017
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file;
+
+import static
org.apache.jackrabbit.oak.segment.file.ManifestChecker.newManifestChecker;
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.nio.file.Files;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class ManifestCheckerTest {
+
+ @Rule
+ public TemporaryFolder root = new TemporaryFolder(new File("target"));
+
+ private File manifest;
+
+ @Before
+ public void setUp() throws Exception {
+ manifest = root.newFile();
+ }
+
+ @Test(expected = InvalidFileStoreVersionException.class)
+ public void testManifestShouldExist() throws Exception {
+ Files.delete(manifest.toPath());
+ newManifestChecker(manifest, true, 1, 2).checkManifest();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testInvalidVersion() throws Exception {
+ Manifest m = Manifest.load(manifest);
+ m.setStoreVersion(0);
+ m.save(manifest);
+ newManifestChecker(manifest, true, 1, 2).checkManifest();
+ }
+
+ @Test(expected = InvalidFileStoreVersionException.class)
+ public void testVersionTooLow() throws Exception {
+ Manifest m = Manifest.load(manifest);
+ m.setStoreVersion(1);
+ m.save(manifest);
+ newManifestChecker(manifest, true, 2, 3).checkManifest();
+ }
+
+ @Test(expected = InvalidFileStoreVersionException.class)
+ public void testVersionTooHigh() throws Exception {
+ Manifest m = Manifest.load(manifest);
+ m.setStoreVersion(4);
+ m.save(manifest);
+ newManifestChecker(manifest, true, 2, 3).checkManifest();
+ }
+
+ @Test
+ public void testUpdateExistingManifest() throws Exception {
+ Manifest before = Manifest.load(manifest);
+ before.setStoreVersion(2);
+ before.save(manifest);
+ newManifestChecker(manifest, true, 2, 3).checkAndUpdateManifest();
+ assertEquals(3, Manifest.load(manifest).getStoreVersion(0));
+ }
+
+ @Test
+ public void testUpdateNonExistingManifest() throws Exception {
+ Files.delete(manifest.toPath());
+ newManifestChecker(manifest, false, 2, 3).checkAndUpdateManifest();
+ assertEquals(3, Manifest.load(manifest).getStoreVersion(0));
+ }
+
+}
Propchange:
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/ManifestCheckerTest.java
------------------------------------------------------------------------------
svn:eol-style = native