Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConcurrentHashMap in StoreManager #5061

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions java/arcs/core/storage/StoreManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
package arcs.core.storage

import arcs.core.crdt.CrdtData
import arcs.core.crdt.CrdtOperationAtTime
import arcs.core.util.guardedBy
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import arcs.core.crdt.CrdtOperation
import java.util.concurrent.ConcurrentHashMap
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConcurrentHashMap is part of the Java standard library, so it's not going to compile to JS/Wasm...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, right :(


/**
* Store manager provides a central holding places for the [Store] instances that a runtime will
* use, so that only one instance of a [Store] can be created per [StorageKey].
*/
class StoreManager {
private val storesMutex = Mutex()
private val stores by guardedBy(storesMutex, mutableMapOf<StorageKey, Store<*, *, *>>())
private val stores = ConcurrentHashMap<StorageKey, Store<*, *, *>>()

/** Return a list of all [StorageKeys]s for [Store]s managed by this [StoreManager] */
val storageKeys
get() = stores.keys

/** Get a [Store] for the provided key. One will be created if it does not exist yet. */
@Suppress("UNCHECKED_CAST")
suspend fun <Data : CrdtData, Op : CrdtOperationAtTime, T> get(
fun <Data : CrdtData, Op : CrdtOperation, T> get(
storeOptions: StoreOptions<Data, Op, T>
) = storesMutex.withLock {
stores.getOrPut(storeOptions.storageKey) {
Store(storeOptions)
} as Store<Data, Op, T>
}
) = stores.getOrPut(storeOptions.storageKey) { Store(storeOptions) } as Store<Data, Op, T>
}
9 changes: 4 additions & 5 deletions java/arcs/sdk/android/storage/service/StorageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import arcs.android.storage.service.StorageServiceManager
import arcs.android.storage.ttl.PeriodicCleanupTask
import arcs.android.util.AndroidBinderStats
import arcs.core.storage.ProxyMessage
import arcs.core.storage.StorageKey
import arcs.core.storage.Store
import arcs.core.storage.StoreManager
import arcs.core.storage.database.name
import arcs.core.storage.database.persistent
import arcs.core.storage.driver.DatabaseDriverProvider
Expand All @@ -38,7 +38,6 @@ import arcs.core.util.performance.MemoryStats
import arcs.core.util.performance.PerformanceStatistics
import java.io.FileDescriptor
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand All @@ -53,7 +52,7 @@ import kotlinx.coroutines.runBlocking
class StorageService : ResurrectorService() {
private val coroutineContext = Dispatchers.IO + CoroutineName("StorageService")
private val scope = CoroutineScope(coroutineContext)
private val stores = ConcurrentHashMap<StorageKey, Store<*, *, *>>()
private val stores = StoreManager()
private var startTime: Long? = null
private val stats = BindingContextStatsImpl()
private val log = TaggedLog { "StorageService" }
Expand Down Expand Up @@ -88,7 +87,7 @@ class StorageService : ResurrectorService() {

val options = parcelableOptions.actual
return BindingContext(
stores.computeIfAbsent(options.storageKey) { Store(options) },
stores.get(options),
parcelableOptions.crdtType,
coroutineContext,
stats
Expand All @@ -108,7 +107,7 @@ class StorageService : ResurrectorService() {

override fun dump(fd: FileDescriptor, writer: PrintWriter, args: Array<out String>) {
val elapsedTime = System.currentTimeMillis() - (startTime ?: System.currentTimeMillis())
val storageKeys = stores.keys.map { it }.toSet()
val storageKeys = stores.storageKeys

val statsPercentiles = stats.roundtripPercentiles

Expand Down