ジェネリクスと変位について覚え書き -- なぜimmutableに書くのか
「setterをむやみに生やさずにimmutableに書こう」
という理由のひとつとして、「共変にできる」というのもあるんだなぁと思ったのでメモ。
よく聞く「T[]
は不変(invariant)だよ」というやつ
- 電車で考えてみる
Male
extendsHuman
,Female
extendsHuman
Train<Human>
... 一般車両Train<Female>
... 女性専用車両
Train<Female>
extendsTrain<Human>
と言えるか?- 一般には言えない
- 反例:
train.push(aMale)
Train<Female>
はpush<Human>(aHuman: Human)
を実装できないのでTrain<Human>
としては使えない- is-a関係が崩れるので、とくに継承関係のない無関係な型になる(invariant)
必ずinvariantかというと、そうではない
Train<T>::at<T>(index:int): T
これは問題ないTrain<Female>
から取り出したFemale
はもれなくHuman
として使えるTrain<Female>::at<Female>(index:int): Female
はTrain<Human>::at<Human>(index:int): Human
として使えるということ- 「共変戻り値」という
Train<T>::push<T>(t: T): T
これが駄目- 引数の場合、型を拡大できても、狭めることはできない
- 「反変引数」という
- 電車の例でいうと、女性が通常車両に乗るぶんには問題ない
- 「共変かつ反変」というのが無理なので不変になるよ、という話
- 反変なメソッドさえ生えていなければ、共変になりうる
共変なジェネリクスの例: ScalaのimmutableのList[+A]
- Scalaだと型パラメータを
[]
で書くらしい+
が共変、-
が反変を表す
def head: A Selects the first element of this iterable collection.
- 要素取得。共変戻り値
final def map[B](f: (A) => B): List[B] Builds a new list by applying a function to all elements of this list.
- コールバック関数
f
の型が(A) => B
- 「反変引数」なので、引数型を広げるぶんには問題ない。
(Female) => B
として(Human) => B
を使うことができる - つまり、
List[Female]
はList[Human]
の機能を満たす。まだ共変
def appended[B >: A](elem: B): List[B] A copy of this sequence with an element appended.
- リストに変更を加えるのではなく、新しいリストを返す
- 引数型にも戻り値型にも
List[A]
の型パラメータA
が出てこないので、変位にはとくに影響しない。共変を崩さない
そんなこんなでScalaのimmutableのList[A]
は共変らしい
追記
PHP でいうと psalm にも template-covariant というのがあったりします https://t.co/ltMbK4vvo0
— sji (@sji_ch) 2020年8月14日
PHPでは、PsalmとPHPDocコメントを使って共変の変位指定ができるらしい
CCNA試験対策 下巻ch12: Miscellaneous IP Services
https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com
- First Hop Redundancy Protocol
- Simple Network Management Protocol
- FTP and TFTP
First Hop Redundancy Protocol
- FHRP: First Hop Redundancy Protocol
- デフォルトゲートウェイの冗長化に用いるプロトコル群の総称
- cf. L2冗長化時にブロードキャストストームを回避するためのプロトコルはSTP
The Need for Redundancy in Networks
The Need for a First Hop Redundancy Protocol
- デフォルトゲートウェイが物理的にSPOFになるのを避ける
- ホストのデフォルトゲートウェイは1つしか設定できないので、ルータ側で冗長化する必要がある
- FHRPのやること
- 複数の物理router間で仮想IPアドレスを共有する
- ホストはこの仮想IPアドレスをデフォルトゲートウェイに設定する
- 複数の物理router間でactive/standbyやロードバランシングの合意をとる
- フェイルオーバー
- 複数の物理router間で仮想IPアドレスを共有する
The Three Solutions for First-Hop Redundancy
HSRP | VRRP | GLBP | |
---|---|---|---|
Full Name | Hot Standby Router Protocol | Virtual Router Redundancy Protocol | Gateway Load Balancing Protocol |
Origin | Cisco | RFC 6798 | Cisco |
Redundancy Approach | active/standby | active/standby | active/active |
Load Balancing per... | subnet | subnet | host |
仮想MACアドレス | 0000.0c07.acxx xx はHSRPのグループ番号を16進数にしたもの |
0000.5e00.01xx xx はVRRPのグループ番号を16進数にしたもの |
0007.b400.xxyy xx はGLBPグループ番号yy はその中でのシーケンス(01-04) |
HSRP Concepts
R2(config)#interface g0/0/0 R2(config-if)# R2(config-if)#ip address 10.0.0.200 255.255.255.0 R2(config-if)# R2(config-if)#standby 10 ip 10.0.0.254 R2(config-if)# R2(config-if)#no shutdown
C:\>ping 10.0.0.254 Pinging 10.0.0.254 with 32 bytes of data: Reply from 10.0.0.254: bytes=32 time=1ms TTL=255 Reply from 10.0.0.254: bytes=32 time<1ms TTL=255 Reply from 10.0.0.254: bytes=32 time<1ms TTL=255 Reply from 10.0.0.254: bytes=32 time<1ms TTL=255 Ping statistics for 10.0.0.254: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 1ms, Average = 0ms C:\>arp -a Internet Address Physical Address Type 10.0.0.254 0000.0c07.ac0a dynamic
- echoは両routerに届くが、echo-replyはactive routerからのみ帰る (今回はR2)
- active/standby
- priority値でどのrouterがactiveになるか決まる
- tie-breakerは物理IPアドレス
- 最も大きいものがactiveになる
- 後からpriority値の高いrouterを追加しても、OSPFのDR/BDRよろしくpreemptionはおきない
- インタフェースコンフィグで
standby <group-number> preempt
を設定することでpreemptionを有効化できる
- priority値でどのrouterがactiveになるか決まる
HSRP Failover
HSRP Load Balancing
- HSRPではVLANごとにHSRPグループを作成し、仮想IPアドレスを割り当てる
- 各HSRPグループでpriority値を調整し、「VLAN1ではR1をactive」「VLAN2ではR2をactive」という形で負荷分散を実現できる
- cf. GLBPは1つの仮想IPアドレスに対して複数の仮想MACアドレスをARP応答し、負荷分散を実現する
Simple Network Management Protocol
- interface, IPアドレス、バッファといった変数を標準化したデータベースを作ることで、監視や管理を簡単にするという思想のもと生まれた
- 機器上でagentを動かし、managerから操作を行う
- MIB: Management Information Base
- 「変数のデータベース」
- ツリー構造
- NMS: Network Management Station
- managerが動作するPCやサーバ
SNMP Variable Reading and Writing: SNMP Get and Set
SNMP Notifications: Traps and Informs
The Management Information Base
Securing SNMP
- SNMPv3でようやく本格的なセキュリティが搭載された
- Message integrity
- Authentication
- Encryption
FTP and TFTP
Managing Cisco IOS Images with FTP/TFTP
The IOS File System
Router#show file systems File Systems: Size(b) Free(b) Type Flags Prefixes * 3249049600 2761893909 flash rw flash: 29688 23590 nvram rw nvram:
Router#show flash: System flash directory: File Length Name/status 3 486899872isr4300-universalk9.16.06.04.SPA.bin 2 28282 sigdef-category.xml 1 227537 sigdef-default.xml [487155691 bytes used, 2761893909 available, 3249049600 total] 3.17338e+06K bytes of processor board System flash (Read/Write)
Upgrading IOS Images
Copying a New IOS Image to a Local IOS File System Using TFTP
- TFTPサーバからコピーする
Router#copy tftp flash Address or name of remote host []? 10.0.0.1 Source filename []? c2960-lanbasek9-mz.150-2.SE4.bin Destination filename [c2960-lanbasek9-mz.150-2.SE4.bin]? Accessing tftp://10.0.0.1/c2960-lanbasek9-mz.150-2.SE4.bin... Loading c2960-lanbasek9-mz.150-2.SE4.bin from 10.0.0.1: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [OK - 4670455 bytes] 4670455 bytes copied in 0.058 secs (6473925 bytes/sec)
- コピーしたファイルを確認
Router#show flash: System flash directory: File Length Name/status 4 4670455 c2960-lanbasek9-mz.150-2.SE4.bin 3 486899872isr4300-universalk9.16.06.04.SPA.bin 2 28282 sigdef-category.xml 1 227537 sigdef-default.xml [491826146 bytes used, 2757223454 available, 3249049600 total] 3.17338e+06K bytes of processor board System flash (Read/Write) Router#dir flash0: Directory of flash:/ 4 -rw- 4670455 <no date> c2960-lanbasek9-mz.150-2.SE4.bin 3 -rw- 486899872 <no date> isr4300-universalk9.16.06.04.SPA.bin 2 -rw- 28282 <no date> sigdef-category.xml 1 -rw- 227537 <no date> sigdef-default.xml 3249049600 bytes total (2757223454 bytes free)
Verifying IOS Code Integrity with MD5
Copying Images with FTP
Router#copy ? flash: Copy from flash: file system ftp: Copy from ftp: file system running-config Copy from current system configuration scp: Copy from scp: file system startup-config Copy from startup configuration tftp: Copy from tftp: file system
- ftpやscpも使える
The FTP and TFTP Protocols
FTP Protocol Basics
- over TCP
- 認証がある
FTP Active and Passive Modes
- 2本のTCPコネクションを用いる
- FTP control connection
- TCP21番
- FTP data connection
- TCP20番が予約されているが、client/serverともにephemeral portを使うことが多い
- active mode
- passive mode
- active mode誕生の経緯
- TCP20番を決めて使ってしまうと、複数のFTP clientを動かせない
- passive mode誕生の経緯
FTP over TLS (FTP Secure)
TFTP Protocol Basics
- Trivial File Transfer Protocol
- over UDP
- 認証なし
CCNA試験対策 下巻ch17: Cisco Software-Defined Access (SDA)
https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com
- SDA Fabric, Underlay, and Overlay
- DNA Center and SDA Operation
- DNA Center as a Network Management Platform
SDA Fabric, Underlay, and Overlay
- Fabric
- Underlayとoverlayが組み合わさった全体をさす言葉
- Underlay
- 物理的なL2/L3ネットワーク
- Overlay
- Underlayの上で構築される論理的なネットワーク
- VXLAN tunnelなど
- Underlayの上で構築される論理的なネットワーク
The SDA Underlay
Using Existing Gear for the SDA Underlay
- brownfield design
- ノードには役割があり、要求するスペックが公式にまとめられている
- 既存のデバイスを用いる場合、このカタログに照らす必要がある
Using New Gear for the SDA Underlay
- greenfield design
- カタログ通りの製品を買えばいいだけ
- プロビジョニング自体は依然として必要なことに留意する
- 帯域幅
- PoEの電力供給
- etc.
- Routed Access Layer Design
- 全SwitchをL3にするすすめ
- STPいらず
- FHRPもいらなくなる
- 全SwitchをL3にするすすめ
The SDA Overlay
VXLAN Tunnens in the Overlay (Data Plane)
- VXLAN: Virtual Extensible LAN
- 拡張性の高いトンネリングプロトコル
- VXLANを用いて、Underlayの物理的なネットワークを隠蔽してOverlay上の論理的なネットワークを構築する
LISP For Overlay Discovery and Location (Control Plane)
- EID: endpoint identifier
- Underlay Network上のサブネット
- RLOC: Routing Location
- Overlay上のアドレス
- LISP map server
- EIDとRLOCとの対応を管理する
- Fabric Control Node上で動作する
DNA Center and SDA Operation
Cisco DNA Center
- Cisco DNA Center対応機器を買うとプリインストールされているアプリケーション
Cisco DNA Center and Scalable Groups
Issues with Traditional IP-Based Security
SDA Security Based on User Groups
- SGT: Scalable Group Tag
- Employee, Internet, Partner, Guest といった論理的なタグ付けを行う
- タグ間でアクセス制限を行う
DNA Center as a Network Management Platform
DNA Center Similarities to Traditional Management
- トポロジマップの生成・表示、直感的なGUIなどは従来と差がない
DNA Center Differences with Traditional Management
CCNA試験対策 下巻ch16: Introduction to Controller-Based Networking
https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com
- SDN and Controller-Based Networks
- Examples of Network Programmability and SDN
- Comparing Traditional Versus Controller-Based Networks
- 英語
SDN and Controller-Based Networks
The Data, Control, and Management Planes
- ネットワークの再定義
The Data Plane
- Data Plane: データの通信そのものに関する要素
- Forwarding Planeとも
- これがないとそもそも通信できない
The Control Plane
- これがなくても通信はできるが、運用やパフォーマンスに難があるよ、というもの
The Management Plane
Cisco Switch Data Plane Internals
- SwitchはData Planeにあたる高負荷の処理を専用のハードウェアでこなしている
- ASIC: application-specific integrated circuit
- TCAM: ternary content-addressable memory
- MACアドレステーブルなどの実装
- 検索ロジック不要
- Control Planeにあたるものは汎用CPUで処理している
Controllers and Software-Defined Architecture
Controllers and Centralized Control
- 従来は各デバイスで分散管理していたControl Planeを一元化する
Application ↓ call NBI Controller ↓ call SBI Network Devices
The Southbound Interface
- SBI: Southbound Interface
- 「紙面上で下側のAPI」の意
- 図面上でネットワーク機器はコントローラよりも下側に描かれることから
- ネットワーク機器が提供する
SBIs | ACI | Cisco APIC-EM | Cisco SDA |
---|---|---|---|
OpenFlow | o | ||
OpFlex | o | o | |
CLI(SSH/Telnet) | o | o | |
SNMP | o | ||
NETCONF/RESTCONF | o |
The Northbound Interface
Examples of Network Programmability and SDN
OpenDaylight and OpenFlow
- ONF: Open Networking Foundation
- OpenFlow
- ONFによりつくられたSBI
- Switchの抽象化・標準化など
The OpenDaylight Controller
- OpenDaylight
- オープンソースのSDNコントローラ
- OpenFlow含むさまざまなSBIを叩き、NBIを提供する
The Cisco Open SDN Controller (OSC)
- OSC: Open SDN Controller
- CiscoによるOpenDaylightの商用フォーク
- すでに終わった製品
Cisco Application Centric Infrastructure (ACI)
ACI Physical Design: Spine and Leaf
ACI Operating Model with Intent-Based Networking
- IBN: Intent-Based Networking
- CiscoはOpenFlow的なSDNからIBN的な方向へ舵を切っていくことに
- ネットワークのインフラを設定するのではなく、ネットワークやアプリケーションを設定する
- ACI: Application Centric Infrastructure
- 「Webサーバ-アプリケーションサーバ間の通信は許可する」「アプリケーションサーバ-DBサーバ間の通信は許可する」というような方向性
- EPG: Endpoint Group
- 「Webサーバ-アプリケーションサーバ」といったインフラのまとまり
Cisco APIC Enterprise Module
- APIC: Application Policy Infrastructure
APIC-EM Basics
APIC-EM Replacement
- APIC-EMは終わろうとしている製品
- DNA Center (DNAC)へ移行しよう
Comparing Traditional Versus Controller-Based Networks
How Automation Impacts Network Management
- 汎用プログラム言語でNBI (RESTful)を叩いて設定を行えるように
- 従来型のネットワーク管理から変わったこと
- 自動化が簡単に
- 堅牢
- ネットワークの再定義 -- SDN
- インフラのデータをControllerに集約して分析することで、より意味のあるデータを得られるように
- Path Traceなど
- 運用コスト(時間)の削減
- 自動化が簡単に
Comparing Traditional Networks with Controller-Based Networks
- 従来型から変わったこと
- 「デバイスごとの個別の設定」よりもすぐれた運用モデル
- NBIの提供するIntent-Basedな設定
- SBI経由の一貫した設定
- 誤りが減り、トラブルシューティングのコストも下がる
- ネットワークにもDevOpsが適用可能に
- 「高い品質を確保しつつ、システムへの変更をコミットしてから通常の運用に移るまでの時間を短縮することを目的とした一連のプラクティス」のこと
英語
- overt
- 明白な
- tidbit
- ひと口、一片
- 役立つ情報
CCNA試験対策 下巻ch19: Understanding Ansible, Puppet, and Chef
https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com
Device Configuration Challenges and Solutions
- ネットワークエンジニアが1人で1台のデバイスを設定しているうちは
configure terminal
で設定してcopy running-config startup-config
していればよい - 複数人のネットワークエンジニアで数百数千のデバイスを管理するようになると破綻する
Configuration Drift
- デバイス上で手作業で直接設定を行うと、誰がいつ・どの行の・何を変更し・何を削除したか、といった情報がない
- 一貫性のない状態になる
Centralized Configuration Files and Version Control
- 設定ファイルを一元化する
- 方針は2通り考えられる
- デバイス上の設定を正として、一元管理しているものはバックアップとする
- 一元管理しているほうを正とする
- 後者だといろいろなツールの恩恵を得られて良い
- バージョン管理使え
Configuration Monitoring and Enforcement
- 設定を一元管理して、バージョン管理もするようになりました
- まだ問題がある
- 一元管理している設定ファイルをどうやってデバイスに反映するの?
- 人の手が介在すると、ここでも構成ドリフトのおそれが生じる
- Configuration Monitoring, Configuration Enforcementという類のツールを使え
- 構成ドリフトを監視する
- 修正もしくはネットワークエンジニアに通知する
Configuration Provisioning
- 構成管理システムで変更した設定のプロビジョン・デプロイ
- 求められる機能
- メイン: 一元管理している設定ファイルのデプロイ
- デプロイ対象の管理
- 全部
- 一部
- 設定変更が受理/拒否されたときのアクションの設定
- revert
- dry-run
- 設定が正しく反映されていることの確認
- running-configからstartup-configに永続化するかどうか選べる
- 設定ファイルのテンプレート化
- 自動デプロイ
Configuration Templates and Variables
Files That Control Configuration Automation
- 構成管理システムの構成も構成ファイルで行えること
Ansible, Puppet, and Chef Basics
Ansible | Puppet | Chef | |
---|---|---|---|
設定ファイル | Playbook | Manifest | Recipe,Runlist |
Protocol | SSH, NETCONF | HTTP (RESTful) | HTTP (RESTful) |
agentのインストールが必要 | no | yes | yes |
push/pull | push | pull | pull |
CCNA試験対策 下巻ch15: Cloud Architecture
https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com
Server Virtualization
Cisco Server Hardware
- No KVM
- keyboard, video display, mouseがない、の意
- 人が前に座って操作する代物ではない
- 人が直接操作しないので、省スペースが重要になってくる
- データセンターのラックにはホスト(物理マシン)がぎっしり
Server Virtualization Basics
- 今日びのハードウェアは1台で1つのOS・アプリケーションを動かすにはリソースが余りがち
- なので、Hypervisorを動かして、その上でOSを複数動かす
- 各OSに適切にリソースを割り当てる
- Hypervisor製品の有名どころ
Networking with Virtual Switches on a Virtualized Host
- virtual switch (vSwitch)
The Physical Data Center Network
- 物理ホストは当然物理スイッチに接続されている必要がある
- host - ToR SW - EoR SW というトポロジー
- ToR: Top of Rack
- サーバが詰まっているラックの最上段にいるswitch
- access layerに相当
- EoR: End of Row
- distribution layerに相当
- host, ToRとは別のラックに詰まってるやつ
- ToR: Top of Rack
Workflow with a Virtualized Data Center
Cloud Computing Services
- 「クラウド」って何?
- NISTによる定義
- On-demand self-service
- データセンターのサーバ仮想化エンジニアに作業依頼するのはこの条件を満たさない
- Broad network access
- 様々な種類のネットワークをサポートしていること
- インターネット含む
- 様々な種類のネットワークをサポートしていること
- Resource pooling
- Rapid elasticity
- Measured service
- 透明性と従量課金のため
- On-demand self-service
Private Cloud (On-Premise)
- NISTによる「クラウド」の定義5つを満たすサービスを、社内向けに提供する形態のこと
Public Cloud
- 外部向けに売り出すやつ
Cloud and the "As a Service" Model
- Infrastructure as a Service
- Software as as Service
- (Development) Platform as a Service
しってるので略
WAN Traffic Paths to Reach Cloud Services
- public cloud利用時、WANの向こう側のpublic cloudにどうやって接続するか
- 良し悪し
Internet | Internet VPN | MPLS VPN | Ethernet WAN | Intercloud Exchange | |
---|---|---|---|---|---|
セキュリティ | x | o | o | o | o |
QoS | x | x | o | o | o |
キャパシティプランニング必要 | o | o | o | o | o |
クラウド乗り換えの障壁が低い | o | o | x | x | o |
導入の速さ | o | o | x | x | x |