2019年3月13日 星期三

AWS DynamoDB

AWS DynamoDB

https://docs.aws.amazon.com/en_us/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html


2019年3月12日 星期二

AWS Lambda 自動打包

AWS Lambda 有好工具自動打包,事實上也可以打包到各個雲上。

工具 serverless ,是用 node.js 開發的。只要用 npm 安裝即可,npm 這個工具的介紹就不贅述,網路上資料很多。

安裝工具

npm install -g serverless

試用 serverless

這個工具有提供 很多範例程式 供人測試,我們這次使用 python / aws dynamoDB 這個例子。

git clone https://github.com/serverless/examples.git  (載下範例來)

cd aws-python-rest-api-with-dynamodb  (我們選用這個範例)

原本是可以直接用 serverless deploy 這個指令做 deploy,但是我們現在還沒有設好我們 AWS 上的 credentials,所以還需要設定。

設定 AWS credentials 

這個影片教你如何在 IAM 中拿到 key ID 以及 secret,並且透過指令

serverless config credentials --provider aws --key xxxx --secret xxxx

將其存成預設執行的設定檔。


也有一些設定 credentials 的其他方法。

正式執行 deploy

serverless deploy

輸出如下: 
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service serverless-rest-api-with-dynamodb.zip file to S3 (5.37 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................................................................................
Serverless: Stack update finished...
Service Information
service: serverless-rest-api-with-dynamodb
stage: dev
region: us-east-1
stack: serverless-rest-api-with-dynamodb-dev
resources: 34
api keys:
  None
endpoints:
  POST - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos
  GET - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos
  GET - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
  PUT - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
  DELETE - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
functions:
  create: serverless-rest-api-with-dynamodb-dev-create
  list: serverless-rest-api-with-dynamodb-dev-list
  get: serverless-rest-api-with-dynamodb-dev-get
  update: serverless-rest-api-with-dynamodb-dev-update
  delete: serverless-rest-api-with-dynamodb-dev-delete
layers:
  None

部署完畢。

測試一下能否執行

範例裡面,會個別教學怎麼執行測試。我們直接列出執行狀況:

注意,REST 接觸的端口,就是上面部署完後的輸出結果,上面我用紅色標註。

建立 Todo 資料
$curl -X POST https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos --data '{ "text": "Learn Serverless" }'

輸出
{"text": "Learn Serverless", "checked": false, "id": "0828b186-4549-11e9-923d-eaf73cc0b517", "createdAt": 1552451558329, "updatedAt": 1552451558329}

查詢 Todo 資料
$ curl https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos

輸出
[{"text": "Learn Serverless", "checked": false, "id": "0828b186-4549-11e9-923d-eaf73cc0b517", "createdAt": 1552451558329, "updatedAt": 1552451558329}]

是可以正確執行的。


2019年2月11日 星期一

FMEA

FMEA
失效模式效應分析(Failure Mode Effect Analysis: FMEA)
  • 由美國陸軍在 1949 年開發出來的(Military Procedure MIL-P-1629)
  • 現在 FMEA 分析模式是以國際電工委員會 International Electrotechnical Commission ;IEC)所提出之規格為主


FMEA手法最初是用來
  • 改善產品的可靠性
  • 目前其應用跨越產品的生命週期,從產品規劃到設計、製造、建造以及使用等階段,以提高產品或系統的可靠性
  • 預防性技術,分析方法,為了排除失效,在設計確定之前先尋求可能原因或徵兆,在失效發生前,設法減少發生機率,提高產品可靠性


FMEA的實行方式
  • 是以表單形式來解析,指出故障原因,思考對策來改善系統品質
  • 如:對產品地識別名稱、圖面編號、使用條件、故障對系統的影響度、故障等級、故障發生頻率等,找出故障模式的影響度與發生頻率,將其量化可進行故障效應分析
  • 10 個步驟 https://fmea-training.com/10-steps-process-failure-mode-and-effects-analysis/
    • STEP 1: Review the process. ...
      STEP 2: Brainstorm potential failure modes. ...
      STEP 3: List potential effects of each failure. ...
      STEP 4: Assign Severity rankings. ...
      STEP 5: Assign Occurrence rankings. ...
      STEP 6: Assign Detection rankings. ...
      STEP 7: Calculate the RPN.


PFMEA vs DFMEA
  • Process FMEA's (PFMEA) and Design FMEA's (DFMEA) defined in the AIAG FMEA 4th edition


論文


模板

2019年1月31日 星期四

2019年1月28日 星期一

Mirror

git clone --mirror source_repo
git push --mirror dest_repo

即使 更新也可以這樣使用

2019年1月14日 星期一

OpenShift 介紹

OpenShift 介紹


Openshift 是一個 Redhat 所建的 PaaS 服務,讓使用者可以在 k8s 跟 docker 的基礎上,方便快速建構應用程式。



整體架構如下,包含可以讓使用者程式碼可以被建成 image 的 source-to-image


