Skip to content

Instantly share code, notes, and snippets.

@jasonmimick
Created March 26, 2018 19:47
Show Gist options
  • Save jasonmimick/4135e658e4bbed755b68b8b9ee9ce2c8 to your computer and use it in GitHub Desktop.
Save jasonmimick/4135e658e4bbed755b68b8b9ee9ce2c8 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;
namespace retry
{
class Program
{
private static Random _random = new Random();
static string randomString(int length)
{
string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder result = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
//System.Console.WriteLine("i="+i+" _ranom="+_random.Next(characters.Length));
result.Append(characters[_random.Next(0, characters.Length)]);
}
return result.ToString();
}
static String randomText()
{
var sizeText = (_random).Next(1, 1000);
return randomString(sizeText);
}
static BsonArray randomTags()
{
var numTags = (_random).Next(1, 20);
BsonArray tags = new BsonArray();
for (var i = 0; i < numTags; i++)
{
tags.Add(randomString(10));
}
return tags;
}
static String randomName()
{
return randomString(15) + ", " + randomString(10);
}
public static BsonDocument getArticle(int i, int j)
{
var a = new BsonDocument() {
{ "_id", i+":"+j },
{ "author", randomName() },
{ "title", "Article #"+i },
{ "text", randomText() },
{ "tags", randomTags() }
};
return a;
}
public static void Main(string[] args)
{
var numDocs = Int32.Parse(args[0]);
var sleepMillis = 500;
var batchSize = 10;
var primaryPause = 30; // pause 30 seconds when primary shutdown
var connstring = "mongodb://localhost:28000,localhost:28001,localhost:28002/test?replicaSet=testrs&retryWrites=true";
var client = new MongoClient(connstring);
var db = client.GetDatabase("test");
// Get an article collection
db.DropCollection("articles");
var articles = db.GetCollection<BsonDocument>("articles");
var batchCount = 0;
for (var i = 0; i < numDocs; i++)
{
System.Threading.Thread.Sleep(sleepMillis);
var models = new WriteModel<BsonDocument>[batchSize];
for (var j = 0; j < batchSize; j++)
{
System.Threading.Thread.Sleep(10);
var a = getArticle(i, j);
models[j] = new InsertOneModel<BsonDocument>(a);
}
batchCount++;
System.Console.WriteLine("Writing batch #" + i);
try
{
articles.BulkWrite(models);
}
catch (MongoCommandException e)
{
System.Console.WriteLine("Got MongoCommandException --- ");
System.Console.WriteLine(e);
if (e.Message.Contains("interrupted at shutdown"))
{
System.Console.WriteLine("***** primary was shutdown! *****");
System.Console.WriteLine("***** pausing "+primaryPause+" seconds for new primary to be elected. *****");
for (var k=0; k<primaryPause;k++) {
System.Threading.Thread.Sleep(1000); // wait 30 seconds for new primary
System.Console.WriteLine( (primaryPause-k)+" - ");
}
System.Console.WriteLine(" DONE. Retrying write. *****");
try
{
articles.BulkWrite(models);
System.Console.WriteLine("***** retry was successful! *****");
}
catch (Exception ee)
{
Console.WriteLine("***** retry FAILED *****");
Console.WriteLine(ee);
}
}
}
catch (Exception e)
{
Console.WriteLine("Got --- Exception --- ");
Console.WriteLine(e);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment