simplestar-tech.hatenablog.com
の続き
ローカル環境で Cube データを仮で Redis Mass Insertion するための plotocol を生成する C# コードがこちら
using System.IO; using System.Text; namespace CubeWalkDataMaker { class Program { static void Main(string[] args) { using (StreamWriter writer = new StreamWriter("data.txt", false, Encoding.Default)) { writer.Write("*2\r\n$6\r\nSELECT\r\n$1\r\n0\r\n"); for (int i = 0; i < 16 * 16 * 16; i++) { var key = i.ToString("X6"); var val = i.ToString("X8"); writer.Write($"*3\r\n$3\r\nSET\r\n$6\r\n{key}\r\n$8\r\n{val}\r\n"); } } } } }
できた data.txt を redis の volume マウントしたフォルダに配置して redis に流し込みます。
# cat /redis/data.txt | redis-cli --pipe All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 16777217
全部入りましたね
バックアップファイルも作成しましょう
# redis-cli save OK root@1faa380aff8b:/data# mv dump.rdb /redis/dump_20190922b.rdb root@1faa380aff8b:/data# redis-cli dbsize (integer) 16777216
作られた rdb ファイルのサイズは 270 MB でした。。。
Redis のメモリ使用量はどの程度でしょうか?
root@1faa380aff8b:/data# redis-cli info # Memory used_memory:1208828840 used_memory_human:1.13G used_memory_rss:1239535616 used_memory_rss_human:1.15G used_memory_peak:1208828840 used_memory_peak_human:1.13G used_memory_peak_perc:100.00% used_memory_overhead:806148326 used_memory_startup:792264 used_memory_dataset:402680514 used_memory_dataset_perc:33.33% allocator_allocated:1208823624 allocator_active:1208979456 allocator_resident:1238646784 total_system_memory:2096144384 total_system_memory_human:1.95G used_memory_lua:37888 used_memory_lua_human:37.00K used_memory_scripts:0 used_memory_scripts_human:0B number_of_cached_scripts:0 maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction allocator_frag_ratio:1.00 allocator_frag_bytes:155832 allocator_rss_ratio:1.02 allocator_rss_bytes:29667328 rss_overhead_ratio:1.00 rss_overhead_bytes:888832 mem_fragmentation_ratio:1.03 mem_fragmentation_bytes:30769656 mem_not_counted_for_evict:0 mem_replication_backlog:0 mem_clients_slaves:0 mem_clients_normal:49694 mem_aof_buffer:0 mem_allocator:jemalloc-5.1.0 active_defrag_running:0 lazyfree_pending_objects:0 # Keyspace db0:keys=16777216,expires=0,avg_ttl=0
つまり used_memory_peak_human:1.13G ということでした。
最後に AWS の料金の話をして締めます。
オンデマンドノード
>オンデマンドノードでは、長期契約なしに、ノードを実行する時間単位で、メモリ容量に対してお支払いいただきます。
ElastiCache の AWS 料金表によれば 1.13 GB のメモリを持つ最小モデルは
cache.t2.small 0.052USD/h
月々 37.44 USD = 約 4000円の利用料となります。
最後に ElastiCache でバックアップからデータを起こす処理を行ってみましょう。
ElastiCache をローカルバックアップから復元
ローカルに書き出した 270MB の .rdb ファイルを S3 の適当なバケットにアップロードして配置します。
[+ Add account] を選択してその他すべての AWS リージョンの正規IDに読み取り権限を与えて
myBucket/myFolder/myBackupFilename.rdb のようにパスを与えて rdb ファイルを ElastiCache 作成時に指定します。
クラスター内のノードのタイプcache.t2.small を指定しました。
しばらく待ちます。(5分くらい?)
無事ノードが作成され ElastiCache のプライマリエンドポイントが作成されました。
これをコピーして、以前の API Gateway ⇄ ElastiCache の連携の作業につなげます。
simplestar-tech.hatenablog.com
※セキュリティグループの設定が正しいか確認します。
Lambda の実装は次の通り、バックアップから復元したデータが取れるか見るものにしてみました。
from __future__ import print_function import redis import uuid #elasticache settings r = redis.StrictRedis(host='xxxxxxxxxxxxxxxxxxxxxxxxx.apne1.cache.amazonaws.com', port="6379", db=0) def handler(event, context): data_obtained = r.get('FFFFFF') if data_obtained is not None: # this print should go to the CloudWatch Logs and Lambda console. print ("Success: Fetched value %s from memcache" %(data_obtained)) else: raise Exception("Cannot obtained FFFFFF key data.") return { "statusCode": 200, "body": "\"\"" }
Lambda のテスト実行結果、つながって ElastiCache からデータを取得できました。
curl コマンドからも、インターネット経由で ElastiCache にアクセスできている様子もわかったので、これにて疎通確認完了