Hi, just a quick note to say that record­ings of my talk with Com­mu­nity for F# is now available.

In this ses­sion, I shared some of our expe­ri­ences of build­ing a suc­cess­ful MMORPG for a social audi­ence and insights into some of the tech­ni­cal chal­lenges that my team has had to over­come along the way.

 

 

How you enjoy them!

Share

A cou­ple of weeks ear­lier, Ama­zon announced sup­port for Local Sec­ondary Indexes (LSI) for DynamoDB. You can now per­form fast, effi­cient queries against DynamoDB tables using attrib­utes that are not part of the exist­ing Hash and Range key model with­out resort­ing to the use of scans.

As a result to the new fea­ture the DynamoDB query API has also gone through some changes, as did the AWSSDK for .Net. From ver­sion 1.5.18.0 onwards, there’s a new top level name­space Amazon.DynamoDBv2 which con­tains a mir­ror set of types to those under the orig­i­nal Amazon.DynamoDB name­space, albeit with minor changes to sup­port the new LSI feature.

Query syn­tax change

Due to the changes in the under­ly­ing AWSSDK, I have decided to make some changes to the query syn­tax sup­ported by DynamoDB.SQL too – namely, to remove the need for the spe­cial key­words @HashKey and @RangeKey and instead allow you to use the attrib­utes names for your hash and range keys.

For exam­ple, given a table like the one out­lined in the DynamoDB docs:

image

To write a query to find all sub­jects start­ing with “a” in the “S3” forum, you would pre­vi­ously write:

SELECT * FROM Thread WHERE @HashKey = \”S3\” AND @RangeKey BEGINS WITH \”a\”

In the new ver­sion of DynamoDB.SQL, you would write the fol­low­ing instead:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Sub­ject BEGINS WITH \”a\”

This syn­tax change only applies to the exten­sion meth­ods for the Ama­zon­Dy­namoD­B­Client and DynamoD­B­Con­text types under the new Amazon.DynamoDBv2 name­space. The exten­sion meth­ods them­selves are only avail­able under a new name­space DynamoDbV2.SQL.Execution in the DynamoDb.SQL.dll.

The syn­tax for scans on the other hand, has remained the same in both the new and the old API.

Local Sec­ondary Index support

You can spec­ify that a query should use a Local Sec­ondary Index (LSI) by using the Index option in the WITH clause.

For exam­ple, given a Thread table and an index Last­PostIn­dex, as out­lined in the DynamoDB docs:

image

To find all the posts in the “S3” forum since the 1st May 2013, you can write the query as following:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \”2013–05-01\”

WITH (Index(LastPostIndex, true))

The WITH clause is where you spec­ify optional query para­me­ters, such as NoCon­sis­ten­tRead, and Page­Size. (please refer to the Get­ting Started guide on avail­able query parameters).

The Index option allows you to spec­ify the name of the index, in this case that’s “Last­PostIn­dex”, and a boolean flag to spec­ify whether or not all attrib­utes should be returned.

For the above query, because we’re ask­ing for all attrib­utes to be sent back with *, and that attrib­utes such as Replies are not pro­jected into the index, they will be fetched (auto­mat­i­cally per­formed by DynamoDB) from the main table at addi­tional con­sumed capac­ity units.

 

On the other hand, if you want only the pro­jected attrib­utes back from the index, we can tweak the query slightly:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \”2013–05-01\”

WITH (Index(LastPostIndex, false))

In which case, only Forum­Name, Last­Post­Date­Time and Sub­ject will be returned by the query.

 

Finally, if you are inter­ested in a spe­cific set of attrib­utes, you can also spec­ify them in the SELECT clause:

SELECT Forum­Name, Sub­ject FROM Thread

WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \“2013–05-01\”

WITH     (Index(LastPostIndex, false))

 

Some ref­er­ence links:

AWS announces Local Sec­ondary Index sup­port for DynamoDB

DynamoDB docs on Local Sec­ondary Indexes

DynamoDB docs on Query

Query­ing with an Index attribute in DynamoDB.SQL

Get­ting started with DynamoDB.SQL

Share

Hi, just a quick note to men­tion that I’ll be talk­ing to Com­mu­nity for F# on the 14th May at 11AM PDT about our expe­ri­ence of build­ing an MMORPG, some of the chal­lenges we face along the way and how F# helps us solve some of these problems.

You can reg­is­ter for the event here.

Hope to see you there!

 

p.s. here is some of the things that have been hap­pen­ing in Here Be Mon­sters so far, feel free to come join us in the game Winking smile

Share

.Net 4 intro­duced the Lazy<T> type which allows you to cre­ate an object that can be lazily ini­tial­ized so that you can delay the cre­ation of large objects, for instance.

How­ever, if your ini­tial­iza­tion logic has the poten­tial to except at run­time (e.g. time out excep­tions read­ing from some exter­nal data source) then you should pay close atten­tion to which con­struc­tor you use to cre­ate a new instance of the Lazy<T> type. Depend­ing on the selected LazyThread­Safe­t­y­Mode, excep­tions in the ini­tial­iza­tion code might be cached and rethrown on all sub­se­quent attempts to fetch the lazily ini­tial­ized value. Whilst this ensures that threads will always get the same result, hence remov­ing ambi­gu­ity, it does mean that you’ve got only one shot at ini­tial­iz­ing that value…

 

LazyThread­Safe­t­y­Mode

In cases where you need to be able to tol­er­ate occa­sional ini­tial­iza­tion errors (e.g. read­ing a large object from S3 can fail from time to time for a num­ber of rea­sons) and be able to try again at a sec­ond attempt, the rule of thumb is to instan­ti­ate the Lazy<T> type by set­ting LazyThread­Safe­t­y­Mode to Pub­li­ca­tionOnly. In Pub­li­ca­tionOnly thread safety mode, mul­ti­ple threads can invoke the ini­tial­iza­tion logic but the first thread to com­plete the ini­tial­iza­tion suc­cess­fully sets the value of the Lazy<T> instance.

For exam­ple, the fol­low­ing only works under the Pub­li­ca­tionOnly mode:

 

F#

F# pro­vides a slightly nicer syn­tax for defin­ing a lazy com­pu­ta­tion:

image

the Control.Lazy<T> type is an abbre­vi­a­tion of the BCL Lazy<T> type with a Force exten­sion method which under the hood just calls Lazy<T>.Value.

Pre­sum­ably the above trans­lates roughly to the fol­low­ing C# code:

var x = 10;

var result = new Lazy<int>(() => x + 10);

and the thread safety mode using the Lazy(Func<T>) con­struc­tor is LazyThreadSafetyMode.ExecutionAndPublication which caches and rethrows any excep­tions caught in the ini­tial­iza­tion. E.g.:

image

Share

After a long Easter hol­i­day filled with late night cod­ing ses­sions I find myself wide awake at 2am… good job I’ve still got my plu­ral­sight sub­scrip­tion and a quick look at the Algo­rithms and Data Struc­tures course again at least gave me some­thing to do to relax the mind with some back-to-school style imple­men­ta­tion of com­mon sort­ing algo­rithms in F#:

Whilst not the most per­for­mant imple­men­ta­tions I’m sure, I hope at least it goes to show how eas­ily and con­cisely these sim­ple algo­rithms can be expressed in F#! Now back to that sleep thing…

Share