Skip to content

Commit 98ff63c

Browse files
authored
fix: Using one single function call for utcnow(). (#4307)
Signed-off-by: Shuchu Han <shuchu.han@gmail.com>
1 parent 398ea3b commit 98ff63c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+176
-157
lines changed

sdk/python/feast/feature_store.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
from feast.repo_contents import RepoContents
8787
from feast.saved_dataset import SavedDataset, SavedDatasetStorage, ValidationReference
8888
from feast.stream_feature_view import StreamFeatureView
89+
from feast.utils import _utc_now
8990
from feast.version import get_version
9091

9192
warnings.simplefilter("once", DeprecationWarning)
@@ -1246,7 +1247,7 @@ def materialize_incremental(
12461247
>>> from feast import FeatureStore, RepoConfig
12471248
>>> from datetime import datetime, timedelta
12481249
>>> fs = FeatureStore(repo_path="project/feature_repo")
1249-
>>> fs.materialize_incremental(end_date=datetime.utcnow() - timedelta(minutes=5))
1250+
>>> fs.materialize_incremental(end_date=_utc_now() - timedelta(minutes=5))
12501251
Materializing...
12511252
<BLANKLINE>
12521253
...
@@ -1270,15 +1271,15 @@ def materialize_incremental(
12701271
f" either a ttl to be set or for materialize() to have been run at least once."
12711272
)
12721273
elif feature_view.ttl.total_seconds() > 0:
1273-
start_date = datetime.utcnow() - feature_view.ttl
1274+
start_date = _utc_now() - feature_view.ttl
12741275
else:
12751276
# TODO(felixwang9817): Find the earliest timestamp for this specific feature
12761277
# view from the offline store, and set the start date to that timestamp.
12771278
print(
12781279
f"Since the ttl is 0 for feature view {Style.BRIGHT + Fore.GREEN}{feature_view.name}{Style.RESET_ALL}, "
12791280
"the start date will be set to 1 year before the current time."
12801281
)
1281-
start_date = datetime.utcnow() - timedelta(weeks=52)
1282+
start_date = _utc_now() - timedelta(weeks=52)
12821283
provider = self._get_provider()
12831284
print(
12841285
f"{Style.BRIGHT + Fore.GREEN}{feature_view.name}{Style.RESET_ALL}"
@@ -1335,7 +1336,7 @@ def materialize(
13351336
>>> from datetime import datetime, timedelta
13361337
>>> fs = FeatureStore(repo_path="project/feature_repo")
13371338
>>> fs.materialize(
1338-
... start_date=datetime.utcnow() - timedelta(hours=3), end_date=datetime.utcnow() - timedelta(minutes=10)
1339+
... start_date=_utc_now() - timedelta(hours=3), end_date=_utc_now() - timedelta(minutes=10)
13391340
... )
13401341
Materializing...
13411342
<BLANKLINE>

sdk/python/feast/infra/offline_stores/bigquery.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from feast.on_demand_feature_view import OnDemandFeatureView
4646
from feast.repo_config import FeastConfigBaseModel, RepoConfig
4747
from feast.saved_dataset import SavedDatasetStorage
48-
from feast.utils import get_user_agent
48+
from feast.utils import _utc_now, get_user_agent
4949

5050
from .bigquery_source import (
5151
BigQueryLoggingDestination,
@@ -701,7 +701,7 @@ def _upload_entity_df(
701701

702702
# Ensure that the table expires after some time
703703
table = client.get_table(table=table_name)
704-
table.expires = datetime.utcnow() + timedelta(minutes=30)
704+
table.expires = _utc_now() + timedelta(minutes=30)
705705
client.update_table(table, ["expires"])
706706

707707
return table

sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino_queries.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import datetime
43
import signal
54
from dataclasses import dataclass
65
from enum import Enum
@@ -16,6 +15,7 @@
1615
from feast.infra.offline_stores.contrib.trino_offline_store.trino_type_map import (
1716
trino_to_pa_value_type,
1817
)
18+
from feast.utils import _utc_now
1919

2020

2121
class QueryStatus(Enum):
@@ -97,12 +97,12 @@ def __init__(self, query_text: str, cursor: Cursor):
9797
def execute(self) -> Results:
9898
try:
9999
self.status = QueryStatus.RUNNING
100-
start_time = datetime.datetime.utcnow()
100+
start_time = _utc_now()
101101

102102
self._cursor.execute(operation=self.query_text)
103103
rows = self._cursor.fetchall()
104104

105-
end_time = datetime.datetime.utcnow()
105+
end_time = _utc_now()
106106
self.execution_time = end_time - start_time
107107
self.status = QueryStatus.COMPLETED
108108

sdk/python/feast/infra/online_stores/datastore.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
4545
from feast.protos.feast.types.Value_pb2 import Value as ValueProto
4646
from feast.repo_config import FeastConfigBaseModel, RepoConfig
47-
from feast.utils import get_user_agent
47+
from feast.utils import _utc_now, get_user_agent
4848

4949
LOGGER = logging.getLogger(__name__)
5050

@@ -122,7 +122,7 @@ def update(
122122
entity = datastore.Entity(
123123
key=key, exclude_from_indexes=("created_ts", "event_ts", "values")
124124
)
125-
entity.update({"created_ts": datetime.utcnow()})
125+
entity.update({"created_ts": _utc_now()})
126126
client.put(entity)
127127

128128
for table in tables_to_delete:
@@ -457,7 +457,7 @@ def update(self):
457457
entity = datastore.Entity(
458458
key=key, exclude_from_indexes=("created_ts", "event_ts", "values")
459459
)
460-
entity.update({"created_ts": datetime.utcnow()})
460+
entity.update({"created_ts": _utc_now()})
461461
client.put(entity)
462462

463463
def teardown(self):

sdk/python/feast/infra/registry/caching_registry.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from abc import abstractmethod
3-
from datetime import datetime, timedelta
3+
from datetime import timedelta
44
from threading import Lock
55
from typing import List, Optional
66

@@ -15,6 +15,7 @@
1515
from feast.project_metadata import ProjectMetadata
1616
from feast.saved_dataset import SavedDataset, ValidationReference
1717
from feast.stream_feature_view import StreamFeatureView
18+
from feast.utils import _utc_now
1819

1920
logger = logging.getLogger(__name__)
2021

@@ -27,7 +28,7 @@ def __init__(
2728
):
2829
self.cached_registry_proto = self.proto()
2930
proto_registry_utils.init_project_metadata(self.cached_registry_proto, project)
30-
self.cached_registry_proto_created = datetime.utcnow()
31+
self.cached_registry_proto_created = _utc_now()
3132
self._refresh_lock = Lock()
3233
self.cached_registry_proto_ttl = timedelta(
3334
seconds=cache_ttl_seconds if cache_ttl_seconds is not None else 0
@@ -318,7 +319,7 @@ def refresh(self, project: Optional[str] = None):
318319
self.cached_registry_proto, project
319320
)
320321
self.cached_registry_proto = self.proto()
321-
self.cached_registry_proto_created = datetime.utcnow()
322+
self.cached_registry_proto_created = _utc_now()
322323

323324
def _refresh_cached_registry_if_necessary(self):
324325
with self._refresh_lock:
@@ -329,7 +330,7 @@ def _refresh_cached_registry_if_necessary(self):
329330
self.cached_registry_proto_ttl.total_seconds()
330331
> 0 # 0 ttl means infinity
331332
and (
332-
datetime.utcnow()
333+
_utc_now()
333334
> (
334335
self.cached_registry_proto_created
335336
+ self.cached_registry_proto_ttl

sdk/python/feast/infra/registry/contrib/azure/azure_registry_store.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
import os
55
import uuid
6-
from datetime import datetime
76
from pathlib import Path
87
from tempfile import TemporaryFile
98
from urllib.parse import urlparse
109

1110
from feast.infra.registry.registry import RegistryConfig
1211
from feast.infra.registry.registry_store import RegistryStore
1312
from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto
13+
from feast.utils import _utc_now
1414

1515
REGISTRY_SCHEMA_VERSION = "1"
1616

@@ -89,7 +89,7 @@ def teardown(self):
8989

9090
def _write_registry(self, registry_proto: RegistryProto):
9191
registry_proto.version_id = str(uuid.uuid4())
92-
registry_proto.last_updated.FromDatetime(datetime.utcnow())
92+
registry_proto.last_updated.FromDatetime(_utc_now())
9393

9494
file_obj = TemporaryFile()
9595
file_obj.write(registry_proto.SerializeToString())

sdk/python/feast/infra/registry/file.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import uuid
2-
from datetime import datetime
32
from pathlib import Path
43

54
from feast.infra.registry.registry_store import RegistryStore
65
from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto
76
from feast.repo_config import RegistryConfig
7+
from feast.utils import _utc_now
88

99

1010
class FileRegistryStore(RegistryStore):
@@ -37,7 +37,7 @@ def teardown(self):
3737

3838
def _write_registry(self, registry_proto: RegistryProto):
3939
registry_proto.version_id = str(uuid.uuid4())
40-
registry_proto.last_updated.FromDatetime(datetime.utcnow())
40+
registry_proto.last_updated.FromDatetime(_utc_now())
4141
file_dir = self._filepath.parent
4242
file_dir.mkdir(exist_ok=True)
4343
with open(self._filepath, mode="wb", buffering=0) as f:

sdk/python/feast/infra/registry/gcs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import uuid
2-
from datetime import datetime
32
from pathlib import Path
43
from tempfile import TemporaryFile
54
from urllib.parse import urlparse
65

76
from feast.infra.registry.registry_store import RegistryStore
87
from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto
98
from feast.repo_config import RegistryConfig
9+
from feast.utils import _utc_now
1010

1111

1212
class GCSRegistryStore(RegistryStore):
@@ -62,7 +62,7 @@ def teardown(self):
6262

6363
def _write_registry(self, registry_proto: RegistryProto):
6464
registry_proto.version_id = str(uuid.uuid4())
65-
registry_proto.last_updated.FromDatetime(datetime.utcnow())
65+
registry_proto.last_updated.FromDatetime(_utc_now())
6666
# we have already checked the bucket exists so no need to do it again
6767
gs_bucket = self.gcs_client.get_bucket(self._bucket)
6868
blob = gs_bucket.blob(self._blob)

sdk/python/feast/infra/registry/registry.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from feast.repo_contents import RepoContents
4848
from feast.saved_dataset import SavedDataset, ValidationReference
4949
from feast.stream_feature_view import StreamFeatureView
50+
from feast.utils import _utc_now
5051

5152
REGISTRY_SCHEMA_VERSION = "1"
5253

@@ -217,7 +218,7 @@ def clone(self) -> "Registry":
217218
if self.cached_registry_proto
218219
else RegistryProto()
219220
)
220-
new_registry.cached_registry_proto_created = datetime.utcnow()
221+
new_registry.cached_registry_proto_created = _utc_now()
221222
new_registry._registry_store = NoopRegistryStore()
222223
return new_registry
223224

@@ -248,7 +249,7 @@ def get_infra(self, project: str, allow_cache: bool = False) -> Infra:
248249
def apply_entity(self, entity: Entity, project: str, commit: bool = True):
249250
entity.is_valid()
250251

251-
now = datetime.utcnow()
252+
now = _utc_now()
252253
if not entity.created_timestamp:
253254
entity.created_timestamp = now
254255
entity.last_updated_timestamp = now
@@ -334,7 +335,7 @@ def delete_data_source(self, name: str, project: str, commit: bool = True):
334335
def apply_feature_service(
335336
self, feature_service: FeatureService, project: str, commit: bool = True
336337
):
337-
now = datetime.utcnow()
338+
now = _utc_now()
338339
if not feature_service.created_timestamp:
339340
feature_service.created_timestamp = now
340341
feature_service.last_updated_timestamp = now
@@ -390,7 +391,7 @@ def apply_feature_view(
390391
):
391392
feature_view.ensure_valid()
392393

393-
now = datetime.utcnow()
394+
now = _utc_now()
394395
if not feature_view.created_timestamp:
395396
feature_view.created_timestamp = now
396397
feature_view.last_updated_timestamp = now
@@ -517,7 +518,7 @@ def apply_materialization(
517518
existing_feature_view.materialization_intervals.append(
518519
(start_date, end_date)
519520
)
520-
existing_feature_view.last_updated_timestamp = datetime.utcnow()
521+
existing_feature_view.last_updated_timestamp = _utc_now()
521522
feature_view_proto = existing_feature_view.to_proto()
522523
feature_view_proto.spec.project = project
523524
del self.cached_registry_proto.feature_views[idx]
@@ -539,7 +540,7 @@ def apply_materialization(
539540
existing_stream_feature_view.materialization_intervals.append(
540541
(start_date, end_date)
541542
)
542-
existing_stream_feature_view.last_updated_timestamp = datetime.utcnow()
543+
existing_stream_feature_view.last_updated_timestamp = _utc_now()
543544
stream_feature_view_proto = existing_stream_feature_view.to_proto()
544545
stream_feature_view_proto.spec.project = project
545546
del self.cached_registry_proto.stream_feature_views[idx]
@@ -664,7 +665,7 @@ def apply_saved_dataset(
664665
project: str,
665666
commit: bool = True,
666667
):
667-
now = datetime.utcnow()
668+
now = _utc_now()
668669
if not saved_dataset.created_timestamp:
669670
saved_dataset.created_timestamp = now
670671
saved_dataset.last_updated_timestamp = now
@@ -812,7 +813,7 @@ def _prepare_registry_for_changes(self, project: str):
812813
registry_proto = RegistryProto()
813814
registry_proto.registry_schema_version = REGISTRY_SCHEMA_VERSION
814815
self.cached_registry_proto = registry_proto
815-
self.cached_registry_proto_created = datetime.utcnow()
816+
self.cached_registry_proto_created = _utc_now()
816817

817818
# Initialize project metadata if needed
818819
assert self.cached_registry_proto
@@ -848,7 +849,7 @@ def _get_registry_proto(
848849
self.cached_registry_proto_ttl.total_seconds()
849850
> 0 # 0 ttl means infinity
850851
and (
851-
datetime.utcnow()
852+
_utc_now()
852853
> (
853854
self.cached_registry_proto_created
854855
+ self.cached_registry_proto_ttl
@@ -871,7 +872,7 @@ def _get_registry_proto(
871872
logger.info("Registry cache expired, so refreshing")
872873
registry_proto = self._registry_store.get_registry_proto()
873874
self.cached_registry_proto = registry_proto
874-
self.cached_registry_proto_created = datetime.utcnow()
875+
self.cached_registry_proto_created = _utc_now()
875876

876877
if not project:
877878
return registry_proto

sdk/python/feast/infra/registry/s3.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import uuid
3-
from datetime import datetime
43
from pathlib import Path
54
from tempfile import TemporaryFile
65
from urllib.parse import urlparse
@@ -9,6 +8,7 @@
98
from feast.infra.registry.registry_store import RegistryStore
109
from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto
1110
from feast.repo_config import RegistryConfig
11+
from feast.utils import _utc_now
1212

1313
try:
1414
import boto3
@@ -70,7 +70,7 @@ def teardown(self):
7070

7171
def _write_registry(self, registry_proto: RegistryProto):
7272
registry_proto.version_id = str(uuid.uuid4())
73-
registry_proto.last_updated.FromDatetime(datetime.utcnow())
73+
registry_proto.last_updated.FromDatetime(_utc_now())
7474
# we have already checked the bucket exists so no need to do it again
7575
file_obj = TemporaryFile()
7676
file_obj.write(registry_proto.SerializeToString())

0 commit comments

Comments
 (0)