| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4d64ebc commit a0da993
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,150 @@ | |||
| 1 | + # Copyright 2026 Google LLC | ||
| 2 | + # | ||
| 3 | + # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + # you may not use this file except in compliance with the License. | ||
| 5 | + # You may obtain a copy of the License at | ||
| 6 | + # | ||
| 7 | + # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + # | ||
| 9 | + # Unless required by applicable law or agreed to in writing, software | ||
| 10 | + # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + # See the License for the specific language governing permissions and | ||
| 13 | + # limitations under the License. | ||
| 14 | + | ||
| 15 | + """In-memory LRU cache for bucket metadata supporting App-centric Observability (ACO).""" | ||
| 16 | + | ||
| 17 | + import logging | ||
| 18 | + import threading | ||
| 19 | + | ||
| 20 | + from google.api_core import exceptions as api_exceptions | ||
| 21 | + from google.cloud.exceptions import NotFound | ||
| 22 | + from google.cloud.storage._lru_cache import LRUCache | ||
| 23 | + | ||
| 24 | + logger = logging.getLogger(__name__) | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + class BucketMetadataCache: | ||
| 28 | + """Thread-safe LRU cache for storing GCS bucket metadata (project number and location). | ||
| 29 | + | ||
| 30 | + Supports Singleflight asynchronous background fetching to prevent stampedes on cache misses. | ||
| 31 | + """ | ||
| 32 | + | ||
| 33 | + def __init__(self, client, max_size=10000): | ||
| 34 | + self._client = client | ||
| 35 | + self._cache = LRUCache(max_size) | ||
| 36 | + self._lock = threading.Lock() | ||
| 37 | + self._inflight_fetches = set() | ||
| 38 | + self._inflight_checks = set() | ||
| 39 | + | ||
| 40 | + def get(self, bucket_name): | ||
| 41 | + """Thread-safely retrieve cached metadata without queueing fetch.""" | ||
| 42 | + with self._lock: | ||
| 43 | + return self._cache.get(bucket_name) | ||
| 44 | + | ||
| 45 | + def get_or_queue_fetch(self, bucket_name): | ||
| 46 | + """Retrieve bucket metadata or queue a background fetch on cache miss. | ||
| 47 | + | ||
| 48 | + Returns None immediately on cache miss so caller does not block. | ||
| 49 | + """ | ||
| 50 | + with self._lock: | ||
| 51 | + if bucket_name in self._cache: | ||
| 52 | + return self._cache.get(bucket_name) | ||
| 53 | + elif bucket_name in self._inflight_fetches: | ||
| 54 | + # This handles a thundering herd where 'n' threads | ||
| 55 | + # simultaneously experience a cache miss while 1 is already | ||
| 56 | + # fetching metadata. The remaining n - 1 threads should | ||
| 57 | + # bypass starting duplicate fetches. | ||
| 58 | + return None | ||
| 59 | + else: | ||
| 60 | + # fire a background thread and get bucket metadata. | ||
| 61 | + self._inflight_fetches.add(bucket_name) | ||
| 62 | + threading.Thread( | ||
| 63 | + target=self._fetch_background, args=(bucket_name,), daemon=True | ||
| 64 | + ).start() | ||
| 65 | + return None | ||
| 66 | + | ||
| 67 | + def check_and_evict(self, bucket_name): | ||
| 68 | + """Asynchronously verify if a bucket exists on 404 and evict if deleted.""" | ||
| 69 | + with self._lock: | ||
| 70 | + if bucket_name not in self._cache: | ||
| 71 | + return | ||
| 72 | + if bucket_name in self._inflight_checks: | ||
| 73 | + return | ||
| 74 | + self._inflight_checks.add(bucket_name) | ||
| 75 | + threading.Thread( | ||
| 76 | + target=self._verify_existence_background, | ||
| 77 | + args=(bucket_name,), | ||
| 78 | + daemon=True, | ||
| 79 | + ).start() | ||
| 80 | + | ||
| 81 | + def _verify_existence_background(self, bucket_name): | ||
| 82 | + try: | ||
| 83 | + bucket = self._client.bucket(bucket_name) | ||
| 84 | + if not bucket.exists(): | ||
| 85 | + self.evict(bucket_name) | ||
| 86 | + except Exception as e: | ||
| 87 | + logger.debug( | ||
| 88 | + f"Background verification for bucket existence failed for {bucket_name}: {e}" | ||
| 89 | + ) | ||
| 90 | + finally: | ||
| 91 | + with self._lock: | ||
| 92 | + self._inflight_checks.discard(bucket_name) | ||
| 93 | + | ||
| 94 | + def _fetch_background(self, bucket_name): | ||
| 95 | + """Asynchronously fetch bucket metadata and update the cache.""" | ||
| 96 | + try: | ||
| 97 | + bucket = self._client.get_bucket(bucket_name, timeout=10.0) | ||
| 98 | + self.update_from_bucket(bucket) | ||
| 99 | + except (NotFound, api_exceptions.NotFound): | ||
| 100 | + self.evict(bucket_name) | ||
| 101 | + except api_exceptions.Forbidden: | ||
| 102 | + # On 403 (Forbidden), cache fallback values permanently to avoid retry storms | ||
| 103 | + self.update_cache( | ||
| 104 | + bucket_name, f"projects/_/buckets/{bucket_name}", "global" | ||
| 105 | + ) | ||
| 106 | + except Exception as e: | ||
| 107 | + logger.debug( | ||
| 108 | + f"Background fetch for bucket metadata failed for {bucket_name}: {e}" | ||
| 109 | + ) | ||
| 110 | + finally: | ||
| 111 | + with self._lock: | ||
| 112 | + self._inflight_fetches.discard(bucket_name) | ||
| 113 | + | ||
| 114 | + def update_from_bucket(self, bucket): | ||
| 115 | + """Update cache from a Bucket instance.""" | ||
| 116 | + if not bucket or not bucket.name: | ||
| 117 | + return | ||
| 118 | + | ||
| 119 | + project_number = getattr(bucket, "project_number", None) | ||
| 120 | + location = getattr(bucket, "location", None) or "global" | ||
| 121 | + location = location.lower() | ||
| 122 | + location_type = getattr(bucket, "location_type", None) or "region" | ||
| 123 | + location_type = location_type.lower() | ||
| 124 | + | ||
| 125 | + if location_type in ("multi-region", "dual-region"): | ||
| 126 | + location = "global" | ||
| 127 | + | ||
| 128 | + if project_number: | ||
| 129 | + destination_id = f"projects/{project_number}/buckets/{bucket.name}" | ||
| 130 | + else: | ||
| 131 | + destination_id = f"projects/_/buckets/{bucket.name}" | ||
| 132 | + | ||
| 133 | + self.update_cache(bucket.name, destination_id, location) | ||
| 134 | + | ||
| 135 | + def update_cache(self, bucket_name, destination_id, location): | ||
| 136 | + """Thread-safely update or insert a cache entry with bounded size.""" | ||
| 137 | + with self._lock: | ||
| 138 | + self._cache.put(bucket_name, (destination_id, location)) | ||
| 139 | + | ||
| 140 | + def evict(self, bucket_name): | ||
| 141 | + """Remove a bucket from the cache (e.g., on 404).""" | ||
| 142 | + with self._lock: | ||
| 143 | + self._cache.delete(bucket_name) | ||
| 144 | + | ||
| 145 | + def clear(self): | ||
| 146 | + """Clear all cached metadata.""" | ||
| 147 | + with self._lock: | ||
| 148 | + self._cache.clear() | ||
| 149 | + self._inflight_fetches.clear() | ||
| 150 | + self._inflight_checks.clear() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,20 +19,30 @@ | |||
| 19 | 19 | ||
| 20 | 20 | import base64 | |
| 21 | 21 | import datetime | |
| 22 | + import logging | ||
| 22 | 23 | import os | |
| 23 | 24 | import secrets | |
| 24 | 25 | import sys | |
| 26 | + from contextlib import contextmanager | ||
| 25 | 27 | from hashlib import md5 | |
| 26 | 28 | from urllib.parse import urlsplit, urlunsplit | |
| 27 | 29 | from uuid import uuid4 | |
| 28 | 30 | ||
| 31 | + from google.api_core import exceptions as api_exceptions | ||
| 32 | + from google.cloud.exceptions import NotFound | ||
| 33 | + | ||
| 29 | 34 | from google.auth import environment_vars | |
| 30 | 35 | ||
| 31 | 36 | from google.cloud.storage.constants import _DEFAULT_TIMEOUT | |
| 32 | 37 | from google.cloud.storage.retry import ( | |
| 33 | 38 | DEFAULT_RETRY, | |
| 34 | 39 | DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED, | |
| 35 | 40 | ) | |
| 41 | + from google.cloud.storage._opentelemetry_tracing import ( | ||
| 42 | + create_trace_span as _base_create_trace_span, | ||
| 43 | + ) | ||
| 44 | + | ||
| 45 | + _logger = logging.getLogger(__name__) | ||
| 36 | 46 | ||
| 37 | 47 | STORAGE_EMULATOR_ENV_VAR = "STORAGE_EMULATOR_HOST" # Despite name, includes scheme. | |
| 38 | 48 | """Environment variable defining host for Storage emulator.""" | |
@@ -137,6 +147,62 @@ def _validate_name(name): | |||
| 137 | 147 | return name | |
| 138 | 148 | ||
| 139 | 149 | ||
| 150 | + @contextmanager | ||
| 151 | + def create_trace_span_helper(client, bucket_name, name, attributes=None, **kwargs): | ||
| 152 | + span_attrs = dict(attributes) if attributes else {} | ||
| 153 | + | ||
| 154 | + if ( | ||
| 155 | + bucket_name | ||
| 156 | + and isinstance(bucket_name, str) | ||
| 157 | + and client | ||
| 158 | + and hasattr(client, "_bucket_metadata_cache") | ||
| 159 | + and client._bucket_metadata_cache | ||
| 160 | + ): | ||
| 161 | + try: | ||
| 162 | + if name in ( | ||
| 163 | + "Storage.Client.getBucket", | ||
| 164 | + "Storage.Client.lookupBucket", | ||
| 165 | + "Storage.Bucket.reload", | ||
| 166 | + "Storage.Bucket.exists", | ||
| 167 | + ): | ||
| 168 | + cached = client._bucket_metadata_cache.get(bucket_name) | ||
| 169 | + else: | ||
| 170 | + cached = client._bucket_metadata_cache.get_or_queue_fetch(bucket_name) | ||
| 171 | + | ||
| 172 | + if cached and isinstance(cached, tuple) and len(cached) == 2: | ||
| 173 | + dest_id, loc = cached | ||
| 174 | + span_attrs.update( | ||
| 175 | + { | ||
| 176 | + "gcp.resource.destination.id": dest_id, | ||
| 177 | + "gcp.resource.destination.location": loc, | ||
| 178 | + } | ||
| 179 | + ) | ||
| 180 | + except Exception as e: | ||
| 181 | + _logger.debug(f"Failed cache lookup in create_trace_span_helper: {e}") | ||
| 182 | + | ||
| 183 | + if "client" not in kwargs and client: | ||
| 184 | + kwargs["client"] = client | ||
| 185 | + | ||
| 186 | + with _base_create_trace_span(name, attributes=span_attrs, **kwargs) as span: | ||
| 187 | + try: | ||
| 188 | + yield span | ||
| 189 | + except (NotFound, api_exceptions.NotFound): | ||
| 190 | + if ( | ||
| 191 | + bucket_name | ||
| 192 | + and isinstance(bucket_name, str) | ||
| 193 | + and client | ||
| 194 | + and hasattr(client, "_bucket_metadata_cache") | ||
| 195 | + and client._bucket_metadata_cache | ||
| 196 | + ): | ||
| 197 | + try: | ||
| 198 | + client._bucket_metadata_cache.check_and_evict(bucket_name) | ||
| 199 | + except Exception as e: | ||
| 200 | + _logger.debug( | ||
| 201 | + f"Failed cache eviction on 404 in create_trace_span_helper: {e}" | ||
| 202 | + ) | ||
| 203 | + raise | ||
| 204 | + | ||
| 205 | + | ||
| 140 | 206 | class _PropertyMixin(object): | |
| 141 | 207 | """Abstract mixin for cloud storage classes with associated properties. | |
| 142 | 208 | ||
@@ -185,6 +251,42 @@ def _require_client(self, client): | |||
| 185 | 251 | client = self.client | |
| 186 | 252 | return client | |
| 187 | 253 | ||
| 254 | + @contextmanager | ||
| 255 | + def _create_trace_span(self, name, attributes=None, **kwargs): | ||
| 256 | + from google.cloud.storage.blob import Blob | ||
| 257 | + from google.cloud.storage.bucket import Bucket | ||
| 258 | + | ||
| 259 | + if isinstance(self, Bucket): | ||
| 260 | + client = self.client | ||
| 261 | + bucket_name = self.name | ||
| 262 | + elif isinstance(self, Blob): | ||
| 263 | + bucket = getattr(self, "bucket", None) | ||
| 264 | + client = ( | ||
| 265 | + getattr(bucket, "client", None) | ||
| 266 | + if bucket and hasattr(bucket, "client") | ||
| 267 | + else None | ||
| 268 | + ) | ||
| 269 | + bucket_name = getattr(bucket, "name", None) if bucket else None | ||
| 270 | + else: | ||
| 271 | + client = None | ||
| 272 | + bucket_name = None | ||
| 273 | + | ||
| 274 | + if callable(bucket_name): | ||
| 275 | + try: | ||
| 276 | + bucket_name = bucket_name() | ||
| 277 | + except Exception as e: | ||
| 278 | + _logger.debug( | ||
| 279 | + f"Failed callable bucket_name resolution in _create_trace_span: {e}" | ||
| 280 | + ) | ||
| 281 | + | ||
| 282 | + client_override = kwargs.pop("client", None) | ||
| 283 | + active_client = client_override or client | ||
| 284 | + | ||
| 285 | + with create_trace_span_helper( | ||
| 286 | + active_client, bucket_name, name, attributes=attributes, **kwargs | ||
| 287 | + ) as span: | ||
| 288 | + yield span | ||
| 289 | + | ||
| 188 | 290 | def _encryption_headers(self): | |
| 189 | 291 | """Return any encryption headers needed to fetch the object. | |
| 190 | 292 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,10 +15,20 @@ | |||
| 15 | 15 | """Create / interact with Google Cloud Storage connections.""" | |
| 16 | 16 | ||
| 17 | 17 | import functools | |
| 18 | + import logging | ||
| 19 | + import re | ||
| 18 | 20 | ||
| 21 | + from google.api_core import exceptions as api_exceptions | ||
| 19 | 22 | from google.cloud import _http | |
| 23 | + from google.cloud.exceptions import NotFound | ||
| 20 | 24 | from google.cloud.storage import __version__, _helpers | |
| 21 | - from google.cloud.storage._opentelemetry_tracing import create_trace_span | ||
| 25 | + from google.cloud.storage._opentelemetry_tracing import ( | ||
| 26 | + create_trace_span, | ||
| 27 | + enable_otel_traces, | ||
| 28 | + HAS_OPENTELEMETRY, | ||
| 29 | + ) | ||
| 30 | + | ||
| 31 | + logger = logging.getLogger(__name__) | ||
| 22 | 32 | ||
| 23 | 33 | ||
| 24 | 34 | class Connection(_http.JSONConnection): | |
@@ -71,11 +81,30 @@ def api_request(self, *args, **kwargs): | |||
| 71 | 81 | span_attributes = { | |
| 72 | 82 | "gccl-invocation-id": invocation_id, | |
| 73 | 83 | } | |
| 84 | + client = self._client | ||
| 85 | + if ( | ||
| 86 | + HAS_OPENTELEMETRY | ||
| 87 | + and enable_otel_traces | ||
| 88 | + and hasattr(client, "_bucket_metadata_cache") | ||
| 89 | + and client._bucket_metadata_cache | ||
| 90 | + ): | ||
| 91 | + path = kwargs.get("path") or "" | ||
| 92 | + match = re.search(r"/b/([^/?#]+)", path) | ||
| 93 | + if match: | ||
| 94 | + try: | ||
| 95 | + cached = client._bucket_metadata_cache.get(match.group(1)) | ||
| 96 | + if cached and isinstance(cached, tuple) and len(cached) == 2: | ||
| 97 | + dest_id, loc = cached | ||
| 98 | + span_attributes["gcp.resource.destination.id"] = dest_id | ||
| 99 | + span_attributes["gcp.resource.destination.location"] = loc | ||
| 100 | + except Exception as e: | ||
| 101 | + logger.debug(f"Failed cache.get_or_queue_fetch in api_request: {e}") | ||
| 102 | + | ||
| 74 | 103 | call = functools.partial(super(Connection, self).api_request, *args, **kwargs) | |
| 75 | 104 | with create_trace_span( | |
| 76 | 105 | name="Storage.Connection.api_request", | |
| 77 | 106 | attributes=span_attributes, | |
| 78 | - client=self._client, | ||
| 107 | + client=client, | ||
| 79 | 108 | api_request=kwargs, | |
| 80 | 109 | retry=retry, | |
| 81 | 110 | ): | |
@@ -87,4 +116,24 @@ def api_request(self, *args, **kwargs): | |||
| 87 | 116 | pass | |
| 88 | 117 | if retry: | |
| 89 | 118 | call = retry(call) | |
| 90 | - return call() | ||
| 119 | + try: | ||
| 120 | + return call() | ||
| 121 | + except (NotFound, api_exceptions.NotFound): | ||
| 122 | + if ( | ||
| 123 | + HAS_OPENTELEMETRY | ||
| 124 | + and enable_otel_traces | ||
| 125 | + and hasattr(client, "_bucket_metadata_cache") | ||
| 126 | + and client._bucket_metadata_cache | ||
| 127 | + ): | ||
| 128 | + path = kwargs.get("path") or "" | ||
| 129 | + match = re.search(r"/b/([^/?#]+)", path) | ||
| 130 | + if match: | ||
| 131 | + try: | ||
| 132 | + client._bucket_metadata_cache.check_and_evict( | ||
| 133 | + match.group(1) | ||
| 134 | + ) | ||
| 135 | + except Exception as e: | ||
| 136 | + logger.debug( | ||
| 137 | + f"Failed cache.check_and_evict on 404 in api_request: {e}" | ||
| 138 | + ) | ||
| 139 | + raise | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments