Tuesday, May 16, 2017

GCP Architecture Examples

Hi GCP lovers!

Today I just want to quickly share a extremely useful site that provide GCP architecture examples per domain/industry.


Just to give you an idea of the kind of architecture the site is providing, let's look what would be the architecture for a mobile game backend.


This is very helpful when starting using GCP and you don't know the best practices or design patterns yet. You can find more information about the critical components presented in the architecture (in green) by just clicking on the component.

To access this site, just click here!

Hope it helps :)
Samir

Friday, April 28, 2017

Instance Template for GPU (Beta) - gcloud command

Hi GCP lovers!

Today I would like to talk about a new functionality that has been made available through the latest Cloud SDK (153.0.0).

Until now, you could create "clusters" of GCE instances by creating Managed Instance Groups (MIG). A MIG uses an Instance Template as base image to scale out.

For heavy load processes like Machine Learning, people use more and more GPU instances. GCP offers GPU instances (currently in beta) with the Nvidia K80 GPU.
Until now, instance template for GPU wasn't available and auto-scaling of instances with GPU couldn't be implemented.

Finally, yesterday, Cloud SDK has been updated to v153.0.0 and includes that functionality:










Please find below an example of glcoud command to create an instance template with GPU (in red, the new flag that has been added to the SDK 153.0.0).

gcloud beta compute instance-templates create gpu-instance-template-1 \
--machine-type n1-standard-2 \
--accelerator type=nvidia-tesla-k80,count=1 \
--image-family ubuntu-1604-lts \
--image-project ubuntu-os-cloud \
--maintenance-policy TERMINATE \
--boot-disk-size 100 \
--boot-disk-type "pd-ssd" \
--restart-on-failure \
--metadata startup-script='#!/bin/bash
    echo "Checking for CUDA and installing."
    # Check for CUDA and try to install.
    if ! dpkg-query -W cuda; then
      curl -O http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
      dpkg -i ./cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
      apt-get update
      apt-get install cuda -y
    fi'

Hope it helps!
See you next time :)

Monday, April 24, 2017

Make your chatbot translate stuff for you! (how to use Cloud Functions with api.ai)

Hi GCP lovers!

Today I would like to talk about the cool integration you can do between api.ai and Cloud Functions.
All the code presented here is available on GitHub here.


About Api.ai

If you have never heard of api.ai, it is a promising chatbot product that has been acquired by Google recently. You can test it for free, it's code-less and it integrates flawlessly with other communication services like Google Assistant, Slack, Facebook Messenger and also offers a ML (machine learning) features to understand more than the input that you hard-programmed.


About Cloud Functions

Cloud Functions (currently in beta) is one of the serverless environment that GCP offers. Its function is extremely simple: do something when some trigger happens. Cloud Functions is an event-based function. It appears to be extremely handy when it comes to call Google APIs.

Cloud Functions offers 3 types of triggers:
  1. HTTP trigger
  2. Cloud Pub/Sub topic
  3. Cloud Storage bucket (GCS = Google Cloud Storage)
An easy use case is for instance to do some resizing (thumbnail creation) of a picture when it gets uploaded in a GCS bucket. Or even more cool, call Cloud Vision API to analyze the content of a picture when it gets uploaded in GCS.

Cloud Functions only supports Node.js.

Using the webhook feature of api.ai to call Cloud Functions

Today in this article, I will explain that integration by providing you an example: I want my chatbot to be able to translate me some text I will input in French.

