Author: alexparvulescu Date: Tue Dec 3 11:03:07 2013 New Revision: 1547342
URL: http://svn.apache.org/r1547342 Log: OAK-1159 Backup and restore - backup initial bits Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java (with props) jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java (with props) Modified: jackrabbit/oak/trunk/oak-core/pom.xml jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentRootBuilder.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java Modified: jackrabbit/oak/trunk/oak-core/pom.xml URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/pom.xml?rev=1547342&r1=1547341&r2=1547342&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/pom.xml (original) +++ jackrabbit/oak/trunk/oak-core/pom.xml Tue Dec 3 11:03:07 2013 @@ -47,7 +47,7 @@ org.apache.jackrabbit.oak.kernel, org.apache.jackrabbit.oak.util, org.apache.jackrabbit.oak.namepath, - org.apache.jackrabbit.oak.plugins.value, + org.apache.jackrabbit.oak.plugins.backup, org.apache.jackrabbit.oak.plugins.commit, org.apache.jackrabbit.oak.plugins.identifier, org.apache.jackrabbit.oak.plugins.index, @@ -61,6 +61,7 @@ org.apache.jackrabbit.oak.plugins.nodetype.write, org.apache.jackrabbit.oak.plugins.observation, org.apache.jackrabbit.oak.plugins.observation.filter, + org.apache.jackrabbit.oak.plugins.value, org.apache.jackrabbit.oak.plugins.version, org.apache.jackrabbit.oak.spi.query, org.apache.jackrabbit.oak.spi.commit, Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java?rev=1547342&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java Tue Dec 3 11:03:07 2013 @@ -0,0 +1,80 @@ +/* + * 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.plugins.backup; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import org.apache.jackrabbit.oak.plugins.segment.Journal; +import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeState; +import org.apache.jackrabbit.oak.plugins.segment.SegmentRootBuilder; +import org.apache.jackrabbit.oak.plugins.segment.file.FileStore; +import org.apache.jackrabbit.oak.spi.state.ApplyDiff; +import org.apache.jackrabbit.oak.spi.state.NodeState; +import org.apache.jackrabbit.oak.spi.state.NodeStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileStoreBackup { + + private static final Logger log = LoggerFactory + .getLogger(FileStoreBackup.class); + + private static final long DEFAULT_LIFETIME = TimeUnit.HOURS.toMillis(1); + + static int CACHE_SIZE = 256; + + public static void backup(NodeStore store, File destination) + throws IOException { + long s = System.currentTimeMillis(); + + // 1. create a new checkpoint with the current state + String checkpoint = store.checkpoint(DEFAULT_LIFETIME); + NodeState current = store.retrieve(checkpoint); + if (current == null) { + log.debug("Unable to retrieve checkpoint {}", checkpoint); + return; + } + + // 2. init filestore + destination.mkdirs(); + FileStore backup = null; + try { + backup = new FileStore(destination, current, CACHE_SIZE, + CACHE_SIZE, false); + + // TODO optimize incremental backup + Journal root = backup.getJournal("root"); + SegmentNodeState state = new SegmentNodeState(backup.getWriter() + .getDummySegment(), root.getHead()); + SegmentRootBuilder builder = state.builder(); + current.compareAgainstBaseState(state, + new ApplyDiff(builder.child("root"))); + root.setHead(state.getRecordId(), builder.getNodeState() + .getRecordId()); + + } finally { + if (backup != null) { + backup.close(); + } + log.debug("Backup done in {} ms.", System.currentTimeMillis() - s); + } + } +} Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackup.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentRootBuilder.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentRootBuilder.java?rev=1547342&r1=1547341&r2=1547342&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentRootBuilder.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentRootBuilder.java Tue Dec 3 11:03:07 2013 @@ -18,7 +18,7 @@ package org.apache.jackrabbit.oak.plugin import org.apache.jackrabbit.oak.spi.state.NodeState; -class SegmentRootBuilder extends SegmentNodeBuilder { +public class SegmentRootBuilder extends SegmentNodeBuilder { /** * Number of content updates that need to happen before the updates Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java?rev=1547342&r1=1547341&r2=1547342&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java Tue Dec 3 11:03:07 2013 @@ -41,6 +41,7 @@ import org.apache.jackrabbit.oak.plugins import org.apache.jackrabbit.oak.plugins.segment.Segment; import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeState; import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeState; public class FileStore extends AbstractStore { @@ -66,11 +67,16 @@ public class FileStore extends AbstractS public FileStore(File directory, int maxFileSizeMB, boolean memoryMapping) throws IOException { - this(directory, maxFileSizeMB, DEFAULT_MEMORY_CACHE_SIZE, memoryMapping); + this(directory, EMPTY_NODE, maxFileSizeMB, DEFAULT_MEMORY_CACHE_SIZE, memoryMapping); } public FileStore(File directory, int maxFileSizeMB, int cacheSizeMB, boolean memoryMapping) throws IOException { + this(directory, EMPTY_NODE, maxFileSizeMB, cacheSizeMB, memoryMapping); + } + + public FileStore(File directory, NodeState initial, int maxFileSizeMB, + int cacheSizeMB, boolean memoryMapping) throws IOException { super(cacheSizeMB); checkNotNull(directory).mkdirs(); this.directory = directory; @@ -118,7 +124,7 @@ public class FileStore extends AbstractS if (!journals.containsKey("root")) { NodeBuilder builder = EMPTY_NODE.builder(); - builder.setChildNode("root", EMPTY_NODE); + builder.setChildNode("root", initial); journals.put("root", new FileJournal(this, builder.getNodeState())); writeJournals(); } Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java?rev=1547342&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java (added) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java Tue Dec 3 11:03:07 2013 @@ -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.plugins.backup; + +import static org.apache.commons.io.FileUtils.deleteQuietly; +import static org.apache.jackrabbit.oak.plugins.backup.FileStoreBackup.CACHE_SIZE; +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; + +import org.apache.jackrabbit.oak.Oak; +import org.apache.jackrabbit.oak.api.CommitFailedException; +import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent; +import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeStore; +import org.apache.jackrabbit.oak.plugins.segment.file.FileStore; +import org.apache.jackrabbit.oak.spi.commit.EmptyHook; +import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeStore; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileStoreBackupTest { + + private File src; + private File destination; + + @Before + public void before() { + long run = System.currentTimeMillis(); + File root = new File("target"); + src = new File(root, "tar-src-" + run); + destination = new File(root, "tar-dest-" + run); + } + + @After + public void after() { + deleteQuietly(src); + deleteQuietly(destination); + } + + @Test + public void testBackup() throws Exception { + NodeStore store = newSegmentNodeStore(src); + init(store); + + // initial content + FileStoreBackup.backup(store, destination); + + compare(store, destination); + + addTestContent(store); + FileStoreBackup.backup(store, destination); + compare(store, destination); + } + + private static void addTestContent(NodeStore store) + throws CommitFailedException { + NodeBuilder builder = store.getRoot().builder(); + builder.child("test-backup"); + store.merge(builder, EmptyHook.INSTANCE, null); + } + + private static void compare(NodeStore store, File destination) + throws IOException { + NodeStore backup = newSegmentNodeStore(destination); + assertEquals(store.getRoot(), backup.getRoot()); + } + + private static void init(NodeStore store) { + new Oak(store).with(new OpenSecurityProvider()) + .with(new InitialContent()).createContentRepository(); + } + + private static SegmentNodeStore newSegmentNodeStore(File file) + throws IOException { + return new SegmentNodeStore(new FileStore(file, CACHE_SIZE, CACHE_SIZE, + true)); + } +} Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL
