-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #4025 from stejbac/make-persistence-thread-safe
Make serialisation in FileManager::saveToFile thread-safe
- Loading branch information
Showing
31 changed files
with
214 additions
and
90 deletions.
There are no files selected for viewing
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
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
44 changes: 44 additions & 0 deletions
44
common/src/main/java/bisq/common/proto/persistable/ThreadedPersistableEnvelope.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,44 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.common.proto.persistable; | ||
|
||
import com.google.protobuf.Message; | ||
|
||
/** | ||
* Interface for the outer envelope object persisted to disk, where its serialization | ||
* during persistence takes place on a separate thread (for performance). | ||
* <p> | ||
* To make the serialization thread-safe, all modifications of the object must be | ||
* synchronized with it. This may be achieved by wrapping such modifications with the | ||
* provided {@link ThreadedPersistableEnvelope#modifySynchronized(Runnable)} method. | ||
*/ | ||
public interface ThreadedPersistableEnvelope extends PersistableEnvelope { | ||
|
||
@Override | ||
default Message toPersistableMessage() { | ||
synchronized (this) { | ||
return toProtoMessage(); | ||
} | ||
} | ||
|
||
default void modifySynchronized(Runnable modifyTask) { | ||
synchronized (this) { | ||
modifyTask.run(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
common/src/main/java/bisq/common/proto/persistable/UserThreadMappedPersistableEnvelope.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,45 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.common.proto.persistable; | ||
|
||
import bisq.common.UserThread; | ||
|
||
import com.google.protobuf.Message; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
|
||
import java.util.concurrent.FutureTask; | ||
|
||
/** | ||
* Interface for the outer envelope object persisted to disk, where its serialization | ||
* during persistence is forced to take place on the user thread. | ||
* <p> | ||
* To avoid jitter, this should be only be used for small, safely critical stores. Larger | ||
* or frequently written stores should either implement {@link PersistableEnvelope} | ||
* directly (where thread-safety isn't needed) or use {@link ThreadedPersistableEnvelope}. | ||
*/ | ||
public interface UserThreadMappedPersistableEnvelope extends PersistableEnvelope { | ||
|
||
@Override | ||
default Message toPersistableMessage() { | ||
FutureTask<Message> toProtoOnUserThread = new FutureTask<>(this::toProtoMessage); | ||
UserThread.execute(toProtoOnUserThread); | ||
//noinspection UnstableApiUsage | ||
return Futures.getUnchecked(toProtoOnUserThread); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
common/src/main/java/bisq/common/proto/persistable/UserThreadMappedPersistableList.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,31 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.common.proto.persistable; | ||
|
||
import java.util.List; | ||
|
||
public class UserThreadMappedPersistableList<T extends PersistablePayload> extends PersistableList<T> | ||
implements UserThreadMappedPersistableEnvelope { | ||
|
||
public UserThreadMappedPersistableList(List<T> list) { | ||
super(list); | ||
} | ||
|
||
public UserThreadMappedPersistableList() { | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.