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