annotate src/goodjava/lucene/backup/Backup.java @ 1510:20375c20289d

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2020 21:57:27 -0600
parents 0ba144491a42
children 31b543826ca9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
4 import java.io.InputStream;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
5 import java.io.FileInputStream;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.IOException;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.List;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.ArrayList;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.Map;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.Arrays;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import goodjava.io.IoUtils;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
12 import goodjava.io.BufferedInputStream;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import goodjava.rpc.RpcServer;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import goodjava.rpc.RpcCall;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import goodjava.rpc.RpcResult;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 import goodjava.logging.Logger;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 import goodjava.logging.LoggerFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 import goodjava.lucene.logging.LogFile;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 import goodjava.lucene.logging.LoggingIndexWriter;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import goodjava.lucene.logging.LogOutputStream;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 class Backup {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 private static final Logger logger = LoggerFactory.getLogger(Backup.class);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 private final File dir;
1500
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
27 private final File index;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 Backup(File dir) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 this.dir = dir;
1500
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
31 this.index = new File(dir,"index");
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
34 void handle(RpcServer rpc,RpcCall call) {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 try {
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
36 IoUtils.mkdirs(dir);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
37 if( call.cmd.equals("zip") ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
38 handleZip(rpc);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
39 } else {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
40 handle2(rpc,call);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
41 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 } catch(IOException e) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 throw new RuntimeException(e);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
47 private static final RpcResult OK = new RpcResult(new Object[]{"ok"});
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
48
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
49 synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 //logger.info(call.cmd+" "+Arrays.asList(call.args));
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 String fileName = null;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 if( call.cmd.equals("check") ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 // nothing
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 fileName = (String)call.args[2];
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 File f = new File(dir,fileName);
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
57 if( call.cmd.equals("add") )
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
58 IoUtils.delete(f);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 LogFile log = new LogFile(f);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 LogOutputStream out = log.output();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 IoUtils.copyAll(call.in,out);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 out.commit();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 out.close();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 logger.info(call.cmd+" "+fileName+" "+call.lenIn);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 } else
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 throw new RuntimeException("cmd "+call.cmd);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 List logInfo = (List)call.args[1];
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 logger.info("check "+logInfo);
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
69 RpcResult result = OK;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 for( Object obj : logInfo ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 Map fileInfo = (Map)obj;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 String name = (String)fileInfo.get("name");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 File f = new File(dir,name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 if( !f.exists() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 if( name.equals(fileName) ) logger.error("missing");
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
76 result = new RpcResult(new Object[]{"missing",name});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 long end = (Long)fileInfo.get("end");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 LogFile log = new LogFile(f);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 long logEnd = log.end();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 if( logEnd > end ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end);
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
84 result = new RpcResult(new Object[]{"missing",name});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 if( logEnd < end ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 if( name.equals(fileName) ) logger.error("incomplete");
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
89 result = new RpcResult(new Object[]{"incomplete",name,logEnd});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
92 Object checksumObj = fileInfo.get("checksum");
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
93 if( checksumObj != null ) {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
94 long checksum = (Long)checksumObj;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
95 if( log.checksum() != checksum ) {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
96 index.delete();
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
97 result = new RpcResult(new Object[]{"bad_checksum",name});
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
98 break;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
99 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
100 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 if( call.cmd.equals("add") ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 boolean complete = true;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 List<LogFile> logs = new ArrayList<LogFile>();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 for( Object obj : logInfo ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 Map fileInfo = (Map)obj;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 String name = (String)fileInfo.get("name");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 File f = new File(dir,name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 if( !f.exists() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 complete = false;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 logs.add( new LogFile(f) );
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 if( complete ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 LoggingIndexWriter.writeIndex(logs,index);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 logger.info("write index");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 rpc.write(result);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
123 void handleZip(RpcServer rpc) throws IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
124 File zip = File.createTempFile("luan_",".zip");
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
125 IoUtils.delete(zip);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
126 String cmd = "zip -r " + zip + " " + dir.getName();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
127 logger.info("cmd = "+cmd);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
128 synchronized(this) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
129 Process proc = Runtime.getRuntime().exec(cmd,null,dir.getParentFile());
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
130 IoUtils.waitFor(proc);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
131 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
132 InputStream in = new BufferedInputStream(new FileInputStream(zip));
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
133 RpcResult result = new RpcResult(in,zip.length(),new Object[0]);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
134 rpc.write(result);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
135 IoUtils.delete(zip);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
136 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 }