MCPcopy Create free account
hub / github.com/aliyun/data-agent-skill / _resolve_dms_unit

Method _resolve_dms_unit

scripts/data_agent/client.py:146–199  ·  view source on GitHub ↗

Resolve the DMSUnit for the current tenant. Resolution priority: 1. Explicit config/environment override (``DATA_AGENT_DMS_UNIT``). 2. dms-enterprise ``GetActiveRouteUnit`` (2018-11-01). 3. Region fallback. ``region_id`` lets callers resolve for a non-

(self, force_refresh: bool = False, region_id: Optional[str] = None)

Source from the content-addressed store, hash-verified

144 self._initialize_client()
145
146 def _resolve_dms_unit(self, force_refresh: bool = False, region_id: Optional[str] = None) -> str:
147 """Resolve the DMSUnit for the current tenant.
148
149 Resolution priority:
150 1. Explicit config/environment override (``DATA_AGENT_DMS_UNIT``).
151 2. dms-enterprise ``GetActiveRouteUnit`` (2018-11-01).
152 3. Region fallback.
153
154 ``region_id`` lets callers resolve for a non-default RegionId while
155 still honoring an explicit DMSUnit override.
156
157 Falls back to the selected region when:
158 - the response has no ``Route`` object (tenant belongs to the
159 default unit, which equals the current region);
160 - the call fails for any reason (network, auth, etc.).
161
162 Result is cached at both instance and process level. Set
163 ``force_refresh=True`` to bypass the cache.
164 """
165 if self._config.dms_unit:
166 self._dms_unit = self._config.dms_unit
167 self._dms_unit_region = region_id or self._config.region
168 self._dms_unit_source = "config"
169 return self._dms_unit
170
171 region = region_id or self._config.region
172 if not force_refresh and self._dms_unit and self._dms_unit_region == region:
173 return self._dms_unit
174
175 cache_key = (self._auth_type, region)
176 if not force_refresh and cache_key in DataAgentClient._dms_unit_cache:
177 self._dms_unit = DataAgentClient._dms_unit_cache[cache_key]
178 self._dms_unit_source = "cache"
179 return self._dms_unit
180
181 resolved = region # safe default
182 source = "fallback"
183 try:
184 resp = self._call_dms_enterprise_route_unit(region)
185 route = resp.get("Route") or resp.get("data") or resp.get("Data")
186 if isinstance(route, dict):
187 unit = route.get("RegionId") or route.get("regionId")
188 if isinstance(unit, str) and unit:
189 resolved = unit
190 source = "route"
191 except Exception:
192 # Silent fallback: DMSUnit resolution must never block API calls.
193 pass
194
195 self._dms_unit = resolved
196 self._dms_unit_region = region
197 self._dms_unit_source = source
198 DataAgentClient._dms_unit_cache[cache_key] = resolved
199 return resolved
200
201 def _call_dms_enterprise_route_unit(self, region_id: Optional[str] = None) -> dict:
202 """Call dms-enterprise.GetActiveRouteUnit via a short-lived OpenApiClient.

Callers 11

_resolve_workspace_idMethod · 0.95
create_sessionMethod · 0.95
describe_sessionMethod · 0.95
send_messageMethod · 0.95
get_chat_contentMethod · 0.95
add_data_center_tableMethod · 0.95
list_sessionsMethod · 0.95
list_workspacesMethod · 0.95
list_custom_agentsMethod · 0.95
describe_custom_agentMethod · 0.95

Calls 1

Tested by

no test coverage detected