Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/BackupIndexWriter.java @ 1547:f24a9ba7551e
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 24 Sep 2020 15:33:56 -0600 |
parents | 634f6765830e |
children | 736ec76bbf42 |
rev | line source |
---|---|
1488 | 1 package goodjava.lucene.backup; |
2 | |
3 import java.io.File; | |
1499 | 4 import java.io.InputStream; |
1488 | 5 import java.io.IOException; |
1499 | 6 import java.net.Socket; |
1488 | 7 import java.util.List; |
8 import java.util.ArrayList; | |
1499 | 9 import java.util.Map; |
10 import java.util.HashMap; | |
11 import java.util.Arrays; | |
1504 | 12 import java.util.concurrent.Executors; |
13 import java.util.concurrent.ExecutorService; | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
14 import org.apache.lucene.search.SortField; |
1488 | 15 import goodjava.io.IoUtils; |
1499 | 16 import goodjava.rpc.RpcClient; |
17 import goodjava.rpc.RpcCall; | |
18 import goodjava.rpc.RpcResult; | |
19 import goodjava.rpc.RpcException; | |
1488 | 20 import goodjava.lucene.api.LuceneIndexWriter; |
1499 | 21 import goodjava.logging.Logger; |
22 import goodjava.logging.LoggerFactory; | |
1488 | 23 import goodjava.lucene.logging.LoggingIndexWriter; |
24 import goodjava.lucene.logging.LogFile; | |
25 | |
26 | |
27 public class BackupIndexWriter extends LoggingIndexWriter { | |
1499 | 28 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class); |
29 public static String[] backupDomains; | |
1488 | 30 private final String name; |
1512 | 31 private final String password; |
1488 | 32 private final File dir; |
1499 | 33 private boolean isSyncPending = false; |
1504 | 34 private final ExecutorService exec = Executors.newSingleThreadExecutor(); |
1488 | 35 |
1512 | 36 public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name,String password) throws IOException { |
1488 | 37 super(indexWriter,logDir); |
1499 | 38 if( backupDomains == null ) |
39 throw new RuntimeException("must set backupDomains"); | |
1488 | 40 this.name = name; |
1512 | 41 this.password = password; |
1488 | 42 File f = new File(System.getProperty("java.io.tmpdir")); |
43 dir = new File(f,"goodjava.lucene/"+name); | |
1501 | 44 IoUtils.mkdirs(dir); |
1488 | 45 } |
46 | |
1504 | 47 public synchronized void close() throws IOException { |
48 super.close(); | |
49 exec.shutdown(); | |
50 } | |
51 | |
1488 | 52 public synchronized void commit() throws IOException { |
53 super.commit(); | |
1499 | 54 //sync(); |
55 if( !isSyncPending ) { | |
1504 | 56 exec.execute(sync); |
1499 | 57 isSyncPending = true; |
1488 | 58 } |
59 } | |
60 | |
1538
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
61 protected boolean doCheck(SortField sortField) throws IOException { |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
62 boolean ok = super.doCheck(sortField); |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
63 if( ok ) |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
64 runSyncWithChecksum(); |
634f6765830e
use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1512
diff
changeset
|
65 return ok; |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
66 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
67 |
1499 | 68 public void runSync() { |
1504 | 69 try { |
70 exec.submit(sync).get(); | |
71 } catch(Exception e) { | |
72 throw new RuntimeException(e); | |
73 } | |
1499 | 74 } |
75 | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
76 public void runSyncWithChecksum() { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
77 try { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
78 exec.submit(syncWithChecksum).get(); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
79 } catch(Exception e) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
80 throw new RuntimeException(e); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
81 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
82 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
83 |
1499 | 84 private final Runnable sync = new Runnable() { |
1504 | 85 public void run() { |
1499 | 86 try { |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
87 sync(false); |
1499 | 88 } catch(IOException e) { |
89 throw new RuntimeException(e); | |
90 } | |
91 } | |
92 }; | |
93 | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
94 private final Runnable syncWithChecksum = new Runnable() { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
95 public void run() { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
96 try { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
97 sync(true); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
98 } catch(IOException e) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
99 throw new RuntimeException(e); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
100 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
101 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
102 }; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
103 |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
104 private void sync(boolean withChecksum) throws IOException { |
1499 | 105 List<LogFile> logs = new ArrayList<LogFile>(); |
106 synchronized(this) { | |
107 isSyncPending = false; | |
1500 | 108 clearDir(); |
1499 | 109 for( LogFile log : this.logs ) { |
110 File f = new File(dir,log.file.getName()); | |
111 IoUtils.link(log.file,f); | |
112 logs.add( new LogFile(f) ); | |
113 } | |
114 } | |
115 List logInfo = new ArrayList(); | |
116 Map<String,LogFile> logMap = new HashMap<String,LogFile>(); | |
117 for( LogFile log : logs ) { | |
118 Map fileInfo = new HashMap(); | |
119 fileInfo.put("name",log.file.getName()); | |
120 fileInfo.put("end",log.end()); | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
121 if( withChecksum ) |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
122 fileInfo.put("checksum",log.checksum()); |
1499 | 123 logInfo.add(fileInfo); |
124 logMap.put(log.file.getName(),log); | |
125 } | |
126 for( String backupDomain : backupDomains ) { | |
1509 | 127 RpcClient rpc = BackupServer.rpcClient(backupDomain); |
1499 | 128 try { |
1512 | 129 RpcCall call = new RpcCall("login",name,password); |
130 rpc.write(call); | |
131 rpc.read(); | |
132 call = new RpcCall("check",logInfo); | |
1499 | 133 while(true) { |
134 rpc.write(call); | |
135 RpcResult result = rpc.read(); | |
1512 | 136 //logger.info(Arrays.asList(result.returnValues).toString()); |
1499 | 137 String status = (String)result.returnValues[0]; |
138 if( status.equals("ok") ) { | |
139 break; | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
140 } else if( status.equals("missing") || status.equals("bad_checksum") ) { |
1499 | 141 String fileName = (String)result.returnValues[1]; |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
142 if( status.equals("bad_checksum") ) |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
143 logger.error("bad_checksum "+fileName); |
1499 | 144 LogFile log = logMap.get(fileName); |
145 long len = log.end() - 8; | |
146 InputStream in = log.input(); | |
1512 | 147 call = new RpcCall(in,len,"add",logInfo,fileName); |
1499 | 148 } else if( status.equals("incomplete") ) { |
149 String fileName = (String)result.returnValues[1]; | |
150 long logEnd = (Long)result.returnValues[2]; | |
151 LogFile log = logMap.get(fileName); | |
152 long len = log.end() - logEnd; | |
153 InputStream in = log.input(); | |
154 in.skip(logEnd-8); | |
1512 | 155 call = new RpcCall(in,len,"append",logInfo,fileName); |
1499 | 156 } else |
157 throw new RuntimeException("status "+status); | |
158 } | |
159 } catch(RpcException e) { | |
160 logger.warn("",e); | |
161 } | |
162 rpc.close(); | |
163 } | |
1500 | 164 clearDir(); |
165 } | |
166 | |
167 private void clearDir() throws IOException { | |
168 for( File f : dir.listFiles() ) { | |
169 IoUtils.delete(f); | |
170 } | |
1499 | 171 } |
172 | |
1488 | 173 } |