Skip to content
54 changes: 53 additions & 1 deletion .github/workflows/examples-published.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ name: Examples vs Published Packages
# ci/packages.json marks for publishing is installed from PyPI, and only the
# three examples packages and their tests come from this checkout. Each
# package is pinned to the version given when the workflow is run by hand,
# or otherwise to its latest version on PyPI.
# after a publish to the version the publish released it at, and otherwise
# to its latest version on PyPI.
#
# It runs by hand on a branch (gh workflow run "Examples vs Published
# Packages" --ref <branch>), after each run of the two workflows that
Expand Down Expand Up @@ -148,20 +149,71 @@ jobs:

# Each package is pinned, so pip installs that version or fails, and
# never an older one that happens to satisfy the others.
#
# After a publish, each package it released has the tag
# '<package>/<version>' on the commit it published, which
# ci/publish-package.ps1 pushes once the package is uploaded. PyPI can
# take a while to list a new version, so such a package is pinned to
# the version its tag names, waiting until PyPI lists it, rather than
# to whatever PyPI gives as the latest at that moment. Any other
# package is pinned to its latest version on PyPI.
- name: Choose the published versions
if: steps.keys.outputs.run == 'true'
env:
_51DEGREES_PACKAGE_VERSION: ${{ inputs.version }}
_51DEGREES_PUBLISHED_COMMIT: ${{ github.event.workflow_run.head_sha }}
run: |
# The longest to wait for PyPI to list a version a publish tagged,
# and how long to wait between asking, in seconds.
$PyPIWaitSeconds = 900
$PyPIRetrySeconds = 15
$version = "$env:_51DEGREES_PACKAGE_VERSION".Trim()
if ($version -and $version -notmatch '^\d+\.\d+\.\d+$') {
Write-Host "::error::'$version' is not a version such as 4.6.2."
exit 1
}
# The version of each package the publish released, by the name
# PyPI gives it, read from the tags on the published commit.
$tagged = @{}
$commit = "$env:_51DEGREES_PUBLISHED_COMMIT".Trim()
if (-not $version -and $commit) {
$PSNativeCommandUseErrorActionPreference = $true
foreach ($line in (git ls-remote --tags origin)) {
$sha, $ref = $line -split "`t", 2
$tag = $ref -replace '^refs/tags/' -replace '\^\{\}$'
if ($sha -eq $commit -and
$tag -match '^(?<name>\w+)/(?<version>\d+\.\d+\.\d+)$') {
$tagged[$Matches.name -replace '_', '-'] = $Matches.version
}
}
}
# Whether PyPI's simple index, which pip reads, lists a version.
function Test-OnPyPI([string]$Name, [string]$Version) {
try {
$index = Invoke-RestMethod "https://pypi.org/simple/$Name/" `
-Headers @{ Accept = 'application/vnd.pypi.simple.v1+json' }
return @($index.versions) -contains $Version
} catch {
return $false
}
}
$names = Get-Content -Raw ci/packages.json | ConvertFrom-Json |
Where-Object Publish | ForEach-Object { $_.Name -replace '_', '-' }
$pins = foreach ($name in $names) {
$pinned = $version
if (-not $pinned -and $tagged.ContainsKey($name)) {
$pinned = $tagged[$name]
$deadline = (Get-Date).AddSeconds($PyPIWaitSeconds)
while (-not (Test-OnPyPI $name $pinned)) {
if ((Get-Date) -ge $deadline) {
Write-Host ("::error title=Not on PyPI::The publish " +
"tagged $name $pinned, which PyPI did not list " +
"within $PyPIWaitSeconds seconds.")
exit 1
}
Start-Sleep -Seconds $PyPIRetrySeconds
}
}
if (-not $pinned) {
$pinned = (Invoke-RestMethod "https://pypi.org/pypi/$name/json").info.version
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ def test_the_same_user_agent_again_is_answered_from_the_cache(self):
self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(2, cloud.data_requests)

def test_with_the_javascript_builder_each_request_reaches_the_cloud(self):
# The builder's defaults add the sequence element, which gives every
# request a session id and a sequence, so the Cloud Request Engine
# sends each request to the cloud service and stores no answer.
cloud = StandInCloud()
cache = LRUEngineCache()
pipeline = self.pipeline(cloud, cache)

for _ in range(3):
self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(3, cloud.data_requests)
self.assertEqual(0, len(cache.cache))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
from .constants import Constants

import json
import logging
import threading

from fiftyone_pipeline_engines.missingproperty_service import MissingPropertyService

# The logger a cloud engine warns through while no pipeline has given it one.
_LOGGER = logging.getLogger(__name__)

# Why a pipeline with a second CloudRequestEngine is refused.
_ANOTHER_REQUEST_ENGINE = (
"This cloud engine is already in a pipeline with another Cloud Request "
Expand All @@ -46,11 +50,13 @@
# The values HumanConfidence can have. Unknown is not one of them.
HUMAN_CONFIDENCE_VALUES = ("High", "Medium", "Low")

# Why a cache on a cloud engine is refused.
CACHE_ON_REQUEST_ENGINE = (
"A cloud engine accepts no evidence, so there is no key to use in a "
"results cache. Add the cache to the Cloud Request Engine instead, as "
"the pipeline builders' cache setting does.")
# Logged once for each cloud engine given a cache, with the engine's class
# name and data key.
CACHE_IGNORED = (
"The cloud engine {} ('{}') was given a cache, which it ignores. A cloud "
"engine accepts no evidence, so there is no key to use in a results "
"cache. Add the cache to the Cloud Request Engine instead, as the "
"pipeline builders' cache setting does.")

class OnPremiseMissingPropertyService(MissingPropertyService):

Expand Down Expand Up @@ -83,6 +89,9 @@ def __init__(self):
self._properties_final = False
self._properties_lock = threading.Lock()

# Whether set_cache has logged that this engine ignores a cache.
self._cache_ignored_logged = False

def get_evidence_key_filter(self):

"""!
Expand All @@ -95,14 +104,29 @@ def get_evidence_key_filter(self):
def set_cache(self, cache):

"""!
Not supported. A cloud engine accepts no evidence, so there is no
key to use in a cache, as the Cloud Aspect Engine specification
says. The Cloud Request Engine's cache is keyed on the evidence it
sends and holds the response this engine reads, so the cache goes
there.
Accepted and ignored, so code that gives a cloud engine a cache
still runs. A cloud engine accepts no evidence, so there is no key
to use in a cache, as the Cloud Aspect Engine specification says.
The Cloud Request Engine's cache is keyed on the evidence it sends
and holds the response this engine reads, so the cache goes there.

The engine keeps no cache, and logs a warning that says so once,
however often it is given one. The warning goes to the logger of
each pipeline the engine is in, or to this module's logger while it
is in none.

@type cache: DataKeyedCache
@param cache: the cache, which is never read or written
"""

raise Exception(CACHE_ON_REQUEST_ENGINE)
if getattr(self, "_cache_ignored_logged", False):
return
self._cache_ignored_logged = True
message = CACHE_IGNORED.format(type(self).__name__, self.datakey)
if getattr(self, "pipelines", None):
self._log("warning", message)
else:
_LOGGER.warning(message)

def process(self, flowdata):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ def _mark_request_made(error):
pass


def is_answer_key(key):

"""!
Whether an evidence key is a page script's answer, being a query or
cookie value whose name starts with 51D_ in any letter case, the bare
name 51D_ included.
"""

prefix, separator, name = str(key).partition(
Constants.EVIDENCE_SEPERATOR)
start = name[:len(Constants.ANSWER_NAME_START)]
return (separator != ""
and prefix.lower() in Constants.ANSWER_PREFIXES
and start.isascii()
and start.lower() == Constants.ANSWER_NAME_START)


class CloudEvidenceKeyFilter(BasicListEvidenceKeyFilter):

"""!
The evidence the Cloud Request Engine accepts, being the keys the cloud
service lists and every page script's answer, as is_answer_key says.
Accepting the answers puts them in the cache key, so two requests that
differ only in an answer never share a cached response. The answers have
no names to list, so list holds the listed keys alone.
"""

def filter_evidence_key(self, key):

return super().filter_evidence_key(key) or is_answer_key(key)


# Engine that makes a call to the 51Degrees cloud service. It gives the raw
# JSON as its "cloud" property, read under the "cloud-response" key, or
# under the deprecated key "cloud".
Expand Down Expand Up @@ -557,24 +589,25 @@ def get_evidence_keys(self):

def get_evidence_key_filter(self):
"""!
Instance of EvidenceKeyFilter based on evidence_keys. If the
constructor could not fetch them, this asks the cloud service again
and raises if it fails again, so that processing reports the
failure rather than using a filter that accepts nothing.
Instance of EvidenceKeyFilter based on evidence_keys, which also
accepts every page script's answer, as CloudEvidenceKeyFilter says.
If the constructor could not fetch the keys, this asks the cloud
service again and raises if it fails again, so that processing
reports the failure rather than using a filter that accepts nothing.

The filter is kept, as it is asked for once for each evidence key
of every request. It is made again only when evidence_keys holds
another list.

@type: BasicListEvidenceKeyFilter
@return: Returns BasicListEvidenceKeyFilter
@type: CloudEvidenceKeyFilter
@return: Returns CloudEvidenceKeyFilter

"""

keys = self.evidence_keys
kept = self._evidence_key_filter
if kept is None or kept[0] is not keys:
kept = (keys, BasicListEvidenceKeyFilter(keys))
kept = (keys, CloudEvidenceKeyFilter(keys))
self._evidence_key_filter = kept
return kept[1]

Expand Down Expand Up @@ -968,12 +1001,12 @@ def bypasses_cache(evidence):
holds, and its answer is not stored. A page iteration carries the
session id and the sequence, and a server may supply the visitor's
JA4 and HTTP/2 settings, so a request carrying any of them is made
for one visitor. The cache key is made from the evidence keys the
service lists, but the engine sends every evidence value, and the
service lists neither the session id nor the sequence. Two
iterations of one page that differ only in those would share a key,
and the later one, which may carry the browser test answers, would
be answered from the cache and never reach the service. So a
for one visitor. The cache key is made from the evidence the engine
accepts, being the keys the service lists and the page scripts'
answers, but the engine sends every evidence value, and the service
lists neither the session id nor the sequence. Two iterations of one
page that differ only in those would share a key, and the later one
would be answered from the cache and never reach the service. So a
request carrying any of Constants.UNCACHED_EVIDENCE_NAMES, under any
prefix and in any letter case, bypasses the cache, and every other
request is cached as before.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ class Constants:
# CloudRequestEngine.bypasses_cache.
UNCACHED_EVIDENCE_NAMES = ("session-id", "sequence", "ja4", "h2")

# The start, in lower case, of the name a page's scripts send their
# answers under, such as the browser tests' answers, and the prefixes
# the answers arrive under. The rest of the name changes with each
# script, so the service cannot list it among its evidence keys. See
# is_answer_key in cloudrequestengine.py.
ANSWER_NAME_START = "51d_"
ANSWER_PREFIXES = ("query", "cookie")

# The members of a response that hold a value made for one visitor,
# compared in any case. See CloudRequestEngine.is_cacheable. A 51Did is
# a top-level member. HumanConfidence and the browser tests' script are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ class DerivedCloud(CloudEngine):
request that carries a sealed result, and no value, with the reason, on
any other request.

Like every cloud engine, it refuses a cache and never uses one, so a
redeemed result is never stored or served again. The Cloud Request
Engine's cache neither stores nor serves a response to a request that
carries a sealed result.
Like every cloud engine, it ignores a cache it is given, with a
warning, and never uses one, so a redeemed result is never stored or
served again. The Cloud Request Engine's cache neither stores nor
serves a response to a request that carries a sealed result.
"""

DATA_KEY = "derived"
Expand Down
Loading
Loading