-
Notifications
You must be signed in to change notification settings - Fork 0
Improved the agent demand calculation logic. #3
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
b4a0ecf
eebc327
3f39e57
0684913
989c5d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,18 +6,42 @@ import com.google.common.io.BaseEncoding | |
|
|
||
| import scala.collection.mutable | ||
| import scalaj.http._ | ||
| import play.api.libs.json._ | ||
|
|
||
| case class GOCDPoller(conf: FrameworkConfig) { | ||
|
|
||
| val authToken = BaseEncoding.base64().encode(s"${conf.goUserName}:${conf.goPassword}".getBytes("UTF-8")); | ||
|
|
||
| val responseHistory: scala.collection.mutable.MutableList[Int] = mutable.MutableList.empty[Int] | ||
|
|
||
|
|
||
| private[mesos] def request(url: String) = { | ||
| val request = if(conf.goAuthEnabled) { | ||
| Http(url) | ||
| .header("Authorization", s"Basic $authToken") | ||
| } else { | ||
| Http(url) | ||
| }.header("Accept", "application/vnd.go.cd.v1+json") | ||
| request.asString.body | ||
| } | ||
|
|
||
| private[mesos] def jsonRequest(url: String) = { | ||
| Json.parse(request(url)) | ||
| } | ||
|
|
||
| private[mesos] def xmlRequest(url: String) = { | ||
| scala.xml.XML.loadString(request(url)) | ||
| } | ||
|
|
||
| private def buildUrl() = { | ||
| s"http://${conf.goServerHost}:${conf.goServerPort}" | ||
| } | ||
|
|
||
|
|
||
| def goTaskQueueSize(): Int = { | ||
| println("Polling GO Server for scheduled jobs") | ||
| try { | ||
| val response: HttpResponse[String] = Http(s"http://${conf.goServerHost}:${conf.goServerPort}" + "/go/api/jobs/scheduled.xml").asString //.header("Authorization", s"Basic ${authToken}").asString | ||
| val responseXml = scala.xml.XML.loadString(response.body) | ||
| val responseXml = xmlRequest(buildUrl() + "/go/api/jobs/scheduled.xml") | ||
| (responseXml \\ "scheduledJobs" \\ "job").size | ||
| } catch { | ||
| case e: SocketTimeoutException => { | ||
|
|
@@ -28,19 +52,48 @@ case class GOCDPoller(conf: FrameworkConfig) { | |
| } | ||
|
|
||
| def goIdleAgentsCount() = { | ||
| println("Polling Go server for idle agents") | ||
| try { | ||
| val responseJson = jsonRequest(buildUrl() + "/go/api/agents") | ||
| (responseJson \ "_embedded" \ "agents").toOption.map(jsValue => { | ||
| jsValue.as[JsArray].value.map(agent => (agent \ "status").get.as[String]).count(_.equalsIgnoreCase("idle")) | ||
| }).getOrElse(0) | ||
| } catch { | ||
| case e: SocketTimeoutException => { | ||
| println("GOCD Server timed out!!") | ||
| 1 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Return true when the demand remains, same or increased during the last five attempts. | ||
| private[mesos] def isDemandPositive(): Boolean = { | ||
| val demandTrend = responseHistory.foldRight(0)((h1, h2) => { | ||
| if(h2 >= h1) { | ||
| 1 | ||
| } else { | ||
| -1 | ||
| } | ||
| }) | ||
| demandTrend == 1 | ||
| } | ||
|
|
||
| def pollAndAddTask() = { | ||
| val scheduled : Int = goTaskQueueSize() | ||
| println(s"Go server has ${scheduled.toString} pending jobs to be scheduled") | ||
| if(scheduled > 0) | ||
| if(scheduled > 0) { | ||
| responseHistory += scheduled | ||
|
|
||
| } | ||
| if(responseHistory.size > 5) { | ||
|
||
| println(s"More than 5 jobs pending in the GOCD. queuing a new agent launch now.") | ||
| TaskQueue.enqueue(GoTask("", conf.goAgentDocker, "")) | ||
| responseHistory.clear() | ||
| println(s"More than 5 jobs pending in the GOCD. checking if any agents are idle.") | ||
| if(goIdleAgentsCount() == 0 && isDemandPositive()) { | ||
| println("No idle agents found. Launching a new agent launch now.") | ||
| TaskQueue.enqueue(GoTask("", conf.goAgentDocker, "")) | ||
| responseHistory.clear() | ||
| } else { | ||
| println("Idle agents found. Not launching any new Go Agent now.") | ||
| responseHistory.drop(0) | ||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tamizhgeek I know the api.go.cd response says this is the schema of the response, but can you quickly check the same on our servers? Our main build is running the latest 16.2.0. They look different actually. We might want to file a bug against it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ashwanthkumar The difference in the responses are because of the Accept header we send. The difference is this:
curl -X GET http://build.gocd.io/go/api/agents -H "Authorization: Basic asdadsadasdasd="and
curl -X GET http://build.gocd.io/go/api/agents -H "Authorization: Basic adsdasdasda=" -H "Accept: application/vnd.go.cd.v1+json":)