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

Update dependencies and reduce warnings #1052

Merged
merged 8 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pgdata*/
test-postgres-path/*
.DS_Store
*.log
.metals/
.bloop/
2 changes: 1 addition & 1 deletion conseil-api/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ conseil {
keys: []
keys+= ${?CONSEIL_API_KEY}

allow-blank: false
allow-blank: true
piotrkosecki marked this conversation as resolved.
Show resolved Hide resolved
allow-blank: ${?CONSEIL_API_ALLOW_BLANK_KEYS}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tech.cryptonomic.conseil.api

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.stream.{ActorMaterializer, Materializer}
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import tech.cryptonomic.conseil.api.config.{ConseilAppConfig, ConseilConfiguration}
import tech.cryptonomic.conseil.api.util.Retry.retry
Expand All @@ -12,7 +12,7 @@ import tech.cryptonomic.conseil.common.config.Platforms.PlatformsConfiguration
import tech.cryptonomic.conseil.common.config._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor}
import scala.concurrent.{Await, ExecutionContextExecutor}
import scala.language.postfixOps
import scala.util.Failure

Expand All @@ -27,7 +27,7 @@ object Conseil extends App with ConseilAppConfig with FailFastCirceSupport with
logger.error("There is an error in the provided configuration")
case Right(config) =>
implicit val system: ActorSystem = ActorSystem("conseil-system")
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val materializer: Materializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = system.dispatcher

val retries = if (config.failFast.on) Some(0) else None
Expand Down Expand Up @@ -69,8 +69,8 @@ object Conseil extends App with ConseilAppConfig with FailFastCirceSupport with
server: ConseilConfiguration,
platforms: PlatformsConfiguration,
verbose: VerboseOutput
)(implicit executionContext: ExecutionContext, system: ActorSystem, mat: ActorMaterializer) = {
val bindingFuture = Http().bindAndHandle(api.route, server.hostname, server.port)
)(implicit system: ActorSystem) = {
val bindingFuture = Http().newServerAt(server.hostname, server.port).bindFlow(api.route)
displayInfo(server)
if (verbose.on) displayConfiguration(platforms)
bindingFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.stream.ActorMaterializer
import cats.effect.{ContextShift, IO}
import akka.stream.{ActorMaterializer, Materializer}
import cats.effect.IO
import ch.megard.akka.http.cors.scaladsl.CorsDirectives.cors
import tech.cryptonomic.conseil.api.ConseilApi.NoNetworkEnabledError
import tech.cryptonomic.conseil.api.config.ConseilAppConfig.CombinedConfiguration
Expand All @@ -30,12 +30,13 @@ import tech.cryptonomic.conseil.common.config.Platforms
import tech.cryptonomic.conseil.common.config.Platforms.BlockchainPlatform
import tech.cryptonomic.conseil.common.io.Logging.ConseilLogSupport
import tech.cryptonomic.conseil.common.sql.DatabaseRunner
import tech.cryptonomic.conseil.common.util.DatabaseUtil

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.DurationInt
import scala.util.{Failure, Success}

import cats.effect.unsafe.implicits.global

object ConseilApi {

/** Exception, which is thrown when no network is enabled in configuration */
Expand All @@ -49,18 +50,15 @@ class ConseilApi(config: CombinedConfiguration)(implicit system: ActorSystem)
extends EnableCORSDirectives
with ConseilLogSupport {

import scala.collection.JavaConverters._

private val transformation = new UnitTransformation(config.metadata)
private val cacheOverrides = new AttributeValuesCacheConfiguration(config.metadata)

implicit private val mat: ActorMaterializer = ActorMaterializer()
implicit private val mat: Materializer = ActorMaterializer()
implicit private val dispatcher: ExecutionContext = system.dispatcher
implicit private val contextShift: ContextShift[IO] = IO.contextShift(dispatcher)

config.nautilusCloud match {
case ncc @ NautilusCloudConfiguration(true, _, _, _, _, delay, interval) =>
system.scheduler.schedule(delay, interval)(Security.updateKeys(ncc))
system.scheduler.scheduleWithFixedDelay(delay, interval)(() => Security.updateKeys(ncc))
case _ => ()
}

Expand Down Expand Up @@ -106,7 +104,7 @@ class ConseilApi(config: CombinedConfiguration)(implicit system: ActorSystem)
ip =>
extractStrictEntity(10.seconds) {
ent =>
recordResponseValues(ip, ent.data.utf8String)(mat, correlationId) {
recordResponseValues(ip, ent.data.utf8String)(correlationId) {
timeoutHandler {
concat(
validateApiKey { _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,34 @@ import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.http.scaladsl.server.{Directive, ExceptionHandler, Route}
import akka.stream.Materializer
import cats.effect.concurrent.MVar
import cats.effect.{Concurrent, ContextShift, IO}
import cats.effect.{Async, IO, Ref}
import tech.cryptonomic.conseil.common.io.Logging.ConseilLogSupport
import org.slf4j.MDC

import cats.effect.unsafe.implicits.global

/** Utility class for recording responses */
class RecordingDirectives(implicit concurrent: Concurrent[IO]) extends ConseilLogSupport {
class RecordingDirectives(implicit concurrent: Async[IO]) extends ConseilLogSupport {

type RequestMap = Map[UUID, RequestValues]
private val requestInfoMap: MVar[IO, Map[UUID, RequestValues]] =
MVar.of[IO, RequestMap](Map.empty[UUID, RequestValues]).unsafeRunSync()
private val requestInfoMap: Ref[IO, Map[UUID, RequestValues]] =
Ref[IO].of(Map.empty[UUID, RequestValues]).unsafeRunSync()

private def requestMapModify[A](
modify: RequestMap => RequestMap
)(useValues: RequestValues => A)(implicit correlationId: UUID) =
for {
map <- requestInfoMap.take
_ <- requestInfoMap.put(modify(map))
} yield useValues(map(correlationId))
(for {
map <- requestInfoMap.get
_ <- requestInfoMap.update(_ => modify(map))
} yield useValues(map(correlationId)))

/** Directive adding recorded values to the MDC */
def recordResponseValues(
ip: RemoteAddress,
stringEntity: String
)(implicit materializer: Materializer, correlationId: UUID): Directive[Unit] =
def recordResponseValues(ip: RemoteAddress, stringEntity: String)(implicit correlationId: UUID): Directive[Unit] =
BasicDirectives.extractRequest.flatMap { request =>
(for {
requestMap <- requestInfoMap.take
requestMap <- requestInfoMap.get
value = RequestValues.fromHttpRequestAndIp(request, ip, stringEntity)
_ <- requestInfoMap.put(requestMap.updated(correlationId, value))
_ <- requestInfoMap.update(_ => requestMap.updated(correlationId, value))
} yield ()).unsafeRunSync()

requestMapModify(
Expand All @@ -50,7 +47,7 @@ class RecordingDirectives(implicit concurrent: Concurrent[IO]) extends ConseilLo
modify = _.filterNot(_._1 == correlationId)
) { values =>
values.logResponse(resp)
}.unsafeRunAsyncAndForget()
}.unsafeRunAndForget()
resp
}
response
Expand All @@ -66,7 +63,7 @@ class RecordingDirectives(implicit concurrent: Concurrent[IO]) extends ConseilLo
modify = _.filterNot(_._1 == correlationId)
) { values =>
values.logResponse(response, Some(e))
}.unsafeRunAsyncAndForget()
}.unsafeRunAndForget()
complete(response)
}

Expand All @@ -78,7 +75,7 @@ class RecordingDirectives(implicit concurrent: Concurrent[IO]) extends ConseilLo
modify = _.filterNot(_._1 == correlationId)
) { values =>
values.logResponse(response)
}.unsafeRunAsyncAndForget()
}.unsafeRunAndForget()
response
}(route)

Expand Down Expand Up @@ -118,9 +115,7 @@ class RecordingDirectives(implicit concurrent: Concurrent[IO]) extends ConseilLo
object RequestValues {

/** Extracts Request values from request context and ip address */
def fromHttpRequestAndIp(request: HttpRequest, ip: RemoteAddress, stringEntity: String)(
implicit materializer: Materializer
): RequestValues =
def fromHttpRequestAndIp(request: HttpRequest, ip: RemoteAddress, stringEntity: String): RequestValues =
RequestValues(
httpMethod = request.method.value,
requestBody = stringEntity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.cryptonomic.conseil.api.routes.platform.discovery

import cats.effect.{ContextShift, IO}
import cats.effect.IO
import com.rklaehn.radixtree.RadixTree
import slick.dbio.{DBIO, DBIOAction}
import slick.jdbc.meta.{MColumn, MIndexInfo, MPrimaryKey, MTable}
Expand All @@ -22,6 +22,8 @@ import tech.cryptonomic.conseil.common.metadata._
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}

import cats.effect.unsafe.implicits.global

/** Companion object providing apply method implementation */
object GenericPlatformDiscoveryOperations {

Expand All @@ -31,7 +33,7 @@ object GenericPlatformDiscoveryOperations {
cacheOverrides: AttributeValuesCacheConfiguration,
cacheTTL: FiniteDuration,
highCardinalityLimit: Int
)(implicit executionContext: ExecutionContext, contextShift: ContextShift[IO]): GenericPlatformDiscoveryOperations =
)(implicit executionContext: ExecutionContext): GenericPlatformDiscoveryOperations =
new GenericPlatformDiscoveryOperations(dbRunners, caching, cacheOverrides, cacheTTL, highCardinalityLimit)
}

Expand All @@ -42,7 +44,7 @@ class GenericPlatformDiscoveryOperations(
cacheOverrides: AttributeValuesCacheConfiguration,
cacheTTL: FiniteDuration,
highCardinalityLimit: Int
)(implicit executionContext: ExecutionContext, contextShift: ContextShift[IO])
)(implicit executionContext: ExecutionContext)
extends PlatformDiscoveryOperations {

import MetadataCaching._
Expand Down Expand Up @@ -194,15 +196,14 @@ class GenericPlatformDiscoveryOperations(
} else {
(for {
_ <- caching.putEntities(key, ent)
_ <- contextShift.shift
updatedEntities <- IO.fromFuture(
IO(
dbRunners((networkPath.up.platform, networkPath.network))
.runQuery(preCacheEntities(networkPath.up.platform, networkPath.network))
)
)
_ <- caching.putEntities(key, updatedEntities(key).value)
} yield ()).unsafeRunAsyncAndForget()
} yield ()).unsafeRunAndForget()
IO.pure(ent)
}
}
Expand Down Expand Up @@ -345,14 +346,13 @@ class GenericPlatformDiscoveryOperations(
AttributeValuesCacheKey(platform, network, tableName, columnName),
oldRadixTree
)
_ <- contextShift.shift
attributeValues <- IO.fromFuture(IO(makeAttributesQuery(platform, network, tableName, columnName, None)))
radixTree = RadixTree(attributeValues.map(x => x.toLowerCase -> x): _*)
_ <- caching.putAttributeValues(
AttributeValuesCacheKey(platform, network, tableName, columnName),
radixTree
)
} yield ()).unsafeRunAsyncAndForget()
} yield ()).unsafeRunAndForget()
IO.pure(oldRadixTree.filterPrefix(attributeFilter).values.take(maxResultLength).toList)
case None =>
IO.pure(List.empty)
Expand Down Expand Up @@ -403,10 +403,9 @@ class GenericPlatformDiscoveryOperations(
} else {
(for {
_ <- caching.putAttributes(key, attributes)
_ <- contextShift.shift
updatedAttributes <- IO.fromFuture(IO(getUpdatedAttributes(entityPath, attributes)))
_ <- caching.putAttributes(key, updatedAttributes)
} yield ()).unsafeRunAsyncAndForget()
} yield ()).unsafeRunAndForget()
IO.pure(attributes)
}
}.sequence
Expand Down Expand Up @@ -483,7 +482,6 @@ class GenericPlatformDiscoveryOperations(
.map(keys => caching.getAllAttributesByKeys(keys))
.sequence
.map(_.reduce(_ ++ _))
_ <- contextShift.shift
updatedAttributes <- entCache.foldMapM(
(entC: CacheEntry[List[Entity]]) =>
getAllUpdatedAttributes(platform.name, network.name, entC.value, attrCache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.Materializer
import cats.effect.IO
import cats.effect.concurrent.Ref
import cats.effect.{IO, Ref}
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport
import pureconfig.error.{ConfigReaderFailures, ThrowableFailure}
import pureconfig.generic.auto._
Expand All @@ -17,6 +16,8 @@ import tech.cryptonomic.conseil.api.config.NautilusCloudConfiguration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

import cats.effect.unsafe.implicits.global

object Security extends ErrorAccumulatingCirceSupport with ConseilLogSupport {

/** Keys cache */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ConseilAppConfigTest extends ConseilSpec {
val typedConfig =
pureconfig.loadConfig[Option[NautilusCloudConfiguration]](conf = config, namespace = "nautilus-cloud")

typedConfig.right.value shouldBe empty
typedConfig.value shouldBe empty
}

"extract Some, when configuration for Nautilus Cloud exists" in {
Expand Down
Loading