Skip to content

Commit

Permalink
finagle-core, twitter-server: Add c.t.finagle.DtabFlags
Browse files Browse the repository at this point in the history
Problem

There is currently no defined way for users of the `c.t.finagle.Dtab` API to
append to the `Dtab.base` delegation table with a supplemental value that is
passed at runtime.

Solution

Add the `c.t.finagle.DtabFlags` trait which defines a `c.t.app.Flag`, "dtab.add"
that can provide a supplemental delegation table (`Dtab`) to append to the
current server `Dtab.base`.

The trait exposes a `addDtabs()` function which applies the parsed Flag value
and appends it to the current `Dtab.base`.

Result

Users are able to augment the base delegation table (`Dtab.base`) of the server
with a value passed at runtime via the command line.

JIRA Issues: CSL-7766

Differential Revision: https://phabricator.twitter.biz/D297596
  • Loading branch information
cacoco authored and jenkins committed Apr 15, 2019
1 parent 90e9ce7 commit a00e394
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Changes

* Remove deprecated uses of `c.t.server.ShadowAdminServer`. ``PHAB_ID=D269149``

* Mix in the `c.t.finagle.DtabFlags` to allow servers to append to the "base" `c.t.finagle.Dtab`
delegation table. Users can now call `c.t.finagle.DtabFlags#addDtabs()` when they want to append
the parsed Flag value to the `Dtab.base` delegation table. Users should note to only call this
method _after_ Flag parsing has occurred (which is after **init** and before **premain**).

We also update the `c.t.server.handler.DtabHandler` to always return a proper JSON response of
the currently configured `c.t.finagle.Dtab.base`. ``PHAB_ID=D297596``

19.3.0
------

Expand Down
4 changes: 2 additions & 2 deletions server/src/main/scala/com/twitter/server/Admin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ trait Admin { self: App with AdminHttpServer with Stats =>
),
Route(
path = "/admin/dtab",
handler = new TextBlockView().andThen(new DtabHandler),
handler = new DtabHandler,
alias = "Dtab",
group = Some(Grouping.ProcessInfo),
includeInIndex = true
Expand Down Expand Up @@ -195,7 +195,7 @@ trait Admin { self: App with AdminHttpServer with Stats =>
),
Route(
path = LoadBalancersHandler.RoutePath,
handler = new LoadBalancersHandler(),
handler = new LoadBalancersHandler,
alias = "Load Balancers",
group = Some(Grouping.ProcessInfo),
includeInIndex = true
Expand Down
5 changes: 3 additions & 2 deletions server/src/main/scala/com/twitter/server/FlagResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import com.twitter.app.GlobalFlag
import com.twitter.finagle.{Addr, Resolver, Name}
import com.twitter.util.Var

// TODO: deprecate in favor of Wily dtabs.

@deprecated(
"Users should prefer using Dtabs which are overridable by setting the `dtab.add` flag",
"2019-04-03")
object resolverMap
extends GlobalFlag[Map[String, String]](
Map.empty,
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/scala/com/twitter/server/TwitterServer.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twitter.server

import com.twitter.app.App
import com.twitter.finagle.DtabFlags
import com.twitter.finagle.util.DefaultTimer
import com.twitter.util.Timer
import com.twitter.util.logging.Logging
Expand Down Expand Up @@ -36,6 +37,7 @@ trait TwitterServer
with Slf4jBridge
with Logging
with Linters
with DtabFlags
with Hooks
with AdminHttpServer
with Admin
Expand Down
36 changes: 29 additions & 7 deletions server/src/main/scala/com/twitter/server/handler/DtabHandler.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package com.twitter.server.handler

import com.twitter.finagle.http.{MediaType, Request, Response}
import com.twitter.finagle.{Dtab, Service}
import com.twitter.finagle.http.{Request, Response}
import com.twitter.server.util.HttpUtils.newOk
import com.twitter.io.Buf
import com.twitter.server.util.{HttpUtils, JsonConverter}
import com.twitter.util.Future

/**
* Dumps a simple string representation of the current Dtab.
* Dumps a simple JSON string representation of the current [[com.twitter.finagle.Dtab.base]].
*
* From the Dtab docs: A Dtab--short for delegation table--comprises a sequence
* of delegation rules. Together, these describe how to bind a
* path to an Addr.
* of delegation rules. Together, these describe how to bind a path to an Addr.
*
* {{{
* {
* "dtab": [
* "/srv => /srv#/production",
* "/srv => /srv#/prod",
* "/s => /srv/local",
* "/$/inet => /$/nil",
* "/zk => /$/nil"
* ]
* }
* }}}
*
* @see [[com.twitter.finagle.Dtab]]
*/
class DtabHandler extends Service[Request, Response] {
final class DtabHandler extends Service[Request, Response] {
def apply(req: Request): Future[Response] =
newOk(Dtab.base.toString)
HttpUtils.newResponse(contentType = MediaType.Json, content = Buf.Utf8(jsonResponse))

private[this] def jsonResponse: String = {
JsonConverter.writeToString(Map("dtab" -> formattedDtabEntries))
}

private[this] def formattedDtabEntries: Seq[String] = {
for (dentry <- Dtab.base) yield { s"${dentry.prefix.show} => ${dentry.dst.show}" }
}
}

0 comments on commit a00e394

Please sign in to comment.