Skip to content

Commit

Permalink
Fix: DNS TTL uses seconds while caching usese millis (akka#25848)
Browse files Browse the repository at this point in the history
* Fix: DNS TTL uses seconds while caching uses millis
* Consistently rename ttl vals in companion objects. Add mima filters.
* Rename fields to ttlInZzzz format
  • Loading branch information
ignasi35 authored and patriknw committed Nov 2, 2018
1 parent 87a033e commit 48cf769
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
8 changes: 8 additions & 0 deletions akka-actor/src/main/mima-filters/2.5.17.backwards.excludes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# #25848 - TTL seconds used when TTL milliseconds expected
# Change of a field name with ApiMayChange
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.UnknownRecord.ttl")
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.ARecord.ttl")
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.CNameRecord.ttl")
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.AAAARecord.ttl")
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.ResourceRecord.ttl")
ProblemFilters.exclude[DirectMissingMethodProblem]("akka.io.dns.SRVRecord.ttl")
42 changes: 21 additions & 21 deletions akka-actor/src/main/scala/akka/io/dns/DnsResourceRecords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import akka.util.{ ByteIterator, ByteString, ByteStringBuilder }
import scala.annotation.switch

@ApiMayChange
sealed abstract class ResourceRecord(val name: String, val ttl: Int, val recType: Short, val recClass: Short)
sealed abstract class ResourceRecord(val name: String, val ttlInSeconds: Int, val recType: Short, val recClass: Short)
extends NoSerializationVerificationNeeded {

/**
Expand All @@ -29,8 +29,8 @@ sealed abstract class ResourceRecord(val name: String, val ttl: Int, val recType
}

@ApiMayChange
final case class ARecord(override val name: String, override val ttl: Int,
ip: InetAddress) extends ResourceRecord(name, ttl, RecordType.A.code, RecordClass.IN.code) {
final case class ARecord(override val name: String, override val ttlInSeconds: Int,
ip: InetAddress) extends ResourceRecord(name, ttlInSeconds, RecordType.A.code, RecordClass.IN.code) {

/**
* INTERNAL API
Expand All @@ -49,16 +49,16 @@ final case class ARecord(override val name: String, override val ttl: Int,
*/
@InternalApi
private[dns] object ARecord {
def parseBody(name: String, ttl: Int, length: Short, it: ByteIterator): ARecord = {
def parseBody(name: String, ttlInSeconds: Int, length: Short, it: ByteIterator): ARecord = {
val addr = Array.ofDim[Byte](4)
it.getBytes(addr)
ARecord(name, ttl, InetAddress.getByAddress(addr).asInstanceOf[Inet4Address])
ARecord(name, ttlInSeconds, InetAddress.getByAddress(addr).asInstanceOf[Inet4Address])
}
}

@ApiMayChange
final case class AAAARecord(override val name: String, override val ttl: Int,
ip: Inet6Address) extends ResourceRecord(name, ttl, RecordType.AAAA.code, RecordClass.IN.code) {
final case class AAAARecord(override val name: String, override val ttlInSeconds: Int,
ip: Inet6Address) extends ResourceRecord(name, ttlInSeconds, RecordType.AAAA.code, RecordClass.IN.code) {

/**
* INTERNAL API
Expand All @@ -82,16 +82,16 @@ private[dns] object AAAARecord {
* INTERNAL API
*/
@InternalApi
def parseBody(name: String, ttl: Int, length: Short, it: ByteIterator): AAAARecord = {
def parseBody(name: String, ttlInSeconds: Int, length: Short, it: ByteIterator): AAAARecord = {
val addr = Array.ofDim[Byte](16)
it.getBytes(addr)
AAAARecord(name, ttl, InetAddress.getByAddress(addr).asInstanceOf[Inet6Address])
AAAARecord(name, ttlInSeconds, InetAddress.getByAddress(addr).asInstanceOf[Inet6Address])
}
}

@ApiMayChange
final case class CNameRecord(override val name: String, override val ttl: Int,
canonicalName: String) extends ResourceRecord(name, ttl, RecordType.CNAME.code, RecordClass.IN.code) {
final case class CNameRecord(override val name: String, override val ttlInSeconds: Int,
canonicalName: String) extends ResourceRecord(name, ttlInSeconds, RecordType.CNAME.code, RecordClass.IN.code) {
/**
* INTERNAL API
*/
Expand All @@ -109,14 +109,14 @@ private[dns] object CNameRecord {
* INTERNAL API
*/
@InternalApi
def parseBody(name: String, ttl: Int, length: Short, it: ByteIterator, msg: ByteString): CNameRecord = {
CNameRecord(name, ttl, DomainName.parse(it, msg))
def parseBody(name: String, ttlInSeconds: Int, length: Short, it: ByteIterator, msg: ByteString): CNameRecord = {
CNameRecord(name, ttlInSeconds, DomainName.parse(it, msg))
}
}

@ApiMayChange
final case class SRVRecord(override val name: String, override val ttl: Int,
priority: Int, weight: Int, port: Int, target: String) extends ResourceRecord(name, ttl, RecordType.SRV.code, RecordClass.IN.code) {
final case class SRVRecord(override val name: String, override val ttlInSeconds: Int,
priority: Int, weight: Int, port: Int, target: String) extends ResourceRecord(name, ttlInSeconds, RecordType.SRV.code, RecordClass.IN.code) {
/**
* INTERNAL API
*/
Expand All @@ -139,18 +139,18 @@ private[dns] object SRVRecord {
* INTERNAL API
*/
@InternalApi
def parseBody(name: String, ttl: Int, length: Short, it: ByteIterator, msg: ByteString): SRVRecord = {
def parseBody(name: String, ttlInSeconds: Int, length: Short, it: ByteIterator, msg: ByteString): SRVRecord = {
val priority = it.getShort
val weight = it.getShort
val port = it.getShort
SRVRecord(name, ttl, priority, weight, port, DomainName.parse(it, msg))
SRVRecord(name, ttlInSeconds, priority, weight, port, DomainName.parse(it, msg))
}
}

@ApiMayChange
final case class UnknownRecord(override val name: String, override val ttl: Int,
final case class UnknownRecord(override val name: String, override val ttlInSeconds: Int,
override val recType: Short, override val recClass: Short,
data: ByteString) extends ResourceRecord(name, ttl, recType, recClass) {
data: ByteString) extends ResourceRecord(name, ttlInSeconds, recType, recClass) {
/**
* INTERNAL API
*/
Expand All @@ -171,8 +171,8 @@ private[dns] object UnknownRecord {
* INTERNAL API
*/
@InternalApi
def parseBody(name: String, ttl: Int, recType: Short, recClass: Short, length: Short, it: ByteIterator): UnknownRecord =
UnknownRecord(name, ttl, recType, recClass, it.toByteString)
def parseBody(name: String, ttlInSeconds: Int, recType: Short, recClass: Short, length: Short, it: ByteIterator): UnknownRecord =
UnknownRecord(name, ttlInSeconds, recType, recClass, it.toByteString)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ import scala.annotation.tailrec
}

@tailrec
private[io] final def put(key: (String, QueryType), records: Answer, ttlMillis: Long): Unit = {
private[io] final def put(key: (String, QueryType), records: Answer, ttlInMillis: Long): Unit = {
val c = cache.get()
if (!cache.compareAndSet(c, c.put(key, records, ttlMillis)))
put(key, records, ttlMillis)
if (!cache.compareAndSet(c, c.put(key, records, ttlInMillis)))
put(key, records, ttlInMillis)
}

@tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ private[io] final class AsyncDnsResolver(
ipv4Recs.flatMap(ipv4Records {
// TODO, do we want config to specify a max for this?
if (ipv4Records.rrs.nonEmpty) {
val minTtl4 = ipv4Records.rrs.minBy(_.ttl).ttl
cache.put((name, Ipv4Type), ipv4Records, minTtl4)
val minTtl4 = ipv4Records.rrs.minBy(_.ttlInSeconds).ttlInSeconds
cache.put((name, Ipv4Type), ipv4Records, minTtl4 * 1000)
}
ipv6Recs.map(ipv6Records {
if (ipv6Records.rrs.nonEmpty) {
val minTtl6 = ipv6Records.rrs.minBy(_.ttl).ttl
cache.put((name, Ipv6Type), ipv6Records, minTtl6)
val minTtl6 = ipv6Records.rrs.minBy(_.ttlInSeconds).ttlInSeconds
cache.put((name, Ipv6Type), ipv6Records, minTtl6 * 1000)
}
ipv4Records.rrs ++ ipv6Records.rrs
}).map(recs DnsProtocol.Resolved(name, recs))
Expand All @@ -134,12 +134,12 @@ private[io] final class AsyncDnsResolver(
Future.successful(DnsProtocol.Resolved(name, r.rrs, r.additionalRecs))
case None
sendQuestion(resolver, SrvQuestion(nextId(), caseFoldedName))
.map(r {
if (r.rrs.nonEmpty) {
val minTtl = r.rrs.minBy(_.ttl).ttl
cache.put((name, SrvType), r, minTtl)
.map(answer {
if (answer.rrs.nonEmpty) {
val minttlInSeconds = answer.rrs.minBy(_.ttlInSeconds).ttlInSeconds
cache.put((name, SrvType), answer, minttlInSeconds * 1000) // cache uses ttl in millis
}
DnsProtocol.Resolved(name, r.rrs, r.additionalRecs)
DnsProtocol.Resolved(name, answer.rrs, answer.additionalRecs)
})
}

Expand Down

0 comments on commit 48cf769

Please sign in to comment.