annotate src/goodjava/lucene/backup/Backup.java @ 1508:86c5e7000ecf

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