Skip to content

Commit d183f6b

Browse files
committed
fixed one bug visible just with Linux (Windows JVM is somewhat flawed)
1 parent 9b3edf7 commit d183f6b

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/main/java/net/sharksystem/asap/engine/ASAPEngine.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public void newEra() {
108108
this.newEra(false, -1);
109109
}
110110

111+
/**
112+
* Set a new ear in this storage - if forced: set newEra as specified without questioning
113+
* @param force
114+
* @param nextEra
115+
*/
111116
private void newEra(boolean force, int nextEra) {
112117
try {
113118
this.syncMemento();
@@ -116,28 +121,48 @@ private void newEra(boolean force, int nextEra) {
116121
}
117122

118123
if(force || this.contentChanged) {
119-
if(this.contentChanged) Log.writeLog(this, this.toString(), "content changed - increment era...");
120-
if(force) Log.writeLog(this, this.toString(), "increment era to add new chunks");
124+
// set as fast as possible to make race conditions less likely
125+
this.contentChanged = false;
126+
127+
if(force) {
128+
Log.writeLog(this, this.toString(), "forced setting era to " + nextEra);
129+
} else {
130+
// calculate new era
131+
nextEra = nextEra < 0 ? this.getNextEra(this.era) : nextEra;
132+
Log.writeLog(this, this.toString(), "era not forced - incremented era to " + nextEra);
133+
}
121134
try {
122135
int oldEra = this.era;
123-
nextEra = nextEra < 0 ? this.getNextEra(this.era) : nextEra;
124-
125-
// set as fast as possible to make race conditions less likely
126-
this.contentChanged = false;
127136

128137
// we are done here - we are in a new era.
129138
this.era = nextEra;
130139

131140
// persistent values
132141
if(this.memento != null) this.memento.save(this);
133142

134-
// drop very very old chunks - if available
135-
this.getChunkStorage().dropChunks(nextEra);
143+
/* now:
144+
oldEra .. era that was used a few milliseconds before. Keep it.
145+
this.era .. era we are going to work with
146+
147+
Now, there could be a very old version with same number as this.era - remove it before you set up
148+
fresh era
149+
*/
136150

137-
// setup new era - copy all chunks
138-
for(ASAPInternalChunk chunk : this.getChunkStorage().getChunks(oldEra)) {
139-
ASAPInternalChunk copyChunk = this.getChunkStorage().getChunk(chunk.getUri(), nextEra);
140-
copyChunk.clone(chunk);
151+
if(this.era == oldEra) {
152+
Log.writeLog(this, this.toString(), "this.era == oldEra - nothing to do");
153+
} else {
154+
Log.writeLog(this, this.toString(), "old era is going to be overwritten: " + this.era);
155+
// drop very very old chunks - if available
156+
this.getChunkStorage().dropChunks(this.era);
157+
158+
Log.writeLog(this, this.toString(),
159+
"setup new era by cloning previous chunk meta data: "
160+
+ this.era + " | " + oldEra);
161+
// setup new era - copy all chunks
162+
for(ASAPInternalChunk chunk : this.getChunkStorage().getChunks(oldEra)) {
163+
ASAPInternalChunk copyChunk = this.getChunkStorage().getChunk(chunk.getUri(), this.era);
164+
copyChunk.clone(chunk);
165+
}
141166
}
142167

143168
Log.writeLog(this, this.toString(), "era incremented");

0 commit comments

Comments
 (0)