changeset 1512:31b543826ca9

lucene.backup login
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 23 May 2020 19:01:57 -0600
parents fb5be0e8ca54
children a0a74f5f490a
files src/goodjava/lucene/backup/Backup.java src/goodjava/lucene/backup/BackupIndexWriter.java src/goodjava/lucene/backup/BackupServer.java src/goodjava/lucene/logging/LoggingIndexWriter.java
diffstat 4 files changed, 58 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/Backup.java	Fri May 22 19:10:14 2020 -0600
+++ b/src/goodjava/lucene/backup/Backup.java	Sat May 23 19:01:57 2020 -0600
@@ -13,6 +13,7 @@
 import goodjava.rpc.RpcServer;
 import goodjava.rpc.RpcCall;
 import goodjava.rpc.RpcResult;
+import goodjava.rpc.RpcException;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 import goodjava.lucene.logging.LogFile;
@@ -52,7 +53,7 @@
 		if( call.cmd.equals("check") ) {
 			// nothing
 		} else if( call.cmd.equals("add") || call.cmd.equals("append")  ) {
-			fileName = (String)call.args[2];
+			fileName = (String)call.args[1];
 			File f = new File(dir,fileName);
 			if( call.cmd.equals("add") )
 				IoUtils.delete(f);
@@ -61,11 +62,14 @@
 			IoUtils.copyAll(call.in,out);
 			out.commit();
 			out.close();
-			logger.info(call.cmd+" "+fileName+" "+call.lenIn);
-		} else
-			throw new RuntimeException("cmd "+call.cmd);
-		List logInfo = (List)call.args[1];
-		logger.info("check "+logInfo);
+			//logger.info(call.cmd+" "+fileName+" "+call.lenIn);
+		} else {
+			logger.error("bad cmd '"+call.cmd+"'");
+			rpc.write( new RpcException("bad cmd '"+call.cmd+"'") );
+			return;
+		}
+		List logInfo = (List)call.args[0];
+		//logger.info("check "+logInfo);
 		RpcResult result = OK;
 		for( Object obj : logInfo ) {
 			Map fileInfo = (Map)obj;
@@ -114,7 +118,7 @@
 			}
 			if( complete ) {
 				LoggingIndexWriter.writeIndex(logs,index);
-				logger.info("write index");
+				//logger.info("write index");
 			}
 		}
 		rpc.write(result);
@@ -124,7 +128,6 @@
 		File zip = File.createTempFile("luan_",".zip");
 		IoUtils.delete(zip);
 		String cmd = "zip -r " + zip + " " + dir.getName();
-logger.info("cmd = "+cmd);
 		synchronized(this) {
 			Process proc = Runtime.getRuntime().exec(cmd,null,dir.getParentFile());
 			IoUtils.waitFor(proc);
--- a/src/goodjava/lucene/backup/BackupIndexWriter.java	Fri May 22 19:10:14 2020 -0600
+++ b/src/goodjava/lucene/backup/BackupIndexWriter.java	Sat May 23 19:01:57 2020 -0600
@@ -28,15 +28,17 @@
 	private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class);
 	public static String[] backupDomains;
 	private final String name;
+	private final String password;
 	private final File dir;
 	private boolean isSyncPending = false;
 	private final ExecutorService exec = Executors.newSingleThreadExecutor();
 
