@azure/cosmos 4.4.1 → 4.5.0-alpha.20250717.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (428) hide show
  1. package/dist/browser/ClientContext.d.ts +19 -10
  2. package/dist/browser/ClientContext.d.ts.map +1 -1
  3. package/dist/browser/ClientContext.js +30 -11
  4. package/dist/browser/ClientContext.js.map +1 -1
  5. package/dist/browser/CosmosClient.d.ts.map +1 -1
  6. package/dist/browser/CosmosClient.js +10 -2
  7. package/dist/browser/CosmosClient.js.map +1 -1
  8. package/dist/browser/PartitionKeyRangeFailoverInfo.d.ts +49 -0
  9. package/dist/browser/PartitionKeyRangeFailoverInfo.d.ts.map +1 -0
  10. package/dist/browser/PartitionKeyRangeFailoverInfo.js +130 -0
  11. package/dist/browser/PartitionKeyRangeFailoverInfo.js.map +1 -0
  12. package/dist/browser/client/ClientUtils.d.ts +3 -1
  13. package/dist/browser/client/ClientUtils.d.ts.map +1 -1
  14. package/dist/browser/client/ClientUtils.js +10 -0
  15. package/dist/browser/client/ClientUtils.js.map +1 -1
  16. package/dist/browser/client/Item/Item.d.ts.map +1 -1
  17. package/dist/browser/client/Item/Item.js +13 -0
  18. package/dist/browser/client/Item/Item.js.map +1 -1
  19. package/dist/browser/client/Item/Items.d.ts +1 -0
  20. package/dist/browser/client/Item/Items.d.ts.map +1 -1
  21. package/dist/browser/client/Item/Items.js +20 -3
  22. package/dist/browser/client/Item/Items.js.map +1 -1
  23. package/dist/browser/common/constants.d.ts +20 -0
  24. package/dist/browser/common/constants.d.ts.map +1 -1
  25. package/dist/browser/common/constants.js +23 -1
  26. package/dist/browser/common/constants.js.map +1 -1
  27. package/dist/browser/common/platform.d.ts +6 -1
  28. package/dist/browser/common/platform.d.ts.map +1 -1
  29. package/dist/browser/common/platform.js +23 -4
  30. package/dist/browser/common/platform.js.map +1 -1
  31. package/dist/browser/diagnostics/DiagnosticNodeInternal.d.ts +1 -0
  32. package/dist/browser/diagnostics/DiagnosticNodeInternal.d.ts.map +1 -1
  33. package/dist/browser/diagnostics/DiagnosticNodeInternal.js.map +1 -1
  34. package/dist/browser/documents/ConnectionPolicy.d.ts +6 -0
  35. package/dist/browser/documents/ConnectionPolicy.d.ts.map +1 -1
  36. package/dist/browser/documents/ConnectionPolicy.js +2 -0
  37. package/dist/browser/documents/ConnectionPolicy.js.map +1 -1
  38. package/dist/browser/documents/DatabaseAccount.d.ts.map +1 -1
  39. package/dist/browser/documents/DatabaseAccount.js +3 -0
  40. package/dist/browser/documents/DatabaseAccount.js.map +1 -1
  41. package/dist/browser/documents/PartitionKeyDefinition.d.ts +1 -1
  42. package/dist/browser/documents/PartitionKeyDefinition.js.map +1 -1
  43. package/dist/browser/extractPartitionKey.d.ts.map +1 -1
  44. package/dist/browser/extractPartitionKey.js +33 -20
  45. package/dist/browser/extractPartitionKey.js.map +1 -1
  46. package/dist/browser/globalEndpointManager.d.ts +6 -0
  47. package/dist/browser/globalEndpointManager.d.ts.map +1 -1
  48. package/dist/browser/globalEndpointManager.js +9 -4
  49. package/dist/browser/globalEndpointManager.js.map +1 -1
  50. package/dist/browser/globalPartitionEndpointManager.d.ts +79 -0
  51. package/dist/browser/globalPartitionEndpointManager.d.ts.map +1 -0
  52. package/dist/browser/globalPartitionEndpointManager.js +279 -0
  53. package/dist/browser/globalPartitionEndpointManager.js.map +1 -0
  54. package/dist/browser/queryExecutionContext/Aggregators/index.d.ts.map +1 -1
  55. package/dist/browser/queryExecutionContext/Aggregators/index.js +1 -0
  56. package/dist/browser/queryExecutionContext/Aggregators/index.js.map +1 -1
  57. package/dist/browser/queryExecutionContext/documentProducer.d.ts +2 -3
  58. package/dist/browser/queryExecutionContext/documentProducer.d.ts.map +1 -1
  59. package/dist/browser/queryExecutionContext/documentProducer.js +16 -21
  60. package/dist/browser/queryExecutionContext/documentProducer.js.map +1 -1
  61. package/dist/browser/queryExecutionContext/hybridQueryExecutionContext.d.ts +3 -1
  62. package/dist/browser/queryExecutionContext/hybridQueryExecutionContext.d.ts.map +1 -1
  63. package/dist/browser/queryExecutionContext/hybridQueryExecutionContext.js +10 -2
  64. package/dist/browser/queryExecutionContext/hybridQueryExecutionContext.js.map +1 -1
  65. package/dist/browser/queryExecutionContext/parallelQueryExecutionContextBase.d.ts.map +1 -1
  66. package/dist/browser/queryExecutionContext/parallelQueryExecutionContextBase.js +2 -1
  67. package/dist/browser/queryExecutionContext/parallelQueryExecutionContextBase.js.map +1 -1
  68. package/dist/browser/queryIterator.d.ts.map +1 -1
  69. package/dist/browser/queryIterator.js +2 -3
  70. package/dist/browser/queryIterator.js.map +1 -1
  71. package/dist/browser/request/ErrorResponse.d.ts +1 -1
  72. package/dist/browser/request/ErrorResponse.d.ts.map +1 -1
  73. package/dist/browser/request/ErrorResponse.js.map +1 -1
  74. package/dist/browser/request/RequestContext.d.ts +5 -0
  75. package/dist/browser/request/RequestContext.d.ts.map +1 -1
  76. package/dist/browser/request/RequestContext.js.map +1 -1
  77. package/dist/browser/request/RequestHandler.d.ts.map +1 -1
  78. package/dist/browser/request/RequestHandler.js +14 -4
  79. package/dist/browser/request/RequestHandler.js.map +1 -1
  80. package/dist/browser/retry/RetryPolicy.d.ts +2 -2
  81. package/dist/browser/retry/RetryPolicy.d.ts.map +1 -1
  82. package/dist/browser/retry/RetryPolicy.js.map +1 -1
  83. package/dist/browser/retry/defaultRetryPolicy.d.ts.map +1 -1
  84. package/dist/browser/retry/defaultRetryPolicy.js +4 -8
  85. package/dist/browser/retry/defaultRetryPolicy.js.map +1 -1
  86. package/dist/browser/retry/endpointDiscoveryRetryPolicy.d.ts +5 -3
  87. package/dist/browser/retry/endpointDiscoveryRetryPolicy.d.ts.map +1 -1
  88. package/dist/browser/retry/endpointDiscoveryRetryPolicy.js +9 -2
  89. package/dist/browser/retry/endpointDiscoveryRetryPolicy.js.map +1 -1
  90. package/dist/browser/retry/retryUtility.d.ts.map +1 -1
  91. package/dist/browser/retry/retryUtility.js +11 -5
  92. package/dist/browser/retry/retryUtility.js.map +1 -1
  93. package/dist/browser/retry/timeoutFailoverRetryPolicy.d.ts +6 -3
  94. package/dist/browser/retry/timeoutFailoverRetryPolicy.d.ts.map +1 -1
  95. package/dist/browser/retry/timeoutFailoverRetryPolicy.js +12 -4
  96. package/dist/browser/retry/timeoutFailoverRetryPolicy.js.map +1 -1
  97. package/dist/browser/routing/partitionKeyRangeCache.d.ts.map +1 -1
  98. package/dist/browser/routing/partitionKeyRangeCache.js +11 -0
  99. package/dist/browser/routing/partitionKeyRangeCache.js.map +1 -1
  100. package/dist/browser/utils/checkURL.js +6 -0
  101. package/dist/browser/utils/checkURL.js.map +1 -1
  102. package/dist/browser/utils/hashing/hash.d.ts +2 -0
  103. package/dist/browser/utils/hashing/hash.d.ts.map +1 -1
  104. package/dist/browser/utils/hashing/hash.js +19 -0
  105. package/dist/browser/utils/hashing/hash.js.map +1 -1
  106. package/dist/commonjs/ClientContext.d.ts +19 -10
  107. package/dist/commonjs/ClientContext.d.ts.map +1 -1
  108. package/dist/commonjs/ClientContext.js +30 -11
  109. package/dist/commonjs/ClientContext.js.map +1 -1
  110. package/dist/commonjs/CosmosClient.d.ts.map +1 -1
  111. package/dist/commonjs/CosmosClient.js +10 -2
  112. package/dist/commonjs/CosmosClient.js.map +1 -1
  113. package/dist/commonjs/PartitionKeyRangeFailoverInfo.d.ts +49 -0
  114. package/dist/commonjs/PartitionKeyRangeFailoverInfo.d.ts.map +1 -0
  115. package/dist/commonjs/PartitionKeyRangeFailoverInfo.js +135 -0
  116. package/dist/commonjs/PartitionKeyRangeFailoverInfo.js.map +1 -0
  117. package/dist/commonjs/client/ClientUtils.d.ts +3 -1
  118. package/dist/commonjs/client/ClientUtils.d.ts.map +1 -1
  119. package/dist/commonjs/client/ClientUtils.js +11 -0
  120. package/dist/commonjs/client/ClientUtils.js.map +1 -1
  121. package/dist/commonjs/client/Item/Item.d.ts.map +1 -1
  122. package/dist/commonjs/client/Item/Item.js +13 -0
  123. package/dist/commonjs/client/Item/Item.js.map +1 -1
  124. package/dist/commonjs/client/Item/Items.d.ts +1 -0
  125. package/dist/commonjs/client/Item/Items.d.ts.map +1 -1
  126. package/dist/commonjs/client/Item/Items.js +18 -1
  127. package/dist/commonjs/client/Item/Items.js.map +1 -1
  128. package/dist/commonjs/common/constants.d.ts +20 -0
  129. package/dist/commonjs/common/constants.d.ts.map +1 -1
  130. package/dist/commonjs/common/constants.js +24 -2
  131. package/dist/commonjs/common/constants.js.map +1 -1
  132. package/dist/commonjs/common/platform.d.ts +6 -1
  133. package/dist/commonjs/common/platform.d.ts.map +1 -1
  134. package/dist/commonjs/common/platform.js +23 -3
  135. package/dist/commonjs/common/platform.js.map +1 -1
  136. package/dist/commonjs/diagnostics/DiagnosticNodeInternal.d.ts +1 -0
  137. package/dist/commonjs/diagnostics/DiagnosticNodeInternal.d.ts.map +1 -1
  138. package/dist/commonjs/diagnostics/DiagnosticNodeInternal.js.map +1 -1
  139. package/dist/commonjs/documents/ConnectionPolicy.d.ts +6 -0
  140. package/dist/commonjs/documents/ConnectionPolicy.d.ts.map +1 -1
  141. package/dist/commonjs/documents/ConnectionPolicy.js +2 -0
  142. package/dist/commonjs/documents/ConnectionPolicy.js.map +1 -1
  143. package/dist/commonjs/documents/DatabaseAccount.d.ts.map +1 -1
  144. package/dist/commonjs/documents/DatabaseAccount.js +3 -0
  145. package/dist/commonjs/documents/DatabaseAccount.js.map +1 -1
  146. package/dist/commonjs/documents/PartitionKeyDefinition.d.ts +1 -1
  147. package/dist/commonjs/documents/PartitionKeyDefinition.js.map +1 -1
  148. package/dist/commonjs/extractPartitionKey.d.ts.map +1 -1
  149. package/dist/commonjs/extractPartitionKey.js +33 -20
  150. package/dist/commonjs/extractPartitionKey.js.map +1 -1
  151. package/dist/commonjs/globalEndpointManager.d.ts +6 -0
  152. package/dist/commonjs/globalEndpointManager.d.ts.map +1 -1
  153. package/dist/commonjs/globalEndpointManager.js +10 -5
  154. package/dist/commonjs/globalEndpointManager.js.map +1 -1
  155. package/dist/commonjs/globalPartitionEndpointManager.d.ts +79 -0
  156. package/dist/commonjs/globalPartitionEndpointManager.d.ts.map +1 -0
  157. package/dist/commonjs/globalPartitionEndpointManager.js +283 -0
  158. package/dist/commonjs/globalPartitionEndpointManager.js.map +1 -0
  159. package/dist/commonjs/queryExecutionContext/Aggregators/index.d.ts.map +1 -1
  160. package/dist/commonjs/queryExecutionContext/Aggregators/index.js +1 -0
  161. package/dist/commonjs/queryExecutionContext/Aggregators/index.js.map +1 -1
  162. package/dist/commonjs/queryExecutionContext/documentProducer.d.ts +2 -3
  163. package/dist/commonjs/queryExecutionContext/documentProducer.d.ts.map +1 -1
  164. package/dist/commonjs/queryExecutionContext/documentProducer.js +15 -20
  165. package/dist/commonjs/queryExecutionContext/documentProducer.js.map +1 -1
  166. package/dist/commonjs/queryExecutionContext/hybridQueryExecutionContext.d.ts +3 -1
  167. package/dist/commonjs/queryExecutionContext/hybridQueryExecutionContext.d.ts.map +1 -1
  168. package/dist/commonjs/queryExecutionContext/hybridQueryExecutionContext.js +10 -2
  169. package/dist/commonjs/queryExecutionContext/hybridQueryExecutionContext.js.map +1 -1
  170. package/dist/commonjs/queryExecutionContext/parallelQueryExecutionContextBase.d.ts.map +1 -1
  171. package/dist/commonjs/queryExecutionContext/parallelQueryExecutionContextBase.js +2 -1
  172. package/dist/commonjs/queryExecutionContext/parallelQueryExecutionContextBase.js.map +1 -1
  173. package/dist/commonjs/queryIterator.d.ts.map +1 -1
  174. package/dist/commonjs/queryIterator.js +2 -3
  175. package/dist/commonjs/queryIterator.js.map +1 -1
  176. package/dist/commonjs/request/ErrorResponse.d.ts +1 -1
  177. package/dist/commonjs/request/ErrorResponse.d.ts.map +1 -1
  178. package/dist/commonjs/request/ErrorResponse.js.map +1 -1
  179. package/dist/commonjs/request/RequestContext.d.ts +5 -0
  180. package/dist/commonjs/request/RequestContext.d.ts.map +1 -1
  181. package/dist/commonjs/request/RequestContext.js.map +1 -1
  182. package/dist/commonjs/request/RequestHandler.d.ts.map +1 -1
  183. package/dist/commonjs/request/RequestHandler.js +12 -2
  184. package/dist/commonjs/request/RequestHandler.js.map +1 -1
  185. package/dist/commonjs/request/defaultAgent.js +4 -4
  186. package/dist/commonjs/request/defaultAgent.js.map +1 -1
  187. package/dist/commonjs/retry/RetryPolicy.d.ts +2 -2
  188. package/dist/commonjs/retry/RetryPolicy.d.ts.map +1 -1
  189. package/dist/commonjs/retry/RetryPolicy.js.map +1 -1
  190. package/dist/commonjs/retry/defaultRetryPolicy.d.ts.map +1 -1
  191. package/dist/commonjs/retry/defaultRetryPolicy.js +3 -7
  192. package/dist/commonjs/retry/defaultRetryPolicy.js.map +1 -1
  193. package/dist/commonjs/retry/endpointDiscoveryRetryPolicy.d.ts +5 -3
  194. package/dist/commonjs/retry/endpointDiscoveryRetryPolicy.d.ts.map +1 -1
  195. package/dist/commonjs/retry/endpointDiscoveryRetryPolicy.js +9 -2
  196. package/dist/commonjs/retry/endpointDiscoveryRetryPolicy.js.map +1 -1
  197. package/dist/commonjs/retry/retryUtility.d.ts.map +1 -1
  198. package/dist/commonjs/retry/retryUtility.js +11 -5
  199. package/dist/commonjs/retry/retryUtility.js.map +1 -1
  200. package/dist/commonjs/retry/timeoutFailoverRetryPolicy.d.ts +6 -3
  201. package/dist/commonjs/retry/timeoutFailoverRetryPolicy.d.ts.map +1 -1
  202. package/dist/commonjs/retry/timeoutFailoverRetryPolicy.js +12 -4
  203. package/dist/commonjs/retry/timeoutFailoverRetryPolicy.js.map +1 -1
  204. package/dist/commonjs/routing/partitionKeyRangeCache.d.ts.map +1 -1
  205. package/dist/commonjs/routing/partitionKeyRangeCache.js +11 -0
  206. package/dist/commonjs/routing/partitionKeyRangeCache.js.map +1 -1
  207. package/dist/commonjs/tsdoc-metadata.json +1 -1
  208. package/dist/commonjs/utils/checkURL.js +7 -0
  209. package/dist/commonjs/utils/checkURL.js.map +1 -1
  210. package/dist/commonjs/utils/hashing/hash.d.ts +2 -0
  211. package/dist/commonjs/utils/hashing/hash.d.ts.map +1 -1
  212. package/dist/commonjs/utils/hashing/hash.js +20 -0
  213. package/dist/commonjs/utils/hashing/hash.js.map +1 -1
  214. package/dist/esm/ClientContext.d.ts +19 -10
  215. package/dist/esm/ClientContext.d.ts.map +1 -1
  216. package/dist/esm/ClientContext.js +30 -11
  217. package/dist/esm/ClientContext.js.map +1 -1
  218. package/dist/esm/CosmosClient.d.ts.map +1 -1
  219. package/dist/esm/CosmosClient.js +10 -2
  220. package/dist/esm/CosmosClient.js.map +1 -1
  221. package/dist/esm/PartitionKeyRangeFailoverInfo.d.ts +49 -0
  222. package/dist/esm/PartitionKeyRangeFailoverInfo.d.ts.map +1 -0
  223. package/dist/esm/PartitionKeyRangeFailoverInfo.js +130 -0
  224. package/dist/esm/PartitionKeyRangeFailoverInfo.js.map +1 -0
  225. package/dist/esm/client/ClientUtils.d.ts +3 -1
  226. package/dist/esm/client/ClientUtils.d.ts.map +1 -1
  227. package/dist/esm/client/ClientUtils.js +10 -0
  228. package/dist/esm/client/ClientUtils.js.map +1 -1
  229. package/dist/esm/client/Item/Item.d.ts.map +1 -1
  230. package/dist/esm/client/Item/Item.js +13 -0
  231. package/dist/esm/client/Item/Item.js.map +1 -1
  232. package/dist/esm/client/Item/Items.d.ts +1 -0
  233. package/dist/esm/client/Item/Items.d.ts.map +1 -1
  234. package/dist/esm/client/Item/Items.js +20 -3
  235. package/dist/esm/client/Item/Items.js.map +1 -1
  236. package/dist/esm/common/constants.d.ts +20 -0
  237. package/dist/esm/common/constants.d.ts.map +1 -1
  238. package/dist/esm/common/constants.js +23 -1
  239. package/dist/esm/common/constants.js.map +1 -1
  240. package/dist/esm/common/platform.d.ts +6 -1
  241. package/dist/esm/common/platform.d.ts.map +1 -1
  242. package/dist/esm/common/platform.js +23 -4
  243. package/dist/esm/common/platform.js.map +1 -1
  244. package/dist/esm/diagnostics/DiagnosticNodeInternal.d.ts +1 -0
  245. package/dist/esm/diagnostics/DiagnosticNodeInternal.d.ts.map +1 -1
  246. package/dist/esm/diagnostics/DiagnosticNodeInternal.js.map +1 -1
  247. package/dist/esm/documents/ConnectionPolicy.d.ts +6 -0
  248. package/dist/esm/documents/ConnectionPolicy.d.ts.map +1 -1
  249. package/dist/esm/documents/ConnectionPolicy.js +2 -0
  250. package/dist/esm/documents/ConnectionPolicy.js.map +1 -1
  251. package/dist/esm/documents/DatabaseAccount.d.ts.map +1 -1
  252. package/dist/esm/documents/DatabaseAccount.js +3 -0
  253. package/dist/esm/documents/DatabaseAccount.js.map +1 -1
  254. package/dist/esm/documents/PartitionKeyDefinition.d.ts +1 -1
  255. package/dist/esm/documents/PartitionKeyDefinition.js.map +1 -1
  256. package/dist/esm/extractPartitionKey.d.ts.map +1 -1
  257. package/dist/esm/extractPartitionKey.js +33 -20
  258. package/dist/esm/extractPartitionKey.js.map +1 -1
  259. package/dist/esm/globalEndpointManager.d.ts +6 -0
  260. package/dist/esm/globalEndpointManager.d.ts.map +1 -1
  261. package/dist/esm/globalEndpointManager.js +9 -4
  262. package/dist/esm/globalEndpointManager.js.map +1 -1
  263. package/dist/esm/globalPartitionEndpointManager.d.ts +79 -0
  264. package/dist/esm/globalPartitionEndpointManager.d.ts.map +1 -0
  265. package/dist/esm/globalPartitionEndpointManager.js +279 -0
  266. package/dist/esm/globalPartitionEndpointManager.js.map +1 -0
  267. package/dist/esm/queryExecutionContext/Aggregators/index.d.ts.map +1 -1
  268. package/dist/esm/queryExecutionContext/Aggregators/index.js +1 -0
  269. package/dist/esm/queryExecutionContext/Aggregators/index.js.map +1 -1
  270. package/dist/esm/queryExecutionContext/documentProducer.d.ts +2 -3
  271. package/dist/esm/queryExecutionContext/documentProducer.d.ts.map +1 -1
  272. package/dist/esm/queryExecutionContext/documentProducer.js +16 -21
  273. package/dist/esm/queryExecutionContext/documentProducer.js.map +1 -1
  274. package/dist/esm/queryExecutionContext/hybridQueryExecutionContext.d.ts +3 -1
  275. package/dist/esm/queryExecutionContext/hybridQueryExecutionContext.d.ts.map +1 -1
  276. package/dist/esm/queryExecutionContext/hybridQueryExecutionContext.js +10 -2
  277. package/dist/esm/queryExecutionContext/hybridQueryExecutionContext.js.map +1 -1
  278. package/dist/esm/queryExecutionContext/parallelQueryExecutionContextBase.d.ts.map +1 -1
  279. package/dist/esm/queryExecutionContext/parallelQueryExecutionContextBase.js +2 -1
  280. package/dist/esm/queryExecutionContext/parallelQueryExecutionContextBase.js.map +1 -1
  281. package/dist/esm/queryIterator.d.ts.map +1 -1
  282. package/dist/esm/queryIterator.js +2 -3
  283. package/dist/esm/queryIterator.js.map +1 -1
  284. package/dist/esm/request/ErrorResponse.d.ts +1 -1
  285. package/dist/esm/request/ErrorResponse.d.ts.map +1 -1
  286. package/dist/esm/request/ErrorResponse.js.map +1 -1
  287. package/dist/esm/request/RequestContext.d.ts +5 -0
  288. package/dist/esm/request/RequestContext.d.ts.map +1 -1
  289. package/dist/esm/request/RequestContext.js.map +1 -1
  290. package/dist/esm/request/RequestHandler.d.ts.map +1 -1
  291. package/dist/esm/request/RequestHandler.js +14 -4
  292. package/dist/esm/request/RequestHandler.js.map +1 -1
  293. package/dist/esm/request/defaultAgent.js +2 -2
  294. package/dist/esm/request/defaultAgent.js.map +1 -1
  295. package/dist/esm/retry/RetryPolicy.d.ts +2 -2
  296. package/dist/esm/retry/RetryPolicy.d.ts.map +1 -1
  297. package/dist/esm/retry/RetryPolicy.js.map +1 -1
  298. package/dist/esm/retry/defaultRetryPolicy.d.ts.map +1 -1
  299. package/dist/esm/retry/defaultRetryPolicy.js +4 -8
  300. package/dist/esm/retry/defaultRetryPolicy.js.map +1 -1
  301. package/dist/esm/retry/endpointDiscoveryRetryPolicy.d.ts +5 -3
  302. package/dist/esm/retry/endpointDiscoveryRetryPolicy.d.ts.map +1 -1
  303. package/dist/esm/retry/endpointDiscoveryRetryPolicy.js +9 -2
  304. package/dist/esm/retry/endpointDiscoveryRetryPolicy.js.map +1 -1
  305. package/dist/esm/retry/retryUtility.d.ts.map +1 -1
  306. package/dist/esm/retry/retryUtility.js +11 -5
  307. package/dist/esm/retry/retryUtility.js.map +1 -1
  308. package/dist/esm/retry/timeoutFailoverRetryPolicy.d.ts +6 -3
  309. package/dist/esm/retry/timeoutFailoverRetryPolicy.d.ts.map +1 -1
  310. package/dist/esm/retry/timeoutFailoverRetryPolicy.js +12 -4
  311. package/dist/esm/retry/timeoutFailoverRetryPolicy.js.map +1 -1
  312. package/dist/esm/routing/partitionKeyRangeCache.d.ts.map +1 -1
  313. package/dist/esm/routing/partitionKeyRangeCache.js +11 -0
  314. package/dist/esm/routing/partitionKeyRangeCache.js.map +1 -1
  315. package/dist/esm/utils/checkURL.js +6 -0
  316. package/dist/esm/utils/checkURL.js.map +1 -1
  317. package/dist/esm/utils/hashing/hash.d.ts +2 -0
  318. package/dist/esm/utils/hashing/hash.d.ts.map +1 -1
  319. package/dist/esm/utils/hashing/hash.js +19 -0
  320. package/dist/esm/utils/hashing/hash.js.map +1 -1
  321. package/dist/react-native/ClientContext.d.ts +19 -10
  322. package/dist/react-native/ClientContext.d.ts.map +1 -1
  323. package/dist/react-native/ClientContext.js +30 -11
  324. package/dist/react-native/ClientContext.js.map +1 -1
  325. package/dist/react-native/CosmosClient.d.ts.map +1 -1
  326. package/dist/react-native/CosmosClient.js +10 -2
  327. package/dist/react-native/CosmosClient.js.map +1 -1
  328. package/dist/react-native/PartitionKeyRangeFailoverInfo.d.ts +49 -0
  329. package/dist/react-native/PartitionKeyRangeFailoverInfo.d.ts.map +1 -0
  330. package/dist/react-native/PartitionKeyRangeFailoverInfo.js +130 -0
  331. package/dist/react-native/PartitionKeyRangeFailoverInfo.js.map +1 -0
  332. package/dist/react-native/client/ClientUtils.d.ts +3 -1
  333. package/dist/react-native/client/ClientUtils.d.ts.map +1 -1
  334. package/dist/react-native/client/ClientUtils.js +10 -0
  335. package/dist/react-native/client/ClientUtils.js.map +1 -1
  336. package/dist/react-native/client/Item/Item.d.ts.map +1 -1
  337. package/dist/react-native/client/Item/Item.js +13 -0
  338. package/dist/react-native/client/Item/Item.js.map +1 -1
  339. package/dist/react-native/client/Item/Items.d.ts +1 -0
  340. package/dist/react-native/client/Item/Items.d.ts.map +1 -1
  341. package/dist/react-native/client/Item/Items.js +20 -3
  342. package/dist/react-native/client/Item/Items.js.map +1 -1
  343. package/dist/react-native/common/constants.d.ts +20 -0
  344. package/dist/react-native/common/constants.d.ts.map +1 -1
  345. package/dist/react-native/common/constants.js +23 -1
  346. package/dist/react-native/common/constants.js.map +1 -1
  347. package/dist/react-native/common/platform.d.ts +6 -1
  348. package/dist/react-native/common/platform.d.ts.map +1 -1
  349. package/dist/react-native/common/platform.js +23 -4
  350. package/dist/react-native/common/platform.js.map +1 -1
  351. package/dist/react-native/diagnostics/DiagnosticNodeInternal.d.ts +1 -0
  352. package/dist/react-native/diagnostics/DiagnosticNodeInternal.d.ts.map +1 -1
  353. package/dist/react-native/diagnostics/DiagnosticNodeInternal.js.map +1 -1
  354. package/dist/react-native/documents/ConnectionPolicy.d.ts +6 -0
  355. package/dist/react-native/documents/ConnectionPolicy.d.ts.map +1 -1
  356. package/dist/react-native/documents/ConnectionPolicy.js +2 -0
  357. package/dist/react-native/documents/ConnectionPolicy.js.map +1 -1
  358. package/dist/react-native/documents/DatabaseAccount.d.ts.map +1 -1
  359. package/dist/react-native/documents/DatabaseAccount.js +3 -0
  360. package/dist/react-native/documents/DatabaseAccount.js.map +1 -1
  361. package/dist/react-native/documents/PartitionKeyDefinition.d.ts +1 -1
  362. package/dist/react-native/documents/PartitionKeyDefinition.js.map +1 -1
  363. package/dist/react-native/extractPartitionKey.d.ts.map +1 -1
  364. package/dist/react-native/extractPartitionKey.js +33 -20
  365. package/dist/react-native/extractPartitionKey.js.map +1 -1
  366. package/dist/react-native/globalEndpointManager.d.ts +6 -0
  367. package/dist/react-native/globalEndpointManager.d.ts.map +1 -1
  368. package/dist/react-native/globalEndpointManager.js +9 -4
  369. package/dist/react-native/globalEndpointManager.js.map +1 -1
  370. package/dist/react-native/globalPartitionEndpointManager.d.ts +79 -0
  371. package/dist/react-native/globalPartitionEndpointManager.d.ts.map +1 -0
  372. package/dist/react-native/globalPartitionEndpointManager.js +279 -0
  373. package/dist/react-native/globalPartitionEndpointManager.js.map +1 -0
  374. package/dist/react-native/queryExecutionContext/Aggregators/index.d.ts.map +1 -1
  375. package/dist/react-native/queryExecutionContext/Aggregators/index.js +1 -0
  376. package/dist/react-native/queryExecutionContext/Aggregators/index.js.map +1 -1
  377. package/dist/react-native/queryExecutionContext/documentProducer.d.ts +2 -3
  378. package/dist/react-native/queryExecutionContext/documentProducer.d.ts.map +1 -1
  379. package/dist/react-native/queryExecutionContext/documentProducer.js +16 -21
  380. package/dist/react-native/queryExecutionContext/documentProducer.js.map +1 -1
  381. package/dist/react-native/queryExecutionContext/hybridQueryExecutionContext.d.ts +3 -1
  382. package/dist/react-native/queryExecutionContext/hybridQueryExecutionContext.d.ts.map +1 -1
  383. package/dist/react-native/queryExecutionContext/hybridQueryExecutionContext.js +10 -2
  384. package/dist/react-native/queryExecutionContext/hybridQueryExecutionContext.js.map +1 -1
  385. package/dist/react-native/queryExecutionContext/parallelQueryExecutionContextBase.d.ts.map +1 -1
  386. package/dist/react-native/queryExecutionContext/parallelQueryExecutionContextBase.js +2 -1
  387. package/dist/react-native/queryExecutionContext/parallelQueryExecutionContextBase.js.map +1 -1
  388. package/dist/react-native/queryIterator.d.ts.map +1 -1
  389. package/dist/react-native/queryIterator.js +2 -3
  390. package/dist/react-native/queryIterator.js.map +1 -1
  391. package/dist/react-native/request/ErrorResponse.d.ts +1 -1
  392. package/dist/react-native/request/ErrorResponse.d.ts.map +1 -1
  393. package/dist/react-native/request/ErrorResponse.js.map +1 -1
  394. package/dist/react-native/request/RequestContext.d.ts +5 -0
  395. package/dist/react-native/request/RequestContext.d.ts.map +1 -1
  396. package/dist/react-native/request/RequestContext.js.map +1 -1
  397. package/dist/react-native/request/RequestHandler.d.ts.map +1 -1
  398. package/dist/react-native/request/RequestHandler.js +14 -4
  399. package/dist/react-native/request/RequestHandler.js.map +1 -1
  400. package/dist/react-native/request/defaultAgent.js +2 -2
  401. package/dist/react-native/request/defaultAgent.js.map +1 -1
  402. package/dist/react-native/retry/RetryPolicy.d.ts +2 -2
  403. package/dist/react-native/retry/RetryPolicy.d.ts.map +1 -1
  404. package/dist/react-native/retry/RetryPolicy.js.map +1 -1
  405. package/dist/react-native/retry/defaultRetryPolicy.d.ts.map +1 -1
  406. package/dist/react-native/retry/defaultRetryPolicy.js +4 -8
  407. package/dist/react-native/retry/defaultRetryPolicy.js.map +1 -1
  408. package/dist/react-native/retry/endpointDiscoveryRetryPolicy.d.ts +5 -3
  409. package/dist/react-native/retry/endpointDiscoveryRetryPolicy.d.ts.map +1 -1
  410. package/dist/react-native/retry/endpointDiscoveryRetryPolicy.js +9 -2
  411. package/dist/react-native/retry/endpointDiscoveryRetryPolicy.js.map +1 -1
  412. package/dist/react-native/retry/retryUtility.d.ts.map +1 -1
  413. package/dist/react-native/retry/retryUtility.js +11 -5
  414. package/dist/react-native/retry/retryUtility.js.map +1 -1
  415. package/dist/react-native/retry/timeoutFailoverRetryPolicy.d.ts +6 -3
  416. package/dist/react-native/retry/timeoutFailoverRetryPolicy.d.ts.map +1 -1
  417. package/dist/react-native/retry/timeoutFailoverRetryPolicy.js +12 -4
  418. package/dist/react-native/retry/timeoutFailoverRetryPolicy.js.map +1 -1
  419. package/dist/react-native/routing/partitionKeyRangeCache.d.ts.map +1 -1
  420. package/dist/react-native/routing/partitionKeyRangeCache.js +11 -0
  421. package/dist/react-native/routing/partitionKeyRangeCache.js.map +1 -1
  422. package/dist/react-native/utils/checkURL.js +6 -0
  423. package/dist/react-native/utils/checkURL.js.map +1 -1
  424. package/dist/react-native/utils/hashing/hash.d.ts +2 -0
  425. package/dist/react-native/utils/hashing/hash.d.ts.map +1 -1
  426. package/dist/react-native/utils/hashing/hash.js +19 -0
  427. package/dist/react-native/utils/hashing/hash.js.map +1 -1
  428. package/package.json +11 -11
