88from django .conf import settings
99from django .db import IntegrityError , router , transaction
1010from django .db .models import Model
11+ from redis .client import StrictRedis
1112from rest_framework import status
1213from rest_framework .exceptions import APIException
13- from sentry_redis_tools .clients import RedisCluster , StrictRedis
1414
1515from sentry .db .postgres .transactions import enforce_constraints
1616from sentry .types .region import RegionContextError , get_local_region
17- from sentry .utils import redis
1817
1918if TYPE_CHECKING :
2019 from sentry .db .models .base import Model as BaseModel
@@ -140,16 +139,14 @@ def generate_snowflake_id(redis_key: str) -> int:
140139 return snowflake_id
141140
142141
143- def get_redis_cluster () -> RedisCluster [ str ] | StrictRedis [str ]:
144- return redis . redis_clusters . get ( settings . SENTRY_SNOWFLAKE_REDIS_CLUSTER )
142+ def get_redis_cluster (redis_key : str ) -> StrictRedis [str ]:
143+ from sentry . utils import redis
145144
146-
147- def get_timestamp_redis_key (redis_key : str , timestamp : int ) -> str :
148- return f"snowflakeid:{ redis_key } :{ str (timestamp )} "
145+ return redis .clusters .get ("default" ).get_local_client_for_key (redis_key )
149146
150147
151148def get_sequence_value_from_redis (redis_key : str , starting_timestamp : int ) -> tuple [int , int ]:
152- cluster = get_redis_cluster ()
149+ cluster = get_redis_cluster (redis_key )
153150
154151 # this is the amount we want to lookback for previous timestamps
155152 # the below is more of a safety net if starting_timestamp is ever
@@ -159,16 +156,14 @@ def get_sequence_value_from_redis(redis_key: str, starting_timestamp: int) -> tu
159156 for i in range (time_range ):
160157 timestamp = starting_timestamp - i
161158
162- timestamp_redis_key = get_timestamp_redis_key (redis_key , timestamp )
163-
164159 # We are decreasing the value by 1 each time since the incr operation in redis
165160 # initializes the counter at 1. For our region sequences, we want the value to
166161 # be from 0-15 and not 1-16
167- sequence_value = cluster .incr (timestamp_redis_key )
162+ sequence_value = cluster .incr (str ( timestamp ) )
168163 sequence_value -= 1
169164
170165 if sequence_value == 0 :
171- cluster .expire (timestamp_redis_key , int (_TTL .total_seconds ()))
166+ cluster .expire (str ( timestamp ) , int (_TTL .total_seconds ()))
172167
173168 if sequence_value < MAX_AVAILABLE_REGION_SEQUENCES :
174169 return timestamp , sequence_value
0 commit comments