4343import com .azure .cosmos .models .FeedResponse ;
4444import com .azure .cosmos .models .ModelBridgeInternal ;
4545import com .azure .cosmos .models .PartitionKey ;
46+ import com .azure .cosmos .models .SqlParameter ;
4647import com .azure .cosmos .models .SqlQuerySpec ;
4748import com .azure .cosmos .models .ThroughputProperties ;
4849import com .azure .cosmos .models .ThroughputResponse ;
4950import com .azure .cosmos .util .Beta ;
5051import com .azure .cosmos .util .CosmosPagedFlux ;
5152import com .azure .cosmos .util .UtilBridgeInternal ;
53+ import com .fasterxml .jackson .databind .node .ObjectNode ;
54+ import org .slf4j .Logger ;
55+ import org .slf4j .LoggerFactory ;
5256import reactor .core .publisher .Flux ;
5357import reactor .core .publisher .Mono ;
5458
59+ import java .util .ArrayList ;
60+ import java .util .Collections ;
5561import java .util .List ;
62+ import java .util .UUID ;
63+ import java .util .concurrent .atomic .AtomicBoolean ;
5664import java .util .function .Function ;
5765import java .util .stream .Collectors ;
5866
6775 */
6876public class CosmosAsyncContainer {
6977
78+ private final static Logger logger = LoggerFactory .getLogger (CosmosAsyncContainer .class );
79+
7080 private final CosmosAsyncDatabase database ;
7181 private final String id ;
7282 private final String link ;
@@ -87,6 +97,7 @@ public class CosmosAsyncContainer {
8797 private final String readAllConflictsSpanName ;
8898 private final String queryConflictsSpanName ;
8999 private final String batchSpanName ;
100+ private final AtomicBoolean isInitialized ;
90101 private CosmosAsyncScripts scripts ;
91102
92103 CosmosAsyncContainer (String id , CosmosAsyncDatabase database ) {
@@ -110,6 +121,7 @@ public class CosmosAsyncContainer {
110121 this .readAllConflictsSpanName = "readAllConflicts." + this .id ;
111122 this .queryConflictsSpanName = "queryConflicts." + this .id ;
112123 this .batchSpanName = "transactionalBatch." + this .id ;
124+ this .isInitialized = new AtomicBoolean (false );
113125 }
114126
115127 /**
@@ -413,6 +425,47 @@ public <T> CosmosPagedFlux<T> queryItems(String query, Class<T> classType) {
413425 return queryItemsInternal (new SqlQuerySpec (query ), new CosmosQueryRequestOptions (), classType );
414426 }
415427
428+ /**
429+ * Initializes the container by warming up the caches and connections for the current read region.
430+ *
431+ * <p><br>The execution of this method is expected to result in some RU charges to your account.
432+ * The number of RU consumed by this request varies, depending on data consistency, size of the overall data in the container,
433+ * item indexing, number of projections. For more information regarding RU considerations please visit
434+ * <a href="https://docs.microsoft.com/en-us/azure/cosmos-db/request-units#request-unit-considerations">https://docs.microsoft.com/en-us/azure/cosmos-db/request-units#request-unit-considerations</a>.
435+ * </p>
436+ *
437+ * <p>
438+ * <br>NOTE: This API ideally should be called only once during application initialization before any workload.
439+ * <br>In case of any transient error, caller should consume the error and continue the regular workload.
440+ * </p>
441+ *
442+ * @return Mono of Void
443+ */
444+ @ Beta (value = Beta .SinceVersion .V4_14_0 , warningText = Beta .PREVIEW_SUBJECT_TO_CHANGE_WARNING )
445+ public Mono <Void > openConnectionsAndInitCaches () {
446+ if (isInitialized .compareAndSet (false , true )) {
447+ return this .getFeedRanges ().flatMap (feedRanges -> {
448+ List <Flux <FeedResponse <ObjectNode >>> fluxList = new ArrayList <>();
449+ SqlQuerySpec querySpec = new SqlQuerySpec ();
450+ querySpec .setQueryText ("select * from c where c.id = @id" );
451+ querySpec .setParameters (Collections .singletonList (new SqlParameter ("@id" ,
452+ UUID .randomUUID ().toString ())));
453+ for (FeedRange feedRange : feedRanges ) {
454+ CosmosQueryRequestOptions options = new CosmosQueryRequestOptions ();
455+ options .setFeedRange (feedRange );
456+ CosmosPagedFlux <ObjectNode > cosmosPagedFlux = this .queryItems (querySpec , options ,
457+ ObjectNode .class );
458+ fluxList .add (cosmosPagedFlux .byPage ());
459+ }
460+ Mono <List <FeedResponse <ObjectNode >>> listMono = Flux .merge (fluxList ).collectList ();
461+ return listMono .flatMap (objects -> Mono .empty ());
462+ });
463+ } else {
464+ logger .warn ("openConnectionsAndInitCaches is already called once on Container {}, no operation will take place in this call" , this .getId ());
465+ return Mono .empty ();
466+ }
467+ }
468+
416469 /**
417470 * Query for items in the current container using a string.
418471 * <p>
0 commit comments