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