Skip to content
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

Merged
merged 5 commits into from
Mar 22, 2022
Merged

Pre-scala 2.13: inline Box #24768

merged 5 commits into from
Mar 22, 2022

Conversation

DavidLawes
Copy link
Contributor

@DavidLawes DavidLawes commented Mar 15, 2022

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:

^^ 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 ?

  • No
  • Yes (please indicate your plans for DCR Implementation)

Screenshots

N/A

What is the value of this and can you measure success?

Checklist

Does this affect other platforms?

  • AMP
  • Apps
  • Other (please specify)

Does this affect GLabs Paid Content Pages? Should it have support for Paid Content?

  • No
  • Yes (please give details)

Does this change break ad-free?

  • No
  • It did, but tests caught it and I fixed it
  • It did, but there was no test coverage so I added that then fixed it

Does this change update the version of CAPI we're using?

Accessibility test checklist

Tested

  • Locally
  • On CODE (optional)

@DavidLawes DavidLawes added the Scala 2.13 See https://github.com/guardian/maintaining-scala-projects/issues/2 label Mar 15, 2022
@DavidLawes DavidLawes linked an issue Mar 15, 2022 that may be closed by this pull request
@@ -0,0 +1,46 @@
package common
Copy link
Contributor Author

@DavidLawes DavidLawes Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,16 @@
package common
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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 {
Copy link
Contributor Author

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())
Copy link
Contributor Author

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?

Copy link
Contributor

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())
Copy link
Contributor Author

@DavidLawes DavidLawes Mar 16, 2022

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)

@DavidLawes DavidLawes marked this pull request as ready for review March 16, 2022 12:37
@DavidLawes DavidLawes requested review from a team as code owners March 16, 2022 12:37
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] {
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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)
Copy link
Contributor

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?

@DavidLawes DavidLawes merged commit 995d8b9 into main Mar 22, 2022
@DavidLawes DavidLawes deleted the dlawes/update-guBox branch March 22, 2022 14:06
@prout-bot prout-bot added Seen-on-PROD and removed Scala 2.13 See https://github.com/guardian/maintaining-scala-projects/issues/2 labels Mar 22, 2022
@prout-bot
Copy link
Collaborator

Seen on PROD (merged by @DavidLawes 7 hours, 29 minutes and 40 seconds ago)

@DavidLawes DavidLawes mentioned this pull request Apr 1, 2022
4 tasks
@ioannakok ioannakok mentioned this pull request Aug 23, 2022
2 tasks
@rtyley rtyley added the Scala 2.13 See https://github.com/guardian/maintaining-scala-projects/issues/2 label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scala 2.13 See https://github.com/guardian/maintaining-scala-projects/issues/2 Seen-on-PROD
Projects
None yet
Development

Successfully merging this pull request may close these issues.

box
5 participants