Monday 26 October 2015

Register/Deregister EC2 Servers under ELB using AWS .NET Toolkit

I have been working with AWS toolkit for some time in last week. I prepared some tools to automate our infra team tasks and for our internal development and testing purpose.

Here I will show you how to register/de-register EC2 instances with Elastic Load Balancer programmatically using AWS .NET toolkit.

First get your awsSecretAccessKey and awsAccessKey,


1
2
 string awsSecretAccessKey = ConfigurationManager.AppSettings["awsSecretAccessKey"];
 string awsAccessKey = ConfigurationManager.AppSettings["awsAccessKey"];

Note: The user account you use here for Secret Key and Access Key must have full access to EC2 service of AWS

Create EC2 Client,


1
 var client = AWSClientFactory.CreateAmazonElasticLoadBalancingClient(awsAccessKey, awsSecretAccessKey, RegionEndpoint.USWest2);

Make sure you specify correct RegionEndPoint where your EC2 instances exist. Here I have used USWest2 region end point.

Thursday 20 August 2015

Automate Cloudfront Cache Invalidation Using AWS .NET Toolkit

Amazon CloudFront is a content delivery web service to give developers and businesses an easy way to distribute content to end users with low latency and high data transfer speeds.

Amazon Cludfront has a number of edge locations worldwide, when your application target audience is scattered worldwide then Amazon Cloudfront is a good choice for caching your files and contents to give high throughput and low latency. Let say, if someone is accessing your application from east zone of USA then your contents will be delivered by one of the cloudfront edge servers located in USEast.

Your files which got cached on cloudfront edge locations will be automatically invalidated as per the default Cache Timeout set in request header. But sometimes you may forcefully want to clear files from Cloudfront cache prior to the actual Cache Timeout.

Wednesday 1 July 2015

Sitecore : Datetime Field with Timezone

Working with Date Time and Timezones is always headache. Recently we faced issue with calendar events generated by our sitecore application in different regions. The start date and end date in generated ICS file was failing to show exact date time specific to end user because of the difference in timezones.

Why that happened?

Curretly there is no mechanism in sitecore to store date and time in different timezones. It simply considers the server timezone - on which the sitecore instance runs - and stores date time accordingly.  So, to make datetime field independent of any specific timezone (more specifically - server timezone) , we needed a custom field that stores UTC (Universal Coordinated Time) , a globally unique datetime, as raw value and presents timezone specific date and time to the content authors.

Note: We can easily do conversion of UTC to any timezone and visa versa, with .NET globalization API


How this prototype works?

Here is how the new field looks to content authors.


This is what sitecore stores in backend.


You can notice the raw value is the UTC equivalent of the IST time content authors can see. And this is the secret of this field :)

You might be wondering from where the the timezone India Standard Time is coming??
 -  In our application, we have separated all settings from actual content items, and those settings are globally applicable to entire site (something like AppSettings in .net application)

-  So here the timezone IST is coming from site level settings, which looks like this



-  Based on above selection, only the display date and time will change, but the raw value will remain same.

Friday 19 June 2015

Transfer Sitecore User Accounts with Passwords from One Instance to Another

Sometimes you need to transfer sitecore users from one instance to another instance having different databases.

For this, you can serialize your users from source instance and then deserialize them on destination instance.

Steps to perform this task

1.  Serialize Users on Source Instance :
 To serialize users, open Content Editor - Security - User Manager





Thursday 7 May 2015

MongoDB M101N Final Exam Question 10

Question 10:

We perform the following query on the enron dataset:

var exp = db.messages.explain('executionStats')

exp.find( { 'headers.Date' : { '$gt' : new Date(2001,3,1) } }, { 'headers.From' : 1, '_id' : 0 } ).sort( { 'headers.From' : 1 } )


and get the following explain output.  

MongoDB M101N Final Exam Question 9

Question 9:

Imagine an electronic medical record database designed to hold the medical records of every individual in the United States. Because each person has more than 16MB of medical history and records, it's not feasible to have a single document for every patient. Instead, there is a patient collection that contains basic information on each person and maps the person to a patient_id, and a record collection that contains one document for each test or procedure. One patient may have dozens or even hundreds of documents in the record collection.
We need to decide on a shard key to shard the record collection. What's the best shard key for the record collection, provided that we are willing to run inefficient scatter-gather operations to do infrequent research and run studies on various diseases and cohorts? That is, think mostly about the operational aspects of such a system. And by operational, we mean, think about what the most common operations that this systems needs to perform day in and day out.

MongoDB M101N Final Exam Question 8

Question 8:

Supposed we executed the following C# code. How many animals will be inserted into the "animals" collection?

using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading.Tasks;

namespace M101DotNet
{
    class InsertTest
    {
        static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
        }

        static async Task MainAsync(string[] args)
        {
            var client = new MongoClient();
            var db = client.GetDatabase("test");

            var animals = db.GetCollection<BsonDocument>("animals");

            var animal = new BsonDocument
                            {
                            {"animal", "monkey"}
                            };

            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "cat");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "lion");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
        }
    }
}

