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