To do that, we will need:
  • api.ai account (sign up for free here)
  • GCP account (sign up for free and get 300$ credit here)
    • Cloud Functions (2 million free invocation per month)
    • Cloud Translation API (don't forget to enable this API from the GCP console!)

Create a function

Open the GCP console and head to Cloud Functions and create a new function that will use HTTP trigger. I published the sample code of the function use here on GitHub.

To start with Cloud Functions, you need to define a function that will take "req" (request content) and "res" (response of this function) parameters.

exports.translatetext = function translatetext(req, res) {
 {...}
};

When calling Cloud Functions from api.ai, the "req" parameter will include a JSON request specific to api.ai. It took me some time to figure out that api.ai wasn't sending the JSON request I was expecting.

When in a normal case, you will manage a "message" and "language" parameters the way shown below. For a JSON request body like this one, you will handle the content in the code like below:

  • JSON request body:
    • {"message": "hello", "language": "french"}


  • In the code, you handle that request this way:
    • req.body.message
    • req.body.language
But for api.ai, the JSON request body is different. Here is an example of request body generated by api.ai:


{
  "id": "c9b569ef-fb6e-43ef-b40e-cfeb71ed73fa",
  "timestamp": "2017-04-24T06:28:15.744Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "translate hello in french",
    "action": "cloudFunctionsTranslate",
    "actionIncomplete": false,
    "parameters": {
      "language": "French",
      "message": "hello"
    },
  ...
}

The blue part of this request is the part we are interested in to call the Translation API.
So to handle correctly the JSON request body coming from api.ai, the code should contains:
  • req.body.result.parameters.message
  • req.body.result.parameters.language
For the response part of our function, we need again to return a JSON response format required by api.ai. To see the detail of the JSON response expected by api.ai, take a look at this doc.
This gives this answer:
  • res.json({ "speech": translation, "displayText": translation, "source": "cloud-function-translate" });
Now that our Cloud Functions is defined and triggers by HTTP, let's configure api.ai to create the correct request sent to Cloud Functions.

Configure api.ai

First, we need to enable the webhook for our bot (I won't cover how to create a bot, api.ai provides many documentation to create one).


The previous image contains:
  • URL: the URL to trigger the previously created Cloud Functions
  • HEADERS
    • Key: Content-type
    • Value: application/json
  • DOMAINS: Enable webhook for all domains
Then let's create an intent to answer to a translation request by the end-user.



The "Action" part will define the parameters to send to the webhook. To understand exactly how this intent works, please to the excellent documentation provided by api.ai.

Let's try our bot now

In the api.ai console, you can try your bot on the right side.
I tried "translate hello in french" and got the answer "bonjour", so it worked!!


You can also verify that it recognized the "message" and "language" parameters from our chat with the bot.

I tried from Facebook messenger (here is the integration doc) and it worked well as well.



Hope you find that article interesting or helpful!

Sunday, February 19, 2017

Cloud Spanner available in public beta!

Hi Cloud lovers!

This week, Google made a very important announcement: they launched the public beta of Cloud Spanner. So what is Cloud Spanner?

What is Cloud Spanner?


In short, it's a globally distributed relational database service. It's a fully managed service so you don't have to worry about maintenance or anything, just have to use it to enjoy its spectacular scalability and performance :)
Also Cloud Spanner is from a category of database called NewSQL. NewSQL databases try to get the best of RDBMS and NoSQL, where RDBMS brings SQL query, schema and ACID transaction and NoSQL brings availability (HA), scalability and flexibility.


So why is that Cloud Spanner so spectacular? it is in the way it keeps a common time between data centers using atomic clock and GPS receivers. Each master server in each datacenter trade reading in order to settle a common time. That way, every time a new data is send to Cloud Spanner, a timestamp is associated with it and so when it is replicated to other data centers, they will be replayed in the correct order.

Google calls this timekeeping technology TrueTime, and only Google has it.


Spanner already has 5 years of internal history


Internally at Google, Spanner has been used for already 5 years for services like Gmail, AdWords, Google Play and more than 2000 other services.  Usually, when a new technology appears in the market, best practices are lacking because of the lack of experience on that new technology. But in Spanner's case, it already has a long life inside Google and there are many Spanner experts that can share experiences as well as best practices on the new released product.

If you visit the official Cloud Spanner documentation site, you will already find so many details on Spanner concept, best practices etc... I strongly recommend visiting this site and learn more about Cloud Spanner.

Cloud Spanner deployment models


Cloud Spanner offers 2 deployment models:

  1. Regional deployment: it will replicate your databases across zones within a region (3 replicas)
  2. Global deployment: it will replicate your databases across 2 regions (5 replicas)

Cloud Spanner sounds amazing, is there any pitfalls?


Right now, Cloud Spanner is available in Beta. There's some features that are not available yet.

  • Global deployment: right now you can only deploy Cloud Spanner in a regional deployment. And in the Beta phase, only the next 3 regions are supported:
    • regional-us-central1 — Central United States
    • regional-europe-west1 — Western Europe
    • regional-asia-east1 — Eastern Asia-Pacific
  • DML (Data Manipulation Language): right now, Cloud Spanner doesn't offer DML (INSERT, UPDATE, DELETE) in SQL. So in SQL, you're able to query using SELECT but for any data manipulation query, you will have to use the client libraries or API/gRPC.


What about the performances?


As a guideline, you will find on the official Cloud Spanner documentation that the read/write queries per second (QPS) offered by this database will depend on the number of node you configure in your Cloud Spanner instance.

In short, if you follow their best practices, you will get per node:
  • up to 10000 QPS of reads 
  • up to 2000 QPS of write  (writing single rows at 1 KB data per row),
  • 2 TiB of storage
If you want more details about how Cloud Spanner performs depending on specific workloads, I recommend reading that excellent article that Quizlet published.

Hope you are excited by Cloud Spanner as much as me! you can start testing it right now here!

Welcome to my new blog about GCP!

Hi cloud lovers!

Welcome to my new blog about Google Cloud Platform (GCP)!



This blog will be covering tips, examples and explanations about GCP.

A little about me. I'm working as a Sales Engineer at Google in Tokyo. I'm covering there all the Google Cloud stacks, means GCP as well as G Suite (Google productivity suite) and I'm helping customers to make best use of GCP to implement the best scalable apps possible.  But I hope to see more and more customers bringing their on-prem workload (even any Windows workloads!) on GCP since I believe that GCP is Enterprise ready!
This year at work, I'm covering the game industry in Japan so I might be able to share you some best practices or learning from some of the projects I would work on.

Before joining Google, I used to work at Microsoft as a consultant first, then as a Sales Engineer (TSP) on infrastructure as well as client side technologies. 
I will try in this blog to leverage my 10 years experience on Infrastructure to bring you as much valuable content as possible that will help you make best use of GCP :)

Happy reading!
Samir