jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dpfis...@apache.org
Subject svn commit: r1242366 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/client/ main/java/org/apache/jackrabbit/mk/cluster/ main/java/org/apache/jackrabbit/mk/json/fast/ test/java/org/apache/jackrabbit/mk/cluster/ test/java/or...
Date Thu, 09 Feb 2012 15:42:47 GMT
Author: dpfister
Date: Thu Feb  9 15:42:46 2012
New Revision: 1242366

URL: http://svn.apache.org/viewvc?rev=1242366&view=rev
Log:
Add hot backup as clustering use-case
- move Jsop Parser from test to main
- return Request from Request.addXXX methods to allow chaining

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
  (with props)
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/fast/
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
      - copied unchanged from r1242363, jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
      - copied unchanged from r1242363, jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
      - copied unchanged from r1242363, jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
  (with props)
Removed:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/Request.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/Request.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/Request.java?rev=1242366&r1=1242365&r2=1242366&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/Request.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/Request.java
Thu Feb  9 15:42:46 2012
@@ -61,10 +61,11 @@ class Request implements Closeable {
      * @param name name
      * @param value value, if <code>null</code> the call is ignored
      */
-    public void addParameter(String name, String value) {
+    public Request addParameter(String name, String value) {
         if (value != null) {
             params.put(name, value);
         }
+        return this;
     }
 
     /**
@@ -74,8 +75,9 @@ class Request implements Closeable {
      * @param name name
      * @param value value
      */
-    public void addParameter(String name, int value) {
+    public Request addParameter(String name, int value) {
         params.put(name, String.valueOf(value));
+        return this;
     }
     
     /**
@@ -85,8 +87,9 @@ class Request implements Closeable {
      * @param name name
      * @param value value
      */
-    public void addParameter(String name, long value) {
+    public Request addParameter(String name, long value) {
         params.put(name, String.valueOf(value));
+        return this;
     }
     
     /**
@@ -95,8 +98,9 @@ class Request implements Closeable {
      * @param name name
      * @param in input stream
      */
-    public void addFileParameter(String name, InputStream in) {
+    public Request addFileParameter(String name, InputStream in) {
         this.in = in;
+        return this;
     }
     
     /**

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java?rev=1242366&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
Thu Feb  9 15:42:46 2012
@@ -0,0 +1,105 @@
+/*
+ * 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.mk.cluster;
+
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.json.fast.Jsop;
+import org.apache.jackrabbit.mk.json.fast.JsopArray;
+import org.apache.jackrabbit.mk.json.fast.JsopObject;
+import org.apache.jackrabbit.mk.util.PathUtils;
+
+/**
+ * This class connects two <code>MicroKernel</code> instances, where one
+ * instance will periodically commit the changes made to the other.
+ * 
+ * TODO do a full sync on first call
+ * TODO add periodic background check
+ */
+public class HotBackup {
+
+    private static final String PATH_PROPERTY_LASTREV = "/:lastrev"; 
+    private final MicroKernel source;
+    private final MicroKernel target;
+    private String lastRev;
+
+    /**
+     * Create a new instance of this class.
+     * 
+     * @param source source microkernel where changes are read
+     * @param target target microkernel where changes are committed
+     */
+    public HotBackup(MicroKernel source, MicroKernel target) {
+        this.source = source;
+        this.target = target;
+        
+        init();
+    }
+    
+    private void init() {
+        lastRev = getProperty(target, PATH_PROPERTY_LASTREV);
+        if (lastRev == null) {
+            lastRev = source.getHeadRevision();
+            
+            // TODO never sync'ed, so do a full copy
+        
+            
+            setProperty(target, PATH_PROPERTY_LASTREV, lastRev);
+        }
+        sync();
+    }
+
+    /**
+     * Read all changes from the source microkernel and commit them to
+     * the target microkernel.
+     */
+    public void sync() {
+        String headRev = source.getHeadRevision();
+        if (lastRev != headRev) {
+            JsopArray journal = (JsopArray) Jsop.parse(source.getJournal(lastRev, headRev));
+            for (int i = 0; i < journal.size(); i++) {
+                JsopObject record = (JsopObject) journal.get(i);
+                String diff = (String) record.get("changes");
+                String message = (String) record.get("msg");
+                target.commit("", diff, target.getHeadRevision(), message);
+            }
+            lastRev = headRev;
+            setProperty(target, PATH_PROPERTY_LASTREV, lastRev);
+        }
+    }
+    
+    private static String getProperty(MicroKernel mk, String path) {
+        String parent = PathUtils.getParentPath(path);
+        String name = PathUtils.getName(path);
+        
+        JsopObject props = (JsopObject) Jsop.parse(mk.getNodes(parent, mk.getHeadRevision(),
-1, 0, -1));
+        return (String) props.get(name);
+    }
+
+    private static void setProperty(MicroKernel mk, String path, String value) {
+        String parent = PathUtils.getParentPath(path);
+        String name = PathUtils.getName(path); 
+        
+        if (value == null) {
+            String diff = new JsopBuilder().tag('-').key(name).value(null).toString();
+            mk.commit(parent, diff, mk.getHeadRevision(), null);
+        } else {
+            String diff = new JsopBuilder().tag('+').key(name).value(value).toString();
+            mk.commit(parent, diff, mk.getHeadRevision(), null);
+        }
+    }
+}

Propchange: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/HotBackup.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java?rev=1242366&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
Thu Feb  9 15:42:46 2012
@@ -0,0 +1,53 @@
+/*
+ * 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.mk.cluster;
+
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class HotBackupTest {
+
+    private MicroKernel source; 
+    private MicroKernel target; 
+    
+    @Before
+    public void setUp() throws Exception {
+        source = MicroKernelFactory.getInstance("fs:{homeDir}/target/mk1;clean");
+        target = MicroKernelFactory.getInstance("fs:{homeDir}/target/mk2;clean");
+    }
+    
+    @After
+    public void tearDown() {
+        if (source != null) {
+            source.dispose();
+        }
+        if (target != null) {
+            target.dispose();
+        }
+    }
+    
+    @Test
+    public void test() {
+        HotBackup hotbackup = new HotBackup(source, target);
+        source.commit("/", "+\"test\":{}", source.getHeadRevision(), null);
+        hotbackup.sync();
+        target.getNodes("/test", target.getHeadRevision());
+    }
+}

Propchange: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/cluster/HotBackupTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url



Mime
View raw message