-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from openmessaging/statemachine
Add Statemachine feature for DLedger
- Loading branch information
Showing
9 changed files
with
712 additions
and
70 deletions.
There are no files selected for viewing
213 changes: 149 additions & 64 deletions
213
src/main/java/io/openmessaging/storage/dledger/DLedgerEntryPusher.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/io/openmessaging/storage/dledger/snapshot/SnapshotReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.openmessaging.storage.dledger.snapshot; | ||
|
||
/** | ||
* Reader for snapshot | ||
*/ | ||
public interface SnapshotReader { | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/openmessaging/storage/dledger/snapshot/SnapshotWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.openmessaging.storage.dledger.snapshot; | ||
|
||
/** | ||
* Writer for snapshot | ||
*/ | ||
public interface SnapshotWriter { | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/io/openmessaging/storage/dledger/statemachine/CommittedEntryIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.openmessaging.storage.dledger.statemachine; | ||
|
||
import io.openmessaging.storage.dledger.entry.DLedgerEntry; | ||
import io.openmessaging.storage.dledger.store.DLedgerStore; | ||
import java.util.Iterator; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* The iterator implementation of committed entries. | ||
*/ | ||
public class CommittedEntryIterator implements Iterator<DLedgerEntry> { | ||
|
||
private final Function<Long, Boolean> completeEntryCallback; | ||
private final DLedgerStore dLedgerStore; | ||
private final long committedIndex; | ||
private final long firstApplyingIndex; | ||
private final AtomicLong applyingIndex; | ||
private long currentIndex; | ||
private int completeAckNums = 0; | ||
|
||
public CommittedEntryIterator(final DLedgerStore dLedgerStore, final long committedIndex, | ||
final AtomicLong applyingIndex, final long lastAppliedIndex, | ||
final Function<Long, Boolean> completeEntryCallback) { | ||
this.dLedgerStore = dLedgerStore; | ||
this.committedIndex = committedIndex; | ||
this.applyingIndex = applyingIndex; | ||
this.firstApplyingIndex = lastAppliedIndex + 1; | ||
this.currentIndex = lastAppliedIndex; | ||
this.completeEntryCallback = completeEntryCallback; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (this.currentIndex >= this.firstApplyingIndex && this.currentIndex <= this.committedIndex) { | ||
completeApplyingEntry(); | ||
} | ||
return this.currentIndex < this.committedIndex; | ||
} | ||
|
||
@Override | ||
public DLedgerEntry next() { | ||
++this.currentIndex; | ||
if (this.currentIndex <= this.committedIndex) { | ||
final DLedgerEntry dLedgerEntry = this.dLedgerStore.get(this.currentIndex); | ||
this.applyingIndex.set(this.currentIndex); | ||
return dLedgerEntry; | ||
} | ||
return null; | ||
} | ||
|
||
private void completeApplyingEntry() { | ||
if (this.completeEntryCallback.apply(this.currentIndex)) { | ||
this.completeAckNums++; | ||
} | ||
} | ||
|
||
public long getIndex() { | ||
return this.currentIndex; | ||
} | ||
|
||
public int getCompleteAckNums() { | ||
return completeAckNums; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/io/openmessaging/storage/dledger/statemachine/StateMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.openmessaging.storage.dledger.statemachine; | ||
|
||
import io.openmessaging.storage.dledger.snapshot.SnapshotReader; | ||
import io.openmessaging.storage.dledger.snapshot.SnapshotWriter; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Finite state machine, which should be implemented by user. | ||
*/ | ||
public interface StateMachine { | ||
|
||
/** | ||
* Update the user statemachine with a batch a tasks that can be accessed | ||
* through |iterator|. | ||
* | ||
* @param iter iterator of committed entry | ||
*/ | ||
void onApply(final CommittedEntryIterator iter); | ||
|
||
/** | ||
* User defined snapshot generate function, this method will block StateMachine#onApply(Iterator). | ||
* Call done.run(status) when snapshot finished. | ||
* | ||
* @param writer snapshot writer | ||
* @param done callback | ||
*/ | ||
void onSnapshotSave(final SnapshotWriter writer, final CompletableFuture<Boolean> done); | ||
|
||
/** | ||
* User defined snapshot load function. | ||
* | ||
* @param reader snapshot reader | ||
* @return true on success | ||
*/ | ||
boolean onSnapshotLoad(final SnapshotReader reader); | ||
|
||
/** | ||
* Invoked once when the raft node was shut down. | ||
* Default do nothing | ||
*/ | ||
void onShutdown(); | ||
} |
Oops, something went wrong.