@@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,EACpB,YAA0B;IAE1B,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;QAC/B,MAAM,sBAAsB,GAAG,MAAM,IAAA,2CAA0B,EAAC,cAAc,EAAE,SAAS,CAAC,CAAC;QAC3F,YAAY,GAAG,qBAAqB,CAAC,sBAAsB,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,IAAA,wCAA6B,EAAC,YAAY,CAAC,CAAC;AACrD,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport type { AzureLogger } from \"@azure/logger\";\nimport { createClientLogger } from \"@azure/logger\";\nimport { parsePath } from \"./common/index.js\";\nimport type {\n PartitionKey,\n PartitionKeyDefinition,\n PartitionKeyInternal,\n PrimitivePartitionKeyValue,\n} from \"./documents/index.js\";\nimport {\n convertToInternalPartitionKey,\n NonePartitionKeyLiteral,\n NullPartitionKeyLiteral,\n} from \"./documents/index.js\";\nimport { DEFAULT_PARTITION_KEY_PATH } from \"./common/partitionKeys.js\";\nimport type { Container } from \"./client/index.js\";\nimport { readPartitionKeyDefinition } from \"./client/ClientUtils.js\";\nimport type { DiagnosticNodeInternal } from \"./diagnostics/DiagnosticNodeInternal.js\";\n\nconst logger: AzureLogger = createClientLogger(\"extractPartitionKey\");\n\n/**\n * Function to extract PartitionKey based on {@link PartitionKeyDefinition}\n * from an object.\n * Retuns\n * 1. PartitionKeyInternal[] if extraction is successful.\n * 2. undefined if either {@link partitionKeyDefinition} is not well formed\n * or an unsupported partitionkey type is encountered.\n * @hidden\n */\nexport function extractPartitionKeys(\n document: unknown,\n partitionKeyDefinition?: PartitionKeyDefinition,\n): PartitionKeyInternal | undefined {\n if (\n partitionKeyDefinition &&\n partitionKeyDefinition.paths &&\n partitionKeyDefinition.paths.length > 0\n ) {\n if (partitionKeyDefinition.systemKey === true) {\n return [];\n }\n\n if (\n partitionKeyDefinition.paths.length === 1 &&\n partitionKeyDefinition.paths[0] === DEFAULT_PARTITION_KEY_PATH\n ) {\n return [extractPartitionKey(DEFAULT_PARTITION_KEY_PATH, document)];\n }\n\n const partitionKeys: PrimitivePartitionKeyValue[] = [];\n partitionKeyDefinition.paths.forEach((path: string) => {\n const obj = extractPartitionKey(path, document);\n if (obj === undefined) {\n logger.warning(\"Unsupported PartitionKey found.\");\n return undefined;\n }\n partitionKeys.push(obj);\n });\n return partitionKeys;\n }\n logger.error(\"Unexpected Partition Key Definition Found.\");\n return undefined;\n}\n\nfunction extractPartitionKey(path: string, obj: unknown): any {\n const pathParts: string[] = parsePath(path);\n for (const part of pathParts) {\n if (typeof obj === \"object\" && obj !== null && part in obj) {\n obj = (obj as Record<string, unknown>)[part];\n } else {\n obj = undefined;\n break;\n }\n }\n if (typeof obj === \"string\" || typeof obj === \"number\" || typeof obj === \"boolean\") {\n return obj;\n } else if (obj === NullPartitionKeyLiteral) {\n return NullPartitionKeyLiteral;\n } else if (obj === undefined || JSON.stringify(obj) === JSON.stringify(NonePartitionKeyLiteral)) {\n return NonePartitionKeyLiteral;\n }\n return undefined;\n}\n\n/**\n * @hidden\n */\nexport function undefinedPartitionKey(\n partitionKeyDefinition: PartitionKeyDefinition,\n): PartitionKeyInternal {\n if (partitionKeyDefinition?.systemKey) {\n return [];\n } else {\n return partitionKeyDefinition?.paths.map(() => NonePartitionKeyLiteral);\n }\n}\n\n/**\n * @hidden\n */\nexport async function setPartitionKeyIfUndefined(\n diagnosticNode: DiagnosticNodeInternal,\n container: Container,\n partitionKey: PartitionKey,\n): Promise<PartitionKeyInternal> {\n if (partitionKey === undefined) {\n const partitionKeyDefinition = await readPartitionKeyDefinition(diagnosticNode, container);\n partitionKey = undefinedPartitionKey(partitionKeyDefinition);\n }\n return convertToInternalPartitionKey(partitionKey);\n}\n"]}
1
+ {"version":3,"file":"extractPartitionKey.js","sourceRoot":"","sources":["../../src/extractPartitionKey.ts"],"names":[],"mappings":";;AAgCA,oDA6CC;AAyBD,sDAQC;AAKD,gEAUC;AA1HD,0CAAmD;AACnD,gDAA8C;AAO9C,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,SAAS,EAAE,CAAC;QACtC,OAAO,EAAE,CAAC;IACZ,CAAC;SAAM,CAAC;QACN,OAAO,sBAAsB,aAAtB,sBAAsB,uBAAtB,sBAAsB,CAAE,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,kCAAuB,CAAC,CAAC;IAC1E,CAAC;AACH,CAAC;AAED;;GAEG;AACI,KAAK,UAAU,0BAA0B,CAC9C,cAAsC,EACtC,SAAoB,EACpB,YAA0B;IAE1B,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;QAC/B,MAAM,sBAAsB,GAAG,MAAM,IAAA,2CAA0B,EAAC,cAAc,EAAE,SAAS,CAAC,CAAC;QAC3F,YAAY,GAAG,qBAAqB,CAAC,sBAAsB,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,IAAA,wCAA6B,EAAC,YAAY,CAAC,CAAC;AACrD,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport type { AzureLogger } from \"@azure/logger\";\nimport { createClientLogger } from \"@azure/logger\";\nimport { parsePath } from \"./common/index.js\";\nimport type {\n PartitionKey,\n PartitionKeyDefinition,\n PartitionKeyInternal,\n PrimitivePartitionKeyValue,\n} from \"./documents/index.js\";\nimport {\n convertToInternalPartitionKey,\n NonePartitionKeyLiteral,\n NullPartitionKeyLiteral,\n} from \"./documents/index.js\";\nimport { DEFAULT_PARTITION_KEY_PATH } from \"./common/partitionKeys.js\";\nimport type { Container } from \"./client/index.js\";\nimport { readPartitionKeyDefinition } from \"./client/ClientUtils.js\";\nimport type { DiagnosticNodeInternal } from \"./diagnostics/DiagnosticNodeInternal.js\";\n\nconst logger: AzureLogger = createClientLogger(\"extractPartitionKey\");\n\n/**\n * Function to extract PartitionKey based on {@link PartitionKeyDefinition}\n * from an object.\n * Retuns\n * 1. PartitionKeyInternal[] if extraction is successful.\n * 2. undefined if either {@link partitionKeyDefinition} is not well formed\n * or an unsupported partitionkey type is encountered.\n * @hidden\n */\nexport function extractPartitionKeys(\n document: unknown,\n partitionKeyDefinition?: PartitionKeyDefinition,\n): PartitionKeyInternal | undefined {\n if (\n !partitionKeyDefinition ||\n !partitionKeyDefinition.paths ||\n partitionKeyDefinition.paths.length <= 0\n ) {\n logger.error(\"Unexpected Partition Key Definition Found.\");\n return undefined;\n }\n\n if (\n partitionKeyDefinition.paths.length === 1 &&\n partitionKeyDefinition.paths[0] === DEFAULT_PARTITION_KEY_PATH\n ) {\n const defaultKey = extractPartitionKey(DEFAULT_PARTITION_KEY_PATH, document);\n if (defaultKey === undefined) {\n if (partitionKeyDefinition.systemKey === true) {\n return [];\n }\n logger.warning(\"Unsupported PartitionKey found.\");\n return undefined;\n } else if (defaultKey === NullPartitionKeyLiteral || defaultKey === NonePartitionKeyLiteral) {\n if (partitionKeyDefinition.systemKey === true) {\n return [];\n }\n }\n return [defaultKey];\n }\n\n if (partitionKeyDefinition.systemKey === true) {\n return [];\n }\n const partitionKeys: PrimitivePartitionKeyValue[] = [];\n partitionKeyDefinition.paths.forEach((path: string) => {\n const obj = extractPartitionKey(path, document);\n if (obj === undefined) {\n logger.warning(\"Unsupported PartitionKey found.\");\n return undefined;\n }\n partitionKeys.push(obj);\n });\n return partitionKeys;\n}\n\nfunction extractPartitionKey(path: string, obj: unknown): any {\n const pathParts: string[] = parsePath(path);\n for (const part of pathParts) {\n if (typeof obj === \"object\" && obj !== null && part in obj) {\n obj = (obj as Record<string, unknown>)[part];\n } else {\n obj = undefined;\n break;\n }\n }\n if (typeof obj === \"string\" || typeof obj === \"number\" || typeof obj === \"boolean\") {\n return obj;\n } else if (obj === NullPartitionKeyLiteral) {\n return NullPartitionKeyLiteral;\n } else if (obj === undefined || JSON.stringify(obj) === JSON.stringify(NonePartitionKeyLiteral)) {\n return NonePartitionKeyLiteral;\n }\n return undefined;\n}\n\n/**\n * @hidden\n */\nexport function undefinedPartitionKey(\n partitionKeyDefinition: PartitionKeyDefinition,\n): PartitionKeyInternal {\n if (partitionKeyDefinition?.systemKey) {\n return [];\n } else {\n return partitionKeyDefinition?.paths.map(() => NonePartitionKeyLiteral);\n }\n}\n\n/**\n * @hidden\n */\nexport async function setPartitionKeyIfUndefined(\n diagnosticNode: DiagnosticNodeInternal,\n container: Container,\n partitionKey: PartitionKey,\n): Promise<PartitionKeyInternal> {\n if (partitionKey === undefined) {\n const partitionKeyDefinition = await readPartitionKeyDefinition(diagnosticNode, container);\n partitionKey = undefinedPartitionKey(partitionKeyDefinition);\n }\n return convertToInternalPartitionKey(partitionKey);\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  import { OperationType, ResourceType } from "./common/index.js";
2
+ import type { Location } from "./documents/index.js";
2
3
  import type { DiagnosticNodeInternal } from "./diagnostics/DiagnosticNodeInternal.js";
3
4
  /**
4
5
  * @hidden
@@ -24,6 +25,7 @@ export declare class GlobalEndpointManager {
24
25
  private readableLocations;
25
26
  private unavailableReadableLocations;
26
27
  private unavailableWriteableLocations;
28
+ private enableMultipleWriteLocations;
27
29
  preferredLocationsCount: number;
28
30
  /**
29
31
  * Gets the current read endpoint from the endpoint cache.
@@ -35,6 +37,10 @@ export declare class GlobalEndpointManager {
35
37
  getWriteEndpoint(diagnosticNode: DiagnosticNodeInternal): Promise<string>;
36
38
  getReadEndpoints(): Promise<ReadonlyArray<string>>;
37
39
  getWriteEndpoints(): Promise<ReadonlyArray<string>>;
40
+ /**
41
+ * Gets the read locations from the endpoint cache.
42
+ */
43
+ getReadLocations(): Promise<ReadonlyArray<Location>>;
38
44
  markCurrentLocationUnavailableForRead(diagnosticNode: DiagnosticNodeInternal, endpoint: string): Promise<void>;
39
45
  markCurrentLocationUnavailableForWrite(diagnosticNode: DiagnosticNodeInternal, endpoint: string): Promise<void>;
40
46
  canUseMultipleWriteLocations(resourceType?: ResourceType, operationType?: OperationType): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"globalEndpointManager.d.ts","sourceRoot":"","sources":["../../src/globalEndpointManager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAA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IzE,gBAAgB,IAAI,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAIlD,iBAAiB,IAAI,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAIhE;;OAEG;IACU,gBAAgB,IAAI,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAIpD,qCAAqC,CAChD,cAAc,EAAE,sBAAsB,EACtC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAUH,sCAAsC,CACjD,cAAc,EAAE,sBAAsB,EACtC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAYT,4BAA4B,CACjC,YAAY,CAAC,EAAE,YAAY,EAC3B,aAAa,CAAC,EAAE,aAAa,GAC5B,OAAO;IAcG,sBAAsB,CACjC,cAAc,EAAE,sBAAsB,EACtC,YAAY,EAAE,YAAY,EAC1B,aAAa,EAAE,aAAa,EAC5B,yBAAyB,GAAE,MAAU,GACpC,OAAO,CAAC,MAAM,CAAC;IAyElB;;;;;OAKG;IACU,mBAAmB,CAAC,cAAc,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAYvF,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,gCAAgC;IAexC;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAiBtB,OAAO,CAAC,4BAA4B;IAYpC;;;;OAIG;YACW,iCAAiC;IAuC/C;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,qBAAqB;CA4BrC"}
@@ -7,6 +7,7 @@ const index_js_1 = require("./common/index.js");
7
7
  const constants_js_1 = require("./common/constants.js");
8
8
  const CosmosDiagnostics_js_1 = require("./CosmosDiagnostics.js");
9
9
  const diagnostics_js_1 = require("./utils/diagnostics.js");
10
+ const checkURL_js_1 = require("./utils/checkURL.js");
10
11
  /**
11
12
  * @hidden
12
13
  * This internal class implements the logic for endpoint management for geo-replicated database accounts.
@@ -47,6 +48,12 @@ class GlobalEndpointManager {
47
48
  async getWriteEndpoints() {
48
49
  return this.writeableLocations.map((loc) => loc.databaseAccountEndpoint);
49
50
  }
51
+ /**
52
+ * Gets the read locations from the endpoint cache.
53
+ */
54
+ async getReadLocations() {
55
+ return this.readableLocations;
56
+ }
50
57
  async markCurrentLocationUnavailableForRead(diagnosticNode, endpoint) {
51
58
  await this.refreshEndpointList(diagnosticNode);
52
59
  const location = this.readableLocations.find((loc) => loc.databaseAccountEndpoint === endpoint);
@@ -66,7 +73,7 @@ class GlobalEndpointManager {
66
73
  }
67
74
  }
68
75
  canUseMultipleWriteLocations(resourceType, operationType) {
69
- let canUse = this.options.connectionPolicy.useMultipleWriteLocations;
76
+ let canUse = this.options.connectionPolicy.useMultipleWriteLocations && this.enableMultipleWriteLocations;
70
77
  if (resourceType) {
71
78
  canUse =
72
79
  canUse &&
@@ -96,6 +103,7 @@ class GlobalEndpointManager {
96
103
  }, diagnosticNode, CosmosDiagnostics_js_1.MetadataLookUpType.DatabaseAccountLookUp);
97
104
  this.writeableLocations = resourceResponse.resource.writableLocations;
98
105
  this.readableLocations = resourceResponse.resource.readableLocations;
106
+ this.enableMultipleWriteLocations = resourceResponse.resource.enableMultipleWritableLocations;
99
107
  }
100
108
  const locations = (0, index_js_1.isReadRequest)(operationType)
101
109
  ? this.readableLocations
@@ -108,7 +116,7 @@ class GlobalEndpointManager {
108
116
  for (let i = startServiceEndpointIndex; i < this.preferredLocations.length; i++) {
109
117
  const preferredLocation = this.preferredLocations[i];
110
118
  location = locations.find((loc) => loc.unavailable !== true &&
111
- normalizeEndpoint(loc.name) === normalizeEndpoint(preferredLocation));
119
+ (0, checkURL_js_1.normalizeEndpoint)(loc.name) === (0, checkURL_js_1.normalizeEndpoint)(preferredLocation));
112
120
  if (location) {
113
121
  break;
114
122
  }
@@ -258,7 +266,4 @@ class GlobalEndpointManager {
258
266
  }
259
267
  }
260
268
  exports.GlobalEndpointManager = GlobalEndpointManager;
261
- function normalizeEndpoint(endpoint) {
262
- return endpoint.split(" ").join("").toLowerCase();
263
- }
264
269
  //# sourceMappingURL=globalEndpointManager.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"globalEndpointManager.js","sourceRoot":"","sources":["../../src/globalEndpointManager.ts"],"names":[],"mappings":";;;AAAA,uCAAuC;AACvC,kCAAkC;AAClC,gDAA+E;AAI/E,wDAAkD;AAElD,iEAA4D;AAE5D,2DAAiE;AAEjE;;;GAGG;AACH,MAAa,qBAAqB;IAqBhC;;;OAGG;IACH,YACE,OAA4B,EACpB,mBAGuC;QAHvC,wBAAmB,GAAnB,mBAAmB,CAGoB;QAfzC,uBAAkB,GAAe,EAAE,CAAC;QACpC,sBAAiB,GAAe,EAAE,CAAC;QACnC,iCAA4B,GAAe,EAAE,CAAC;QAC9C,kCAA6B,GAAe,EAAE,CAAC;QAcrD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC;QACxC,IAAI,CAAC,uBAAuB,GAAG,OAAO,CAAC,gBAAgB,CAAC,uBAAuB,CAAC;QAChF,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,kBAAkB,CAAC;QAC3E,IAAI,CAAC,uBAAuB,GAAG,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9F,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,eAAe,CAAC,cAAsC;QACjE,OAAO,IAAI,CAAC,sBAAsB,CAAC,cAAc,EAAE,uBAAY,CAAC,IAAI,EAAE,wBAAa,CAAC,IAAI,CAAC,CAAC;IAC5F,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,gBAAgB,CAAC,cAAsC;QAClE,OAAO,IAAI,CAAC,sBAAsB,CAAC,cAAc,EAAE,uBAAY,CAAC,IAAI,EA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C,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;AACpD,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport { OperationType, ResourceType, isReadRequest } from \"./common/index.js\";\nimport type { CosmosClientOptions } from \"./CosmosClientOptions.js\";\nimport type { Location, DatabaseAccount } from \"./documents/index.js\";\nimport type { RequestOptions } from \"./index.js\";\nimport { Constants } from \"./common/constants.js\";\nimport type { ResourceResponse } from \"./request/index.js\";\nimport { MetadataLookUpType } from \"./CosmosDiagnostics.js\";\nimport type { DiagnosticNodeInternal } from \"./diagnostics/DiagnosticNodeInternal.js\";\nimport { withMetadataDiagnostics } from \"./utils/diagnostics.js\";\n\n/**\n * @hidden\n * This internal class implements the logic for endpoint management for geo-replicated database accounts.\n */\nexport class GlobalEndpointManager {\n /**\n * The endpoint used to create the client instance.\n */\n private defaultEndpoint: string;\n /**\n * Flag to enable/disable automatic redirecting of requests based on read/write operations.\n */\n public enableEndpointDiscovery: boolean;\n private isRefreshing: boolean;\n private options: CosmosClientOptions;\n /**\n * List of azure regions to be used as preferred locations for read requests.\n */\n private preferredLocations: string[];\n private writeableLocations: Location[] = [];\n private readableLocations: Location[] = [];\n private unavailableReadableLocations: Location[] = [];\n private unavailableWriteableLocations: Location[] = [];\n\n public preferredLocationsCount: number;\n /**\n * @param options - The document client instance.\n * @internal\n */\n constructor(\n options: CosmosClientOptions,\n private readDatabaseAccount: (\n diagnosticNode: DiagnosticNodeInternal,\n opts: RequestOptions,\n ) => Promise<ResourceResponse<DatabaseAccount>>,\n ) {\n this.options = options;\n this.defaultEndpoint = options.endpoint;\n this.enableEndpointDiscovery = options.connectionPolicy.enableEndpointDiscovery;\n this.isRefreshing = false;\n this.preferredLocations = this.options.connectionPolicy.preferredLocations;\n this.preferredLocationsCount = this.preferredLocations ? this.preferredLocations.length : 0;\n }\n\n /**\n * Gets the current read endpoint from the endpoint cache.\n */\n public async getReadEndpoint(diagnosticNode: DiagnosticNodeInternal): Promise<string> {\n return this.resolveServiceEndpoint(diagnosticNode, ResourceType.item, OperationType.Read);\n }\n\n /**\n * Gets the current write endpoint from the endpoint cache.\n */\n public async getWriteEndpoint(diagnosticNode: DiagnosticNodeInternal): Promise<string> {\n return this.resolveServiceEndpoint(diagnosticNode, ResourceType.item, OperationType.Replace);\n }\n\n public async getReadEndpoints(): Promise<ReadonlyArray<string>> {\n return this.readableLocations.map((loc) => loc.databaseAccountEndpoint);\n }\n\n public async getWriteEndpoints(): Promise<ReadonlyArray<string>> {\n return this.writeableLocations.map((loc) => loc.databaseAccountEndpoint);\n }\n\n public async markCurrentLocationUnavailableForRead(\n diagnosticNode: DiagnosticNodeInternal,\n endpoint: string,\n ): Promise<void> {\n await this.refreshEndpointList(diagnosticNode);\n const location = this.readableLocations.find((loc) => loc.databaseAccountEndpoint === endpoint);\n if (location) {\n location.unavailable = true;\n location.lastUnavailabilityTimestampInMs = Date.now();\n this.unavailableReadableLocations.push(location);\n }\n }\n\n public async markCurrentLocationUnavailableForWrite(\n diagnosticNode: DiagnosticNodeInternal,\n endpoint: string,\n ): Promise<void> {\n await this.refreshEndpointList(diagnosticNode);\n const location = this.writeableLocations.find(\n (loc) => loc.databaseAccountEndpoint === endpoint,\n );\n if (location) {\n location.unavailable = true;\n location.lastUnavailabilityTimestampInMs = Date.now();\n this.unavailableWriteableLocations.push(location);\n }\n }\n\n public canUseMultipleWriteLocations(\n resourceType?: ResourceType,\n operationType?: OperationType,\n ): boolean {\n let canUse = this.options.connectionPolicy.useMultipleWriteLocations;\n\n if (resourceType) {\n canUse =\n canUse &&\n (resourceType === ResourceType.item ||\n (resourceType === ResourceType.sproc && operationType === OperationType.Execute));\n }\n\n return canUse;\n }\n\n public async resolveServiceEndpoint(\n diagnosticNode: DiagnosticNodeInternal,\n resourceType: ResourceType,\n operationType: OperationType,\n startServiceEndpointIndex: number = 0, // Represents the starting index for selecting servers.\n ): Promise<string> {\n // If endpoint discovery is disabled, always use the user provided endpoint\n\n if (!this.options.connectionPolicy.enableEndpointDiscovery) {\n diagnosticNode.addData({ readFromCache: true }, \"default_endpoint\");\n diagnosticNode.recordEndpointResolution(this.defaultEndpoint);\n return this.defaultEndpoint;\n }\n\n // If getting the database account, always use the user provided endpoint\n if (resourceType === ResourceType.none) {\n diagnosticNode.addData({ readFromCache: true }, \"none_resource\");\n diagnosticNode.recordEndpointResolution(this.defaultEndpoint);\n return this.defaultEndpoint;\n }\n\n if (this.readableLocations.length === 0 || this.writeableLocations.length === 0) {\n const resourceResponse = await withMetadataDiagnostics(\n async (metadataNode: DiagnosticNodeInternal) => {\n return this.readDatabaseAccount(metadataNode, {\n urlConnection: this.defaultEndpoint,\n });\n },\n diagnosticNode,\n MetadataLookUpType.DatabaseAccountLookUp,\n );\n\n this.writeableLocations = resourceResponse.resource.writableLocations;\n this.readableLocations = resourceResponse.resource.readableLocations;\n }\n\n const locations = isReadRequest(operationType)\n ? this.readableLocations\n : this.writeableLocations;\n\n let location;\n // If we have preferred locations, try each one in order and use the first available one\n if (\n this.preferredLocations &&\n this.preferredLocations.length > 0 &&\n startServiceEndpointIndex < this.preferredLocations.length\n ) {\n for (let i = startServiceEndpointIndex; i < this.preferredLocations.length; i++) {\n const preferredLocation = this.preferredLocations[i];\n location = locations.find(\n (loc) =>\n loc.unavailable !== true &&\n normalizeEndpoint(loc.name) === normalizeEndpoint(preferredLocation),\n );\n if (location) {\n break;\n }\n }\n }\n\n // If no preferred locations or one did not match, just grab the first one that is available\n if (!location) {\n const startIndexValid =\n startServiceEndpointIndex >= 0 && startServiceEndpointIndex < locations.length;\n const locationsToSearch = startIndexValid\n ? locations.slice(startServiceEndpointIndex)\n : locations;\n location = locationsToSearch.find((loc) => {\n return loc.unavailable !== true;\n });\n }\n\n location = location ? location : { name: \"\", databaseAccountEndpoint: this.defaultEndpoint };\n diagnosticNode.recordEndpointResolution(location.databaseAccountEndpoint);\n return location.databaseAccountEndpoint;\n }\n\n /**\n * Refreshes the endpoint list by clearning stale unavailability and then\n * retrieving the writable and readable locations from the geo-replicated database account\n * and then updating the locations cache.\n * We skip the refreshing if enableEndpointDiscovery is set to False\n */\n public async refreshEndpointList(diagnosticNode: DiagnosticNodeInternal): Promise<void> {\n if (!this.isRefreshing && this.enableEndpointDiscovery) {\n this.isRefreshing = true;\n const databaseAccount = await this.getDatabaseAccountFromAnyEndpoint(diagnosticNode);\n if (databaseAccount) {\n this.refreshStaleUnavailableLocations();\n this.refreshEndpoints(databaseAccount);\n }\n this.isRefreshing = false;\n }\n }\n\n private refreshEndpoints(databaseAccount: DatabaseAccount): void {\n for (const location of databaseAccount.writableLocations) {\n const existingLocation = this.writeableLocations.find((loc) => loc.name === location.name);\n if (!existingLocation) {\n this.writeableLocations.push(location);\n }\n }\n for (const location of databaseAccount.readableLocations) {\n const existingLocation = this.readableLocations.find((loc) => loc.name === location.name);\n if (!existingLocation) {\n this.readableLocations.push(location);\n }\n }\n }\n\n private refreshStaleUnavailableLocations(): void {\n const now = Date.now();\n this.updateLocation(now, this.unavailableReadableLocations, this.readableLocations);\n this.unavailableReadableLocations = this.cleanUnavailableLocationList(\n now,\n this.unavailableReadableLocations,\n );\n\n this.updateLocation(now, this.unavailableWriteableLocations, this.writeableLocations);\n this.unavailableWriteableLocations = this.cleanUnavailableLocationList(\n now,\n this.unavailableWriteableLocations,\n );\n }\n\n /**\n * update the locationUnavailability to undefined if the location is available again\n * @param now - current time\n * @param unavailableLocations - list of unavailable locations\n * @param allLocations - list of all locations\n */\n private updateLocation(\n now: number,\n unavailableLocations: Location[],\n allLocations: Location[],\n ): void {\n for (const location of unavailableLocations) {\n const unavaialableLocation = allLocations.find((loc) => loc.name === location.name);\n if (\n unavaialableLocation &&\n now - unavaialableLocation.lastUnavailabilityTimestampInMs >\n Constants.LocationUnavailableExpirationTimeInMs\n ) {\n unavaialableLocation.unavailable = false;\n }\n }\n }\n\n private cleanUnavailableLocationList(now: number, unavailableLocations: Location[]): Location[] {\n return unavailableLocations.filter((loc) => {\n if (\n loc &&\n now - loc.lastUnavailabilityTimestampInMs >= Constants.LocationUnavailableExpirationTimeInMs\n ) {\n return false;\n }\n return true;\n });\n }\n\n /**\n * Gets the database account first by using the default endpoint, and if that doesn't returns\n * use the endpoints for the preferred locations in the order they are specified to get\n * the database account.\n */\n private async getDatabaseAccountFromAnyEndpoint(\n diagnosticNode: DiagnosticNodeInternal,\n ): Promise<DatabaseAccount> {\n try {\n const options = { urlConnection: this.defaultEndpoint };\n const { resource: databaseAccount } = await this.readDatabaseAccount(diagnosticNode, options);\n return databaseAccount;\n // If for any reason(non - globaldb related), we are not able to get the database\n // account from the above call to readDatabaseAccount,\n // we would try to get this information from any of the preferred locations that the user\n // might have specified (by creating a locational endpoint)\n // and keeping eating the exception until we get the database account and return None at the end,\n // if we are not able to get that info from any endpoints\n } catch (err: any) {\n // TODO: Tracing\n }\n\n if (this.preferredLocations) {\n for (const location of this.preferredLocations) {\n try {\n const locationalEndpoint = GlobalEndpointManager.getLocationalEndpoint(\n this.defaultEndpoint,\n location,\n );\n const options = { urlConnection: locationalEndpoint };\n const { resource: databaseAccount } = await this.readDatabaseAccount(\n diagnosticNode,\n options,\n );\n if (databaseAccount) {\n return databaseAccount;\n }\n } catch (err: any) {\n // TODO: Tracing\n }\n }\n }\n }\n\n /**\n * Gets the locational endpoint using the location name passed to it using the default endpoint.\n *\n * @param defaultEndpoint - The default endpoint to use for the endpoint.\n * @param locationName - The location name for the azure region like \"East US\".\n */\n private static getLocationalEndpoint(defaultEndpoint: string, locationName: string): string {\n // For defaultEndpoint like 'https://contoso.documents.azure.com:443/' parse it to generate URL format\n // This defaultEndpoint should be global endpoint(and cannot be a locational endpoint)\n // and we agreed to document that\n const endpointUrl = new URL(defaultEndpoint);\n\n // hostname attribute in endpointUrl will return 'contoso.documents.azure.com'\n if (endpointUrl.hostname) {\n const hostnameParts = endpointUrl.hostname.toString().toLowerCase().split(\".\");\n if (hostnameParts) {\n // globalDatabaseAccountName will return 'contoso'\n const globalDatabaseAccountName = hostnameParts[0];\n\n // Prepare the locationalDatabaseAccountName as contoso-EastUS for location_name 'East US'\n const locationalDatabaseAccountName =\n globalDatabaseAccountName + \"-\" + locationName.replace(\" \", \"\");\n\n // Replace 'contoso' with 'contoso-EastUS' and\n // return locationalEndpoint as https://contoso-EastUS.documents.azure.com:443/\n const locationalEndpoint = defaultEndpoint\n .toLowerCase()\n .replace(globalDatabaseAccountName, locationalDatabaseAccountName);\n return locationalEndpoint;\n }\n }\n\n return null;\n }\n}\n\nfunction normalizeEndpoint(endpoint: string): string {\n return endpoint.split(\" \").join(\"\").toLowerCase();\n}\n"]}
1
+ {"version":3,"file":"globalEndpointManager.js","sourceRoot":"","sources":["../../src/globalEndpointManager.ts"],"names":[],"mappings":";;;AAAA,uCAAuC;AACvC,kCAAkC;AAClC,gDAA+E;AAI/E,wDAAkD;AAElD,iEAA4D;AAE5D,2DAAiE;AACjE,qDAAwD;AAExD;;;GAGG;AACH,MAAa,qBAAqB;IAsBhC;;;OAGG;IACH,YACE,OAA4B,EACpB,mBAGuC;QAHvC,wBAAmB,GAAnB,mBAAmB,CAGoB;QAhBzC,uBAAkB,GAAe,EAAE,CAAC;QACpC,sBAAiB,GAAe,EAAE,CAAC;QACnC,iCAA4B,GAAe,EAAE,CAAC;QAC9C,kCAA6B,GAAe,EAAE,CAAC;QAerD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC;QACxC,IAAI,CAAC,uBAAuB,GAAG,OAAO,CAAC,gBAAgB,CAAC,uBAAuB,CAAC;QAChF,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,kBAAkB,CAAC;QAC3E,IAAI,CAAC,uBAAuB,GAAG,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM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,EAAE,CAAC;gBAClB,kDAAkD;gBAClD,MAAM,yBAAyB,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;gBAEnD,0FAA0F;gBAC1F,MAAM,6BAA6B,GACjC,yBAAyB,GAAG,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;gBAElE,8CAA8C;gBAC9C,+EAA+E;gBAC/E,MAAM,kBAAkB,GAAG,eAAe;qBACvC,WAAW,EAAE;qBACb,OAAO,CAAC,yBAAyB,EAAE,6BAA6B,CAAC,CAAC;gBACrE,OAAO,kBAAkB,CAAC;YAC5B,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAlWD,sDAkWC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport { OperationType, ResourceType, isReadRequest } from \"./common/index.js\";\nimport type { CosmosClientOptions } from \"./CosmosClientOptions.js\";\nimport type { Location, DatabaseAccount } from \"./documents/index.js\";\nimport type { RequestOptions } from \"./index.js\";\nimport { Constants } from \"./common/constants.js\";\nimport type { ResourceResponse } from \"./request/index.js\";\nimport { MetadataLookUpType } from \"./CosmosDiagnostics.js\";\nimport type { DiagnosticNodeInternal } from \"./diagnostics/DiagnosticNodeInternal.js\";\nimport { withMetadataDiagnostics } from \"./utils/diagnostics.js\";\nimport { normalizeEndpoint } from \"./utils/checkURL.js\";\n\n/**\n * @hidden\n * This internal class implements the logic for endpoint management for geo-replicated database accounts.\n */\nexport class GlobalEndpointManager {\n /**\n * The endpoint used to create the client instance.\n */\n private defaultEndpoint: string;\n /**\n * Flag to enable/disable automatic redirecting of requests based on read/write operations.\n */\n public enableEndpointDiscovery: boolean;\n private isRefreshing: boolean;\n private options: CosmosClientOptions;\n /**\n * List of azure regions to be used as preferred locations for read requests.\n */\n private preferredLocations: string[];\n private writeableLocations: Location[] = [];\n private readableLocations: Location[] = [];\n private unavailableReadableLocations: Location[] = [];\n private unavailableWriteableLocations: Location[] = [];\n private enableMultipleWriteLocations: boolean;\n\n public preferredLocationsCount: number;\n /**\n * @param options - The document client instance.\n * @internal\n */\n constructor(\n options: CosmosClientOptions,\n private readDatabaseAccount: (\n diagnosticNode: DiagnosticNodeInternal,\n opts: RequestOptions,\n ) => Promise<ResourceResponse<DatabaseAccount>>,\n ) {\n this.options = options;\n this.defaultEndpoint = options.endpoint;\n this.enableEndpointDiscovery = options.connectionPolicy.enableEndpointDiscovery;\n this.isRefreshing = false;\n this.preferredLocations = this.options.connectionPolicy.preferredLocations;\n this.preferredLocationsCount = this.preferredLocations ? this.preferredLocations.length : 0;\n }\n\n /**\n * Gets the current read endpoint from the endpoint cache.\n */\n public async getReadEndpoint(diagnosticNode: DiagnosticNodeInternal): Promise<string> {\n return this.resolveServiceEndpoint(diagnosticNode, ResourceType.item, OperationType.Read);\n }\n\n /**\n * Gets the current write endpoint from the endpoint cache.\n */\n public async getWriteEndpoint(diagnosticNode: DiagnosticNodeInternal): Promise<string> {\n return this.resolveServiceEndpoint(diagnosticNode, ResourceType.item, OperationType.Replace);\n }\n\n public async getReadEndpoints(): Promise<ReadonlyArray<string>> {\n return this.readableLocations.map((loc) => loc.databaseAccountEndpoint);\n }\n\n public async getWriteEndpoints(): Promise<ReadonlyArray<string>> {\n return this.writeableLocations.map((loc) => loc.databaseAccountEndpoint);\n }\n\n /**\n * Gets the read locations from the endpoint cache.\n */\n public async getReadLocations(): Promise<ReadonlyArray<Location>> {\n return this.readableLocations;\n }\n\n public async markCurrentLocationUnavailableForRead(\n diagnosticNode: DiagnosticNodeInternal,\n endpoint: string,\n ): Promise<void> {\n await this.refreshEndpointList(diagnosticNode);\n const location = this.readableLocations.find((loc) => loc.databaseAccountEndpoint === endpoint);\n if (location) {\n location.unavailable = true;\n location.lastUnavailabilityTimestampInMs = Date.now();\n this.unavailableReadableLocations.push(location);\n }\n }\n\n public async markCurrentLocationUnavailableForWrite(\n diagnosticNode: DiagnosticNodeInternal,\n endpoint: string,\n ): Promise<void> {\n await this.refreshEndpointList(diagnosticNode);\n const location = this.writeableLocations.find(\n (loc) => loc.databaseAccountEndpoint === endpoint,\n );\n if (location) {\n location.unavailable = true;\n location.lastUnavailabilityTimestampInMs = Date.now();\n this.unavailableWriteableLocations.push(location);\n }\n }\n\n public canUseMultipleWriteLocations(\n resourceType?: ResourceType,\n operationType?: OperationType,\n ): boolean {\n let canUse =\n this.options.connectionPolicy.useMultipleWriteLocations && this.enableMultipleWriteLocations;\n\n if (resourceType) {\n canUse =\n canUse &&\n (resourceType === ResourceType.item ||\n (resourceType === ResourceType.sproc && operationType === OperationType.Execute));\n }\n\n return canUse;\n }\n\n public async resolveServiceEndpoint(\n diagnosticNode: DiagnosticNodeInternal,\n resourceType: ResourceType,\n operationType: OperationType,\n startServiceEndpointIndex: number = 0, // Represents the starting index for selecting servers.\n ): Promise<string> {\n // If endpoint discovery is disabled, always use the user provided endpoint\n\n if (!this.options.connectionPolicy.enableEndpointDiscovery) {\n diagnosticNode.addData({ readFromCache: true }, \"default_endpoint\");\n diagnosticNode.recordEndpointResolution(this.defaultEndpoint);\n return this.defaultEndpoint;\n }\n\n // If getting the database account, always use the user provided endpoint\n if (resourceType === ResourceType.none) {\n diagnosticNode.addData({ readFromCache: true }, \"none_resource\");\n diagnosticNode.recordEndpointResolution(this.defaultEndpoint);\n return this.defaultEndpoint;\n }\n\n if (this.readableLocations.length === 0 || this.writeableLocations.length === 0) {\n const resourceResponse = await withMetadataDiagnostics(\n async (metadataNode: DiagnosticNodeInternal) => {\n return this.readDatabaseAccount(metadataNode, {\n urlConnection: this.defaultEndpoint,\n });\n },\n diagnosticNode,\n MetadataLookUpType.DatabaseAccountLookUp,\n );\n\n this.writeableLocations = resourceResponse.resource.writableLocations;\n this.readableLocations = resourceResponse.resource.readableLocations;\n this.enableMultipleWriteLocations = resourceResponse.resource.enableMultipleWritableLocations;\n }\n\n const locations = isReadRequest(operationType)\n ? this.readableLocations\n : this.writeableLocations;\n\n let location;\n // If we have preferred locations, try each one in order and use the first available one\n if (\n this.preferredLocations &&\n this.preferredLocations.length > 0 &&\n startServiceEndpointIndex < this.preferredLocations.length\n ) {\n for (let i = startServiceEndpointIndex; i < this.preferredLocations.length; i++) {\n const preferredLocation = this.preferredLocations[i];\n location = locations.find(\n (loc) =>\n loc.unavailable !== true &&\n normalizeEndpoint(loc.name) === normalizeEndpoint(preferredLocation),\n );\n if (location) {\n break;\n }\n }\n }\n\n // If no preferred locations or one did not match, just grab the first one that is available\n if (!location) {\n const startIndexValid =\n startServiceEndpointIndex >= 0 && startServiceEndpointIndex < locations.length;\n const locationsToSearch = startIndexValid\n ? locations.slice(startServiceEndpointIndex)\n : locations;\n location = locationsToSearch.find((loc) => {\n return loc.unavailable !== true;\n });\n }\n\n location = location ? location : { name: \"\", databaseAccountEndpoint: this.defaultEndpoint };\n diagnosticNode.recordEndpointResolution(location.databaseAccountEndpoint);\n return location.databaseAccountEndpoint;\n }\n\n /**\n * Refreshes the endpoint list by clearning stale unavailability and then\n * retrieving the writable and readable locations from the geo-replicated database account\n * and then updating the locations cache.\n * We skip the refreshing if enableEndpointDiscovery is set to False\n */\n public async refreshEndpointList(diagnosticNode: DiagnosticNodeInternal): Promise<void> {\n if (!this.isRefreshing && this.enableEndpointDiscovery) {\n this.isRefreshing = true;\n const databaseAccount = await this.getDatabaseAccountFromAnyEndpoint(diagnosticNode);\n if (databaseAccount) {\n this.refreshStaleUnavailableLocations();\n this.refreshEndpoints(databaseAccount);\n }\n this.isRefreshing = false;\n }\n }\n\n private refreshEndpoints(databaseAccount: DatabaseAccount): void {\n for (const location of databaseAccount.writableLocations) {\n const existingLocation = this.writeableLocations.find((loc) => loc.name === location.name);\n if (!existingLocation) {\n this.writeableLocations.push(location);\n }\n }\n for (const location of databaseAccount.readableLocations) {\n const existingLocation = this.readableLocations.find((loc) => loc.name === location.name);\n if (!existingLocation) {\n this.readableLocations.push(location);\n }\n }\n }\n\n private refreshStaleUnavailableLocations(): void {\n const now = Date.now();\n this.updateLocation(now, this.unavailableReadableLocations, this.readableLocations);\n this.unavailableReadableLocations = this.cleanUnavailableLocationList(\n now,\n this.unavailableReadableLocations,\n );\n\n this.updateLocation(now, this.unavailableWriteableLocations, this.writeableLocations);\n this.unavailableWriteableLocations = this.cleanUnavailableLocationList(\n now,\n this.unavailableWriteableLocations,\n );\n }\n\n /**\n * update the locationUnavailability to undefined if the location is available again\n * @param now - current time\n * @param unavailableLocations - list of unavailable locations\n * @param allLocations - list of all locations\n */\n private updateLocation(\n now: number,\n unavailableLocations: Location[],\n allLocations: Location[],\n ): void {\n for (const location of unavailableLocations) {\n const unavaialableLocation = allLocations.find((loc) => loc.name === location.name);\n if (\n unavaialableLocation &&\n now - unavaialableLocation.lastUnavailabilityTimestampInMs >\n Constants.LocationUnavailableExpirationTimeInMs\n ) {\n unavaialableLocation.unavailable = false;\n }\n }\n }\n\n private cleanUnavailableLocationList(now: number, unavailableLocations: Location[]): Location[] {\n return unavailableLocations.filter((loc) => {\n if (\n loc &&\n now - loc.lastUnavailabilityTimestampInMs >= Constants.LocationUnavailableExpirationTimeInMs\n ) {\n return false;\n }\n return true;\n });\n }\n\n /**\n * Gets the database account first by using the default endpoint, and if that doesn't returns\n * use the endpoints for the preferred locations in the order they are specified to get\n * the database account.\n */\n private async getDatabaseAccountFromAnyEndpoint(\n diagnosticNode: DiagnosticNodeInternal,\n ): Promise<DatabaseAccount> {\n try {\n const options = { urlConnection: this.defaultEndpoint };\n const { resource: databaseAccount } = await this.readDatabaseAccount(diagnosticNode, options);\n return databaseAccount;\n // If for any reason(non - globaldb related), we are not able to get the database\n // account from the above call to readDatabaseAccount,\n // we would try to get this information from any of the preferred locations that the user\n // might have specified (by creating a locational endpoint)\n // and keeping eating the exception until we get the database account and return None at the end,\n // if we are not able to get that info from any endpoints\n } catch (err: any) {\n // TODO: Tracing\n }\n\n if (this.preferredLocations) {\n for (const location of this.preferredLocations) {\n try {\n const locationalEndpoint = GlobalEndpointManager.getLocationalEndpoint(\n this.defaultEndpoint,\n location,\n );\n const options = { urlConnection: locationalEndpoint };\n const { resource: databaseAccount } = await this.readDatabaseAccount(\n diagnosticNode,\n options,\n );\n if (databaseAccount) {\n return databaseAccount;\n }\n } catch (err: any) {\n // TODO: Tracing\n }\n }\n }\n }\n\n /**\n * Gets the locational endpoint using the location name passed to it using the default endpoint.\n *\n * @param defaultEndpoint - The default endpoint to use for the endpoint.\n * @param locationName - The location name for the azure region like \"East US\".\n */\n private static getLocationalEndpoint(defaultEndpoint: string, locationName: string): string {\n // For defaultEndpoint like 'https://contoso.documents.azure.com:443/' parse it to generate URL format\n // This defaultEndpoint should be global endpoint(and cannot be a locational endpoint)\n // and we agreed to document that\n const endpointUrl = new URL(defaultEndpoint);\n\n // hostname attribute in endpointUrl will return 'contoso.documents.azure.com'\n if (endpointUrl.hostname) {\n const hostnameParts = endpointUrl.hostname.toString().toLowerCase().split(\".\");\n if (hostnameParts) {\n // globalDatabaseAccountName will return 'contoso'\n const globalDatabaseAccountName = hostnameParts[0];\n\n // Prepare the locationalDatabaseAccountName as contoso-EastUS for location_name 'East US'\n const locationalDatabaseAccountName =\n globalDatabaseAccountName + \"-\" + locationName.replace(\" \", \"\");\n\n // Replace 'contoso' with 'contoso-EastUS' and\n // return locationalEndpoint as https://contoso-EastUS.documents.azure.com:443/\n const locationalEndpoint = defaultEndpoint\n .toLowerCase()\n .replace(globalDatabaseAccountName, locationalDatabaseAccountName);\n return locationalEndpoint;\n }\n }\n\n return null;\n }\n}\n"]}
@@ -0,0 +1,79 @@
1
+ import { DiagnosticNodeInternal, type RequestContext } from "./index.js";
2
+ /**
3
+ * @hidden
4
+ * This class is used to failover single partitions to different regions.
5
+ */
6
+ export declare class GlobalPartitionEndpointManager {
7
+ private globalEndpointManager;
8
+ private readonly partitionKeyRangeToLocationForWrite;
9
+ private readonly partitionKeyRangeToLocationForReadAndWrite;
10
+ private enablePartitionLevelFailover;
11
+ private enablePartitionLevelCircuitBreaker;
12
+ private preferredLocations;
13
+ preferredLocationsCount: number;
14
+ private circuitBreakerFailbackBackgroundRefresher;
15
+ /**
16
+ * Checks eligibility of the request for partition failover and
17
+ * tries to mark the endpoint unavailable for the partition key range. Future
18
+ * requests will be routed to the next location if available.
19
+ */
20
+ tryPartitionLevelFailover(requestContext: RequestContext, diagnosticNode: DiagnosticNodeInternal): Promise<boolean>;
21
+ /**
22
+ * Updates the DocumentServiceRequest routing location to point
23
+ * new a location based if a partition level failover occurred.
24
+ */
25
+ tryAddPartitionLevelLocationOverride(requestContext: RequestContext, diagnosticNode: DiagnosticNodeInternal): Promise<RequestContext>;
26
+ /**
27
+ * This method clears the background refresher for circuit breaker failback
28
+ * and stops the periodic checks for unhealthy endpoints.
29
+ */
30
+ dispose(): void;
31
+ private tryMarkEndpointUnavailableForPartitionKeyRange;
32
+ /**
33
+ * Increments the failure counter for the specified partition and checks if the partition can fail over.
34
+ * This method is used to determine if a partition should be failed over based on the number of request failures.
35
+ */
36
+ private incrementFailureCounterAndCheckFailover;
37
+ /** Validates if the given request is eligible for partition failover. */
38
+ private isRequestEligibleForPartitionFailover;
39
+ /** Determines if partition level failover locations can be used for the given request. */
40
+ private canUsePartitionLevelFailoverLocations;
41
+ /**
42
+ * Determines if a request is eligible for per-partition automatic failover.
43
+ * A request is eligible if it is a write request, partition level failover is enabled,
44
+ * and the global endpoint manager cannot use multiple write locations for the request.
45
+ */
46
+ private isRequestEligibleForPerPartitionAutomaticFailover;
47
+ /**
48
+ * Determines if a request is eligible for partition-level circuit breaker.
49
+ * This method checks if partition-level circuit breaker is enabled, and if the request is a read-only request or
50
+ * the global endpoint manager can use multiple write locations for the request.
51
+ */
52
+ private isRequestEligibleForPartitionLevelCircuitBreaker;
53
+ /**
54
+ * Attempts to add or update the partition failover information and move to the next available location.
55
+ * This method checks if the current location for the partition key range has failed and updates the failover
56
+ * information to route the request to the next available location. If all locations have been tried, it removes
57
+ * the failover information for the partition key range. Return True if the failover information was successfully
58
+ * updated and the request was routed to a new location, otherwise false.
59
+ */
60
+ private tryAddOrUpdatePartitionFailoverInfoAndMoveToNextLocation;
61
+ /**
62
+ * Initiates a background loop that periodically checks for unhealthy endpoints
63
+ * and attempts to open connections to them. If a connection is successfully
64
+ * established, it initiates a failback to the original location for the partition key range.
65
+ * This is useful for scenarios where a partition key range has been marked as unavailable
66
+ * due to a circuit breaker, and we want to periodically check if the original location
67
+ * has become healthy again.
68
+ * The loop runs at a defined interval specified by Constants.StalePartitionUnavailabilityRefreshIntervalInMs.
69
+ */
70
+ private initiateCircuitBreakerFailbackLoop;
71
+ /**
72
+ * Attempts to open connections to unhealthy endpoints and initiates failback if the connections are successful.
73
+ * This method checks the partition key ranges that have failed locations and tries to re-establish connections
74
+ * to those locations. If a connection is successfully re-established, it initiates a failback to the original
75
+ * location for the partition key range.
76
+ */
77
+ private openConnectionToUnhealthyEndpointsWithFailback;
78
+ }
79
+ //# sourceMappingURL=globalPartitionEndpointManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globalPartitionEndpointManager.d.ts","sourceRoot":"","sources":["../../src/globalPartitionEndpointManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,sBAAsB,EAGtB,KAAK,cAAc,EACpB,MAAM,YAAY,CAAC;AAKpB;;;GAGG;AACH,qBAAa,8BAA8B;IAiBvC,OAAO,CAAC,qBAAqB;IAhB/B,OAAO,CAAC,QAAQ,CAAC,mCAAmC,CAA6C;IACjG,OAAO,CAAC,QAAQ,CAAC,0CAA0C,CAGzD;IACF,OAAO,CAAC,4BAA4B,CAAU;IAC9C,OAAO,CAAC,kCAAkC,CAAU;IACpD,OAAO,CAAC,kBAAkB,CAAW;IAC9B,uBAAuB,EAAE,MAAM,CAAC;IACvC,OAAO,CAAC,yCAAyC,CAAiB;IA2BlE;;;;OAIG;IACU,yBAAyB,CACpC,cAAc,EAAE,cAAc,EAC9B,cAAc,EAAE,sBAAsB,GACrC,OAAO,CAAC,OAAO,CAAC;IA6BnB;;;OAGG;IACU,oCAAoC,CAC/C,cAAc,EAAE,cAAc,EAC9B,cAAc,EAAE,sBAAsB,GACrC,OAAO,CAAC,cAAc,CAAC;IAiC1B;;;OAGG;IACI,OAAO,IAAI,IAAI;YAMR,8CAA8C;IA2D5D;;;OAGG;YACW,uCAAuC;IA6CrD,yEAAyE;YAC3D,qCAAqC;IA2BnD,0FAA0F;YAC5E,qCAAqC;IAmBnD;;;;OAIG;IACH,OAAO,CAAC,iDAAiD;IAazD;;;;OAIG;IACH,OAAO,CAAC,gDAAgD;IAexD;;;;;;OAMG;YACW,wDAAwD;IA8BtE;;;;;;;;OAQG;IACH,OAAO,CAAC,kCAAkC;IAY1C;;;;;OAKG;YACW,8CAA8C;CAmB7D"}
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GlobalPartitionEndpointManager = void 0;
4
+ // Copyright (c) Microsoft Corporation.
5
+ // Licensed under the MIT License.
6
+ const index_js_1 = require("./common/index.js");
7
+ const index_js_2 = require("./index.js");
8
+ const PartitionKeyRangeFailoverInfo_js_1 = require("./PartitionKeyRangeFailoverInfo.js");
9
+ const checkURL_js_1 = require("./utils/checkURL.js");
10
+ const typeChecks_js_1 = require("./utils/typeChecks.js");
11
+ /**
12
+ * @hidden
13
+ * This class is used to failover single partitions to different regions.
14
+ */
15
+ class GlobalPartitionEndpointManager {
16
+ /**
17
+ * @internal
18
+ */
19
+ constructor(options, globalEndpointManager) {
20
+ this.globalEndpointManager = globalEndpointManager;
21
+ this.partitionKeyRangeToLocationForWrite = new Map();
22
+ this.partitionKeyRangeToLocationForReadAndWrite = new Map();
23
+ this.enablePartitionLevelFailover = options.connectionPolicy.enablePartitionLevelFailover;
24
+ this.enablePartitionLevelCircuitBreaker =
25
+ options.connectionPolicy.enablePartitionLevelCircuitBreaker ||
26
+ options.connectionPolicy.enablePartitionLevelFailover;
27
+ this.preferredLocations = options.connectionPolicy.preferredLocations;
28
+ this.preferredLocationsCount = this.preferredLocations ? this.preferredLocations.length : 0;
29
+ if (this.enablePartitionLevelCircuitBreaker) {
30
+ this.initiateCircuitBreakerFailbackLoop();
31
+ }
32
+ }
33
+ /**
34
+ * Checks eligibility of the request for partition failover and
35
+ * tries to mark the endpoint unavailable for the partition key range. Future
36
+ * requests will be routed to the next location if available.
37
+ */
38
+ async tryPartitionLevelFailover(requestContext, diagnosticNode) {
39
+ if (!(await this.isRequestEligibleForPartitionFailover(requestContext, true))) {
40
+ return false;
41
+ }
42
+ const isRequestEligibleForPerPartitionAutomaticFailover = this.isRequestEligibleForPerPartitionAutomaticFailover(requestContext);
43
+ const isRequestEligibleForPartitionLevelCircuitBreaker = this.isRequestEligibleForPartitionLevelCircuitBreaker(requestContext);
44
+ if (isRequestEligibleForPerPartitionAutomaticFailover ||
45
+ (isRequestEligibleForPartitionLevelCircuitBreaker &&
46
+ (await this.incrementFailureCounterAndCheckFailover(requestContext, isRequestEligibleForPerPartitionAutomaticFailover, isRequestEligibleForPartitionLevelCircuitBreaker)))) {
47
+ return this.tryMarkEndpointUnavailableForPartitionKeyRange(requestContext, diagnosticNode, isRequestEligibleForPerPartitionAutomaticFailover, isRequestEligibleForPartitionLevelCircuitBreaker);
48
+ }
49
+ return false;
50
+ }
51
+ /**
52
+ * Updates the DocumentServiceRequest routing location to point
53
+ * new a location based if a partition level failover occurred.
54
+ */
55
+ async tryAddPartitionLevelLocationOverride(requestContext, diagnosticNode) {
56
+ if (!(await this.isRequestEligibleForPartitionFailover(requestContext, false))) {
57
+ return requestContext;
58
+ }
59
+ const partitionKeyRangeId = requestContext.partitionKeyRangeId;
60
+ if (this.isRequestEligibleForPerPartitionAutomaticFailover(requestContext)) {
61
+ if (this.partitionKeyRangeToLocationForWrite.has(partitionKeyRangeId)) {
62
+ const partitionFailOver = this.partitionKeyRangeToLocationForWrite.get(partitionKeyRangeId);
63
+ requestContext.endpoint = partitionFailOver.getCurrentEndPoint();
64
+ diagnosticNode.recordEndpointResolution(requestContext.endpoint);
65
+ return requestContext;
66
+ }
67
+ }
68
+ else if (this.isRequestEligibleForPartitionLevelCircuitBreaker(requestContext)) {
69
+ if (this.partitionKeyRangeToLocationForReadAndWrite.has(partitionKeyRangeId)) {
70
+ const partitionFailOver = this.partitionKeyRangeToLocationForReadAndWrite.get(partitionKeyRangeId);
71
+ const canCircuitBreakerTriggerPartitionFailOver = await partitionFailOver.canCircuitBreakerTriggerPartitionFailOver((0, index_js_1.isReadRequest)(requestContext.operationType));
72
+ if (canCircuitBreakerTriggerPartitionFailOver) {
73
+ requestContext.endpoint = partitionFailOver.getCurrentEndPoint();
74
+ diagnosticNode.recordEndpointResolution(requestContext.endpoint);
75
+ return requestContext;
76
+ }
77
+ }
78
+ }
79
+ return requestContext;
80
+ }
81
+ /**
82
+ * This method clears the background refresher for circuit breaker failback
83
+ * and stops the periodic checks for unhealthy endpoints.
84
+ */
85
+ dispose() {
86
+ if (this.circuitBreakerFailbackBackgroundRefresher) {
87
+ clearTimeout(this.circuitBreakerFailbackBackgroundRefresher);
88
+ }
89
+ }
90
+ async tryMarkEndpointUnavailableForPartitionKeyRange(requestContext, diagnosticNode, isRequestEligibleForPerPartitionAutomaticFailover, isRequestEligibleForPartitionLevelCircuitBreaker) {
91
+ const partitionKeyRangeId = requestContext.partitionKeyRangeId;
92
+ const failedEndPoint = requestContext.endpoint;
93
+ const readLocations = await this.globalEndpointManager.getReadLocations();
94
+ const readEndPoints = [];
95
+ if (isRequestEligibleForPerPartitionAutomaticFailover) {
96
+ // For any single master write accounts, the next locations to fail over will be the read regions configured at the account level.
97
+ for (const location of readLocations) {
98
+ readEndPoints.push(location.databaseAccountEndpoint);
99
+ }
100
+ return this.tryAddOrUpdatePartitionFailoverInfoAndMoveToNextLocation(partitionKeyRangeId, failedEndPoint, readEndPoints, this.partitionKeyRangeToLocationForWrite, diagnosticNode);
101
+ }
102
+ else if (isRequestEligibleForPartitionLevelCircuitBreaker) {
103
+ // For the read requests or multi-master write requests, the next locations to fail over will be the preferred locations
104
+ // configured at the account level plus any other read locations that are not already in the preferred locations.
105
+ if (this.preferredLocations && this.preferredLocations.length > 0) {
106
+ for (const preferredLocation of this.preferredLocations) {
107
+ const location = readLocations.find((loc) => (0, checkURL_js_1.normalizeEndpoint)(loc.name) === (0, checkURL_js_1.normalizeEndpoint)(preferredLocation));
108
+ if (location) {
109
+ readEndPoints.push(location.databaseAccountEndpoint);
110
+ }
111
+ }
112
+ // Add the rest of the locations not already added
113
+ for (const location of readLocations) {
114
+ if (!readEndPoints.includes(location.databaseAccountEndpoint)) {
115
+ readEndPoints.push(location.databaseAccountEndpoint);
116
+ }
117
+ }
118
+ }
119
+ else {
120
+ for (const location of readLocations) {
121
+ readEndPoints.push(location.databaseAccountEndpoint);
122
+ }
123
+ }
124
+ return this.tryAddOrUpdatePartitionFailoverInfoAndMoveToNextLocation(partitionKeyRangeId, failedEndPoint, readEndPoints, this.partitionKeyRangeToLocationForReadAndWrite, diagnosticNode);
125
+ }
126
+ return false;
127
+ }
128
+ /**
129
+ * Increments the failure counter for the specified partition and checks if the partition can fail over.
130
+ * This method is used to determine if a partition should be failed over based on the number of request failures.
131
+ */
132
+ async incrementFailureCounterAndCheckFailover(requestContext, isRequestEligibleForPerPartitionAutomaticFailover, isRequestEligibleForPartitionLevelCircuitBreaker) {
133
+ const partitionKeyRangeId = requestContext.partitionKeyRangeId;
134
+ const failedEndPoint = requestContext.endpoint;
135
+ let partitionKeyRangeFailoverInfo;
136
+ if (isRequestEligibleForPerPartitionAutomaticFailover) {
137
+ if (!this.partitionKeyRangeToLocationForWrite.has(partitionKeyRangeId)) {
138
+ // If the partition key range is not already in the map, add it
139
+ const failoverInfo = new PartitionKeyRangeFailoverInfo_js_1.PartitionKeyRangeFailoverInfo(failedEndPoint);
140
+ this.partitionKeyRangeToLocationForWrite.set(partitionKeyRangeId, failoverInfo);
141
+ }
142
+ partitionKeyRangeFailoverInfo =
143
+ this.partitionKeyRangeToLocationForWrite.get(partitionKeyRangeId);
144
+ }
145
+ else if (isRequestEligibleForPartitionLevelCircuitBreaker) {
146
+ if (!this.partitionKeyRangeToLocationForReadAndWrite.has(partitionKeyRangeId)) {
147
+ // If the partition key range is not already in the map, add it
148
+ const failoverInfo = new PartitionKeyRangeFailoverInfo_js_1.PartitionKeyRangeFailoverInfo(failedEndPoint);
149
+ this.partitionKeyRangeToLocationForReadAndWrite.set(partitionKeyRangeId, failoverInfo);
150
+ }
151
+ partitionKeyRangeFailoverInfo =
152
+ this.partitionKeyRangeToLocationForReadAndWrite.get(partitionKeyRangeId);
153
+ }
154
+ else {
155
+ return false;
156
+ }
157
+ (0, typeChecks_js_1.assertNotUndefined)(partitionKeyRangeFailoverInfo, "partitionKeyRangeFailoverInfo should be set if failover flags are true.");
158
+ const currentTimeInMilliseconds = Date.now();
159
+ await partitionKeyRangeFailoverInfo.incrementRequestFailureCounts((0, index_js_1.isReadRequest)(requestContext.operationType), currentTimeInMilliseconds);
160
+ return partitionKeyRangeFailoverInfo.canCircuitBreakerTriggerPartitionFailOver((0, index_js_1.isReadRequest)(requestContext.operationType));
161
+ }
162
+ /** Validates if the given request is eligible for partition failover. */
163
+ async isRequestEligibleForPartitionFailover(requestContext, shouldValidateFailedLocation) {
164
+ if (!requestContext ||
165
+ !requestContext.operationType ||
166
+ !requestContext.resourceType ||
167
+ !requestContext.partitionKeyRangeId) {
168
+ return false;
169
+ }
170
+ const canUsePartitionLevelFailoverLocations = await this.canUsePartitionLevelFailoverLocations(requestContext.operationType, requestContext.resourceType);
171
+ if (!canUsePartitionLevelFailoverLocations) {
172
+ return false;
173
+ }
174
+ if (shouldValidateFailedLocation && !requestContext.endpoint) {
175
+ return false;
176
+ }
177
+ return true;
178
+ }
179
+ /** Determines if partition level failover locations can be used for the given request. */
180
+ async canUsePartitionLevelFailoverLocations(operationType, resourceType) {
181
+ const readEndPoints = await this.globalEndpointManager.getReadEndpoints();
182
+ if (readEndPoints.length <= 1) {
183
+ return false;
184
+ }
185
+ if (resourceType === index_js_1.ResourceType.item ||
186
+ (resourceType === index_js_1.ResourceType.sproc && operationType === index_js_1.OperationType.Execute)) {
187
+ // Right now, for single-master only reads are supported for circuit breaker, and writes are supported for automatic.
188
+ // failover. For multi master, both reads and writes are supported. Hence return true for both the cases.
189
+ return true;
190
+ }
191
+ return false;
192
+ }
193
+ /**
194
+ * Determines if a request is eligible for per-partition automatic failover.
195
+ * A request is eligible if it is a write request, partition level failover is enabled,
196
+ * and the global endpoint manager cannot use multiple write locations for the request.
197
+ */
198
+ isRequestEligibleForPerPartitionAutomaticFailover(requestContext) {
199
+ return (this.enablePartitionLevelFailover &&
200
+ !(0, index_js_1.isReadRequest)(requestContext.operationType) &&
201
+ !this.globalEndpointManager.canUseMultipleWriteLocations(requestContext.resourceType, requestContext.operationType));
202
+ }
203
+ /**
204
+ * Determines if a request is eligible for partition-level circuit breaker.
205
+ * This method checks if partition-level circuit breaker is enabled, and if the request is a read-only request or
206
+ * the global endpoint manager can use multiple write locations for the request.
207
+ */
208
+ isRequestEligibleForPartitionLevelCircuitBreaker(requestContext) {
209
+ if (!this.enablePartitionLevelCircuitBreaker) {
210
+ return false;
211
+ }
212
+ if ((0, index_js_1.isReadRequest)(requestContext.operationType)) {
213
+ return true;
214
+ }
215
+ return this.globalEndpointManager.canUseMultipleWriteLocations(requestContext.resourceType, requestContext.operationType);
216
+ }
217
+ /**
218
+ * Attempts to add or update the partition failover information and move to the next available location.
219
+ * This method checks if the current location for the partition key range has failed and updates the failover
220
+ * information to route the request to the next available location. If all locations have been tried, it removes
221
+ * the failover information for the partition key range. Return True if the failover information was successfully
222
+ * updated and the request was routed to a new location, otherwise false.
223
+ */
224
+ async tryAddOrUpdatePartitionFailoverInfoAndMoveToNextLocation(partitionKeyRangeId, failedEndPoint, nextEndPoints, partitionKeyRangeToLocation, diagnosticNode) {
225
+ if (!partitionKeyRangeToLocation.has(partitionKeyRangeId)) {
226
+ // If the partition key range is not already in the map, add it
227
+ const failoverInfo = new PartitionKeyRangeFailoverInfo_js_1.PartitionKeyRangeFailoverInfo(failedEndPoint);
228
+ partitionKeyRangeToLocation.set(partitionKeyRangeId, failoverInfo);
229
+ }
230
+ const partitionFailOver = partitionKeyRangeToLocation.get(partitionKeyRangeId);
231
+ // Will return true if it was able to update to a new region
232
+ if (await partitionFailOver.tryMoveNextLocation(nextEndPoints, failedEndPoint, diagnosticNode, partitionKeyRangeId)) {
233
+ return true;
234
+ }
235
+ // All the locations have been tried. Remove the override information
236
+ partitionKeyRangeToLocation.delete(partitionKeyRangeId);
237
+ return false;
238
+ }
239
+ /**
240
+ * Initiates a background loop that periodically checks for unhealthy endpoints
241
+ * and attempts to open connections to them. If a connection is successfully
242
+ * established, it initiates a failback to the original location for the partition key range.
243
+ * This is useful for scenarios where a partition key range has been marked as unavailable
244
+ * due to a circuit breaker, and we want to periodically check if the original location
245
+ * has become healthy again.
246
+ * The loop runs at a defined interval specified by Constants.StalePartitionUnavailabilityRefreshIntervalInMs.
247
+ */
248
+ initiateCircuitBreakerFailbackLoop() {
249
+ this.circuitBreakerFailbackBackgroundRefresher = setInterval(() => {
250
+ (async () => {
251
+ try {
252
+ await this.openConnectionToUnhealthyEndpointsWithFailback();
253
+ }
254
+ catch (err) {
255
+ console.error("Failed to open connection to unhealthy endpoints: ", err);
256
+ }
257
+ })();
258
+ }, index_js_2.Constants.StalePartitionUnavailabilityRefreshIntervalInMs);
259
+ }
260
+ /**
261
+ * Attempts to open connections to unhealthy endpoints and initiates failback if the connections are successful.
262
+ * This method checks the partition key ranges that have failed locations and tries to re-establish connections
263
+ * to those locations. If a connection is successfully re-established, it initiates a failback to the original
264
+ * location for the partition key range.
265
+ */
266
+ async openConnectionToUnhealthyEndpointsWithFailback() {
267
+ for (const pkRange of this.partitionKeyRangeToLocationForReadAndWrite.keys()) {
268
+ const partitionFailover = this.partitionKeyRangeToLocationForReadAndWrite.get(pkRange);
269
+ if (!partitionFailover)
270
+ continue;
271
+ const { firstRequestFailureTime } = await partitionFailover.snapshotPartitionFailoverTimestamps();
272
+ const now = new Date();
273
+ if (now.getTime() - firstRequestFailureTime >
274
+ index_js_2.Constants.AllowedPartitionUnavailabilityDurationInMs) {
275
+ // Un-deterministically marking the original failed endpoint for the PkRange back to healthy.
276
+ // Initiate Failback to the original failed location.
277
+ this.partitionKeyRangeToLocationForReadAndWrite.delete(pkRange);
278
+ }
279
+ }
280
+ }
281
+ }
282
+ exports.GlobalPartitionEndpointManager = GlobalPartitionEndpointManager;
283
+ //# sourceMappingURL=globalPartitionEndpointManager.js.map