Skip to content

Commit

Permalink
Issue #4570 WAL backup feature is implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
laa committed Jun 29, 2015
1 parent b1c41b4 commit d775b6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
Expand Up @@ -33,7 +33,6 @@
import com.orientechnologies.orient.core.command.OCommandRequestText; import com.orientechnologies.orient.core.command.OCommandRequestText;
import com.orientechnologies.orient.core.config.OGlobalConfiguration; import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.config.OStorageClusterConfiguration; import com.orientechnologies.orient.core.config.OStorageClusterConfiguration;
import com.orientechnologies.orient.core.config.OStorageConfiguration;
import com.orientechnologies.orient.core.config.OStoragePaginatedClusterConfiguration; import com.orientechnologies.orient.core.config.OStoragePaginatedClusterConfiguration;
import com.orientechnologies.orient.core.conflict.ORecordConflictStrategy; import com.orientechnologies.orient.core.conflict.ORecordConflictStrategy;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal; import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
Expand Down Expand Up @@ -87,17 +86,13 @@
import com.orientechnologies.orient.core.version.ORecordVersion; import com.orientechnologies.orient.core.version.ORecordVersion;
import com.orientechnologies.orient.core.version.OVersionFactory; import com.orientechnologies.orient.core.version.OVersionFactory;


import javax.swing.text.DateFormatter;
import java.io.*; import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;


Expand Down Expand Up @@ -2395,7 +2390,6 @@ private boolean restoreFrom(OLogSequenceNumber lsn) throws IOException {
OLogManager.instance().info(this, "%d operations were processed, current LSN is %s last LSN is %s", recordsProcessed, OLogManager.instance().info(this, "%d operations were processed, current LSN is %s last LSN is %s", recordsProcessed,
lsn, writeAheadLog.end()); lsn, writeAheadLog.end());


// throw new Exception("test");
} }


lsn = writeAheadLog.next(lsn); lsn = writeAheadLog.next(lsn);
Expand Down Expand Up @@ -2441,49 +2435,54 @@ private void backUpWAL(Exception e) {
} }


final FileOutputStream archiveOutputStream = new FileOutputStream(archiveFile); final FileOutputStream archiveOutputStream = new FileOutputStream(archiveFile);
final ZipOutputStream archiveZipOutputStream = new ZipOutputStream(archiveOutputStream); final ZipOutputStream archiveZipOutputStream = new ZipOutputStream(new BufferedOutputStream(archiveOutputStream));


final ZipEntry metadataEntry = new ZipEntry(metadataName); final ZipEntry metadataEntry = new ZipEntry(metadataName);


archiveZipOutputStream.putNextEntry(metadataEntry); archiveZipOutputStream.putNextEntry(metadataEntry);


final PrintWriter metadataFileWriter = new PrintWriter(archiveZipOutputStream); final PrintWriter metadataFileWriter = new PrintWriter(archiveZipOutputStream);
metadataFileWriter.append("Storage name : ").append(getName()).append("\n"); metadataFileWriter.append("Storage name : ").append(getName()).append("\r\n");
metadataFileWriter.append("Date : ").append(strDate).append("\n"); metadataFileWriter.append("Date : ").append(strDate).append("\r\n");
metadataFileWriter.append("Stacktrace : \n"); metadataFileWriter.append("Stacktrace : \r\n");
e.printStackTrace(metadataFileWriter); e.printStackTrace(metadataFileWriter);
metadataFileWriter.flush(); metadataFileWriter.flush();
archiveZipOutputStream.closeEntry(); archiveZipOutputStream.closeEntry();


final List<String> walPaths = ((ODiskWriteAheadLog) writeAheadLog).getFiles(); final List<String> walPaths = ((ODiskWriteAheadLog) writeAheadLog).getWalFiles();
for (String walSegment : walPaths) { for (String walSegment : walPaths) {
final File walFile = new File(walSegment); archiveEntry(archiveZipOutputStream, walSegment);
final ZipEntry walZipEntry = new ZipEntry(walFile.getName());
archiveZipOutputStream.putNextEntry(walZipEntry);

final FileInputStream walInputStream = new FileInputStream(walFile);
final BufferedInputStream walBufferedInputStream = new BufferedInputStream(walInputStream);
final BufferedOutputStream walBufferedOutputStream = new BufferedOutputStream(archiveOutputStream);

final byte[] buffer = new byte[1024];
int readBytes = 0;

while ((readBytes = walBufferedInputStream.read(buffer)) > -1) {
walBufferedOutputStream.write(buffer, 0, readBytes);
}

walInputStream.close();
walBufferedOutputStream.flush();
archiveZipOutputStream.closeEntry();
} }


archiveEntry(archiveZipOutputStream, ((ODiskWriteAheadLog) writeAheadLog).getWMRFile());

archiveZipOutputStream.close(); archiveZipOutputStream.close();
} catch (Exception ioe) { } catch (Exception ioe) {
OLogManager.instance().error(this, "Error during WAL backup.", ioe); OLogManager.instance().error(this, "Error during WAL backup.", ioe);
} }


} }


private void archiveEntry(ZipOutputStream archiveZipOutputStream, String walSegment) throws IOException {
final File walFile = new File(walSegment);
final ZipEntry walZipEntry = new ZipEntry(walFile.getName());
archiveZipOutputStream.putNextEntry(walZipEntry);

final FileInputStream walInputStream = new FileInputStream(walFile);
final BufferedInputStream walBufferedInputStream = new BufferedInputStream(walInputStream);

final byte[] buffer = new byte[1024];
int readBytes = 0;

while ((readBytes = walBufferedInputStream.read(buffer)) > -1) {
archiveZipOutputStream.write(buffer, 0, readBytes);
}

walBufferedInputStream.close();

archiveZipOutputStream.closeEntry();
}

protected void restoreAtomicUnit(List<OWALRecord> atomicUnit, OModifiableBoolean atLeastOnePageUpdate) throws IOException { protected void restoreAtomicUnit(List<OWALRecord> atomicUnit, OModifiableBoolean atLeastOnePageUpdate) throws IOException {
assert atomicUnit.get(atomicUnit.size() - 1) instanceof OAtomicUnitEndRecord; assert atomicUnit.get(atomicUnit.size() - 1) instanceof OAtomicUnitEndRecord;


Expand Down
Expand Up @@ -923,7 +923,7 @@ public long size() {
} }
} }


public List<String> getFiles() { public List<String> getWalFiles() {
final ArrayList<String> result = new ArrayList<String>(); final ArrayList<String> result = new ArrayList<String>();
syncObject.lock(); syncObject.lock();
try { try {
Expand All @@ -937,6 +937,15 @@ public List<String> getFiles() {
return result; return result;
} }


public String getWMRFile() {
syncObject.lock();
try {
return masterRecordFile.getAbsolutePath();
} finally {
syncObject.unlock();
}
}

public void truncate() throws IOException { public void truncate() throws IOException {
syncObject.lock(); syncObject.lock();
try { try {
Expand Down

0 comments on commit d775b6e

Please sign in to comment.