Skip to content

Commit c147c12

Browse files
committed
added comments and minor changes
1 parent 8e5fe05 commit c147c12

File tree

5 files changed

+118
-48
lines changed

5 files changed

+118
-48
lines changed

samples/ChangeFeedMigrationTool/App.config

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
55
</startup>
66
<appSettings>
7-
<add key="monitoredUri" value="monitoredURI" />
8-
<add key="monitoredSecretKey" value="monitoredKey" />
7+
<add key="monitoredUri" value="https://yourmonitoredservice.documents.azure.com" />
8+
<add key="monitoredSecretKey" value="monitoredKeyNzfQ1dkiIyxfhVMQAQLzQQzg==" />
99
<add key="monitoredDbName" value="monitoredDbName" />
1010
<add key="monitoredCollectionName" value="monitoredColl" />
1111
<add key="monitoredThroughput" value="400" />
12-
<add key="leaseUri" value="leaseURI" />
13-
<add key="leaseSecretKey" value="leaseKey" />
12+
<add key="leaseUri" value="https://yourleaseservice.documents.azure.com" />
13+
<add key="leaseSecretKey" value="leaseKeyNzfQ1dkiIyxfhVMQAQLzQQzg==" />
1414
<add key="leaseDbName" value="leaseDbName" />
1515
<add key="leaseCollectionName" value="leaseColl" />
1616
<add key="leaseThroughput" value="400" />
17-
<add key="destUri" value="destURI" />
18-
<add key="destSecretKey" value="destKey" />
17+
<add key="destUri" value="https://yourdestinationservice.documents.azure.com" />
18+
<add key="destSecretKey" value="destKeyNzfQ1dkiIyxfhVMQAQLzQQzg==" />
1919
<add key="destDbName" value="destDbName" />
2020
<add key="destCollectionName" value="destColl" />
2121
<add key="destThroughput" value="400" />

samples/ChangeFeedMigrationTool/DocumentFeedObserver.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,64 @@ namespace ChangeFeedMigrationSample
2222
using Microsoft.Azure.Documents.Client;
2323

2424
/// <summary>
25-
/// Cass to create instance of document feed observer.
25+
/// This class implements the IChangeFeedObserver interface and is used to observe
26+
/// changes on change feed. ChangeFeedEventHost will create as many instances of
27+
/// this class as needed.
2628
/// </summary>
2729
public class DocumentFeedObserver : IChangeFeedObserver
2830
{
29-
private static int s_totalDocs = 0;
30-
private DocumentCollectionInfo collectionInfo;
31+
private static int totalDocs = 0;
3132
private DocumentClient client;
3233
private Uri destinationCollectionUri;
3334

35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="DocumentFeedObserver" /> class.
37+
/// Saves input DocumentClient and DocumentCollectionInfo parameters to class fields
38+
/// </summary>
39+
/// <param name="client"> Client connected to destination collection </param>
40+
/// <param name="destCollInfo"> Destination collection information </param>
3441
public DocumentFeedObserver(DocumentClient client, DocumentCollectionInfo destCollInfo)
3542
{
3643
this.client = client;
37-
this.collectionInfo = destCollInfo;
38-
this.destinationCollectionUri = UriFactory.CreateDocumentCollectionUri(this.collectionInfo.DatabaseName, this.collectionInfo.CollectionName);
44+
this.destinationCollectionUri = UriFactory.CreateDocumentCollectionUri(destCollInfo.DatabaseName, destCollInfo.CollectionName);
3945
}
4046

47+
/// <summary>
48+
/// Called when change feed observer is opened;
49+
/// this function prints out observer partition key id.
50+
/// </summary>
51+
/// <param name="context">The context specifying partition for this observer, etc.</param>
52+
/// <returns>A Task to allow asynchronous execution</returns>
4153
public Task OpenAsync(ChangeFeedObserverContext context)
4254
{
43-
Console.WriteLine("Worker opened, {0}", context.PartitionKeyRangeId);
55+
Console.WriteLine("Observer opened, {0}", context.PartitionKeyRangeId);
4456
return Task.CompletedTask;
4557
}
4658

59+
/// <summary>
60+
/// Called when change feed observer is closed;
61+
/// this function prints out observer partition key id and reason for shut down.
62+
/// </summary>
63+
/// <param name="context">The context specifying partition for this observer, etc.</param>
64+
/// <param name="reason">Specifies the reason the observer is closed.</param>
65+
/// <returns>A Task to allow asynchronous execution</returns>
4766
public Task CloseAsync(ChangeFeedObserverContext context, ChangeFeedObserverCloseReason reason)
4867
{
49-
Console.WriteLine("Worker closed, {0}", context.PartitionKeyRangeId);
68+
Console.WriteLine("Observer closed, {0}", context.PartitionKeyRangeId);
5069
Console.WriteLine("Reason for shutdown, {0}", reason);
5170
return Task.CompletedTask;
5271
}
5372

73+
/// <summary>
74+
/// When document changes are available on change feed, changes are copied to destination connection;
75+
/// this function prints out the changed document ID.
76+
/// </summary>
77+
/// <param name="context">The context specifying partition for this observer, etc.</param>
78+
/// <param name="docs">The documents changed.</param>
79+
/// <returns>A Task to allow asynchronous execution</returns>
5480
public Task ProcessChangesAsync(ChangeFeedObserverContext context, IReadOnlyList<Document> docs)
5581
{
56-
Console.WriteLine("Change feed: total {0} doc(s)", Interlocked.Add(ref s_totalDocs, docs.Count));
82+
Console.WriteLine("Change feed: total {0} doc(s)", Interlocked.Add(ref totalDocs, docs.Count));
5783
foreach (Document doc in docs)
5884
{
5985
Console.WriteLine(doc.Id.ToString());

samples/ChangeFeedMigrationTool/DocumentFeedObserverFactory.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ public class DocumentFeedObserverFactory : IChangeFeedObserverFactory
2424
private DocumentClient client;
2525
private DocumentCollectionInfo collectionInfo;
2626

27-
public DocumentFeedObserverFactory(DocumentCollectionInfo destCollInfo, DocumentClient destClient)
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="DocumentFeedObserverFactory" /> class.
29+
/// Saves input DocumentClient and DocumentCollectionInfo parameters to class fields
30+
/// </summary>
31+
/// <param name="destClient">Client connected to destination collection</param>
32+
/// <param name="destCollInfo">Destination collection information</param>
33+
public DocumentFeedObserverFactory(DocumentClient destClient, DocumentCollectionInfo destCollInfo)
2834
{
2935
this.collectionInfo = destCollInfo;
3036
this.client = destClient;
3137
}
3238

39+
/// <summary>
40+
/// Creates document observer instance with client and destination collection information
41+
/// </summary>
42+
/// <returns>DocumentFeedObserver with client and destination collection information</returns>
3343
public IChangeFeedObserver CreateObserver()
3444
{
3545
DocumentFeedObserver newObserver = new DocumentFeedObserver(this.client, this.collectionInfo);

samples/ChangeFeedMigrationTool/Program.cs

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace ChangeFeedMigrationSample
2121
using Microsoft.Azure.Documents.Client;
2222

2323
/// ------------------------------------------------------------------------------------------------
24-
/// This sample demonstrates using change processor library to read changes from source collection
25-
/// to destination collection
24+
/// <summary> This sample demonstrates using change processor library to read changes from source collection
25+
/// to destination collection </summary>
2626
/// ------------------------------------------------------------------------------------------------
2727
public class Program
2828
{
@@ -50,55 +50,83 @@ public class Program
5050
private string destCollectionName = ConfigurationManager.AppSettings["destCollectionName"];
5151
private int destThroughput = int.Parse(ConfigurationManager.AppSettings["destThroughput"]);
5252

53+
/// <summary>
54+
/// Main program function; called when program runs
55+
/// </summary>
56+
/// <param name="args">Command line parameters (not used)</param>
5357
public static void Main(string[] args)
5458
{
5559
Console.WriteLine("Change Feed Migration Sample");
5660
Program newApp = new Program();
61+
newApp.MainAsync().Wait();
62+
}
5763

58-
// create collections
59-
newApp.CreateCollectionAsync(
60-
newApp.monitoredUri,
61-
newApp.monitoredSecretKey,
62-
newApp.monitoredDbName,
63-
newApp.monitoredCollectionName,
64-
newApp.monitoredThroughput).Wait();
65-
66-
newApp.CreateCollectionAsync(
67-
newApp.leaseUri,
68-
newApp.leaseSecretKey,
69-
newApp.leaseDbName,
70-
newApp.leaseCollectionName,
71-
newApp.leaseThroughput).Wait();
72-
73-
newApp.CreateCollectionAsync(
74-
newApp.destUri,
75-
newApp.destSecretKey,
76-
newApp.destDbName,
77-
newApp.destCollectionName,
78-
newApp.destThroughput).Wait();
79-
80-
// run change feed processor
81-
newApp.RunChangeFeedHostAsync().Wait();
64+
/// <summary>
65+
/// Main Async function; checks for or creates monitored/lease collections and runs
66+
/// Change Feed Host (RunChangeFeedHostAsync)
67+
/// </summary>
68+
/// <returns>A Task to allow asynchronous execution</returns>
69+
public async Task MainAsync()
70+
{
71+
await this.CreateCollectionIfNotExistsAsync(
72+
this.monitoredUri,
73+
this.monitoredSecretKey,
74+
this.monitoredDbName,
75+
this.monitoredCollectionName,
76+
this.monitoredThroughput);
77+
78+
await this.CreateCollectionIfNotExistsAsync(
79+
this.leaseUri,
80+
this.leaseSecretKey,
81+
this.leaseDbName,
82+
this.leaseCollectionName,
83+
this.leaseThroughput);
84+
85+
await this.CreateCollectionIfNotExistsAsync(
86+
this.destUri,
87+
this.destSecretKey,
88+
this.destDbName,
89+
this.destCollectionName,
90+
this.destThroughput);
91+
92+
await this.RunChangeFeedHostAsync();
8293
}
8394

84-
public async Task CreateCollectionAsync(string newUri, string secretKey, string dbName, string collectionName, int throughput)
95+
/// <summary>
96+
/// Checks whether collections exists. Creates new collection if collection does not exist
97+
/// WARNING: CreateCollectionIfNotExistsAsync will create a new
98+
/// with reserved throughput which has pricing implications. For details
99+
/// visit: https://azure.microsoft.com/en-us/pricing/details/cosmos-db/
100+
/// </summary>
101+
/// <param name="endPointUri">End point URI for account </param>
102+
/// <param name="secretKey">Primary key to access the account </param>
103+
/// <param name="databaseName">Name of database </param>
104+
/// <param name="collectionName">Name of collection</param>
105+
/// <param name="throughput">Amount of throughput to provision</param>
106+
/// <returns>A Task to allow asynchronous execution</returns>
107+
public async Task CreateCollectionIfNotExistsAsync(string endPointUri, string secretKey, string databaseName, string collectionName, int throughput)
85108
{
86109
// connecting client
87-
using (DocumentClient client = new DocumentClient(new Uri(newUri), secretKey))
110+
using (DocumentClient client = new DocumentClient(new Uri(endPointUri), secretKey))
88111
{
89-
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = dbName });
112+
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName });
90113

91-
// create monitor collection if it does not exist
114+
// create collection if it does not exist
92115
// WARNING: CreateDocumentCollectionIfNotExistsAsync will create a new
93-
// with reserved through pul which has pricing implications. For details
116+
// with reserved throughput which has pricing implications. For details
94117
// visit: https://azure.microsoft.com/en-us/pricing/details/cosmos-db/
95118
await client.CreateDocumentCollectionIfNotExistsAsync(
96-
UriFactory.CreateDatabaseUri(dbName),
119+
UriFactory.CreateDatabaseUri(databaseName),
97120
new DocumentCollection { Id = collectionName },
98121
new RequestOptions { OfferThroughput = throughput });
99122
}
100123
}
101124

125+
/// <summary>
126+
/// Registers change feed observer to update changes read on change feed to destination
127+
/// collection. Deregisters change feed observer and closes process when enter key is pressed
128+
/// </summary>
129+
/// <returns>A Task to allow asynchronous execution</returns>
102130
public async Task RunChangeFeedHostAsync()
103131
{
104132
string hostName = Guid.NewGuid().ToString();
@@ -145,7 +173,7 @@ public async Task RunChangeFeedHostAsync()
145173

146174
using (DocumentClient destClient = new DocumentClient(destCollInfo.Uri, destCollInfo.MasterKey))
147175
{
148-
DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(destCollInfo, destClient);
176+
DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(destClient, destCollInfo);
149177

150178
ChangeFeedEventHost host = new ChangeFeedEventHost(hostName, documentCollectionLocation, leaseCollectionLocation, feedOptions, feedHostOptions);
151179

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Azure.DocumentDB" version="1.14.1" targetFramework="net47" />
4+
<package id="Microsoft.Azure.DocumentDB.ChangeFeedProcessor" version="1.0.0" targetFramework="net47" />
5+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" />
6+
</packages>

0 commit comments

Comments
 (0)