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