MongoDB M101N Final Exam Question 7

Question 7:

You have been tasked to cleanup a photo-sharing database. The database consists of two collections, albums, and images. Every image is supposed to be in an album, but there are orphan images that appear in no album. Here are some example documents (not from the collections you will be downloading). 

> db.albums.findOne()
{
    "_id" : 67
    "images" : [
        4745,
        7651,
        15247,
        17517,
        17853,
        20529,
        22640,
        27299,
        27997,
        32930,
        35591,
        48969,
        52901,
        57320,
        96342,
        99705
    ]
}

> db.images.findOne()
{ "_id" : 99705, "height" : 480, "width" : 640, "tags" : [ "dogs", "kittens", "work" ] }


Wednesday 6 May 2015

MongoDB M101N Final Exam Question 6

Question 6:

Suppose you have a collection of students of the following form:
{
 "_id" : ObjectId("50c598f582094fb5f92efb96"),
 "first_name" : "John",
 "last_name" : "Doe",
 "date_of_admission" : ISODate("2010-02-21T05:00:00Z"),
 "residence_hall" : "Fairweather",
 "has_car" : true,
 "student_id" : "2348023902",
 "current_classes" : [
  "His343",
  "Math234",
  "Phy123",
  "Art232"
 ]
}

Now suppose that basic inserts into the collection, which only include the last name, first name and student_id, are too slow (we can't do enough of them per second from our program). What could potentially improve the speed of inserts. Check all that apply.

Tuesday 5 May 2015

MongoDB M101N Final Exam Question 5

Question 5:

Suppose your have a collection fubar with the following indexes created:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "test.fubar",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "b" : 1
        },
        "ns" : "test.fubar",
        "name" : "a_1_b_1"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "c" : 1
        },
        "ns" : "test.fubar",
        "name" : "a_1_c_1"
    },
    {
        "v" : 1,
        "key" : {
            "c" : 1
        },
        "ns" : "test.fubar",
        "name" : "c_1"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "b" : 1,
            "c" : -1
        },
        "ns" : "test.fubar",
        "name" : "a_1_b_1_c_-1"
    }
]


Monday 4 May 2015

MongoDB M101N Final Exam Question 4

Question 4:

For this problem, you will be implementing the functionality needed to "like" a comment in the blog.
First, download and unpack the blog solution handout from here
Next, find the "XXX" in HomeController.cs. This is where you will need to code in the ability to "like" a comment.
When you are done, the following functionality will be in place:

  • When you are logged in, and you view a post in its own page (/Home/Post/<post_id>), each post will have a number of "Likes".
  • When you click on the "Likes" button, it gets incremented by one.
  • You can like a comment as many times as you wish; each click will increment it by one.
  • You can even like your own comments.
In your "blog.posts" documents, the "Comments" subdocuments will each contain a "Likes" field. Its value will be a number, and that number will be the number of "Likes" for that comment. Here is an example:

MongoDB M101N Final Exam Question 3

Question 3:

In this problem, the database will begin in an initial state, you will manipulate it, and we will verify that the database is in the correct final state when you click 'submit'. If you need to start over at any point, you can click 'reset' to re-initialize the database, but this will not change your answer if you have already clicked 'submit'. If you wish to change your answer, get the database into the correct state, and then click 'submit'. If you leave the question and come back, the database will re-initialize. If you have clicked the 'submit' button at least once, you will see the word "Submitted" below the shell.
 
In this problem you will update a document in the messages collection to illustrate your mastery of updating documents from the shell. In fact, we've created a collection with a very similar schema to the Enron dataset, but filled instead with randomly generated data.

MongoDB M101N Final Exam Question 2

Question 2:

Please use the Enron dataset you imported for the previous problem. For this question you will use the aggregation framework to figure out pairs of people that tend to communicate a lot. To do this, you will need to unwind the To list for each message.

This problem is a little tricky because a recipient may appear more than once in the To list for a message. You will need to fix that in a stage of the aggregation before doing your grouping and counting of (sender, recipient) pairs.

MongoDB M101N Final Exam Question 1

Question 1:

Please download the Enron email dataset enron.zip, unzip it and then restore it using mongorestore. It should restore to a collection called "messages" in a database called "enron". Note that this is an abbreviated version of the full corpus. There should be 120,477 documents after restore.

Inspect a few of the documents to get a basic understanding of the structure. Enron was an American corporation that engaged in a widespread accounting fraud and subsequently failed.

Thursday 30 April 2015

A potentially dangerous Request value was detected from the client

Recently, In our application I faced an issue while working with special kind of URLs (URL was referring to pdf file and pdf file name had & in it). I got a message saying A potentially dangerous Request value was detected from the client. 

After speding 2 hours in troubleshooting this issue, I come to below solution