-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (147 loc) · 6.19 KB
/
Program.cs
File metadata and controls
177 lines (147 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace CosmosMongoDBReadWriteSample
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
class Program
{
private static MongoClient srcMongoClient;
private static string srcDbName = ConfigurationManager.AppSettings["srcDbName"];
private static string srcCollectionName = ConfigurationManager.AppSettings["srcCollectionName"];
private static IMongoDatabase srcDatabase;
private static IMongoCollection<BsonDocument> srcDocStoreCollection;
private static MongoClient destMongoClient;
private static string destDbName = ConfigurationManager.AppSettings["destDbName"];
private static string destCollectionName = ConfigurationManager.AppSettings["destCollectionName"];
private static IMongoDatabase destDatabase;
private static IMongoCollection<BsonDocument> destDocStoreCollection;
private static List<BsonDocument> insertFailedDocs = new List<BsonDocument>();
private static int insertRetries = Int32.Parse(ConfigurationManager.AppSettings["insertRetries"]);
private static int minWait = 1500;
private static int maxWait = 3000;
private static long docsCount = 0;
static void Main(string[] args)
{
string srcConnectionString =
ConfigurationManager.AppSettings["src-conn"];
MongoClientSettings srcSettings = MongoClientSettings.FromUrl(
new MongoUrl(srcConnectionString)
);
srcMongoClient = new MongoClient(srcSettings);
srcDatabase = srcMongoClient.GetDatabase(srcDbName);
srcDocStoreCollection = srcDatabase.GetCollection<BsonDocument>(srcCollectionName);
string destConnectionString =
ConfigurationManager.AppSettings["dest-conn"];
MongoClientSettings destSettings = MongoClientSettings.FromUrl(
new MongoUrl(destConnectionString)
);
destMongoClient = new MongoClient(destSettings);
destDatabase = destMongoClient.GetDatabase(destDbName);
destDocStoreCollection = destDatabase.GetCollection<BsonDocument>(destCollectionName);
ExportDocuments().Wait();
if (insertFailedDocs.Any())
{
using (var sw = new StreamWriter(ConfigurationManager.AppSettings["failedDocsPath"]))
{
foreach (var doc in insertFailedDocs)
{
sw.WriteLine(doc.ToJson());
}
}
Console.WriteLine("Not all documents were exported, failed documents located @: {0}", ConfigurationManager.AppSettings["failedDocsPath"]);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
private static async Task InsertAllDocuments(IEnumerable<BsonDocument> docs)
{
var tasks = new List<Task>();
for (int j = 0; j < docs.Count(); j++)
{
tasks.Add(InsertDocument(docs.ToList()[j]));
}
await Task.WhenAll(tasks).ConfigureAwait(false);
docsCount = docsCount + docs.Count();
Console.WriteLine("Total documents copied so far: {0}",docsCount);
}
private static async Task ExportDocuments()
{
FilterDefinition<BsonDocument> filter = FilterDefinition<BsonDocument>.Empty;
FindOptions<BsonDocument> options = new FindOptions<BsonDocument>
{
BatchSize = Int32.Parse(ConfigurationManager.AppSettings["batchsize"]),
NoCursorTimeout = false
};
using (IAsyncCursor<BsonDocument> cursor = await srcDocStoreCollection.FindAsync(filter,options))
{
bool isSucceed = false;
while (!isSucceed)
{
try
{
while (await cursor.MoveNextAsync())
{
IEnumerable<BsonDocument> batch = cursor.Current;
await InsertAllDocuments(batch);
}
}
catch (Exception ex)
{
if (!IsThrottled(ex))
{
Console.WriteLine("ERROR: With collection {0}", ex.ToString());
throw;
}
else
{
// Thread will wait in between 1.5 secs and 3 secs.
System.Threading.Thread.Sleep(new Random().Next(minWait, maxWait));
}
}
isSucceed = true;
break;
}
}
}
private static async Task InsertDocument(BsonDocument doc)
{
bool isSucceed = false;
for (int i = 0; i < insertRetries; i++)
{
try
{
await destDocStoreCollection.InsertOneAsync(doc);
isSucceed = true;
//Operation succeed just break the loop
break;
}
catch (Exception ex)
{
if (!IsThrottled(ex))
{
Console.WriteLine("ERROR: With collection {0}", ex.ToString());
throw;
}
else
{
// Thread will wait in between 1.5 secs and 3 secs.
System.Threading.Thread.Sleep(new Random().Next(minWait, maxWait));
}
}
}
if (!isSucceed)
{
insertFailedDocs.Add(doc);
}
}
private static bool IsThrottled(Exception ex)
{
return ex.Message.ToLower().Contains("Request rate is large".ToLower());
}
}
}