Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Remove QuotaCalculation due to unexpected sideeffects. (#1196) (EXPOU…
Browse files Browse the repository at this point in the history
…SREAPP-2640)
  • Loading branch information
d4rken committed Sep 21, 2020
1 parent 35711de commit daf5e55
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 502 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import de.rki.coronawarnapp.CoronaWarnApplication
import de.rki.coronawarnapp.R
import de.rki.coronawarnapp.risk.RiskLevel
import de.rki.coronawarnapp.util.security.SecurityHelper.globalEncryptedSharedPreferencesInstance
import org.joda.time.Instant
import java.util.Date

/**
Expand All @@ -19,11 +18,6 @@ object LocalData {

private val TAG: String? = LocalData::class.simpleName

private const val PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS =
"preference_next_time_rate_limiting_unlocks"
private const val PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT =
"preference_google_api_provide_diagnosis_keys_call_count"

/****************************************************
* ONBOARDING DATA
****************************************************/
Expand Down Expand Up @@ -396,40 +390,6 @@ object LocalData {
}
}

var nextTimeRateLimitingUnlocks: Instant
get() {
return Instant.ofEpochMilli(
getSharedPreferenceInstance().getLong(
PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS,
0L
)
)
}
set(value) {
getSharedPreferenceInstance().edit(true) {
putLong(
PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS,
value.millis
)
}
}

var googleAPIProvideDiagnosisKeysCallCount: Int
get() {
return getSharedPreferenceInstance().getInt(
PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT,
0
)
}
set(value) {
getSharedPreferenceInstance().edit(true) {
putInt(
PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT,
value
)
}
}

/**
* Gets the last time of successful risk level calculation as long
* from the EncryptedSharedPrefs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,17 @@ import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.Retriev
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.CLOSE
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.FETCH_DATE_UPDATE
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.FILES_FROM_WEB_REQUESTS
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.QUOTA_CALCULATION
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.RETRIEVE_RISK_SCORE_PARAMS
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.SETUP
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.TOKEN
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.rollback
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.start
import de.rki.coronawarnapp.util.CachedKeyFileHolder
import de.rki.coronawarnapp.util.GoogleQuotaCalculator
import de.rki.coronawarnapp.util.QuotaCalculator
import de.rki.coronawarnapp.util.di.AppInjector
import de.rki.coronawarnapp.worker.BackgroundWorkHelper
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Duration
import org.joda.time.Instant
import org.joda.time.chrono.GJChronology
import timber.log.Timber
import java.io.File
import java.util.Date
Expand Down Expand Up @@ -95,9 +90,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
/** Initial Setup of the Transaction and Transaction ID Generation and Date Lock */
SETUP,

/** calculates the Quota so that the rate limiting is caught gracefully*/
QUOTA_CALCULATION,

/** Initialisation of the identifying token used during the entire transaction */
TOKEN,

Expand Down Expand Up @@ -126,32 +118,19 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
/** atomic reference for the rollback value for created files during the transaction */
private val exportFilesForRollback = AtomicReference<List<File>>()

private val progressTowardsQuotaForRollback = AtomicReference<Int>()

private val transactionScope: TransactionCoroutineScope by lazy {
AppInjector.component.transRetrieveKeysInjection.transactionScope
}

private val enfClient: ENFClient
get() = AppInjector.component.transRetrieveKeysInjection.cwaEnfClient

private const val QUOTA_RESET_PERIOD_IN_HOURS = 24

private val quotaCalculator: QuotaCalculator<Int> = GoogleQuotaCalculator(
incrementByAmount = 14,
quotaLimit = 20,
quotaResetPeriod = Duration.standardHours(QUOTA_RESET_PERIOD_IN_HOURS.toLong()),
quotaTimeZone = DateTimeZone.UTC,
quotaChronology = GJChronology.getInstanceUTC()
)

