forked from apache/openwhisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CosmosDBArtifactStoreProvider.scala
124 lines (107 loc) · 5.09 KB
/
CosmosDBArtifactStoreProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.openwhisk.core.database.cosmosdb
import java.io.Closeable
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient
import com.typesafe.config.ConfigFactory
import org.apache.openwhisk.common.Logging
import org.apache.openwhisk.core.ConfigKeys
import org.apache.openwhisk.core.database._
import org.apache.openwhisk.core.entity.size._
import org.apache.openwhisk.core.entity.{DocumentReader, WhiskActivation, WhiskAuth, WhiskEntity}
import pureconfig._
import spray.json.RootJsonFormat
import scala.reflect.ClassTag
case class ClientHolder(client: AsyncDocumentClient) extends Closeable {
override def close(): Unit = client.close()
}
object CosmosDBArtifactStoreProvider extends ArtifactStoreProvider {
type DocumentClientRef = ReferenceCounted[ClientHolder]#CountedReference
private val clients = collection.mutable.Map[CosmosDBConfig, ReferenceCounted[ClientHolder]]()
RetryMetricsCollector.registerIfEnabled()
override def makeStore[D <: DocumentSerializer: ClassTag](useBatching: Boolean)(
implicit jsonFormat: RootJsonFormat[D],
docReader: DocumentReader,
actorSystem: ActorSystem,
logging: Logging,
materializer: ActorMaterializer): ArtifactStore[D] = {
val tag = implicitly[ClassTag[D]]
val config = CosmosDBConfig(ConfigFactory.load(), tag.runtimeClass.getSimpleName)
makeStoreForClient(config, getOrCreateReference(config), getAttachmentStore())
}
def makeArtifactStore[D <: DocumentSerializer: ClassTag](config: CosmosDBConfig,
attachmentStore: Option[AttachmentStore])(
implicit jsonFormat: RootJsonFormat[D],
docReader: DocumentReader,
actorSystem: ActorSystem,
logging: Logging,
materializer: ActorMaterializer): CosmosDBArtifactStore[D] = {
makeStoreForClient(config, createReference(config).reference(), attachmentStore)
}
private def makeStoreForClient[D <: DocumentSerializer: ClassTag](config: CosmosDBConfig,
clientRef: DocumentClientRef,
attachmentStore: Option[AttachmentStore])(
implicit jsonFormat: RootJsonFormat[D],
docReader: DocumentReader,
actorSystem: ActorSystem,
logging: Logging,
materializer: ActorMaterializer): CosmosDBArtifactStore[D] = {
val classTag = implicitly[ClassTag[D]]
val (dbName, handler, viewMapper) = handlerAndMapper(classTag)
new CosmosDBArtifactStore(
dbName,
config,
clientRef,
handler,
viewMapper,
loadConfigOrThrow[InliningConfig](ConfigKeys.db),
attachmentStore)
}
private def handlerAndMapper[D](entityType: ClassTag[D])(
implicit actorSystem: ActorSystem,
logging: Logging,
materializer: ActorMaterializer): (String, DocumentHandler, CosmosDBViewMapper) = {
val entityClass = entityType.runtimeClass
if (entityClass == classOf[WhiskEntity]) ("whisks", WhisksHandler, WhisksViewMapper)
else if (entityClass == classOf[WhiskActivation]) ("activations", ActivationHandler, ActivationViewMapper)
else if (entityClass == classOf[WhiskAuth]) ("subjects", SubjectHandler, SubjectViewMapper)
else throw new IllegalArgumentException(s"Unsupported entity type $entityType")
}
/*
* This method ensures that all store instances share same client instance and thus the underlying connection pool.
* Synchronization is required to ensure concurrent init of various store instances share same ref instance
*/
private def getOrCreateReference[D <: DocumentSerializer: ClassTag](config: CosmosDBConfig) = synchronized {
val clientRef = clients.getOrElseUpdate(config, createReference(config))
if (clientRef.isClosed) {
val newRef = createReference(config)
clients.put(config, newRef)
newRef.reference()
} else {
clientRef.reference()
}
}
private def createReference[D <: DocumentSerializer: ClassTag](config: CosmosDBConfig) = {
val clazz = implicitly[ClassTag[D]].runtimeClass
if (clazz != classOf[WhiskActivation]) {
require(config.timeToLive.isEmpty, s"'timeToLive' should not be specified for ${clazz.getSimpleName}")
}
new ReferenceCounted[ClientHolder](ClientHolder(config.createClient()))
}
}