@@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed
66{
77 using System ;
88 using System . Collections . Generic ;
9+ using System . Text . Json ;
910 using System . Threading ;
1011 using System . Threading . Tasks ;
1112 using Microsoft . Azure . Cosmos . ChangeFeed . Bootstrapping ;
@@ -101,44 +102,37 @@ public override async Task StopAsync()
101102 }
102103 }
103104
104- public override async Task < IReadOnlyList < LeaseExportData > > ExportLeasesAsync ( CancellationToken cancellationToken = default )
105+ public override async Task < IReadOnlyList < JsonElement > > ExportLeasesAsync ( CancellationToken cancellationToken = default )
105106 {
106- // Wait for any ongoing start/stop operations to complete
107- // If processor is running, this will wait until StopAsync is called by the user
108- await this . runningLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
109- try
107+ // Initialize if needed to access the lease container
108+ if ( ! this . initialized )
110109 {
111- if ( this . isRunning )
110+ await this . runningLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
111+ try
112112 {
113- // Release lock and throw - user must stop the processor first
114- throw new InvalidOperationException (
115- "Cannot export leases while the ChangeFeedProcessor is running. " +
116- "Please call StopAsync() before exporting leases." ) ;
113+ if ( ! this . initialized )
114+ {
115+ await this . InitializeAsync ( ) . ConfigureAwait ( false ) ;
116+ }
117117 }
118-
119- // Initialize if needed to access the lease container
120- if ( ! this . initialized )
118+ finally
121119 {
122- await this . InitializeAsync ( ) . ConfigureAwait ( false ) ;
120+ this . runningLock . Release ( ) ;
123121 }
122+ }
124123
125- DefaultTrace . TraceInformation ( "Exporting leases..." ) ;
126- IReadOnlyList < LeaseExportData > exportedLeases = await this . documentServiceLeaseStoreManager
127- . LeaseContainer
128- . ExportLeasesAsync ( this . instanceName , cancellationToken )
129- . ConfigureAwait ( false ) ;
124+ DefaultTrace . TraceInformation ( "Exporting leases..." ) ;
125+ IReadOnlyList < JsonElement > exportedLeases = await this . documentServiceLeaseStoreManager
126+ . LeaseContainer
127+ . ExportLeasesAsync ( cancellationToken )
128+ . ConfigureAwait ( false ) ;
130129
131- DefaultTrace . TraceInformation ( "Exported {0} leases." , exportedLeases . Count ) ;
132- return exportedLeases ;
133- }
134- finally
135- {
136- this . runningLock . Release ( ) ;
137- }
130+ DefaultTrace . TraceInformation ( "Exported {0} leases." , exportedLeases . Count ) ;
131+ return exportedLeases ;
138132 }
139133
140134 public override async Task ImportLeasesAsync (
141- IReadOnlyList < LeaseExportData > leases ,
135+ IReadOnlyList < JsonElement > leases ,
142136 bool overwriteExisting = false ,
143137 CancellationToken cancellationToken = default )
144138 {
@@ -147,36 +141,30 @@ public override async Task ImportLeasesAsync(
147141 throw new ArgumentNullException ( nameof ( leases ) ) ;
148142 }
149143
150- // Wait for any ongoing start/stop operations to complete
151- await this . runningLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
152- try
144+ // Initialize if needed to access the lease container
145+ if ( ! this . initialized )
153146 {
154- if ( this . isRunning )
147+ await this . runningLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
148+ try
155149 {
156- // Release lock and throw - user must stop the processor first
157- throw new InvalidOperationException (
158- "Cannot import leases while the ChangeFeedProcessor is running. " +
159- "Please call StopAsync() before importing leases." ) ;
150+ if ( ! this . initialized )
151+ {
152+ await this . InitializeAsync ( ) . ConfigureAwait ( false ) ;
153+ }
160154 }
161-
162- // Initialize if needed to access the lease container
163- if ( ! this . initialized )
155+ finally
164156 {
165- await this . InitializeAsync ( ) . ConfigureAwait ( false ) ;
157+ this . runningLock . Release ( ) ;
166158 }
159+ }
167160
168- DefaultTrace . TraceInformation ( "Importing {0} leases (overwriteExisting={1})..." , leases . Count , overwriteExisting ) ;
169- await this . documentServiceLeaseStoreManager
170- . LeaseContainer
171- . ImportLeasesAsync ( leases , this . instanceName , overwriteExisting , cancellationToken )
172- . ConfigureAwait ( false ) ;
161+ DefaultTrace . TraceInformation ( "Importing {0} leases (overwriteExisting={1})..." , leases . Count , overwriteExisting ) ;
162+ await this . documentServiceLeaseStoreManager
163+ . LeaseContainer
164+ . ImportLeasesAsync ( leases , overwriteExisting , cancellationToken )
165+ . ConfigureAwait ( false ) ;
173166
174- DefaultTrace . TraceInformation ( "Imported {0} leases." , leases . Count ) ;
175- }
176- finally
177- {
178- this . runningLock . Release ( ) ;
179- }
167+ DefaultTrace . TraceInformation ( "Imported {0} leases." , leases . Count ) ;
180168 }
181169
182170 private async Task InitializeAsync ( )
0 commit comments