suspend fun startWithConstraints() {
val currentDate = DateTime(Instant.now(), DateTimeZone.UTC)
val lastFetch = DateTime(
LocalData.lastTimeDiagnosisKeysFromServerFetch(),
DateTimeZone.UTC
)

if (LocalData.lastTimeDiagnosisKeysFromServerFetch() == null ||
currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
) {
Expand Down Expand Up @@ -181,17 +160,8 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
val currentDate = executeSetup()

/****************************************************
* CALCULATE QUOTA FOR PROVIDE DIAGNOSIS KEYS
* RETRIEVE TOKEN
****************************************************/
val hasExceededQuota = executeQuotaCalculation()

// When we are above the Quote, cancel the execution entirely
if (hasExceededQuota) {
Timber.tag(TAG).w("above quota, skipping RetrieveDiagnosisKeys")
executeClose()
return@lockAndExecute
}

val token = executeToken()

// RETRIEVE RISK SCORE PARAMETERS
Expand Down Expand Up @@ -223,10 +193,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
if (TOKEN.isInStateStack()) {
rollbackToken()
}
// we reset the quota only if the submission has not happened yet
if (QUOTA_CALCULATION.isInStateStack() && !API_SUBMISSION.isInStateStack()) {
rollbackProgressTowardsQuota()
}
} catch (e: Exception) {
// We handle every exception through a RollbackException to make sure that a single EntryPoint
// is available for the caller.
Expand All @@ -244,11 +210,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
LocalData.googleApiToken(googleAPITokenForRollback.get())
}

private fun rollbackProgressTowardsQuota() {
Timber.tag(TAG).v("rollback $QUOTA_CALCULATION")
quotaCalculator.resetProgressTowardsQuota(progressTowardsQuotaForRollback.get())
}

/**
* Executes the INIT Transaction State
*/
Expand All @@ -259,16 +220,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
currentDate
}

/**
* Executes the QUOTA_CALCULATION Transaction State
*/
private suspend fun executeQuotaCalculation() = executeState(
QUOTA_CALCULATION
) {
progressTowardsQuotaForRollback.set(quotaCalculator.getProgressTowardsQuota())
quotaCalculator.calculateQuota()
}

/**
* Executes the TOKEN Transaction State
*/
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkObject
import kotlinx.coroutines.runBlocking
import org.joda.time.Instant
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -62,10 +61,6 @@ class RetrieveDiagnosisKeysTransactionTest {
every { LocalData.lastTimeDiagnosisKeysFromServerFetch() } returns Date()
every { LocalData.lastTimeDiagnosisKeysFromServerFetch(any()) } just Runs
every { LocalData.googleApiToken() } returns UUID.randomUUID().toString()
every { LocalData.googleAPIProvideDiagnosisKeysCallCount = any() } just Runs
every { LocalData.googleAPIProvideDiagnosisKeysCallCount } returns 0
every { LocalData.nextTimeRateLimitingUnlocks = any() } just Runs
every { LocalData.nextTimeRateLimitingUnlocks } returns Instant.now()
}

@AfterEach
Expand All @@ -84,7 +79,6 @@ class RetrieveDiagnosisKeysTransactionTest {

coVerifyOrder {
RetrieveDiagnosisKeysTransaction["executeSetup"]()
RetrieveDiagnosisKeysTransaction["executeQuotaCalculation"]()
RetrieveDiagnosisKeysTransaction["executeRetrieveRiskScoreParams"]()
RetrieveDiagnosisKeysTransaction["executeFetchKeyFilesFromServer"](any<Date>())
}
Expand All @@ -108,7 +102,6 @@ class RetrieveDiagnosisKeysTransactionTest {

coVerifyOrder {
RetrieveDiagnosisKeysTransaction["executeSetup"]()
RetrieveDiagnosisKeysTransaction["executeQuotaCalculation"]()
RetrieveDiagnosisKeysTransaction["executeRetrieveRiskScoreParams"]()
RetrieveDiagnosisKeysTransaction["executeFetchKeyFilesFromServer"](any<Date>())
mockEnfClient.provideDiagnosisKeys(listOf(file), any(), any())
Expand Down
Loading

0 comments on commit daf5e55

Please sign in to comment.