Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/BackupIndexWriter.java @ 1674:af18eacf187c
automatic restore
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 29 May 2022 23:38:34 -0600 |
parents | 1b9f9fdb3b41 |
children | 445048f9eca7 |
rev | line source |
---|---|
1488 | 1 package goodjava.lucene.backup; |
2 | |
3 import java.io.File; | |
1499 | 4 import java.io.InputStream; |
1674 | 5 import java.io.OutputStream; |
6 import java.io.BufferedOutputStream; | |
7 import java.io.FileOutputStream; | |
1488 | 8 import java.io.IOException; |
1499 | 9 import java.net.Socket; |
1672 | 10 import java.net.ConnectException; |
1488 | 11 import java.util.List; |
12 import java.util.ArrayList; | |
1499 | 13 import java.util.Map; |
14 import java.util.HashMap; | |
15 import java.util.Arrays; | |
1504 | 16 import java.util.concurrent.Executors; |
17 import java.util.concurrent.ExecutorService; | |
1672 | 18 import java.util.concurrent.ExecutionException; |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
19 import org.apache.lucene.search.SortField; |
1488 | 20 import goodjava.io.IoUtils; |
1499 | 21 import goodjava.rpc.RpcClient; |
22 import goodjava.rpc.RpcCall; | |
23 import goodjava.rpc.RpcResult; | |
24 import goodjava.rpc.RpcException; | |
1488 | 25 import goodjava.lucene.api.LuceneIndexWriter; |
26 import goodjava.lucene.logging.LoggingIndexWriter; | |
27 import goodjava.lucene.logging.LogFile; | |
1672 | 28 import goodjava.logging.Logger; |
29 import goodjava.logging.LoggerFactory; | |
1488 | 30 |
31 | |
1672 | 32 public final class BackupIndexWriter extends LoggingIndexWriter { |
1499 | 33 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class); |
34 public static String[] backupDomains; | |
1488 | 35 private final String name; |
36 private final File dir; | |
1499 | 37 private boolean isSyncPending = false; |
1504 | 38 private final ExecutorService exec = Executors.newSingleThreadExecutor(); |
1488 | 39 |
1673
1b9f9fdb3b41
remove backup password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1672
diff
changeset
|
40 public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,long logTime,String name) |
1548 | 41 throws IOException |
42 { | |
43 super(indexWriter,logDir,logTime); | |
1499 | 44 if( backupDomains == null ) |
45 throw new RuntimeException("must set backupDomains"); | |
1488 | 46 this.name = name; |
47 File f = new File(System.getProperty("java.io.tmpdir")); | |
48 dir = new File(f,"goodjava.lucene/"+name); | |
1501 | 49 IoUtils.mkdirs(dir); |
1488 | 50 } |
51 | |
1672 | 52 @Override public synchronized void close() throws IOException { |
1504 | 53 super.close(); |
54 exec.shutdown(); | |
55 } | |
56 | |
1672 | 57 @Override public synchronized void commit() throws IOException { |
1488 | 58 super.commit(); |
1499 | 59 //sync(); |
60 if( !isSyncPending ) { | |
1504 | 61 exec.execute(sync); |
1499 | 62 isSyncPending = true; |
1488 | 63 } |
64 } | |
65 | |
1672 | 66 @Override protected boolean doCheck(SortField sortField) throws IOException { |
1538
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
67 boolean ok = super.doCheck(sortField); |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
68 if( ok ) |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
69 runSyncWithChecksum(); |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
70 return ok; |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
71 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
72 |
1499 | 73 public void runSync() { |
1672 | 74 exec(sync); |
1499 | 75 } |
76 | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
77 public void runSyncWithChecksum() { |
1672 | 78 exec(syncWithChecksum); |
79 } | |
80 | |
81 private void exec(Runnable r) { | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
82 try { |
1672 | 83 exec.submit(r).get(); |
84 } catch(InterruptedException e) { | |
85 throw new RuntimeException(e); | |
86 } catch(ExecutionException e) { | |
87 Throwable cause = e.getCause(); | |
88 if( cause instanceof RuntimeException ) | |
89 throw (RuntimeException)cause; | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
90 throw new RuntimeException(e); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
91 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
92 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
93 |
1499 | 94 private final Runnable sync = new Runnable() { |
1504 | 95 public void run() { |
1499 | 96 try { |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
97 sync(false); |
1672 | 98 } catch(ConnectException e) { |
99 logger.error("sync failed: "+e.getMessage()); | |
1499 | 100 } catch(IOException e) { |
101 throw new RuntimeException(e); | |
102 } | |
103 } | |
104 }; | |
105 | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
106 private final Runnable syncWithChecksum = new Runnable() { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
107 public void run() { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
108 try { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
109 sync(true); |
1672 | 110 } catch(ConnectException e) { |
111 logger.error("syncWithChecksum failed: "+e.getMessage()); | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
112 } catch(IOException e) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
113 throw new RuntimeException(e); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
114 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
115 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
116 }; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
117 |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
118 private void sync(boolean withChecksum) throws IOException { |
1499 | 119 List<LogFile> logs = new ArrayList<LogFile>(); |
120 synchronized(this) { | |
121 isSyncPending = false; | |
1500 | 122 clearDir(); |
1499 | 123 for( LogFile log : this.logs ) { |
124 File f = new File(dir,log.file.getName()); | |
125 IoUtils.link(log.file,f); | |
126 logs.add( new LogFile(f) ); | |
127 } | |
128 } | |
129 List logInfo = new ArrayList(); | |
130 Map<String,LogFile> logMap = new HashMap<String,LogFile>(); | |
131 for( LogFile log : logs ) { | |
132 Map fileInfo = new HashMap(); | |
133 fileInfo.put("name",log.file.getName()); | |
134 fileInfo.put("end",log.end()); | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
135 if( withChecksum ) |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
136 fileInfo.put("checksum",log.checksum()); |
1499 | 137 logInfo.add(fileInfo); |
138 logMap.put(log.file.getName(),log); | |
139 } | |
140 for( String backupDomain : backupDomains ) { | |
1509 | 141 RpcClient rpc = BackupServer.rpcClient(backupDomain); |
1499 | 142 try { |
1673
1b9f9fdb3b41
remove backup password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1672
diff
changeset
|
143 RpcCall call = new RpcCall("login",name); |
1512 | 144 rpc.write(call); |
145 rpc.read(); | |
146 call = new RpcCall("check",logInfo); | |
1499 | 147 while(true) { |
148 rpc.write(call); | |
149 RpcResult result = rpc.read(); | |
1512 | 150 //logger.info(Arrays.asList(result.returnValues).toString()); |
1499 | 151 String status = (String)result.returnValues[0]; |
152 if( status.equals("ok") ) { | |
153 break; | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
154 } else if( status.equals("missing") || status.equals("bad_checksum") ) { |
1499 | 155 String fileName = (String)result.returnValues[1]; |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
156 if( status.equals("bad_checksum") ) |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
157 logger.error("bad_checksum "+fileName); |
1499 | 158 LogFile log = logMap.get(fileName); |
159 long len = log.end() - 8; | |
160 InputStream in = log.input(); | |
1512 | 161 call = new RpcCall(in,len,"add",logInfo,fileName); |
1499 | 162 } else if( status.equals("incomplete") ) { |
163 String fileName = (String)result.returnValues[1]; | |
164 long logEnd = (Long)result.returnValues[2]; | |
165 LogFile log = logMap.get(fileName); | |
166 long len = log.end() - logEnd; | |
167 InputStream in = log.input(); | |
168 in.skip(logEnd-8); | |
1512 | 169 call = new RpcCall(in,len,"append",logInfo,fileName); |
1499 | 170 } else |
171 throw new RuntimeException("status "+status); | |
172 } | |
173 } catch(RpcException e) { | |
1672 | 174 logger.error("",e); |
1499 | 175 } |
176 rpc.close(); | |
177 } | |
1500 | 178 clearDir(); |
179 } | |
180 | |
181 private void clearDir() throws IOException { | |
182 for( File f : dir.listFiles() ) { | |
183 IoUtils.delete(f); | |
184 } | |
1499 | 185 } |
186 | |
1674 | 187 public static BackupIndexWriter newWithRestore(LuceneIndexWriter indexWriter,File logDir,long logTime,String name) |
188 throws IOException | |
189 { | |
190 if( !logDir.exists() ) { | |
191 RpcClient rpc = BackupServer.rpcClient(backupDomains[0]); | |
192 try { | |
193 RpcCall call; | |
194 RpcResult result; | |
195 call = new RpcCall("exists",name); | |
196 rpc.write(call); | |
197 result = rpc.read(); | |
198 boolean exists = (Boolean)result.returnValues[0]; | |
199 if( exists ) { | |
200 logger.error("restoring "+logDir+" from backup"); | |
201 File zip = File.createTempFile("luan_",".zip"); | |
202 IoUtils.delete(zip); | |
203 call = new RpcCall("login",name); | |
204 rpc.write(call); | |
205 rpc.read(); | |
206 call = new RpcCall("zip"); | |
207 rpc.write(call); | |
208 result = rpc.read(); | |
209 OutputStream out = new BufferedOutputStream(new FileOutputStream(zip)); | |
210 IoUtils.copyAll(result.in,out); | |
211 out.close(); | |
212 IoUtils.mkdirs(logDir); | |
213 String cmd = "unzip " + zip; | |
214 Process proc = Runtime.getRuntime().exec(cmd,null,logDir); | |
215 IoUtils.waitFor(proc); | |
216 IoUtils.delete(zip); | |
217 } | |
218 } catch(RpcException e) { | |
219 throw new RuntimeException(e); | |
220 } | |
221 rpc.close(); | |
222 } | |
223 return new BackupIndexWriter(indexWriter,logDir,logTime,name); | |
224 } | |
225 | |
1488 | 226 } |