1 package org.apache.helix.filestore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23
24 public class RsyncInvoker
25 {
26 private Thread backgroundThread;
27 private final String remoteHost;
28 private final String remoteLogDir;
29 private final String localLogDir;
30
31 public RsyncInvoker(String remoteHost, String remoteBaseDir,
32 String localBaseDir)
33 {
34 this.remoteHost = remoteHost;
35 this.remoteLogDir = remoteBaseDir;
36 this.localLogDir = localBaseDir;
37 }
38
39 public boolean rsync(String relativePath)
40 {
41 int exitVal = -1;
42 try
43 {
44 ProcessBuilder pb = new ProcessBuilder( "rsync",
45 remoteLogDir+"/"+ relativePath , localLogDir);
46 System.out.println("Rsyncing source:"+ remoteLogDir+"/"+ relativePath + " dest:"+ localLogDir);
47 ExternalCommand ec = new ExternalCommand(pb);
48 ec.start();
49 exitVal = ec.waitFor();
50 if (exitVal != 0)
51 {
52 System.out.println("Failed to rsync "+ ec.getStringError());
53 } else
54 {
55 return true;
56 }
57 } catch (IOException e)
58 {
59 e.printStackTrace();
60 } catch (InterruptedException e)
61 {
62 e.printStackTrace();
63 }
64
65 return false;
66 }
67 public boolean stop(){
68 if( backgroundThread!=null){
69 backgroundThread.interrupt();
70 }
71 return true;
72 }
73 public boolean runInBackground()
74 {
75 backgroundThread = new Thread(new Runnable()
76 {
77 public void run()
78 {
79 try
80 {
81 int sleep = 1000;
82 while (true)
83 {
84 int exitVal = -1;
85 try
86 {
87 Thread.sleep(sleep);
88 ProcessBuilder pb = new ProcessBuilder( "rsync", "-rvt",
89 remoteLogDir+"/", localLogDir);
90
91 ExternalCommand ec;
92 ec = new ExternalCommand(pb);
93 ec.start();
94 exitVal = ec.waitFor();
95 String stringError = ec.getStringError();
96 if(stringError!=null && stringError.length()>0)
97 System.err.println(stringError);
98
99
100 } catch (IOException e)
101 {
102 e.printStackTrace();
103 }
104 if (exitVal != 0)
105 {
106
107 sleep = Math.min(2 * sleep, 2 * 60 * 1000);
108 System.out.println("Failed to rsync retrying in " + sleep / 1000
109 + " seconds");
110 } else
111 {
112 sleep = 1000;
113 }
114 }
115 } catch (InterruptedException e)
116 {
117 e.printStackTrace();
118 Thread.currentThread().interrupt();
119 }
120 }
121 });
122 backgroundThread.start();
123 return true;
124 }
125 public static void main(String[] args)
126 {
127 String remoteHost= "localhost";
128 String remoteLogDir="data/localhost_12000/translog";
129 String localLogDir="data/localhost_12001/translog";
130 RsyncInvoker rsyncInvoker = new RsyncInvoker(remoteHost, remoteLogDir, localLogDir);
131 rsyncInvoker.runInBackground();
132 }
133 }