-
Notifications
You must be signed in to change notification settings - Fork 554
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
Pre-scala 2.13: inline Box #24768
Pre-scala 2.13: inline Box #24768
Conversation
@@ -0,0 +1,46 @@ | |||
package common |
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.
This is copypasta from https://github.com/guardian/box/blob/master/src/main/scala/com/gu/Box.scala
@@ -0,0 +1,16 @@ | |||
package common |
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.
This is copypasta from https://github.com/guardian/box/blob/master/src/test/scala/com/gu/BoxSpec.scala
@@ -44,4 +45,44 @@ class DurationMetricTest extends FlatSpec with Matchers { | |||
dataPoints.length should be(7) | |||
dataPoints.splitAt(4)._1 should be(allMetrics) | |||
} | |||
|
|||
"SamplerMetric" should "start off empty" in { |
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.
The SamplerMetric functionality is almost identical to DurationMetric count, but the recording of the metric/sample is handled by 2 different functions. These tests cover the SamplerMetric recording functionality, they were missing before and helped me try to debug what was going on
@@ -128,7 +127,7 @@ final case class DurationMetric(override val name: String, override val metricUn | |||
} | |||
|
|||
// Public for tests. | |||
def record(dataPoint: DurationDataPoint): Unit = dataPoints.alter(dataPoint :: _) | |||
def record(dataPoint: DurationDataPoint): Unit = dataPoints.alter(dataPoint :: dataPoints.get()) |
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.
Without this change I was seeing an 'unbound placeholder parameter error' which I think means scala didn't know how to interpret _
. What I think we want from this function is to append new data points to the list (without this change we just overwrote any existing datapoints, List always had length of 1).
I don't understand why my change introduced this error though. Any ideas?
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.
I'm struggling to understand how it would have understood _
in the first place to be honest!
@@ -160,7 +159,7 @@ final case class SamplerMetric(override val name: String, override val metricUni | |||
} | |||
|
|||
def recordSample(sampleValue: Double, sampleTime: DateTime): Future[List[SampledDataPoint]] = | |||
dataPoints.alter(SampledDataPoint(sampleValue, sampleTime) :: _) | |||
dataPoints.alter(SampledDataPoint(sampleValue, sampleTime) :: dataPoints.get()) |
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.
I was curious if this function exhibited the same behaviour as record
above (i.e. _
results in an 'unbound placeholder parameter error'). After writing tests I confirmed this and believe we now add new sampled data points to the head of the list.
Again, not sure why my change resulted in the need for this change (although here it could've existed already, there was no test coverage)
The box class used the same reference for the initial value and inside the alter method. The result was an error in the alter method that prevent us from ever updating correctly. This change amends the box class and reverts FrontendMetrics. Co-authored-by: David Furey <david.furey@guardian.co.uk>
def apply[T](initialValue: T): Box[T] = new AtomicRefBox[T](initialValue) | ||
} | ||
|
||
private class AtomicRefBox[T](initialValue: T) extends Box[T] { |
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.
Note the omission of modify
, map
and flatMap
(https://github.com/guardian/box/blob/master/src/main/scala/com/gu/Box.scala#L38). These functions aren't used in frontend
and the modify
function has not been implemented correctly in box
.
} | ||
|
||
private class AtomicRefBox[T](initialValue: T) extends Box[T] { | ||
private val ref: AtomicReference[T] = new AtomicReference[T](initialValue) |
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.
Note the renaming of the initial value. In box
this is named t meaning that in the alter and modify methods t
was always resolving to this initial value.
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.
This comment and the above one are good comments, would the be useful to future devs coming to this code?
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.
Good shout, will add :)
} | ||
|
||
private class AtomicRefBox[T](initialValue: T) extends Box[T] { | ||
private val ref: AtomicReference[T] = new AtomicReference[T](initialValue) |
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.
This comment and the above one are good comments, would the be useful to future devs coming to this code?
Seen on PROD (merged by @DavidLawes 7 hours, 29 minutes and 40 seconds ago)
|
What does this change?
In advance of updating the scala version we want to update as many dependencies as possible to be compatible with both 2.12 and 2.13.
We tried updating frontend scala to 2.13. One of the compilation errors was from com.gu.box. Given box is just a very thin wrapper of
AtomicReference
, and only used by a handful of repos, it's been suggested to inline it.In this PR we remove Box as a dependency and define it in
common
. Most of the changes relate to updated imports.One specific error caused me a bit of confusion:
unbound placeholder parameter error
).^^ to resolve the failing tests we had to update the methods defined in the Box class. There was actually an error in the library itself (around variable names and scopes), which meant that we were only ever altering or modifying the original value for our ref, never the latest. Now that these methods in the local Box class are updated (or removed, if not being used), our tests pass.
Does this change need to be reproduced in dotcom-rendering ?
Screenshots
N/A
What is the value of this and can you measure success?
Checklist
Does this affect other platforms?
Does this affect GLabs Paid Content Pages? Should it have support for Paid Content?
Does this change break ad-free?
Does this change update the version of CAPI we're using?
Accessibility test checklist
Tested