-	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException {
+	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name,String password) throws IOException {
 		super(indexWriter,logDir);
 		if( backupDomains == null )
 			throw new RuntimeException("must set backupDomains");
 		this.name = name;
+		this.password = password;
 		File f = new File(System.getProperty("java.io.tmpdir"));
 		dir = new File(f,"goodjava.lucene/"+name);
 		IoUtils.mkdirs(dir);
@@ -121,12 +123,15 @@
 		}
 		for( String backupDomain : backupDomains ) {
 			RpcClient rpc = BackupServer.rpcClient(backupDomain);
-			RpcCall call = new RpcCall("check",name,logInfo);
 			try {
+				RpcCall call = new RpcCall("login",name,password);
+				rpc.write(call);
+				rpc.read();
+				call = new RpcCall("check",logInfo);
 				while(true) {
 					rpc.write(call);
 					RpcResult result = rpc.read();
-					logger.info(Arrays.asList(result.returnValues).toString());
+					//logger.info(Arrays.asList(result.returnValues).toString());
 					String status = (String)result.returnValues[0];
 					if( status.equals("ok") ) {
 						break;
@@ -137,7 +142,7 @@
 						LogFile log = logMap.get(fileName);
 						long len = log.end() - 8;
 						InputStream in = log.input();
-						call = new RpcCall(in,len,"add",name,logInfo,fileName);
+						call = new RpcCall(in,len,"add",logInfo,fileName);
 					} else if( status.equals("incomplete") ) {
 						String fileName = (String)result.returnValues[1];
 						long logEnd = (Long)result.returnValues[2];
@@ -145,7 +150,7 @@
 						long len = log.end() - logEnd;
 						InputStream in = log.input();
 						in.skip(logEnd-8);
-						call = new RpcCall(in,len,"append",name,logInfo,fileName);
+						call = new RpcCall(in,len,"append",logInfo,fileName);
 					} else
 						throw new RuntimeException("status "+status);
 				}
--- a/src/goodjava/lucene/backup/BackupServer.java	Fri May 22 19:10:14 2020 -0600
+++ b/src/goodjava/lucene/backup/BackupServer.java	Sat May 23 19:01:57 2020 -0600
@@ -4,6 +4,10 @@
 import java.io.OutputStream;
 import java.io.FileOutputStream;
 import java.io.BufferedOutputStream;
+import java.io.Writer;
+import java.io.FileWriter;
+import java.io.Reader;
+import java.io.FileReader;
 import java.io.IOException;
 import java.util.Map;
 import java.net.Socket;
@@ -18,6 +22,7 @@
 import goodjava.rpc.RpcCall;
 import goodjava.rpc.RpcClient;
 import goodjava.rpc.RpcResult;
+import goodjava.rpc.Rpc;
 import goodjava.rpc.RpcException;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
@@ -75,12 +80,16 @@
 
 	private void handle(Socket socket) {
 		RpcServer rpc = new RpcServer(socket);
-		while( !rpc.isClosed() ) {
+		Backup backup;
+		{
 			RpcCall call = rpc.read();
-			if( call == null )
-				break;
+			if( !call.cmd.equals("login") ) {
+				rpc.write( new RpcException("login expected") );
+				rpc.close();
+				return;
+			}
 			String name = (String)call.args[0];
-			Backup backup;
+			String password = (String)call.args[1];
 			synchronized(backups) {
 				backup = backups.get(name);
 				if( backup == null ) {
@@ -88,6 +97,29 @@
 					backups.put(name,backup);
 				}
 			}
+			File pwd = new File(backupDir,name+".pwd");
+			try {
+				if( !pwd.exists() ) {
+					Writer out = new FileWriter(pwd);
+					out.write(password);
+					out.close();
+				} else {
+					Reader in = new FileReader(pwd);
+					if( !IoUtils.readAll(in).equals(password) ) {
+						rpc.write( new RpcException("wrong password") );
+						rpc.close();
+						return;
+					}
+				}
+			} catch(IOException e) {
+				throw new RuntimeException(e);
+			}
+			rpc.write(Rpc.OK);
+		}
+		while( !rpc.isClosed() ) {
+			RpcCall call = rpc.read();
+			if( call == null )
+				break;
 			backup.handle(rpc,call);
 		}
 	}
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri May 22 19:10:14 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sat May 23 19:01:57 2020 -0600
@@ -172,7 +172,7 @@
 	}
 
 	private void mergeLogs() throws IOException {
-		logger.info("merge");
+		//logger.info("merge");
 		LogFile first = logs.get(0);
 		LogFile second = logs.get(1);
 		long lastTime = second.file.lastModified();