Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
AMBARI-22365. Add storage support for storing metric definitions usin…
…g LevelDB. (swagle)
- Loading branch information
Showing
8 changed files
with
306 additions
and
6 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
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
@@ -0,0 +1,38 @@ | ||
/** | ||
* 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.ambari.metrics.adservice.configuration | ||
|
||
import javax.validation.constraints.NotNull | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
class MetricDefinitionDBConfiguration { | ||
|
||
@NotNull | ||
private var dbDirPath: String = _ | ||
|
||
@JsonProperty("verifyChecksums") | ||
def verifyChecksums: Boolean = true | ||
|
||
@JsonProperty("performParanoidChecks") | ||
def performParanoidChecks: Boolean = false | ||
|
||
@JsonProperty("dbDirPath") | ||
def getDbDirPath: String = dbDirPath | ||
} |
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
@@ -0,0 +1,73 @@ | ||
/** | ||
* 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.ambari.metrics.adservice.db | ||
|
||
trait MetadataDatasource { | ||
|
||
type Key = Array[Byte] | ||
type Value = Array[Byte] | ||
|
||
/** | ||
* Idempotent call at the start of the application to initialize db | ||
*/ | ||
def initialize(): Unit | ||
|
||
/** | ||
* This function obtains the associated value to a key. It requires the (key-value) pair to be in the DataSource | ||
* | ||
* @param key | ||
* @return the value associated with the passed key. | ||
*/ | ||
def apply(key: Key): Value = get(key).get | ||
|
||
/** | ||
* This function obtains the associated value to a key, if there exists one. | ||
* | ||
* @param key | ||
* @return the value associated with the passed key. | ||
*/ | ||
def get(key: Key): Option[Value] | ||
|
||
|
||
/** | ||
* This function associates a key to a value, overwriting if necessary | ||
*/ | ||
def put(key: Key, value: Value): Unit | ||
|
||
/** | ||
* Delete key from the db | ||
*/ | ||
def delete(key: Key): Unit | ||
|
||
/** | ||
* This function updates the DataSource by deleting, updating and inserting new (key-value) pairs. | ||
* | ||
* @param toRemove which includes all the keys to be removed from the DataSource. | ||
* @param toUpsert which includes all the (key-value) pairs to be inserted into the DataSource. | ||
* If a key is already in the DataSource its value will be updated. | ||
* @return the new DataSource after the removals and insertions were done. | ||
*/ | ||
def update(toRemove: Seq[Key], toUpsert: Seq[(Key, Value)]): Unit | ||
|
||
/** | ||
* This function closes the DataSource, without deleting the files used by it. | ||
*/ | ||
def close(): Unit | ||
|
||
} |
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
@@ -0,0 +1,102 @@ | ||
/** | ||
* 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.ambari.metrics.adservice.leveldb | ||
|
||
import java.io.File | ||
|
||
import org.apache.ambari.metrics.adservice.app.AnomalyDetectionAppConfig | ||
import org.apache.ambari.metrics.adservice.configuration.MetricDefinitionDBConfiguration | ||
import org.apache.ambari.metrics.adservice.db.MetadataDatasource | ||
import org.iq80.leveldb.{DB, Options, WriteOptions} | ||
import org.iq80.leveldb.impl.Iq80DBFactory | ||
|
||
import com.google.inject.Singleton | ||
|
||
@Singleton | ||
class LevelDBDataSource(appConfig: AnomalyDetectionAppConfig) extends MetadataDatasource { | ||
|
||
private var db: DB = _ | ||
@volatile var isInitialized: Boolean = false | ||
|
||
override def initialize(): Unit = { | ||
if (isInitialized) return | ||
|
||
val configuration: MetricDefinitionDBConfiguration = appConfig.getMetricDefinitionDBConfiguration | ||
|
||
db = createDB(new LevelDbConfig { | ||
override val createIfMissing: Boolean = true | ||
override val verifyChecksums: Boolean = configuration.verifyChecksums | ||
override val paranoidChecks: Boolean = configuration.performParanoidChecks | ||
override val path: String = configuration.getDbDirPath | ||
}) | ||
isInitialized = true | ||
} | ||
|
||
private def createDB(levelDbConfig: LevelDbConfig): DB = { | ||
import levelDbConfig._ | ||
|
||
val options = new Options() | ||
.createIfMissing(createIfMissing) | ||
.paranoidChecks(paranoidChecks) // raise an error as soon as it detects an internal corruption | ||
.verifyChecksums(verifyChecksums) // force checksum verification of all data that is read from the file system on behalf of a particular read | ||
|
||
Iq80DBFactory.factory.open(new File(path), options) | ||
} | ||
|
||
override def close(): Unit = { | ||
db.close() | ||
} | ||
|
||
/** | ||
* This function obtains the associated value to a key, if there exists one. | ||
* | ||
* @param key | ||
* @return the value associated with the passed key. | ||
*/ | ||
override def get(key: Key): Option[Value] = Option(db.get(key)) | ||
|
||
/** | ||
* This function updates the DataSource by deleting, updating and inserting new (key-value) pairs. | ||
* | ||
* @param toRemove which includes all the keys to be removed from the DataSource. | ||
* @param toUpsert which includes all the (key-value) pairs to be inserted into the DataSource. | ||
* If a key is already in the DataSource its value will be updated. | ||
*/ | ||
override def update(toRemove: Seq[Key], toUpsert: Seq[(Key, Value)]): Unit = { | ||
val batch = db.createWriteBatch() | ||
toRemove.foreach { key => batch.delete(key) } | ||
toUpsert.foreach { item => batch.put(item._1, item._2) } | ||
db.write(batch, new WriteOptions()) | ||
} | ||
|
||
override def put(key: Key, value: Value): Unit = { | ||
db.put(key, value) | ||
} | ||
|
||
override def delete(key: Key): Unit = { | ||
db.delete(key) | ||
} | ||
} | ||
|
||
trait LevelDbConfig { | ||
val createIfMissing: Boolean | ||
val paranoidChecks: Boolean | ||
val verifyChecksums: Boolean | ||
val path: String | ||
} |
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
@@ -0,0 +1,57 @@ | ||
/** | ||
* 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.ambari.metrics.adservice.leveldb | ||
|
||
import java.io.File | ||
|
||
import org.apache.ambari.metrics.adservice.app.AnomalyDetectionAppConfig | ||
import org.apache.ambari.metrics.adservice.configuration.MetricDefinitionDBConfiguration | ||
import org.iq80.leveldb.util.FileUtils | ||
import org.mockito.Mockito.when | ||
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers} | ||
import org.scalatest.mockito.MockitoSugar | ||
|
||
class LevelDBDataSourceTest extends FunSuite with BeforeAndAfter with Matchers with MockitoSugar { | ||
|
||
var db: LevelDBDataSource = _ | ||
var file : File = FileUtils.createTempDir("adservice-leveldb-test") | ||
|
||
before { | ||
val appConfig: AnomalyDetectionAppConfig = mock[AnomalyDetectionAppConfig] | ||
val mdConfig : MetricDefinitionDBConfiguration = mock[MetricDefinitionDBConfiguration] | ||
|
||
when(appConfig.getMetricDefinitionDBConfiguration).thenReturn(mdConfig) | ||
when(mdConfig.verifyChecksums).thenReturn(true) | ||
when(mdConfig.performParanoidChecks).thenReturn(false) | ||
when(mdConfig.getDbDirPath).thenReturn(file.getAbsolutePath) | ||
|
||
db = new LevelDBDataSource(appConfig) | ||
db.initialize() | ||
} | ||
|
||
test("testOperations") { | ||
db.put("Hello".getBytes(), "World".getBytes()) | ||
assert(db.get("Hello".getBytes()).get.sameElements("World".getBytes())) | ||
db.update(Seq("Hello".getBytes()), Seq(("Hello".getBytes(), "Mars".getBytes()))) | ||
assert(db.get("Hello".getBytes()).get.sameElements("Mars".getBytes())) | ||
} | ||
|
||
after { | ||
FileUtils.deleteRecursively(file) | ||
} | ||
} |