comparison 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
comparison
equal deleted inserted replaced
1671:8066b8882732 1672:8dd8c556c449
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.InputStream; 4 import java.io.InputStream;
5 import java.io.IOException; 5 import java.io.IOException;
6 import java.net.Socket; 6 import java.net.Socket;
7 import java.net.ConnectException;
7 import java.util.List; 8 import java.util.List;
8 import java.util.ArrayList; 9 import java.util.ArrayList;
9 import java.util.Map; 10 import java.util.Map;
10 import java.util.HashMap; 11 import java.util.HashMap;
11 import java.util.Arrays; 12 import java.util.Arrays;
12 import java.util.concurrent.Executors; 13 import java.util.concurrent.Executors;
13 import java.util.concurrent.ExecutorService; 14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.ExecutionException;
14 import org.apache.lucene.search.SortField; 16 import org.apache.lucene.search.SortField;
15 import goodjava.io.IoUtils; 17 import goodjava.io.IoUtils;
16 import goodjava.rpc.RpcClient; 18 import goodjava.rpc.RpcClient;
17 import goodjava.rpc.RpcCall; 19 import goodjava.rpc.RpcCall;
18 import goodjava.rpc.RpcResult; 20 import goodjava.rpc.RpcResult;
19 import goodjava.rpc.RpcException; 21 import goodjava.rpc.RpcException;
20 import goodjava.lucene.api.LuceneIndexWriter; 22 import goodjava.lucene.api.LuceneIndexWriter;
23 import goodjava.lucene.logging.LoggingIndexWriter;
24 import goodjava.lucene.logging.LogFile;
21 import goodjava.logging.Logger; 25 import goodjava.logging.Logger;
22 import goodjava.logging.LoggerFactory; 26 import goodjava.logging.LoggerFactory;
23 import goodjava.lucene.logging.LoggingIndexWriter;
24 import goodjava.lucene.logging.LogFile;
25 27
26 28
27 public class BackupIndexWriter extends LoggingIndexWriter { 29 public final class BackupIndexWriter extends LoggingIndexWriter {
28 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class); 30 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class);
29 public static String[] backupDomains; 31 public static String[] backupDomains;
30 private final String name; 32 private final String name;
31 private final String password; 33 private final String password;
32 private final File dir; 34 private final File dir;
44 File f = new File(System.getProperty("java.io.tmpdir")); 46 File f = new File(System.getProperty("java.io.tmpdir"));
45 dir = new File(f,"goodjava.lucene/"+name); 47 dir = new File(f,"goodjava.lucene/"+name);
46 IoUtils.mkdirs(dir); 48 IoUtils.mkdirs(dir);
47 } 49 }
48 50
49 public synchronized void close() throws IOException { 51 @Override public synchronized void close() throws IOException {
50 super.close(); 52 super.close();
51 exec.shutdown(); 53 exec.shutdown();
52 } 54 }
53 55
54 public synchronized void commit() throws IOException { 56 @Override public synchronized void commit() throws IOException {
55 super.commit(); 57 super.commit();
56 //sync(); 58 //sync();
57 if( !isSyncPending ) { 59 if( !isSyncPending ) {
58 exec.execute(sync); 60 exec.execute(sync);
59 isSyncPending = true; 61 isSyncPending = true;
60 } 62 }
61 } 63 }
62 64
63 protected boolean doCheck(SortField sortField) throws IOException { 65 @Override protected boolean doCheck(SortField sortField) throws IOException {
64 boolean ok = super.doCheck(sortField); 66 boolean ok = super.doCheck(sortField);
65 if( ok ) 67 if( ok )
66 runSyncWithChecksum(); 68 runSyncWithChecksum();
67 return ok; 69 return ok;
68 } 70 }
69 71
70 public void runSync() { 72 public void runSync() {
71 try { 73 exec(sync);
72 exec.submit(sync).get();
73 } catch(Exception e) {
74 throw new RuntimeException(e);
75 }
76 } 74 }
77 75
78 public void runSyncWithChecksum() { 76 public void runSyncWithChecksum() {
77 exec(syncWithChecksum);
78 }
79
80 private void exec(Runnable r) {
79 try { 81 try {
80 exec.submit(syncWithChecksum).get(); 82 exec.submit(r).get();
81 } catch(Exception e) { 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;
82 throw new RuntimeException(e); 89 throw new RuntimeException(e);
83 } 90 }
84 } 91 }
85 92
86 private final Runnable sync = new Runnable() { 93 private final Runnable sync = new Runnable() {
87 public void run() { 94 public void run() {
88 try { 95 try {
89 sync(false); 96 sync(false);
97 } catch(ConnectException e) {
98 logger.error("sync failed: "+e.getMessage());
90 } catch(IOException e) { 99 } catch(IOException e) {
91 throw new RuntimeException(e); 100 throw new RuntimeException(e);
92 } 101 }
93 } 102 }
94 }; 103 };
95 104
96 private final Runnable syncWithChecksum = new Runnable() { 105 private final Runnable syncWithChecksum = new Runnable() {
97 public void run() { 106 public void run() {
98 try { 107 try {
99 sync(true); 108 sync(true);
109 } catch(ConnectException e) {
110 logger.error("syncWithChecksum failed: "+e.getMessage());
100 } catch(IOException e) { 111 } catch(IOException e) {
101 throw new RuntimeException(e); 112 throw new RuntimeException(e);
102 } 113 }
103 } 114 }
104 }; 115 };
157 call = new RpcCall(in,len,"append",logInfo,fileName); 168 call = new RpcCall(in,len,"append",logInfo,fileName);
158 } else 169 } else
159 throw new RuntimeException("status "+status); 170 throw new RuntimeException("status "+status);
160 } 171 }
161 } catch(RpcException e) { 172 } catch(RpcException e) {
162 logger.warn("",e); 173 logger.error("",e);
163 } 174 }
164 rpc.close(); 175 rpc.close();
165 } 176 }
166 clearDir(); 177 clearDir();
167 } 178 }