Openshift Overview (10 mins)


詳細 stack 說明


- Redhat 的 OpenShift 投影片

Openshift Workshop (很多實作)

- 超推薦
https://github.com/RedHatWorkshops/openshiftv3-workshop

- AWS 上實現 openshift
https://aws.amazon.com/tw/quickstart/architecture/openshift/

- AWS 快速開始 openshift

Openshift CI/CD Demo (17mins)


    Sonar : 代碼品質管理
    Nexus : Maven Repo 管理器
    Apache Maven : Java 專案管理與自動構建工具


Openshift Source-to-Image (s2i)



Openshift 產品線(紅帽)

PDF 檔案

紅帽被 IBM 收購後,有風險與質疑。但似乎金融界很多都採用紅帽方案。

認證相關課程
 - Linux Academy 
 - Redhat 

2019年1月7日 星期一

OpenMP : 一個成熟穩定、使用 CPU 多核心的 library

一般單執行緒 (mono-thread) 的 code,多核心處理器是沒有辦法提升處理效能的,但多執行緒(multi-thread)的 code,可以透過多核心的平行計算,來達到加速目的。那要怎麼簡單使用CPU的多核心計算資源呢?  OpenMP 就是一個好答案。

OpenMP 在 1997 年就開始了,一直維護改版至今。算是很穩定與老牌的函式庫了。
https://www.openmp.org/
https://zh.wikipedia.org/wiki/OpenMP

使用方法: (編輯 test.c)

#include     // 引入library
#include
#include

int main(int argc, char* argv[]) 
 { 
   #pragma omp parallel    //這一行很重要
   printf("Hello, world.\n"); 
   return 1; 
 }


編譯方法: (編譯 test.c)

gcc -o test -fopenmp test.c


直接執行編譯出來的執行檔即可


解釋

語法
#pragma omp [clause[[,] clause] ...]  

其中,directive共11個:
  • atomic 記憶體位置將會原子更新(Specifies that a memory location that will be updated atomically.)
  • barrier 執行緒在此等待,直到所有的執行緒都執行到此barrier。用來同步所有執行緒。
  • critical 其後的程式碼塊為臨界區,任意時刻只能被一個執行緒執行。
  • flush 所有執行緒對所有共用物件具有相同的記憶體視圖(view of memory)
  • for 用在for迴圈之前,把for迴圈並列化由多個執行緒執行。迴圈變數只能是整型
  • master 指定由主執行緒來執行接下來的程式。
  • ordered 指定在接下來的程式碼塊中,被並列化的 for迴圈將依序執行(sequential loop)
  • parallel 代表接下來的程式碼塊將被多個執行緒並列各執行一遍。
  • sections 將接下來的程式碼塊包含將被並列執行的section塊。
  • single 之後的程式將只會在一個執行緒(未必是主執行緒)中被執行,不會被並列執行。
  • threadprivate 指定一個變數是執行緒局部儲存(thread local storage)

共計13個clause:
  • copyin 讓threadprivate的變數的值和主執行緒的值相同。
  • copyprivate 不同執行緒中的變數在所有執行緒中共用。
  • default Specifies the behavior of unscoped variables in a parallel region.
  • firstprivate 對於執行緒局部儲存的變數,其初值是進入並列區之前的值。
  • if 判斷條件,可用來決定是否要並列化。
  • lastprivate 在一個迴圈並列執行結束後,指定變數的值為迴圈體在順序最後一次執行時取得的值,或者#pragma sections在中,按文字順序最後一個section中執行取得的值。
  • nowait 忽略barrier的同步等待。
  • num_threads 設定執行緒數量的數量。預設值為目前電腦硬體支援的最大並行數。一般就是CPU的內核數目。超執行緒被作業系統視為獨立的CPU內核。
  • ordered 使用於 for,可以在將迴圈並列化的時候,將程式中有標記 directive ordered 的部份依序執行。
  • private 指定變數為執行緒局部儲存。
  • reduction Specifies that one or more variables that are private to each thread are the subject of a reduction operation at the end of the parallel region.
  • schedule 設定for迴圈的並列化方法;有 dynamic、guided、runtime、static 四種方法。
    • schedule(static, chunk_size) 把chunk_size數目的迴圈體的執行,靜態依序指定給各執行緒。
    • schedule(dynamic, chunk_size) 把迴圈體的執行按照chunk_size(預設值為1)分為若干組(即chunk),每個等待的執行緒獲得目前一組去執行,執行完後重新等待分配新的組。
    • schedule(guided, chunk_size) 把迴圈體的執行分組,分配給等待執行的執行緒。最初的組中的迴圈體執行數目較大,然後逐漸按指數方式下降到chunk_size。
    • schedule(runtime) 迴圈的並列化方式不在編譯時靜態確定,而是推遲到程式執行時動態地根據環境變數OMP_SCHEDULE 來決定要使用的方法。
  • shared 指定變數為所有執行緒共用。


細節可參考 Wiki