Skip to content

Commit

Permalink
* 为事务日志模块增加实时刷新支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyangming committed Jan 23, 2017
1 parent c72e89c commit cae0a42
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public abstract class VirtualLoggingSystemImpl implements VirtualLoggingSystem,
private VirtualLoggingFile master;
private VirtualLoggingFile slaver;

private boolean optimized = true;

public void construct() throws IOException {
if (this.directory == null) {
this.directory = this.getDefaultDirectory();
Expand All @@ -73,6 +75,8 @@ public void construct() throws IOException {
slaverMgr.initialize(false);

this.initialize(masterMgr, slaverMgr);

this.flushAllIfNecessary();
}

private void initialize(VirtualLoggingFile prev, VirtualLoggingFile next) {
Expand All @@ -96,6 +100,7 @@ private void initialize(VirtualLoggingFile prev, VirtualLoggingFile next) {
this.master.clearMarkedFlag();
this.slaver.clearMarkedFlag();
}

}

private void fixSwitchError(VirtualLoggingFile prev, VirtualLoggingFile next) {
Expand Down Expand Up @@ -192,6 +197,8 @@ public void create(Xid xid, byte[] textByteArray) {
try {
this.lock.lock();
this.master.write(byteArray);

this.flushMasterIfNecessary();
} finally {
this.lock.unlock();
}
Expand All @@ -210,6 +217,8 @@ public void delete(Xid xid) {
try {
this.lock.lock();
this.master.write(byteArray);

this.flushMasterIfNecessary();
} finally {
this.lock.unlock();
}
Expand All @@ -229,6 +238,8 @@ public void modify(Xid xid, byte[] textByteArray) {
try {
this.lock.lock();
this.master.write(byteArray);

this.flushMasterIfNecessary();
} finally {
this.lock.unlock();
}
Expand All @@ -239,6 +250,8 @@ public void syncMasterAndSlaver() {
Map<Xid, Boolean> recordMap = this.syncStepOne();
this.master.prepareForReading();
this.syncStepTwo(recordMap);

this.flushSlaverIfNecessary();
}

public Map<Xid, Boolean> syncStepOne() {
Expand Down Expand Up @@ -329,6 +342,8 @@ public void swapMasterAndSlaver() {
this.master.switchToSlaver();
this.slaver.switchToMaster();

this.flushAllIfNecessary();

VirtualLoggingFile theNextMaster = this.slaver;
this.slaver = this.master;
this.master = theNextMaster;
Expand All @@ -338,6 +353,23 @@ public void swapMasterAndSlaver() {
}
}

private void flushAllIfNecessary() {
this.flushMasterIfNecessary();
this.flushSlaverIfNecessary();
}

private void flushMasterIfNecessary() {
if (this.optimized == false) {
this.master.flushImmediately();
}
}

private void flushSlaverIfNecessary() {
if (this.optimized == false) {
this.slaver.flushImmediately();
}
}

public void flushImmediately() {
this.master.flushImmediately();
}
Expand Down Expand Up @@ -367,6 +399,14 @@ public VirtualLoggingFile createTransactionLogging(File file) throws IOException
return logging;
}

public boolean isOptimized() {
return optimized;
}

public void setOptimized(boolean optimized) {
this.optimized = optimized;
}

public File getDirectory() {
return directory;
}
Expand Down

0 comments on commit cae0a42

Please sign in to comment.