Skip to content

Commit

Permalink
updates to run stateless evaluation on native
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed May 4, 2022
1 parent 527916a commit 1c152f2
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions evaluation-core/src/commonMain/kotlin/EvaluationEngine.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.amplitude.experiment.evaluation

import kotlin.native.concurrent.SharedImmutable

interface EvaluationEngine {
fun evaluate(flags: List<FlagConfig>, user: SkylabUser?): Map<String, FlagResult>
}

@SharedImmutable
private const val MAX_HASH_VALUE = 4294967295L

@SharedImmutable
private const val MAX_VARIANT_HASH_VALUE = MAX_HASH_VALUE.floorDiv(100)

internal data class EvaluationResult(val variant: Variant, val description: String) {
Expand Down
3 changes: 3 additions & 0 deletions evaluation-core/src/commonMain/kotlin/FlagConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.amplitude.experiment.evaluation

import kotlin.native.concurrent.SharedImmutable

@SharedImmutable
const val DEFAULT_BUCKETING_KEY = "amplitude_id"

data class FlagConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.amplitude.experiment.evaluation

import kotlin.native.concurrent.SharedImmutable

@SharedImmutable
private val VERSION_USER_PROPS: Set<String> = hashSetOf("version", "start_version")

@SharedImmutable
private val VERSION_OPERATOR_MAP: Map<Operator, Operator> = mapOf(
Operator.LESS_THAN to Operator.VERSION_LESS_THAN,
Operator.LESS_THAN_EQUALS to Operator.VERSION_LESS_THAN_EQUALS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.amplitude.experiment.evaluation

import kotlin.native.concurrent.SharedImmutable

@SharedImmutable
private const val NONE = "(none)"

internal data class StringMatchColumnFilter(
Expand Down Expand Up @@ -38,7 +41,10 @@ internal fun StringMatchColumnFilter.matches(value: String?): Boolean {
Operator.IS_NOT -> !value.matchesIs(values, hasBooleans)
Operator.CONTAINS -> value.matchesContains(values)
Operator.DOES_NOT_CONTAIN -> !value.matchesContains(values)
Operator.LESS_THAN, Operator.LESS_THAN_EQUALS, Operator.GREATER_THAN, Operator.GREATER_THAN_EQUALS -> value.matchesCompare(values, operator)
Operator.LESS_THAN, Operator.LESS_THAN_EQUALS, Operator.GREATER_THAN, Operator.GREATER_THAN_EQUALS -> value.matchesCompare(
values,
operator
)
else -> throw IllegalArgumentException("Unexpected or unsupported operator $operator")
}
}
Expand Down
3 changes: 3 additions & 0 deletions evaluation-core/src/commonMain/kotlin/UserPropertyFilter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.amplitude.experiment.evaluation

import kotlin.native.concurrent.SharedImmutable

@SharedImmutable
private const val COHORT_PROP_KEY = "userdata_cohort"

data class UserPropertyFilter(
Expand Down
9 changes: 7 additions & 2 deletions evaluation-interop/src/commonMain/kotlin/Evaluation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import com.amplitude.experiment.evaluation.EvaluationEngineImpl
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

private val engine = EvaluationEngineImpl()

/**
* [rules] is a JSON representation of Array<[FlagConfig]>
*
Expand All @@ -17,6 +16,12 @@ private val engine = EvaluationEngineImpl()
@OptIn(ExperimentalJsExport::class, ExperimentalSerializationApi::class)
@JsExport
fun evaluate(rules: String, user: String): String {
val format = Json {
ignoreUnknownKeys = true
isLenient = true
coerceInputValues = true
}
val engine = EvaluationEngineImpl()
val flagsDecoded = format.decodeFromString<List<FlagConfig>>(rules)
val userDecoded = format.decodeFromString<ExperimentUser>(user)
val results = engine.evaluate(flagsDecoded.map { it.convert() }, userDecoded.convert())
Expand Down
9 changes: 2 additions & 7 deletions evaluation-interop/src/commonMain/kotlin/Serialization.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import com.amplitude.experiment.evaluation.SkylabUser
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject

internal val format = Json {
ignoreUnknownKeys = true
isLenient = true
coerceInputValues = true
}
import kotlin.native.concurrent.SharedImmutable

@Serializable
internal data class Allocation(
Expand All @@ -33,6 +27,7 @@ internal enum class EvaluationMode(val value: String) {
}
}

@SharedImmutable
internal const val DEFAULT_BUCKETING_KEY = "amplitude_id"

@Serializable
Expand Down
2 changes: 1 addition & 1 deletion go/internal/evaluation/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package evaluation
#cgo darwin,amd64 CFLAGS: -I${SRCDIR}/lib/macosX64
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/macosX64 -levaluation_interop
#cgo darwin,amd64 CFLAGS: -I${SRCDIR}/lib/macosArm64
#cgo darwin,arm64 CFLAGS: -I${SRCDIR}/lib/macosArm64
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/macosArm64 -levaluation_interop
#cgo linux,amd64 CFLAGS: -I${SRCDIR}/lib/linuxX64
Expand Down
Binary file modified go/internal/evaluation/lib/linuxArm64/libevaluation_interop.so
Binary file not shown.
Binary file modified go/internal/evaluation/lib/linuxX64/libevaluation_interop.so
Binary file not shown.
Binary file not shown.
Binary file modified go/internal/evaluation/lib/macosX64/libevaluation_interop.dylib
Binary file not shown.
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
kotlin.code.style=official
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
kotlin.js.generate.executable.default=false
kotlin.mpp.stability.nowarn=true
Expand Down

0 comments on commit 1c152f2

Please sign in to comment.