summaryrefslogtreecommitdiffstats
path: root/standalone-container/src/main/scala/com/yahoo/container/standalone/StandaloneContainerApplication.scala
blob: e1d5ba6577dd6b071d485d0823427edaf9c637ff (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.standalone

import java.io.{File, IOException}
import java.lang.{Boolean => JBoolean}
import java.nio.file.{FileSystems, Files, Path, Paths}

import com.google.inject.name.Names
import com.google.inject.{AbstractModule, Inject, Injector, Key}
import com.yahoo.collections.CollectionUtil.first
import com.yahoo.config.application.api.{ApplicationPackage, FileRegistry, RuleConfigDeriver}
import com.yahoo.config.model.application.provider._
import com.yahoo.config.model.builder.xml.XmlHelper
import com.yahoo.config.model.deploy.DeployState
import com.yahoo.config.model.{ApplicationConfigProducerRoot, ConfigModelRepo}
import com.yahoo.config.provision.Zone
import com.yahoo.container.di.config.SubscriberFactory
import com.yahoo.container.jdisc.ConfiguredApplication
import com.yahoo.container.standalone.Environment._
import com.yahoo.container.standalone.StandaloneContainerApplication._
import com.yahoo.io.IOUtils
import com.yahoo.jdisc.application.Application
import com.yahoo.text.XML
import com.yahoo.vespa.defaults.Defaults
import com.yahoo.vespa.model.VespaModel
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder
import com.yahoo.vespa.model.container.{Container, ContainerModel}
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder.Networking
import com.yahoo.vespa.model.container.xml.{ConfigServerContainerModelBuilder, ContainerModelBuilder}
import org.w3c.dom.Element

import scala.collection.JavaConverters._
import scala.util.Try

/**
 * @author tonytv
 * @author gjoranv
 */
class StandaloneContainerApplication @Inject()(injector: Injector) extends Application {

  ConfiguredApplication.ensureVespaLoggingInitialized()

  val applicationPath: Path = injectedApplicationPath.getOrElse(yinstApplicationPath)

  val distributedFiles = new LocalFileDb(applicationPath)

  val configModelRepo = Try { injector.getInstance(Key.get(classOf[ConfigModelRepo], configModelRepoName))}.getOrElse(new ConfigModelRepo)

  val networkingOption = Try {
    injector.getInstance(Key.get(classOf[JBoolean], Names.named(disableNetworkingAnnotation)))
  }.map  {
    case JBoolean.TRUE => Networking.disable
    case JBoolean.FALSE => Networking.enable
  }.getOrElse(Networking.enable)

  val (modelRoot, container) = withTempDir(
    preprocessedApplicationDir => createContainerModel(applicationPath, distributedFiles, preprocessedApplicationDir, networkingOption, configModelRepo))

  val configuredApplication = createConfiguredApplication(container)

  def createConfiguredApplication(container: Container): Application = {
    val augmentedInjector = injector.createChildInjector(new AbstractModule {
      def configure() {
        bind(classOf[SubscriberFactory]).toInstance(new StandaloneSubscriberFactory(modelRoot))
      }
    })

    System.setProperty("config.id", container.getConfigId) //TODO: DRY
    augmentedInjector.getInstance(classOf[ConfiguredApplication])
  }

  def injectedApplicationPath = Try {
    injector.getInstance(Key.get(classOf[Path], applicationPathName))
  }.toOption

  def yinstApplicationPath = path(yinstVariable(applicationLocationYinstVariable))

  override def start() {
    try {
      com.yahoo.container.Container.get().setCustomFileAcquirer(distributedFiles)
      configuredApplication.start()
    }
    catch {
      case e: Exception => { com.yahoo.container.Container.resetInstance(); throw e; }
    }
  }

  override def stop() {
    configuredApplication.stop()
  }

  override def destroy() {
    com.yahoo.container.Container.resetInstance()
    configuredApplication.destroy()
  }
}

object StandaloneContainerApplication {
  val packageName = "standalone_jdisc_container"
  val applicationLocationYinstVariable = s"$packageName.app_location"
  val deploymentProfileYinstVariable = s"$packageName.deployment_profile"

  val applicationPathName = Names.named(applicationLocationYinstVariable)

  val disableNetworkingAnnotation = "JDisc.disableNetworking"
  val configModelRepoName = Names.named("ConfigModelRepo")
  val configDefinitionRepo = new StaticConfigDefinitionRepo()

  val defaultTmpBaseDir = Defaults.getDefaults().underVespaHome("tmp")
  val tmpDirName = "standalone_container"

  private def withTempDir[T](f: File => T): T = {
    val tmpDir = createTempDir()
    try {
      f(tmpDir)
    } finally {
      IOUtils.recursiveDeleteDir(tmpDir)
    }
  }

  private def createTempDir(): File = {
    def getBaseDir: Path = {
      val tmpBaseDir =
        if (new File(defaultTmpBaseDir).exists())
          defaultTmpBaseDir
        else
          System.getProperty("java.io.tmpdir")

      Paths.get(tmpBaseDir)
    }

    val basePath: Path = getBaseDir
    val tmpDir = Files.createTempDirectory(basePath, tmpDirName)
    tmpDir.toFile
  }

  private def validateApplication(applicationPackage: ApplicationPackage) = {
    try {
      applicationPackage.validateXML()
    } catch {
      case e: IOException => throw new IllegalArgumentException(e)
    }
  }

  def newContainerModelBuilder(networkingOption: Networking): ContainerModelBuilder = {
    optionalYinstVariable(deploymentProfileYinstVariable) match {
      case None => new ContainerModelBuilder(true, networkingOption)
      case Some("configserver") => new ConfigServerContainerModelBuilder(new CloudConfigYinstVariables)
      case profileName => throw new RuntimeException(s"Invalid deployment profile '$profileName'")
    }
  }

  def createContainerModel(applicationPath: Path,
                           fileRegistry: FileRegistry,
                           preprocessedApplicationDir: File,
                           networkingOption: Networking,
                           configModelRepo: ConfigModelRepo = new ConfigModelRepo): (VespaModel, Container) = {
    val logger = new BaseDeployLogger
    val rawApplicationPackage = new FilesApplicationPackage.Builder(applicationPath.toFile).includeSourceFiles(true).preprocessedDir(preprocessedApplicationDir).build()
    // TODO: Needed until we get rid of semantic rules
    val applicationPackage = rawApplicationPackage.preprocess(Zone.defaultZone().id, new RuleConfigDeriver {
      override def derive(ruleBaseDir: String, outputDir: String): Unit = {}
    }, logger)
    validateApplication(applicationPackage)
    val deployState = new DeployState.Builder().
      applicationPackage(applicationPackage).
      fileRegistry(fileRegistry).
      deployLogger(logger).
      configDefinitionRepo(configDefinitionRepo).
      build()

    val root = VespaModel.createIncomplete(deployState)
    val vespaRoot = new ApplicationConfigProducerRoot(root,
      "vespa",
      deployState.getDocumentModel,
      deployState.getProperties.vespaVersion(),
      deployState.getProperties.applicationId())
    

    val spec = containerRootElement(applicationPackage)
    val containerModel = newContainerModelBuilder(networkingOption).build(deployState, configModelRepo, vespaRoot, spec)
    containerModel.getCluster().prepare()
    DeprecationSuppressor.initializeContainerModel(containerModel, configModelRepo)
    val container = first(containerModel.getCluster().getContainers)

    // TODO: If we can do the mutations below on the builder, we can separate out model finalization from the
    // VespaModel constructor, such that the above and below code to finalize the container can be
    // replaced by root.finalize();

    // Always disable rpc server for standalone container. This server will soon be removed anyway.
    container.setRpcServerEnabled(false)
    container.setHttpServerEnabled(networkingOption == Networking.enable)

    initializeContainer(container, spec)

    root.freezeModelTopology()
    (root, container)
  }

  def initializeContainer(container: Container, spec: Element) {
    val host = container.getRoot.getHostSystem.getHost(Container.SINGLENODE_CONTAINER_SERVICESPEC)

    container.setBasePort(VespaDomBuilder.getXmlWantedPort(spec))
    container.setHostResource(host)
    container.initService()
  }

  def getJDiscInServices(element: Element): Element = {
    def nameAndId(elements: List[Element]): List[String] = {
      elements map { e => s"${e.getNodeName} id='${e.getAttribute("id")}'" }
    }

    val jDiscElements = ContainerModelBuilder.configModelIds.asScala flatMap { name => XML.getChildren(element, name.getName).asScala }
    jDiscElements.toList match {
      case List(e) => e
      case Nil  => throw new RuntimeException("No jdisc element found under services.")
      case multipleElements: List[Element] => throw new RuntimeException("Found multiple JDisc elements: " + nameAndId(multipleElements).mkString(", "))
    }
  }

  def containerRootElement(applicationPackage: ApplicationPackage) : Element = {
    val element = XmlHelper.getDocument(applicationPackage.getServices).getDocumentElement
    val nodeName = element.getNodeName

    if (ContainerModelBuilder.configModelIds.asScala.map(_.getName).contains(nodeName)) element
    else getJDiscInServices(element)
  }

  def path(s: String) = FileSystems.getDefault.getPath(s)

  // Ugly hack required since Scala cannot suppress warnings. https://issues.scala-lang.org/browse/SI-7934
  private object DeprecationSuppressor extends DeprecationSuppressor
  @deprecated("", "")
  private class DeprecationSuppressor {
    def initializeContainerModel(containerModel: ContainerModel, configModelRepo: ConfigModelRepo): Unit = {
      containerModel.initialize(configModelRepo)
    }
  }
}