-
Notifications
You must be signed in to change notification settings - Fork 90
/
NussknackerApp.scala
391 lines (321 loc) · 20.3 KB
/
NussknackerApp.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package pl.touk.nussknacker.ui
import akka.actor.ActorSystem
import akka.http.scaladsl.server.{Directives, Route}
import akka.http.scaladsl.{Http, HttpsConnectionContext}
import akka.stream.Materializer
import com.typesafe.config.Config
import com.typesafe.scalalogging.LazyLogging
import fr.davit.akka.http.metrics.core.HttpMetrics._
import fr.davit.akka.http.metrics.core.{HttpMetricsRegistry, HttpMetricsSettings}
import fr.davit.akka.http.metrics.dropwizard.{DropwizardRegistry, DropwizardSettings}
import io.dropwizard.metrics5.MetricRegistry
import io.dropwizard.metrics5.jmx.JmxReporter
import net.ceedubs.ficus.readers.ArbitraryTypeReader.arbitraryTypeValueReader
import org.asynchttpclient.DefaultAsyncHttpClientConfig
import pl.touk.nussknacker.engine.dict.ProcessDictSubstitutor
import pl.touk.nussknacker.engine.util.config.ConfigFactoryExt
import pl.touk.nussknacker.engine.util.loader.ScalaServiceLoader
import pl.touk.nussknacker.engine.util.multiplicity.{Empty, Many, Multiplicity, One}
import pl.touk.nussknacker.engine.util.{JavaClassVersionChecker, SLF4JBridgeHandlerRegistrar}
import pl.touk.nussknacker.engine.{CombinedProcessingTypeData, ConfigWithUnresolvedVersion, ProcessingTypeData}
import pl.touk.nussknacker.http.backend.FixedAsyncHttpClientBackendProvider
import pl.touk.nussknacker.processCounts.influxdb.InfluxCountsReporterCreator
import pl.touk.nussknacker.processCounts.{CountsReporter, CountsReporterCreator}
import pl.touk.nussknacker.ui.api._
import pl.touk.nussknacker.ui.component.DefaultComponentService
import pl.touk.nussknacker.ui.config.{AnalyticsConfig, AttachmentsConfig, ComponentLinksConfigExtractor, DesignerConfigLoader, FeatureTogglesConfig, UsageStatisticsReportsConfig}
import pl.touk.nussknacker.ui.db.{DatabaseInitializer, DbConfig}
import pl.touk.nussknacker.ui.initialization.Initialization
import pl.touk.nussknacker.ui.listener.ProcessChangeListenerLoader
import pl.touk.nussknacker.ui.listener.services.NussknackerServices
import pl.touk.nussknacker.ui.metrics.RepositoryGauges
import pl.touk.nussknacker.ui.notifications.{NotificationConfig, NotificationServiceImpl}
import pl.touk.nussknacker.ui.process._
import pl.touk.nussknacker.ui.process.deployment._
import pl.touk.nussknacker.ui.process.migrate.{HttpRemoteEnvironment, TestModelMigrations}
import pl.touk.nussknacker.ui.process.processingtypedata._
import pl.touk.nussknacker.ui.process.repository._
import pl.touk.nussknacker.ui.process.fragment.{DbFragmentRepository, FragmentResolver}
import pl.touk.nussknacker.ui.process.test.ScenarioTestService
import pl.touk.nussknacker.ui.processreport.ProcessCounter
import pl.touk.nussknacker.ui.security.api._
import pl.touk.nussknacker.ui.security.ssl._
import pl.touk.nussknacker.ui.statistics.{UsageStatisticsReportsSettings, UsageStatisticsReportsSettingsDeterminer}
import pl.touk.nussknacker.ui.suggester.ExpressionSuggester
import pl.touk.nussknacker.ui.uiresolving.UIProcessResolving
import pl.touk.nussknacker.ui.util.{CorsSupport, OptionsMethodSupport, SecurityHeadersSupport, WithDirectives}
import pl.touk.nussknacker.ui.validation.ProcessValidation
import slick.jdbc.{HsqldbProfile, JdbcBackend, JdbcProfile, PostgresProfile}
import sttp.client3.SttpBackend
import sttp.client3.asynchttpclient.future.AsyncHttpClientFutureBackend
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import scala.util.control.NonFatal
trait NusskanckerAppRouter extends Directives with LazyLogging {
def create(config: ConfigWithUnresolvedVersion, dbConfig: DbConfig, metricsRegistry: MetricRegistry)(implicit system: ActorSystem, materializer: Materializer): (Route, Iterable[AutoCloseable])
}
object NusskanckerDefaultAppRouter extends NusskanckerDefaultAppRouter
trait NusskanckerDefaultAppRouter extends NusskanckerAppRouter {
import net.ceedubs.ficus.Ficus._
//override this method to e.g. run UI with local model
protected def prepareProcessingTypeData(designerConfig: ConfigWithUnresolvedVersion, getDeploymentService: () => DeploymentService, categoriesService: ProcessCategoryService)
(implicit ec: ExecutionContext, actorSystem: ActorSystem,
sttpBackend: SttpBackend[Future, Any]): (ProcessingTypeDataProvider[ProcessingTypeData, CombinedProcessingTypeData], ProcessingTypeDataReload with Initialization) = {
BasicProcessingTypeDataReload.wrapWithReloader(
() => {
implicit val deploymentService: DeploymentService = getDeploymentService()
implicit val categoriesServiceImp: ProcessCategoryService = categoriesService
ProcessingTypeDataReader.loadProcessingTypeData(designerConfig)
}
)
}
def initMetrics(metricsRegistry: MetricRegistry,
config: Config,
processRepository: DBFetchingProcessRepository[Future] with BasicRepository): Unit = {
new RepositoryGauges(metricsRegistry, config.getDuration("repositoryGaugesCacheDuration"), processRepository).prepareGauges()
}
override def create(designerConfig: ConfigWithUnresolvedVersion, dbConfig: DbConfig, metricsRegistry: MetricRegistry)(implicit system: ActorSystem, materializer: Materializer): (Route, Iterable[AutoCloseable]) = {
import system.dispatcher
implicit val sttpBackend: SttpBackend[Future, Any] = AsyncHttpClientFutureBackend.usingConfig(new DefaultAsyncHttpClientConfig.Builder().build())
val resolvedConfig = designerConfig.resolved
val environment = resolvedConfig.getString("environment")
val featureTogglesConfig = FeatureTogglesConfig.create(resolvedConfig)
logger.info(s"Designer config loaded: \nfeatureTogglesConfig: $featureTogglesConfig")
// TODO: this ugly hack is because we have cycle in dependencies: deploymentService -> repostories -> modelData -> typeToConfig -> deploymentService
// We should figure out how to split ModelData to be not passed to repositories
var deploymentService: DeploymentService = null
val getDeploymentService: () => DeploymentService = () => {
assert(deploymentService != null, "Illegal initialization: DeploymentService should be initialized before ProcessingTypeData")
deploymentService
}
val processCategoryService: ProcessCategoryService = new ConfigProcessCategoryService(resolvedConfig)
val (typeToConfig, reload) = prepareProcessingTypeData(designerConfig, getDeploymentService, processCategoryService)
val stateDefinitionService = new ProcessStateDefinitionService(typeToConfig.mapCombined(_.statusNameToStateDefinitionsMapping), processCategoryService)
val analyticsConfig = AnalyticsConfig(resolvedConfig)
val modelData = typeToConfig.mapValues(_.modelData)
val managers = typeToConfig.mapValues(_.deploymentManager)
val fragmentRepository = new DbFragmentRepository(dbConfig, system.dispatcher)
val fragmentResolver = new FragmentResolver(fragmentRepository)
val additionalProperties = typeToConfig.mapValues(_.additionalPropertiesConfig)
val processValidation = ProcessValidation(modelData, additionalProperties, typeToConfig.mapValues(_.additionalValidators), fragmentResolver)
val substitutorsByProcessType = modelData.mapValues(modelData => ProcessDictSubstitutor(modelData.uiDictServices.dictRegistry))
val processResolving = new UIProcessResolving(processValidation, substitutorsByProcessType)
val dbioRunner = DBIOActionRunner(dbConfig)
val actionRepository = DbProcessActionRepository.create(dbConfig, modelData)
val processRepository = DBFetchingProcessRepository.create(dbConfig, actionRepository)
// TODO: get rid of Future based repositories - it is easier to use everywhere one implementation - DBIOAction based which allows transactions handling
val futureProcessRepository = DBFetchingProcessRepository.createFutureRespository(dbConfig, actionRepository)
val writeProcessRepository = ProcessRepository.create(dbConfig, modelData)
val notificationsConfig = resolvedConfig.as[NotificationConfig]("notifications")
val processChangeListener = ProcessChangeListenerLoader
.loadListeners(getClass.getClassLoader, resolvedConfig, NussknackerServices(new PullProcessRepository(futureProcessRepository)))
val scenarioResolver = new ScenarioResolver(fragmentResolver)
val dmDispatcher = new DeploymentManagerDispatcher(managers, futureProcessRepository)
deploymentService = new DeploymentServiceImpl(dmDispatcher, processRepository, actionRepository, dbioRunner,
processValidation, scenarioResolver, processChangeListener, featureTogglesConfig.scenarioStateTimeout)
deploymentService.invalidateInProgressActions()
reload.init() // we need to init processing type data after deployment service creation to make sure that it will be done using correct classloader and that won't cause further delays during handling requests
val processActivityRepository = new DbProcessActivityRepository(dbConfig)
val authenticationResources = AuthenticationResources(resolvedConfig, getClass.getClassLoader)
val authorizationRules = AuthenticationConfiguration.getRules(resolvedConfig)
val counter = new ProcessCounter(fragmentRepository)
Initialization.init(modelData.mapValues(_.migrations), dbConfig, processRepository, environment)
val newProcessPreparer = NewProcessPreparer(typeToConfig, additionalProperties)
val customActionInvokerService = new CustomActionInvokerServiceImpl(futureProcessRepository, dmDispatcher, deploymentService)
val testExecutorService = new ScenarioTestExecutorServiceImpl(scenarioResolver, dmDispatcher)
val processService = new DBProcessService(deploymentService, newProcessPreparer,
processCategoryService, processResolving, dbioRunner, futureProcessRepository, actionRepository,
writeProcessRepository, processValidation
)
val scenarioTestService = ScenarioTestService(modelData, featureTogglesConfig.testDataSettings,
processResolving, counter, testExecutorService)
val configProcessToolbarService = new ConfigProcessToolbarService(resolvedConfig, processCategoryService.getAllCategories)
val processAuthorizer = new AuthorizeProcess(futureProcessRepository)
val appResources = new AppResources(
config = resolvedConfig,
processingTypeDataReload = reload,
modelData = modelData,
processRepository = futureProcessRepository,
processValidation = processValidation,
deploymentService = deploymentService,
exposeConfig = featureTogglesConfig.enableConfigEndpoint,
processCategoryService = processCategoryService
)
val countsReporter = featureTogglesConfig.counts.flatMap(prepareCountsReporter(environment, _))
val componentService = DefaultComponentService(ComponentLinksConfigExtractor.extract(resolvedConfig), typeToConfig.mapCombined(_.componentIdProvider), processService, processCategoryService)
val notificationService = new NotificationServiceImpl(actionRepository, dbioRunner, notificationsConfig)
initMetrics(metricsRegistry, resolvedConfig, futureProcessRepository)
val apiResourcesWithAuthentication: List[RouteWithUser] = {
val routes = List(
new ProcessesResources(
processRepository = futureProcessRepository,
processService = processService,
deploymentService = deploymentService,
processToolbarService = configProcessToolbarService,
processResolving = processResolving,
processAuthorizer = processAuthorizer,
processChangeListener = processChangeListener
),
new NodesResources(
futureProcessRepository,
fragmentRepository,
typeToConfig.mapValues(_.modelData),
processValidation,
typeToConfig.mapValues(v => ExpressionSuggester(v.modelData))
),
new ProcessesExportResources(futureProcessRepository, processActivityRepository, processResolving),
new ProcessActivityResource(processActivityRepository, futureProcessRepository, processAuthorizer),
new ManagementResources(processAuthorizer, futureProcessRepository, featureTogglesConfig.deploymentCommentSettings,
deploymentService, dmDispatcher, customActionInvokerService, metricsRegistry, scenarioTestService, typeToConfig.mapValues(_.modelData)),
new ValidationResources(futureProcessRepository, processResolving),
new DefinitionResources(modelData, typeToConfig, fragmentRepository, processCategoryService),
new UserResources(processCategoryService),
new NotificationResources(notificationService),
appResources,
new TestInfoResources(processAuthorizer, futureProcessRepository, scenarioTestService),
new ComponentResource(componentService),
new AttachmentResources(new ProcessAttachmentService(AttachmentsConfig.create(resolvedConfig), processActivityRepository), futureProcessRepository, processAuthorizer),
new StatusResources(stateDefinitionService),
)
val optionalRoutes = List(
featureTogglesConfig.remoteEnvironment
.map(migrationConfig => new HttpRemoteEnvironment(migrationConfig, new TestModelMigrations(modelData.mapValues(_.migrations), processValidation), environment))
.map(remoteEnvironment => new RemoteEnvironmentResources(remoteEnvironment, futureProcessRepository, processAuthorizer)),
countsReporter
.map(reporter => new ProcessReportResources(reporter, counter, futureProcessRepository)),
).flatten
routes ++ optionalRoutes
}
val usageStatisticsReportsConfig = resolvedConfig.as[UsageStatisticsReportsConfig]("usageStatisticsReports")
val usageStatisticsReportsSettingsDeterminer = UsageStatisticsReportsSettingsDeterminer(usageStatisticsReportsConfig, typeToConfig.mapValues(_.usageStatistics))
//TODO: WARNING now all settings are available for not sign in user. In future we should show only basic settings
val apiResourcesWithoutAuthentication: List[Route] = List(
new SettingsResources(featureTogglesConfig, authenticationResources.name, analyticsConfig, usageStatisticsReportsSettingsDeterminer.determineSettings()).publicRoute(),
appResources.publicRoute(),
authenticationResources.routeWithPathPrefix,
)
//TODO: In the future will be nice to have possibility to pass authenticator.directive to resource and there us it at concrete path resource
val webResources = new WebResources(resolvedConfig.getString("http.publicPath"))
val route = WithDirectives(CorsSupport.cors(featureTogglesConfig.development), SecurityHeadersSupport(), OptionsMethodSupport()) {
pathPrefixTest(!"api") {
webResources.route
} ~ pathPrefix("api") {
apiResourcesWithoutAuthentication.reduce(_ ~ _)
} ~ authenticationResources.authenticate() { authenticatedUser =>
pathPrefix("api") {
authorize(authenticatedUser.roles.nonEmpty) {
val loggedUser = LoggedUser(authenticatedUser, authorizationRules, processCategoryService.getAllCategories)
apiResourcesWithAuthentication.map(_.securedRoute(loggedUser)).reduce(_ ~ _)
}
}
}
}
val sttpClosable: AutoCloseable = () => sttpBackend.close()
(route, typeToConfig.all.values.toList ++ countsReporter.toList :+ sttpClosable)
}
//by default, we use InfluxCountsReporterCreator
private def prepareCountsReporter(env: String, config: Config)
(implicit backend: SttpBackend[Future, Any]): Option[CountsReporter[Future]] = {
val configAtKey = config.atKey(CountsReporterCreator.reporterCreatorConfigPath)
val creator = Multiplicity(ScalaServiceLoader.load[CountsReporterCreator](getClass.getClassLoader)) match {
case One(cr) =>
cr
case Empty() =>
new InfluxCountsReporterCreator
case Many(many) =>
throw new IllegalArgumentException(s"Many CountsReporters found: ${many.mkString(", ")}")
}
Try(Option(creator.createReporter(env, configAtKey))).recover {
case NonFatal(ex) =>
logger.warn(s"Error while setting up counts mechanism: ${ex.getMessage}. Counts mechanism will be disabled.")
None
}.get
}
}
object NussknackerAppInitializer extends NussknackerAppInitializer(ConfigFactoryExt.parseUnresolved(classLoader = getClass.getClassLoader))
class NussknackerAppInitializer(baseUnresolvedConfig: Config) extends LazyLogging {
import net.ceedubs.ficus.Ficus._
protected val config: ConfigWithUnresolvedVersion = DesignerConfigLoader.load(baseUnresolvedConfig, getClass.getClassLoader)
protected implicit val system: ActorSystem = ActorSystem("nussknacker-designer", config.resolved)
protected implicit val materializer: Materializer = Materializer(system)
val interface: String = config.resolved.getString("http.interface")
val port: Int = config.resolved.getInt("http.port")
def init(router: NusskanckerAppRouter): (Route, Iterable[AutoCloseable]) = {
JavaClassVersionChecker.check()
SLF4JBridgeHandlerRegistrar.register()
//we prepare temporary ExceptionHandler to shutdown actorSystem in case of exception during creation
prepareUncaughtExceptionHandler(Nil)
val db = initDb(config.resolved)
val metricsRegistry = new MetricRegistry
val (route, objectsToClose) = router.create(config, db, metricsRegistry)
prepareUncaughtExceptionHandler(objectsToClose)
Runtime.getRuntime.addShutdownHook(new ShutdownHandler(objectsToClose))
//JmxReporter does not allocate resources, safe to close
JmxReporter.forRegistry(metricsRegistry).build().start()
val bindingResultF = SslConfigParser.sslEnabled(config.resolved) match {
case Some(keyStoreConfig) =>
bindHttps(interface, port, HttpsConnectionContextFactory.createServerContext(keyStoreConfig), route, metricsRegistry)
case None =>
bindHttp(interface, port, route, metricsRegistry)
}
bindingResultF.foreach { bindingResult =>
logger.info(s"Nussknacker designer started on ${interface}:${bindingResult.localAddress.getPort}")
}(ExecutionContext.global)
(route, objectsToClose)
}
def bindHttp(interface: String, port: Int, route: Route, metricsRegistry: MetricRegistry)(implicit system: ActorSystem, materializer: Materializer): Future[Http.ServerBinding] = {
Http().newMeteredServerAt(
interface = interface,
port = port,
prepareHttpMetricRegistry(metricsRegistry)
).bind(route)
}
def bindHttps(interface: String, port: Int, httpsContext: HttpsConnectionContext, route: Route, metricsRegistry: MetricRegistry)(implicit system: ActorSystem, materializer: Materializer): Future[Http.ServerBinding] = {
Http().newMeteredServerAt(
interface = interface,
port = port,
prepareHttpMetricRegistry(metricsRegistry)
).enableHttps(httpsContext).bind(route)
}
private def prepareHttpMetricRegistry(metricsRegistry: MetricRegistry): HttpMetricsRegistry = {
val settings: HttpMetricsSettings = DropwizardSettings.default
new DropwizardRegistry(settings)(metricsRegistry)
}
def initDb(config: Config): DbConfig = {
val db = JdbcBackend.Database.forConfig("db", config)
val profile = chooseDbProfile(config)
val dbConfig = DbConfig(db, profile)
DatabaseInitializer.initDatabase("db", config)
dbConfig
}
private def chooseDbProfile(config: Config): JdbcProfile = {
val jdbcUrlPattern = "jdbc:([0-9a-zA-Z]+):.*".r
config.getAs[String]("db.url") match {
case Some(jdbcUrlPattern("postgresql")) => PostgresProfile
case Some(jdbcUrlPattern("hsqldb")) => HsqldbProfile
case None => HsqldbProfile
case _ => throw new IllegalStateException("unsupported jdbc url")
}
}
//we do it, because akka creates non-daemon threads, so we have to stop ActorSystem explicitly, if initialization fails
private def prepareUncaughtExceptionHandler(objectsToClose: Iterable[AutoCloseable]): Unit = {
//TODO: should we set it only on main thread?
Thread.currentThread().setUncaughtExceptionHandler((t: Thread, e: Throwable) => {
logger.error("Main thread stopped unexpectedly, terminating ActorSystem", e)
closeAndShutdownAll(objectsToClose)
})
}
class ShutdownHandler(objectsToClose: Iterable[AutoCloseable]) extends Thread {
override def run(): Unit = {
logger.info("Stopping application")
closeAndShutdownAll(objectsToClose)
}
}
private def closeAndShutdownAll(objectsToClose: Iterable[AutoCloseable]): Unit = {
objectsToClose.foreach(_.close())
system.terminate()
}
}
object NussknackerApp extends App {
NussknackerAppInitializer.init(NusskanckerDefaultAppRouter)
}